Skip to content

Commit

Permalink
add support for custom tls config
Browse files Browse the repository at this point in the history
  • Loading branch information
fate0 committed May 28, 2020
1 parent 3661474 commit dab6a69
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/cassowary/cli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"

Expand Down Expand Up @@ -151,6 +154,27 @@ func validateCLI(c *cli.Context) error {
httpMethod = "GET"
}

tlsConfig := new(tls.Config)
if c.String("ca") != "" {
pemCerts, err := ioutil.ReadFile(c.String("ca"))
if err != nil {
return err
}
ca := x509.NewCertPool()
if !ca.AppendCertsFromPEM(pemCerts) {
return fmt.Errorf("failed to read CA from PEM")
}
tlsConfig.RootCAs = ca
}

if c.String("cert") != "" && c.String("key") != "" {
cert, err := tls.LoadX509KeyPair(c.String("cert"), c.String("key"))
if err != nil {
return err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}

cass := &client.Cassowary{
FileMode: false,
BaseURL: c.String("url"),
Expand All @@ -159,6 +183,7 @@ func validateCLI(c *cli.Context) error {
RequestHeader: header,
Duration: duration,
PromExport: prometheusEnabled,
TLSConfig: tlsConfig,
PromURL: c.String("prompushgwurl"),
Cloudwatch: c.Bool("cloudwatch"),
ExportMetrics: c.Bool("json-metrics"),
Expand Down Expand Up @@ -282,6 +307,18 @@ func runCLI(args []string) {
Name: "disable-keep-alive",
Usage: "use this flag to disable http keep-alive",
},
cli.StringFlag{
Name: "ca",
Usage: "certificate authority",
},
cli.StringFlag{
Name: "cert",
Usage: "authentication certificate",
},
cli.StringFlag{
Name: "key",
Usage: "authentication key",
},
},
Action: validateCLIFile,
},
Expand Down Expand Up @@ -345,6 +382,18 @@ func runCLI(args []string) {
Name: "disable-keep-alive",
Usage: "use this flag to disable http keep-alive",
},
cli.StringFlag{
Name: "ca",
Usage: "certificate authority",
},
cli.StringFlag{
Name: "cert",
Usage: "authentication certificate",
},
cli.StringFlag{
Name: "key",
Usage: "authentication key",
},
},
Action: validateCLI,
},
Expand Down
1 change: 1 addition & 0 deletions pkg/client/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (c *Cassowary) Coordinate() (ResultMetrics, error) {
c.Client = &http.Client{
Timeout: time.Second * time.Duration(c.Timeout),
Transport: &http.Transport{
TLSClientConfig: c.TLSConfig,
MaxIdleConnsPerHost: 10000,
DisableCompression: false,
DisableKeepAlives: c.DisableKeepAlive,
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"crypto/tls"
"net/http"

"github.com/schollz/progressbar"
Expand All @@ -18,6 +19,7 @@ type Cassowary struct {
ExportMetricsFile string
PromExport bool
Cloudwatch bool
TLSConfig *tls.Config
PromURL string
RequestHeader []string
URLPaths []string
Expand Down

0 comments on commit dab6a69

Please sign in to comment.