Skip to content
17 changes: 16 additions & 1 deletion winrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
NTLM
Kerberos
)
const WINRM_HTTP_PORT = 5985
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a detail but this is unused... so I'm wondering why you're adding it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed


// WinRM client used for executing scripts
// TODO: Add support for NTLM and Kerberos, Only basic is supported for now
Expand Down Expand Up @@ -55,6 +56,8 @@ type winrmSettings struct {
operationTimeout string
// Timeout of each HTTP call made
timeout int
//Whether the call is http or https
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits:

  • Missing space before "Whether"
  • http -> HTTP / https -> HTTPS

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

isSecure bool
}

type getEndpointDetails func() endpointDetails
Expand Down Expand Up @@ -108,6 +111,13 @@ func Port(num int) winrmSettingsOption {
}
}

func IsSecure(b bool) winrmSettingsOption {
return func(ws winrmSettings) winrmSettings {
ws.isSecure = b
return ws
}
}

func MaxEnvelopeSize(size string) winrmSettingsOption {
return func(ws winrmSettings) winrmSettings {
ws.maxEnvelopeSize = size
Expand Down Expand Up @@ -156,6 +166,7 @@ var defaultWinrmSettings winrmSettings = winrmSettings{
maxEnvelopeSize: "153200",
locale: "en-US",
operationTimeout: "PT60.000S",
isSecure: true,
}

// Creates a new WinRM client
Expand Down Expand Up @@ -185,7 +196,11 @@ func NewWinRMClient(details getEndpointDetails, options ...winrmSettingsOption)
for _, o := range options {
client.winrmSettings = o(client.winrmSettings)
}
client.url = fmt.Sprintf("https://%s:%d/wsman", client.ipAddress, client.port)
if client.isSecure {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a detail but to avoid repeating the whole Sprintf line, how about:

if client.isSecure {
    protocol = "https"
} else {
    protocol = "http"
}

then use protocol

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

client.url = fmt.Sprintf("https://%s:%d/wsman", client.ipAddress, client.port)
} else {
client.url = fmt.Sprintf("http://%s:%d/wsman", client.ipAddress, client.port)
}
if client.endpointDetails.auth&NTLM == NTLM {
client.client.Transport = ntlmssp.Negotiator{RoundTripper: client.client.Transport}
}
Expand Down