diff --git a/.goreleaser.yml b/.goreleaser.yml index 684e698..d59f1a9 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -16,5 +16,5 @@ archives: name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}" release: github: - owner: kazeburo + owner: monitoring-forge name: check_http2 diff --git a/Makefile b/Makefile index fabf1c8..c5defc1 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,11 @@ all: check_http2 .PHONY: check_http2 -check_http2: main.go - go build $(LDFLAGS) -o check_http2 main.go +check_http2: writer.go checker.go main.go + go build $(LDFLAGS) -o check_http2 writer.go checker.go main.go -linux: main.go - GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o check_http2 main.go +linux: writer.go checker.go main.go + GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o check_http2 writer.go checker.go main.go check: go test -v ./... diff --git a/README.md b/README.md index 6ece959..204c044 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Application Options: --tls-max=[1.0|1.1|1.2|1.3] maximum supported TLS version -4 use tcp4 only -6 use tcp6 only + --verify-ssl verify SSL certificate -v, --version Show version Help Options: diff --git a/checker.go b/checker.go index c969631..ee20268 100644 --- a/checker.go +++ b/checker.go @@ -53,6 +53,7 @@ type Opt struct { TLSMaxVersion string `long:"tls-max" description:"maximum supported TLS version" choice:"1.0" choice:"1.1" choice:"1.2" choice:"1.3"` TCP4 bool `short:"4" description:"use tcp4 only"` TCP6 bool `short:"6" description:"use tcp6 only"` + VerifySSL bool `long:"verify-ssl" description:"verify SSL certificate"` Version bool `short:"v" long:"version" description:"Show version"` bufferSize uint64 expectByte []byte @@ -90,7 +91,7 @@ func (opt *Opt) MakeTransport() http.RoundTripper { } tlsConfig := &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: !opt.VerifySSL, } if opt.SNI { host, _, err := net.SplitHostPort(opt.Hostname) diff --git a/checker_test.go b/checker_test.go index 205e5a1..61e069d 100644 --- a/checker_test.go +++ b/checker_test.go @@ -3,6 +3,7 @@ package main import ( "context" "crypto/tls" + "fmt" "net/http" "testing" ) @@ -112,6 +113,18 @@ func TestBuildRequestWithInvalidAuthorization(t *testing.T) { } } +func getTransport(opt *Opt) (*http.Transport, error) { + tripper := opt.MakeTransport() + if tripper == nil { + return nil, fmt.Errorf("MakeTransport() returned nil") + } + transport, ok := tripper.(*http.Transport) + if !ok { + return nil, fmt.Errorf("MakeTransport() returned non-http.Transport") + } + return transport, nil +} + // MakeTransport tests func TestMakeTransport(t *testing.T) { opt := Opt{ @@ -119,14 +132,9 @@ func TestMakeTransport(t *testing.T) { SNI: true, Hostname: "example.com:443", } - - tripper := opt.MakeTransport() - if tripper == nil { - t.Fatalf("MakeTransport() returned nil, want non-nil") - } - transport, ok := tripper.(*http.Transport) - if !ok { - t.Fatalf("MakeTransport() returned non-http.Transport, want http.Transport") + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) } if transport.TLSClientConfig.ServerName != "example.com" { t.Fatalf("MakeTransport() TLSClientConfig.ServerName = %q, want %q", transport.TLSClientConfig.ServerName, "example.com") @@ -140,13 +148,9 @@ func TestMakeTransportWithTLSMaxVersion(t *testing.T) { SSL: true, TLSMaxVersion: "1.2", } - tripper := opt.MakeTransport() - transport, ok := tripper.(*http.Transport) - if !ok { - t.Fatalf("MakeTransport() returned non-http.Transport, want http.Transport") - } - if transport == nil { - t.Fatalf("MakeTransport() returned nil, want non-nil") + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) } if transport.TLSClientConfig.MaxVersion != tls.VersionTLS12 { t.Fatalf("MakeTransport() TLSClientConfig.MaxVersion = %d, want %d", transport.TLSClientConfig.MaxVersion, tls.VersionTLS12) @@ -157,13 +161,9 @@ func TestMakeTransportWithTLSMaxVersion(t *testing.T) { SSL: true, TLSMaxVersion: "1.3", } - tripper := opt.MakeTransport() - transport, ok := tripper.(*http.Transport) - if !ok { - t.Fatalf("MakeTransport() returned non-http.Transport, want http.Transport") - } - if transport == nil { - t.Fatalf("MakeTransport() returned nil, want non-nil") + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) } if transport.TLSClientConfig.MaxVersion != tls.VersionTLS13 { t.Fatalf("MakeTransport() TLSClientConfig.MaxVersion = %d, want %d", transport.TLSClientConfig.MaxVersion, tls.VersionTLS13) @@ -174,13 +174,9 @@ func TestMakeTransportWithTLSMaxVersion(t *testing.T) { SSL: true, TLSMaxVersion: "1.1", } - tripper := opt.MakeTransport() - transport, ok := tripper.(*http.Transport) - if !ok { - t.Fatalf("MakeTransport() returned non-http.Transport, want http.Transport") - } - if transport == nil { - t.Fatalf("MakeTransport() returned nil, want non-nil") + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) } if transport.TLSClientConfig.MaxVersion != tls.VersionTLS11 { t.Fatalf("MakeTransport() TLSClientConfig.MaxVersion = %d, want %d", transport.TLSClientConfig.MaxVersion, tls.VersionTLS11) @@ -190,3 +186,34 @@ func TestMakeTransportWithTLSMaxVersion(t *testing.T) { } } } + +// MakeTransport tests with VerifySSL false +func TestMakeTransportWithVerifySSLFalse(t *testing.T) { + opt := Opt{ + SSL: true, + } + + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) + } + if transport.TLSClientConfig.InsecureSkipVerify != true { + t.Fatalf("MakeTransport() TLSClientConfig.InsecureSkipVerify = %v, want %v", transport.TLSClientConfig.InsecureSkipVerify, true) + } +} + +// MakeTransport tests with VerifySSL true +func TestMakeTransportWithVerifySSLTrue(t *testing.T) { + opt := Opt{ + SSL: true, + VerifySSL: true, + } + + transport, err := getTransport(&opt) + if err != nil { + t.Fatalf("MakeTransport() error = %v", err) + } + if transport.TLSClientConfig.InsecureSkipVerify != false { + t.Fatalf("MakeTransport() TLSClientConfig.InsecureSkipVerify = %v, want %v", transport.TLSClientConfig.InsecureSkipVerify, false) + } +} diff --git a/go.mod b/go.mod index 4e2ebc3..5bfebcf 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kazeburo/check_http2 +module github.com/monitoring-forge/check_http2 go 1.25.0