Skip to content

Commit

Permalink
feat(wsman): adds logging capability to wsman messages (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidusmani26 authored Jan 19, 2024
1 parent 1736ea3 commit 1b9c903
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.20

require github.com/stretchr/testify v1.8.4

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 16 additions & 7 deletions pkg/wsman/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"io"
"net/http"
"time"

log "github.com/sirupsen/logrus"
)

const ContentType = "application/soap+xml; charset=utf-8"
Expand All @@ -34,16 +36,18 @@ type Client struct {
username string
password string
useDigest bool
useLogging bool
OptimizeEnum bool
challenge *authChallenge
}

func NewClient(target, username, password string, useDigest bool) *Client {
func NewClient(target, username, password string, useDigest, useLogging bool) *Client {
res := &Client{
endpoint: target,
username: username,
password: password,
useDigest: useDigest,
endpoint: target,
username: username,
password: password,
useDigest: useDigest,
useLogging: useLogging,
}
res.Timeout = 10 * time.Second
res.Transport = &http.Transport{
Expand All @@ -52,12 +56,17 @@ func NewClient(target, username, password string, useDigest bool) *Client {
if res.useDigest {
res.challenge = &authChallenge{Username: res.username, Password: res.password}
}
if res.useLogging {
log.SetLevel(log.TraceLevel)
} else {
log.SetLevel(log.PanicLevel)
}
return res
}

// Post overrides http.Client's Post method
func (c *Client) Post(msg string) (response []byte, err error) {

log.Trace("REQUEST: " + msg)
msgBody := []byte(msg)
bodyReader := bytes.NewReader(msgBody)
req, err := http.NewRequest("POST", c.endpoint, bodyReader)
Expand Down Expand Up @@ -114,6 +123,6 @@ func (c *Client) Post(msg string) (response []byte, err error) {
if err != nil {
return nil, err
}

log.Trace("RESPONSE: " + string(response))
return response, nil
}
21 changes: 14 additions & 7 deletions pkg/wsman/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func TestNewClient(t *testing.T) {
username := "user"
password := "password"
useDigest := false
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)

if client.endpoint != target {
t.Errorf("Expected endpoint to be %s, but got %s", target, client.endpoint)
Expand Down Expand Up @@ -46,8 +47,9 @@ func TestClient_Post(t *testing.T) {
username := "user"
password := "password"
useDigest := false
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

response, err := client.Post(msg)
Expand Down Expand Up @@ -91,8 +93,9 @@ func TestClient_PostWithDigestAuth(t *testing.T) {
username := "user"
password := "password"
useDigest := true
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

response, err := client.Post(msg)
Expand All @@ -117,8 +120,9 @@ func TestClient_PostWithDigestAuthUnauthorized(t *testing.T) {
username := "wronguser"
password := "wrongpassword"
useDigest := true
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

_, err := client.Post(msg)
Expand All @@ -145,8 +149,9 @@ func TestClient_PostWithBasicAuth(t *testing.T) {
username := "user"
password := "password"
useDigest := false
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

response, err := client.Post(msg)
Expand All @@ -169,8 +174,9 @@ func TestClient_PostUnauthorized(t *testing.T) {
username := "wronguser"
password := "wrongpassword"
useDigest := false
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

_, err := client.Post(msg)
Expand All @@ -191,8 +197,9 @@ func TestClient_PostInvalidResponse(t *testing.T) {
username := "user"
password := "password"
useDigest := false
useLogging := false

client := NewClient(target, username, password, useDigest)
client := NewClient(target, username, password, useDigest, useLogging)
msg := "<SampleRequest>Request</SampleRequest>"

_, err := client.Post(msg)
Expand Down

0 comments on commit 1b9c903

Please sign in to comment.