Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.
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
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ updates:
interval: weekly
time: '10:00'
open-pull-requests-limit: 10

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: 1.21

- name: Test
run: go test -v ./...
Expand All @@ -29,7 +29,7 @@ jobs:

- name: Run goreleaser in release mode
if: success() && startsWith(github.ref, 'refs/tags/v')
uses: goreleaser/goreleaser-action@v3
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: 1.21

- uses: actions/checkout@v3

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51
43 changes: 33 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
run:
timeout: 5m
skip-files:
- '(.+)_test\.go'

tests: false
linters:
disable-all: false
disable:
- musttag
presets:
- bugs
- unused
fast: false
enable-all: true
disable:
- cyclop
- depguard
- exhaustivestruct
- exhaustruct
- forbidigo
- forcetypeassert
- gci
- gochecknoglobals
- gochecknoinits
- godox
- goerr113
- gofumpt
- gomnd
- ifshort
- lll
- musttag
- nakedret
- nlreturn
- nolintlint
- nonamedreturns
- tagliatelle
- varnamelen
- wrapcheck
linters-settings:
estif:
min-complexity: 4
maligned:
suggest-new: true
funlen:
lines: 80
18 changes: 8 additions & 10 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"time"

log "github.com/sirupsen/logrus"
)

type RestAPI struct {
URL string
Client *http.Client
Logger *slog.Logger
}

func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{}, timeout uint32) (*APICheckResult, error) { //nolint:lll
Expand All @@ -31,14 +31,11 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
defer cancel()

// Build request
requestUrl := a.URL + "/v1/checker?command=" + url.QueryEscape(command)
requestURL := a.URL + "/v1/checker?command=" + url.QueryEscape(command)

log.WithFields(log.Fields{
"body": string(body),
"url": requestUrl,
}).Debug("sending request")
a.Logger.Debug("sending request", "body", string(body), "url", requestURL)

req, err := http.NewRequestWithContext(ctx, "POST", requestUrl, bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("could not build request: %w", err)
}
Expand All @@ -53,6 +50,7 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
if errors.Is(err, context.DeadlineExceeded) {
return nil, fmt.Errorf("timeout during HTTP request: %w", err)
}

return nil, fmt.Errorf("executing API request failed: %w", err)
}

Expand All @@ -64,9 +62,9 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
return nil, fmt.Errorf("could not read result: %w", err)
}

log.WithField("body", string(resultBody)).Debug("received response")
a.Logger.Debug("received response", "body", string(resultBody))

if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API request not successful code=%d: %s", resp.StatusCode, string(resultBody))
}

Expand Down
12 changes: 10 additions & 2 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand All @@ -23,7 +25,7 @@ func TestApiCmd(t *testing.T) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"Invoke-IcingaCheckFoo": {"exitcode": 0, "checkresult": "[OK] \"foo\"", "perfdata": ["'foo'=1.00%;;;0;100 "]}}`))
})),
api: RestAPI{},
api: RestAPI{Logger: slog.New(slog.NewTextHandler(os.Stdout, nil))},
expected: APICheckResult{
ExitCode: 0,
CheckResult: "[OK] \"foo\"",
Expand Down Expand Up @@ -58,7 +60,13 @@ func TestApiCmd(t *testing.T) {
}

func TestApiTimeout(t *testing.T) {
api := RestAPI{}
opts := &slog.HandlerOptions{
Level: slog.LevelDebug,
}

logger := slog.New(slog.NewTextHandler(os.Stdout, opts))

api := RestAPI{Logger: logger}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Wait for the context timeout to kick in
Expand Down
2 changes: 1 addition & 1 deletion api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *APIPerfdataList) UnmarshalJSON(data []byte) error {
var value []string

// catch empty object and return empty []string
if bytes.Compare(data, []byte("{}")) == 0 {
if bytes.Equal(data, []byte("{}")) {
value = []string{}
} else {
err := json.Unmarshal(data, &value)
Expand Down
3 changes: 2 additions & 1 deletion api_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAPIPerfdataList_UnmarshalJSON(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const (
)

type Config struct {
Timeout uint32
API string
Command string
Arguments map[string]interface{}
CertName string
CAFile string
Arguments map[string]interface{}
Timeout uint32
Insecure bool
Debug bool
PrintVersion bool
}

var (
// ErrVersionRequested returned when --version flag is set
// ErrVersionRequested returned when --version flag is set.
ErrVersionRequested = errors.New("version was requested by a flag")

// ErrNoCommand is returned when no PowerShell command could be parsed from flags.
Expand Down
5 changes: 3 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"os"
"testing"

flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

func quietTest() func() {
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module github.com/NETWAYS/icinga-powershell-connector

go 1.16
go 1.21

require (
github.com/NETWAYS/go-check v0.3.0
github.com/sirupsen/logrus v1.8.1
github.com/NETWAYS/go-check v0.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
golang.org/x/sys v0.1.0 // indirect
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
25 changes: 9 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
github.com/NETWAYS/go-check v0.3.0 h1:WiLa/POqkI7TWbNxpUNkQ8gd9G0RTCc4DKUvmpy34Hg=
github.com/NETWAYS/go-check v0.3.0/go.mod h1:PO4R+lr+mcdjltcvvJRPnyGbRqMZHlIRDTVIzOnE0HA=
github.com/NETWAYS/go-check v0.5.0 h1:YahXTyveSOww3mTtKnvbsXZSk9kHi82mnvXHsNWx7AQ=
github.com/NETWAYS/go-check v0.5.0/go.mod h1:3G1p9ZCv41UcKirl+nZWne8oKVPUY5HbtyjRwGgyw6A=
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/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
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.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
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.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/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=
10 changes: 5 additions & 5 deletions icinga.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package main
import (
"crypto/x509"
"encoding/json"
"log/slog"
"os"

log "github.com/sirupsen/logrus"
)

const (
Expand All @@ -27,15 +26,15 @@ func LoadIcingaCACert(path string) *x509.CertPool {
// Load contents
data, err := os.ReadFile(path)
if err != nil {
log.WithError(err).WithField("path", path).Debug("could not read Icinga CA certificate")
slog.Error("could not read Icinga CA certificate", "path", path)

return nil
}

// Build pool
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(data) {
log.WithField("path", path).Warning("could not append any CA certificates to pool")
slog.Error("could not append any CA certificates to pool", "path", path)
}

return pool
Expand All @@ -55,7 +54,8 @@ func LoadIcingaVariables(path string) (vars map[string]string) {

fh, err := os.Open(path)
if err != nil {
log.WithError(err).WithField("path", path).Debug("could not read vars file")
slog.Error("could not read vars file", "path", path)

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion icinga_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadIcingaVariables(t *testing.T) {
Expand Down
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/NETWAYS/go-check"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -37,12 +37,25 @@ func main() {
check.ExitError(err)
}

// Default log options
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
}

handler := slog.NewTextHandler(os.Stdout, opts)

if config.Debug {
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.DebugLevel)
opts.Level = slog.LevelDebug
}

api := RestAPI{URL: config.API, Client: config.NewClient()}
logger := slog.New(handler)
slog.SetDefault(logger)

api := RestAPI{
URL: config.API,
Client: config.NewClient(),
Logger: logger,
}

result, err := api.ExecuteCheck(config.Command, config.Arguments, config.Timeout)
if err != nil {
Expand Down
Loading