From ef689b079f0cb28311a9077e6b9ce37e974cfb63 Mon Sep 17 00:00:00 2001 From: mcorbin Date: Sun, 2 Jun 2024 19:36:50 +0200 Subject: [PATCH] New Go client version --- go.mod | 2 +- go.sum | 2 + .../github.com/appclacks/go-client/client.go | 87 +++++++++++++++++-- vendor/github.com/appclacks/go-client/tls.go | 37 ++++++++ vendor/modules.txt | 2 +- 5 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 vendor/github.com/appclacks/go-client/tls.go diff --git a/go.mod b/go.mod index 0d3cce3..abb5981 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appclacks/cli go 1.22.0 require ( - github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 + github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 github.com/cheynewallace/tabby v1.1.1 github.com/spf13/cobra v1.8.0 ) diff --git a/go.sum b/go.sum index 94224d5..10dadd1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 h1:Bi2DlXudM58MLRjAEWRNrJe8ChG54r5mE8aJTH6AkUg= github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 h1:kTNVmMjdtr/X0ljUCyDCXUzDYnP3JL5fdDMwBSUveOg= +github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= github.com/cheynewallace/tabby v1.1.1 h1:JvUR8waht4Y0S3JF17G6Vhyt+FRhnqVCkk8l4YrOU54= github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/vendor/github.com/appclacks/go-client/client.go b/vendor/github.com/appclacks/go-client/client.go index cda12a5..e7671c9 100644 --- a/vendor/github.com/appclacks/go-client/client.go +++ b/vendor/github.com/appclacks/go-client/client.go @@ -17,6 +17,10 @@ type Client struct { username string password string endpoint string + key string + cert string + cacert string + insecure bool } var ( @@ -35,27 +39,96 @@ func loadEnv(client *Client) { if os.Getenv("APPCLACKS_API_ENDPOINT") != "" { client.endpoint = os.Getenv("APPCLACKS_API_ENDPOINT") } + + if os.Getenv("APPCLACKS_TLS_KEY") != "" { + client.key = os.Getenv("APPCLACKS_TLS_KEY") + } + if os.Getenv("APPCLACKS_TLS_CERT") != "" { + client.cert = os.Getenv("APPCLACKS_TLS_CERT") + } + if os.Getenv("APPCLACKS_TLS_CACERT") != "" { + client.cacert = os.Getenv("APPCLACKS_TLS_CACERT") + } + insecure := os.Getenv("APPCLACKS_TLS_INSECURE") + if insecure == "true" { + client.insecure = true + } } -func New() (*Client, error) { +type ClientOption func(c *Client) error + +func New(options ...ClientOption) (*Client, error) { client := &Client{ http: &http.Client{}, } loadEnv(client) + for _, option := range options { + err := option(client) + if err != nil { + return nil, err + } + } + if client.cert != "" || client.key != "" || client.cacert != "" || client.insecure { + tlsConfig, err := getTLSConfig(client.key, client.cert, client.cacert, "", client.insecure) + if err != nil { + return nil, err + } + transport := &http.Transport{ + TLSClientConfig: tlsConfig, + } + client.http.Transport = transport + } return client, nil } -func (c *Client) SetUsername(username string) { - c.username = username +func WithUsername(username string) ClientOption { + return func(c *Client) error { + c.username = username + return nil + } +} + +func WithPassword(password string) ClientOption { + return func(c *Client) error { + c.password = password + return nil + } } -func (c *Client) SetPassword(password string) { - c.password = password +func WithEndpoint(endpoint string) ClientOption { + return func(c *Client) error { + c.endpoint = endpoint + return nil + } +} + +func WithKey(key string) ClientOption { + return func(c *Client) error { + c.key = key + return nil + } } -func (c *Client) SetEndpoint(endpoint string) { - c.endpoint = endpoint +func WithCert(cert string) ClientOption { + return func(c *Client) error { + c.cert = cert + return nil + } +} + +func WithCacert(cacert string) ClientOption { + return func(c *Client) error { + c.cacert = cacert + return nil + } +} + +func WithInsecure(insecure bool) ClientOption { + return func(c *Client) error { + c.insecure = insecure + return nil + } } func (c *Client) sendRequest(ctx context.Context, url string, method string, body any, result any, queryParams map[string]string) (*http.Response, error) { diff --git a/vendor/github.com/appclacks/go-client/tls.go b/vendor/github.com/appclacks/go-client/tls.go new file mode 100644 index 0000000..7885717 --- /dev/null +++ b/vendor/github.com/appclacks/go-client/tls.go @@ -0,0 +1,37 @@ +package client + +import ( + "crypto/tls" + "crypto/x509" + "fmt" + "os" +) + +func getTLSConfig(keyPath string, certPath string, cacertPath string, serverName string, insecure bool) (*tls.Config, error) { + tlsConfig := &tls.Config{} + if keyPath != "" { + cert, err := tls.LoadX509KeyPair(certPath, keyPath) + if err != nil { + return nil, fmt.Errorf("fail to load certificates: %w", err) + } + tlsConfig.Certificates = []tls.Certificate{cert} + } + if cacertPath != "" { + caCert, err := os.ReadFile(cacertPath) + if err != nil { + return nil, fmt.Errorf("fail to load ca certificate: %w", err) + } + caCertPool := x509.NewCertPool() + result := caCertPool.AppendCertsFromPEM(caCert) + if !result { + return nil, fmt.Errorf("fail to read ca certificate on %s", certPath) + } + tlsConfig.RootCAs = caCertPool + + } + if serverName != "" { + tlsConfig.ServerName = serverName + } + tlsConfig.InsecureSkipVerify = insecure + return tlsConfig, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index c909461..e51d64b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 +# github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 ## explicit; go 1.22.0 github.com/appclacks/go-client # github.com/cheynewallace/tabby v1.1.1