Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from nikkictl/master
Browse files Browse the repository at this point in the history
Fix windows cert bug, job id and linter errors
  • Loading branch information
Caleb Hailey authored May 29, 2020
2 parents cdba7cc + c112312 commit 359e773
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed
- Fixed system root pool bug on Windows.
- Fixed linter, style, and format errors.
- Fixed bug where `--id` would always be overwritten by a random UUID.

## [0.0.1] - 2000-01-01

### Added
Expand Down
86 changes: 39 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"bytes"
"errors"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -27,11 +27,12 @@ type Config struct {
Subscriptions string
Timeout string
RuntimeAssets string
SensuApiUrl string
SensuAPIUrl string
SensuAccessToken string
SensuTrustedCaFile string
}

// JobRequest represents a job request.
type JobRequest struct {
Check string `json:"check"`
Subscriptions []string `json:"subscriptions"`
Expand All @@ -47,16 +48,16 @@ var (
}

options = []*sensu.PluginConfigOption{
&sensu.PluginConfigOption{
{
Path: "id",
Env: "SENSU_RUNBOOK_JOB_ID",
Argument: "id",
Shorthand: "i",
Default: "",
Default: uuid.New().String(),
Usage: "The ID or name to use for the job (i.e. defaults to a random UUIDv4)",
Value: &config.JobID,
},
&sensu.PluginConfigOption{
{
Path: "command",
Env: "SENSU_RUNBOOK_COMMAND",
Argument: "command",
Expand All @@ -65,7 +66,7 @@ var (
Usage: "The command that should be executed by the Sensu Go agent(s)",
Value: &config.Command,
},
&sensu.PluginConfigOption{
{
Path: "timeout",
Env: "SENSU_RUNBOOK_TIMEOUT",
Argument: "timeout",
Expand All @@ -74,7 +75,7 @@ var (
Usage: "Command execution timeout, in seconds",
Value: &config.Command,
},
&sensu.PluginConfigOption{
{
Path: "runtime-assets",
Env: "SENSU_RUNBOOK_ASSETS",
Argument: "runtime-assets",
Expand All @@ -83,7 +84,7 @@ var (
Usage: "Comma-separated list of assets to distribute with the command(s)",
Value: &config.RuntimeAssets,
},
&sensu.PluginConfigOption{
{
Path: "subscriptions",
Env: "SENSU_RUNBOOK_SUBSCRIPTIONS",
Argument: "subscriptions",
Expand All @@ -92,7 +93,7 @@ var (
Usage: "Comma-separated list of subscriptions to execute the command(s) on",
Value: &config.Subscriptions,
},
&sensu.PluginConfigOption{
{
Path: "namespace",
Env: "SENSU_NAMESPACE", // provided by the sensuctl command plugin execution environment
Argument: "namespace",
Expand All @@ -101,16 +102,16 @@ var (
Usage: "Sensu Namespace to perform the runbook automation (defaults to $SENSU_NAMESPACE)",
Value: &config.Namespace,
},
&sensu.PluginConfigOption{
{
Path: "sensu-api-url",
Env: "SENSU_API_URL", // provided by the sensuctl command plugin execution environment
Argument: "sensu-api-url",
Shorthand: "",
Default: "",
Usage: "Sensu API URL (defaults to $SENSU_API_URL)",
Value: &config.SensuApiUrl,
Value: &config.SensuAPIUrl,
},
&sensu.PluginConfigOption{
{
Path: "sensu-access-token",
Env: "SENSU_ACCESS_TOKEN", // provided by the sensuctl command plugin execution environment
Argument: "sensu-access-token",
Expand All @@ -119,7 +120,7 @@ var (
Usage: "Sensu API Access Token (defaults to $SENSU_ACCESS_TOKEN)",
Value: &config.SensuAccessToken,
},
&sensu.PluginConfigOption{
{
Path: "sensu-trusted-ca-file",
Env: "SENSU_TRUSTED_CA_FILE", // provided by the sensuctl command plugin execution environment
Argument: "sensu-trusted-ca-file",
Expand All @@ -137,7 +138,7 @@ func main() {
}

func checkArgs(event *types.Event) (int, error) {
if len(config.SensuApiUrl) == 0 {
if len(config.SensuAPIUrl) == 0 {
return sensu.CheckStateCritical, errors.New("--sensu-api-url flag or $SENSU_API_URL environment variable must be set")
} else if len(config.Namespace) == 0 {
return sensu.CheckStateCritical, errors.New("--namespace flag or $SENSU_NAMESPACE environment variable must be set")
Expand All @@ -153,27 +154,22 @@ func executePlaybook(event *types.Event) (int, error) {
// TODO: use the sensu-plugin-sdk HTTP client (reference: https://github.com/sensu/sensu-ec2-handler/blob/master/main.go#L12)
job, err := generateCheckConfig()
if err != nil {
fmt.Errorf("ERROR: %s\n", err)
return sensu.CheckStateCritical, fmt.Errorf("ERROR: %s", err)
}
log.Printf("registering runbook job ID %s/%s with --command %s\n", job.Namespace, job.Name, config.Command)
err = createJob(&job)
if err != nil {
return sensu.CheckStateCritical, err
} else {
log.Printf("registering runbook job ID %s/%s with --command %s\n", job.Namespace, job.Name, config.Command)
err = createJob(&job)
if err != nil {
return sensu.CheckStateCritical, err
} else {
err = executeJob(&job)
}
if err != nil {
return sensu.CheckStateCritical, nil
} else {
return sensu.CheckStateOK, nil
}
}
err = executeJob(&job)
if err != nil {
return sensu.CheckStateCritical, nil
}
return sensu.CheckStateOK, nil
}

func generateCheckConfig() (types.CheckConfig, error) {
// Build CheckConfig object
config.JobID = uuid.New().String()
// Build CheckConfig object
var timeout, _ = strconv.Atoi(config.Timeout)
var labels = make(map[string]string)
var job = types.CheckConfig{
Expand All @@ -194,11 +190,12 @@ func generateCheckConfig() (types.CheckConfig, error) {
return job, nil
}

// LoadCACerts loads the system cert pool.
func LoadCACerts(path string) (*x509.CertPool, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
log.Fatalf("ERROR: failed to load system cert pool: %s", err)
return nil, err
log.Printf("ERROR: failed to load system cert pool: %s", err)
rootCAs = x509.NewCertPool()
}
if rootCAs == nil {
rootCAs = x509.NewCertPool()
Expand All @@ -208,14 +205,13 @@ func LoadCACerts(path string) (*x509.CertPool, error) {
if err != nil {
log.Fatalf("ERROR: failed to read CA file (%s): %s", path, err)
return nil, err
} else {
rootCAs.AppendCertsFromPEM(certs)
}
rootCAs.AppendCertsFromPEM(certs)
}
return rootCAs, nil
}

func initHttpClient() *http.Client {
func initHTTPClient() *http.Client {
certs, err := LoadCACerts(config.SensuTrustedCaFile)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
Expand All @@ -241,15 +237,15 @@ func createJob(job *types.CheckConfig) error {
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/api/core/v2/namespaces/%s/checks",
config.SensuApiUrl,
config.SensuAPIUrl,
config.Namespace,
),
body,
)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
var httpClient *http.Client = initHttpClient()
var httpClient *http.Client = initHTTPClient()
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.SensuAccessToken))
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
Expand Down Expand Up @@ -282,19 +278,19 @@ func createJob(job *types.CheckConfig) error {
}

func executeJob(job *types.CheckConfig) error {
var job_request = JobRequest{
var jobRequest = JobRequest{
Check: job.Name,
Subscriptions: strings.Split(config.Subscriptions, ","),
}
postBody, err := json.Marshal(job_request)
postBody, err := json.Marshal(jobRequest)
if err != nil {
log.Fatal("ERROR: ", err)
}
body := bytes.NewReader(postBody)
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/api/core/v2/namespaces/%s/checks/%s/execute",
config.SensuApiUrl,
config.SensuAPIUrl,
config.Namespace,
config.JobID,
),
Expand All @@ -303,7 +299,7 @@ func executeJob(job *types.CheckConfig) error {
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
var httpClient *http.Client = initHttpClient()
var httpClient *http.Client = initHTTPClient()
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.SensuAccessToken))
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
Expand All @@ -325,12 +321,8 @@ func executeJob(job *types.CheckConfig) error {
if err != nil {
log.Fatalf("ERROR: %s\n", err)
return err
} else {
fmt.Printf("%s\n", string(b))
return nil
}
fmt.Printf("%s\n", string(b))
return nil
}

return err
}

0 comments on commit 359e773

Please sign in to comment.