Skip to content

Commit

Permalink
Additional validation for endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Plourde <[email protected]>
  • Loading branch information
palourde committed Feb 6, 2020
1 parent 4b05c40 commit 742a51a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
25 changes: 21 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"errors"
"fmt"
"net/url"
"path"

"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-go/types"
Expand All @@ -19,6 +22,10 @@ type Handler struct {
timeout int
}

const (
defaultAPIPath = "pdb/query/v4/nodes"
)

var (
handler = Handler{
PluginConfig: sensu.PluginConfig{
Expand Down Expand Up @@ -89,26 +96,36 @@ func main() {
}

func validate(_ *types.Event) error {
// Make sure all required options are provided
if handler.endpoint == "" {
return errors.New("the PuppetDB API endpoint is required")
}

if handler.keystoreFile == "" {
return errors.New("the path to the SSL certificate keystore is required")
}

if handler.keystorePassword == "" {
return errors.New("the SSL certificate keystore password is required")
}

if handler.truststoreFile == "" {
return errors.New("the path for the SSL certificate truststore is required")
}

if handler.truststorePassword == "" {
return errors.New("the SSL certificate truststore password is required")
}

// Make sure the endpoint URL is valid
u, err := url.Parse(handler.endpoint)
if err != nil {
return fmt.Errorf("invalid PuppetDB API endpoint URL: %s", err)
}
if u.Scheme == "" || u.Host == "" {
return errors.New("invalid PuppetDB API endpoint URL")
}
if u.Path == "" || u.Path == "/" {
u.Path = path.Join(u.Path, defaultAPIPath)
handler.endpoint = u.String()
}

return nil
}

Expand Down
34 changes: 31 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ func TestMain(t *testing.T) {

func Test_validate(t *testing.T) {
tests := []struct {
name string
testHandler Handler
wantErr bool
name string
testHandler Handler
wantEndpoint string
wantErr bool
}{
{
name: "required endpoint",
Expand Down Expand Up @@ -63,13 +64,40 @@ func Test_validate(t *testing.T) {
},
wantErr: false,
},
{
name: "valid endpoint is required",
testHandler: Handler{
endpoint: "foo",
keystoreFile: "keystore.jks",
keystorePassword: "P@ssw0rd!",
truststoreFile: "truststore.jks",
truststorePassword: "P@ssw0rd!",
},
wantErr: true,
},
{
name: "default API path is appended if missing",
testHandler: Handler{
endpoint: "http://127.0.0.1/",
keystoreFile: "keystore.jks",
keystorePassword: "P@ssw0rd!",
truststoreFile: "truststore.jks",
truststorePassword: "P@ssw0rd!",
},
wantErr: false,
wantEndpoint: "http://127.0.0.1/pdb/query/v4/nodes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler = tt.testHandler
if err := validate(nil); (err != nil) != tt.wantErr {
t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
}

if tt.wantEndpoint != "" && tt.wantEndpoint != handler.endpoint {
t.Errorf("validate() endpoint = %v, want %v", handler.endpoint, tt.wantEndpoint)
}
})
}
}

0 comments on commit 742a51a

Please sign in to comment.