From 1b9c903c77bb3391d41eb4357b6b8e132fae5862 Mon Sep 17 00:00:00 2001
From: zaidusmani26 <106174728+zaidusmani26@users.noreply.github.com>
Date: Fri, 19 Jan 2024 15:01:28 -0700
Subject: [PATCH] feat(wsman): adds logging capability to wsman messages (#181)
---
go.mod | 3 +++
go.sum | 8 ++++++++
pkg/wsman/client.go | 23 ++++++++++++++++-------
pkg/wsman/client_test.go | 21 ++++++++++++++-------
4 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/go.mod b/go.mod
index 422cc939..40fa566d 100644
--- a/go.mod
+++ b/go.mod
@@ -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
)
diff --git a/go.sum b/go.sum
index fa4b6e68..a9c72e4e 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
diff --git a/pkg/wsman/client.go b/pkg/wsman/client.go
index 4d78f0d1..15fac9f8 100644
--- a/pkg/wsman/client.go
+++ b/pkg/wsman/client.go
@@ -11,6 +11,8 @@ import (
"io"
"net/http"
"time"
+
+ log "github.com/sirupsen/logrus"
)
const ContentType = "application/soap+xml; charset=utf-8"
@@ -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{
@@ -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)
@@ -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
}
diff --git a/pkg/wsman/client_test.go b/pkg/wsman/client_test.go
index 7ec83533..ce4cd864 100644
--- a/pkg/wsman/client_test.go
+++ b/pkg/wsman/client_test.go
@@ -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)
@@ -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 := "Request"
response, err := client.Post(msg)
@@ -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 := "Request"
response, err := client.Post(msg)
@@ -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 := "Request"
_, err := client.Post(msg)
@@ -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 := "Request"
response, err := client.Post(msg)
@@ -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 := "Request"
_, err := client.Post(msg)
@@ -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 := "Request"
_, err := client.Post(msg)