Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.11.2
version: v2.12.2

- name: Go mod tidy
run: |
Expand Down
72 changes: 37 additions & 35 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
version: "2"
run:
tests: false
linters:
default: all
enable:
- asasalint
- asciicheck
- bidichk
- dogsled
- dupl
- durationcheck
- errchkjson
- errorlint
- exhaustive
- gocheckcompilerdirectives
- gochecksumtype
- gosec
- gosmopolitan
- loggercheck
- makezero
- nilnesserr
- protogetter
- reassign
- recvcheck
- spancheck
- testifylint
- whitespace
- wsl
- zerologlint
- wsl_v5
disable:
- bodyclose
- contextcheck
- funlen
- wsl
- cyclop
- depguard
- err113
- exhaustruct
- forbidigo
- forcetypeassert
- gochecknoglobals
- gochecknoinits
- godot
- godox
- lll
- mnd
- musttag
- nilerr
- noctx
- rowserrcheck
- sqlclosecheck
- unparam
- nakedret
- nlreturn
- nolintlint
- nonamedreturns
- tagliatelle
- varnamelen
- wrapcheck
- funlen
settings:
nestif:
min-complexity: 8
wsl_v5:
allow-first-in-block: true
allow-whole-block: true
branch-max-lines: 2
disable:
- err
exclusions:
generated: lax
presets:
Expand All @@ -43,13 +45,13 @@ linters:
- legacy
- std-error-handling
paths:
- (.+)_test\.go
- internal/config/http_config.go
- internal/config/config.go
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
Expand Down
37 changes: 19 additions & 18 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
Port = 5985
TlsPort = 5986
TLSPort = 5986
AuthDefault = AuthNTLM
AuthBasic = "basic"
AuthNTLM = "ntlm"
Expand All @@ -30,13 +30,13 @@ type Config struct {
Port int
User string
Password string
NoTls bool
NoTLS bool
Insecure bool
TlsCAPath string
TLSCAPath string
tlsCA []byte
TlsCertPath string
TLSCertPath string
tlsCert []byte
TlsKeyPath string
TLSKeyPath string
tlsKey []byte
Command string
IcingaCommand string
Expand All @@ -60,10 +60,10 @@ func BuildConfigFlags(fs *pflag.FlagSet) (config *Config) {

fs.BoolVarP(&config.Insecure, "insecure", "k", false,
"Don't verify the hostname on the returned certificate")
fs.BoolVar(&config.NoTls, "no-tls", false, "Don't use a TLS connection, use the HTTP protocol")
fs.StringVar(&config.TlsCAPath, "ca", "", "CA certificate")
fs.StringVar(&config.TlsCertPath, "cert", "", "Client certificate")
fs.StringVar(&config.TlsKeyPath, "key", "", "Client Key")
fs.BoolVar(&config.NoTLS, "no-tls", false, "Don't use a TLS connection, use the HTTP protocol")
fs.StringVar(&config.TLSCAPath, "ca", "", "CA certificate")
fs.StringVar(&config.TLSCertPath, "cert", "", "Client certificate")
fs.StringVar(&config.TLSKeyPath, "key", "", "Client Key")

fs.StringVar(&config.Command, "cmd", "", "Command to execute on the remote machine")
fs.StringVar(&config.IcingaCommand, "icingacmd", "",
Expand Down Expand Up @@ -104,23 +104,23 @@ func (c *Config) Validate() (err error) {

// Set default port if unset
if c.Port < 1 {
c.Port = TlsPort
if c.NoTls {
c.Port = TLSPort
if c.NoTLS {
c.Port = Port
}
}

if c.TlsCertPath != "" {
c.tlsCert, err = os.ReadFile(c.TlsCertPath)
if c.TLSCertPath != "" {
c.tlsCert, err = os.ReadFile(c.TLSCertPath)
if err != nil {
return fmt.Errorf("could not read certificate: %w", err)
}

if c.TlsKeyPath == "" {
if c.TLSKeyPath == "" {
return errors.New("please specify certificate key when tls is enabled")
}

c.tlsKey, err = os.ReadFile(c.TlsKeyPath)
c.tlsKey, err = os.ReadFile(c.TLSKeyPath)
if err != nil {
return fmt.Errorf("could not read certificate key: %w", err)
}
Expand All @@ -132,8 +132,8 @@ func (c *Config) Validate() (err error) {
}
}

if c.TlsCAPath != "" {
c.tlsCA, err = os.ReadFile(c.TlsCAPath)
if c.TLSCAPath != "" {
c.tlsCA, err = os.ReadFile(c.TLSCAPath)
if err != nil {
return fmt.Errorf("could not read CA file: %w", err)
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (c *Config) Run(timeout time.Duration) (rc int, output string, err error) {
endpoint := winrm.NewEndpoint(
c.Host, // Host to connect to
c.Port, // Winrm port
!c.NoTls, // Use TLS
!c.NoTLS, // Use TLS
c.Insecure, // Allow insecure connection
c.tlsCA, // CA certificate
c.tlsCert, // Client Certificate
Expand All @@ -215,6 +215,7 @@ func (c *Config) Run(timeout time.Duration) (rc int, output string, err error) {
}
case AuthSSH:
var sshClient *ssh.Client

sshClient, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.SSHHost, c.SSHPort), &ssh.ClientConfig{
User: c.SSHUser,
Auth: []ssh.AuthMethod{ssh.Password(c.SSHPassword)},
Expand Down
20 changes: 10 additions & 10 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func TestConfig_Validate(t *testing.T) {
t.Error("Did not expect error got", errVal)
}

if c.Port != TlsPort {
t.Error("Actual", c.Port, "Expected", TlsPort)
if c.Port != TLSPort {
t.Error("Actual", c.Port, "Expected", TLSPort)
}

if c.NoTls != false {
if c.NoTLS != false {
t.Error("Expected NoTls to be false, got true")
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func TestConfig_Run_WithError(t *testing.T) {
User: "admin",
Password: "test",
Command: "Get-Host",
NoTls: true,
NoTLS: true,
}

err := c.Validate()
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestConfig_Run_Basic(t *testing.T) {
}

c := buildEnvConfig(t, AuthBasic)
c.NoTls = true
c.NoTLS = true

fmt.Printf("%v\n", c)

Expand Down Expand Up @@ -145,7 +145,7 @@ func TestConfig_Run_NTLM(t *testing.T) {
}

c := buildEnvConfig(t, AuthNTLM)
c.NoTls = true
c.NoTLS = true

err := c.Validate()
if err != nil {
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestConfig_Run_TLS(t *testing.T) {
c := buildEnvConfig(t, AuthTLS)
setupTlsFromEnv(t, c)

if c.TlsCertPath == "" {
if c.TLSCertPath == "" {
t.Skip("WINRM_TLS_CERT not set")
}

Expand Down Expand Up @@ -246,15 +246,15 @@ func setupTlsFromEnv(t *testing.T, c *Config) {
}

if file := os.Getenv("WINRM_TLS_CA"); file != "" {
c.TlsCAPath = file
c.TLSCAPath = file
}

if file := os.Getenv("WINRM_TLS_CERT"); file != "" {
c.TlsCertPath = file
c.TLSCertPath = file
}

if file := os.Getenv("WINRM_TLS_KEY"); file != "" {
c.TlsKeyPath = file
c.TLSKeyPath = file
}

if file := os.Getenv("WINRM_TLS_PORT"); file != "" {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func main() {
}

fmt.Print(output)
//nolint: gocritic
// We ignore the gocritic since the defer cannot run if we exit here.
os.Exit(rc)
}

Expand Down
Loading