Skip to content

Commit 94ab5ec

Browse files
committedSep 7, 2020
Use a TLS connection by default
Add a flag to disable TLS, and use HTTP
1 parent 72488a8 commit 94ab5ec

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed
 

‎check.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Config struct {
2828
Port int
2929
User string
3030
Password string
31-
Tls bool
31+
NoTls bool
3232
Insecure bool
3333
TlsCAPath string
3434
tlsCA []byte
@@ -55,9 +55,9 @@ func BuildConfigFlags(fs *pflag.FlagSet) (config *Config) {
5555
fs.StringVarP(&config.User, "user", "U", "", "Username of the remote host")
5656
fs.StringVarP(&config.Password, "password", "P", "", "Password of the user")
5757

58-
fs.BoolVarP(&config.Tls, "tls", "S", false, "Use TLS connection (default: false)")
5958
fs.BoolVarP(&config.Insecure, "insecure", "k", false,
6059
"Don't verify the hostname on the returned certificate")
60+
fs.BoolVar(&config.NoTls, "no-tls", false, "Don't use a TLS connection, use the HTTP protocol")
6161
fs.StringVar(&config.TlsCAPath, "ca", "", "CA certificate")
6262
fs.StringVar(&config.TlsCertPath, "cert", "", "Client certificate")
6363
fs.StringVar(&config.TlsKeyPath, "key", "", "Client Key")
@@ -100,9 +100,9 @@ func (c *Config) Validate() (err error) {
100100

101101
// Set default port if unset
102102
if c.Port < 1 {
103-
c.Port = Port
104-
if c.Tls {
105-
c.Port = TlsPort
103+
c.Port = TlsPort
104+
if c.NoTls {
105+
c.Port = Port
106106
}
107107
}
108108

@@ -189,7 +189,7 @@ func (c *Config) Run(timeout time.Duration) (err error, rc int, output string) {
189189
endpoint := winrm.NewEndpoint(
190190
c.Host, // Host to connect to
191191
c.Port, // Winrm port
192-
c.Tls, // Use TLS
192+
!c.NoTls, // Use TLS
193193
c.Insecure, // Allow insecure connection
194194
c.tlsCA, // CA certificate
195195
c.tlsCert, // Client Certificate

‎check_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func TestConfig_Validate(t *testing.T) {
2222
c.Password = "verysecret"
2323

2424
assert.NoError(t, c.Validate())
25-
assert.Equal(t, c.Port, Port)
25+
assert.Equal(t, c.Port, TlsPort)
26+
assert.False(t, c.NoTls)
2627
assert.Equal(t, c.AuthType, AuthBasic)
2728
assert.True(t, c.validated)
2829
}
@@ -51,12 +52,13 @@ func TestConfig_Run_WithError(t *testing.T) {
5152
User: "admin",
5253
Password: "test",
5354
Command: "Get-Host",
55+
NoTls: true,
5456
}
5557

5658
err := c.Validate()
5759
assert.NoError(t, err)
5860

59-
err, _, _ = c.Run(1 * time.Microsecond)
61+
err, _, _ = c.Run(1 * time.Second)
6062
assert.Error(t, err)
6163
assert.Contains(t, err.Error(), "dial tcp 192.0.2.11:")
6264
}
@@ -71,6 +73,7 @@ func TestConfig_Run_Basic(t *testing.T) {
7173
}
7274

7375
c := buildEnvConfig(t, AuthBasic)
76+
c.NoTls = true
7477

7578
runCheck(t, c)
7679
}
@@ -92,6 +95,7 @@ func TestConfig_Run_NTLM(t *testing.T) {
9295
}
9396

9497
c := buildEnvConfig(t, AuthNTLM)
98+
c.NoTls = true
9599

96100
runCheck(t, c)
97101
}
@@ -155,7 +159,6 @@ func setupTlsFromEnv(t *testing.T, c *Config) {
155159
t.Skip("WINRM_SKIP_TLS has been set")
156160
}
157161

158-
c.Tls = true
159162
if os.Getenv("WINRM_INSECURE") != "" {
160163
c.Insecure = true
161164
}

0 commit comments

Comments
 (0)
Please sign in to comment.