Skip to content

Commit d2d8086

Browse files
authored
Merge pull request #12 from Icinga/feature/no-tls
2 parents 72488a8 + d210755 commit d2d8086

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

README.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,29 @@ To compile for a specific platform, you have to set the GOOS and GOARCH environm
6464
GOOS=linux GOARCH=amd64 go build -o check_by_powershell main.go
6565

6666
## Usage
67-
./check_by_powershell -h
68-
Usage of check_by_powershell
69-
70-
This Plugin executes remote commands on Windows machines through the use of WinRM.
71-
72-
Arguments:
73-
-H, --host string Host name, IP Address of the remote host (default "127.0.0.1")
74-
-p, --port int Port number WinRM (default 5985)
75-
--user string Username of the remote host
76-
--password string Password of the user
77-
--tls Use TLS connection (default: false)
78-
-u, --unsecure Verify the hostname on the returned certificate
79-
--ca string CA certificate
80-
--cert string Client certificate
81-
--key string Client Key
82-
--cmd string Command to execute on the remote machine
83-
--icingacmd string Executes commands of Icinga PowerShell Framework (e.g. Invoke-IcingaCheckCPU)
84-
--auth string Authentication mechanism - NTLM | SSH
85-
--sshhost string SSH Host (mandatory if --auth=SSH)
86-
--sshuser string SSH Username (mandatory if --auth=SSH)
87-
--sshpassword string SSH Password (mandatory if --auth=SSH)
88-
-t, --timeout int Abort the check after n seconds (default 10)
89-
-d, --debug Enable debug mode
90-
-v, --verbose Enable verbose mode
91-
-V, --version Print version and exit
67+
68+
```
69+
Arguments:
70+
-H, --host string Host name, IP Address of the remote host (default "127.0.0.1")
71+
-p, --port int Port number WinRM
72+
-U, --user string Username of the remote host
73+
-P, --password string Password of the user
74+
-k, --insecure Don't verify the hostname on the returned certificate
75+
--no-tls Don't use a TLS connection, use the HTTP protocol
76+
--ca string CA certificate
77+
--cert string Client certificate
78+
--key string Client Key
79+
--cmd string Command to execute on the remote machine
80+
--icingacmd string Executes commands of Icinga PowerShell Framework (e.g. Invoke-IcingaCheckCPU)
81+
--auth string Authentication mechanism - NTLM | SSH (default "basic")
82+
--sshhost string SSH Host (mandatory if --auth=SSH)
83+
--sshuser string SSH Username (mandatory if --auth=SSH)
84+
--sshpassword string SSH Password (mandatory if --auth=SSH)
85+
-t, --timeout int Abort the check after n seconds (default 10)
86+
-d, --debug Enable debug mode
87+
-v, --verbose Enable verbose mode
88+
-V, --version Print version and exit
89+
```
9290

9391
### Execute a script over http
9492
./check_by_powershell -H 192.168.172.217 -p 5985 --cmd "cscript.exe /T:30 /NoLogo C:\Windows\system32\check_time.vbs 1.de.pool.ntp.org 20 240" --user "windowsuser" --password 'secret!pw'
@@ -102,4 +100,4 @@ It is necessary that the PowerShell script exits with an exitcode like *exit 2*,
102100

103101
[OK] Check package "CPU Load"
104102
| 'core_23_10'=2.31%;;;0;100 'core_23_3'=2.54%;;;0;100 'core_23_15'=2.12%;;;0;100 'core_23_5'=2.39%;;;0;100
105-
'core_23_1'=2.04%;;;0;100 'core_23'=1.93%;;;0;100 'core_2_15'=2.78%;;;0;100 'core_2_10'=2.89%;;;0;100 [...]
103+
'core_23_1'=2.04%;;;0;100 'core_23'=1.93%;;;0;100 'core_2_15'=2.78%;;;0;100 'core_2_10'=2.89%;;;0;100 [...]

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)