From 602540563c44a533277a8a9c5bf37a2d204a150c Mon Sep 17 00:00:00 2001 From: Jesse Nelson Date: Wed, 9 Jul 2014 09:14:20 -0700 Subject: [PATCH 1/2] allow setting of SSL auth config when creating client --- http.go | 15 +++++++++++---- http_test.go | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/http.go b/http.go index 7507f9aef16..b794c728817 100644 --- a/http.go +++ b/http.go @@ -3,6 +3,7 @@ package chef import ( "bytes" "crypto/rsa" + "crypto/tls" "crypto/x509" "encoding/pem" "fmt" @@ -25,25 +26,31 @@ type AuthConfig struct { // Client is vessel for public methods used against the chef-server type Client struct { - Auth *AuthConfig - client *http.Client + Auth *AuthConfig + client *http.Client + InsecureSSL bool } // NewClient is the client generator used to instantiate a client for talking to a chef-server // It is a simple constructor for the Client struct intended as a easy interface for issuing // signed requests -func NewClient(name string, key string) (*Client, error) { +func NewClient(name string, key string, skipSSL bool) (*Client, error) { pk, err := privateKeyFromString([]byte(key)) if err != nil { return nil, err } + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: skipSSL}, + } + c := &Client{ Auth: &AuthConfig{ privateKey: pk, clientName: name, }, - client: &http.Client{}, + client: &http.Client{Transport: tr}, + InsecureSSL: skipSSL, } return c, nil } diff --git a/http_test.go b/http_test.go index a3fa602ac00..efb93524c61 100644 --- a/http_test.go +++ b/http_test.go @@ -451,7 +451,7 @@ func TestRequestError(t *testing.T) { } func TestNewClient(t *testing.T) { - c, err := NewClient("testclient", privateKey) + c, err := NewClient("testclient", privateKey, false) if err != nil { t.Error("Couldn't make a valid client...\n", err) } @@ -461,13 +461,13 @@ func TestNewClient(t *testing.T) { } // Bad PEM should be an error - c, err = NewClient("blah", "not a key") + c, err = NewClient("blah", "not a key", false) if err == nil { t.Error("Built a client from a bad key string") } // Not a proper key should be an error - c, err = NewClient("blah", badPrivateKey) + c, err = NewClient("blah", badPrivateKey, false) if err == nil { t.Error("Built a client from a bad key string") } @@ -475,7 +475,7 @@ func TestNewClient(t *testing.T) { func TestMakeRequest(t *testing.T) { server := createServer() - c, _ := NewClient("testclient", privateKey) + c, _ := NewClient("testclient", privateKey, false) defer server.Close() resp, err := c.MakeRequest("GET", server.URL, nil) From 1eb127e74708ced7e0a87ba65153f0dfc5662a5c Mon Sep 17 00:00:00 2001 From: Kraig Amador Date: Wed, 9 Jul 2014 10:27:36 -0700 Subject: [PATCH 2/2] Using a Config struct to configure the client --- http.go | 23 ++++++++++++++--------- http_test.go | 16 ++++++++++------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/http.go b/http.go index b794c728817..39ef47c9e43 100644 --- a/http.go +++ b/http.go @@ -26,31 +26,36 @@ type AuthConfig struct { // Client is vessel for public methods used against the chef-server type Client struct { - Auth *AuthConfig - client *http.Client - InsecureSSL bool + Auth *AuthConfig + client *http.Client +} + +// Config contains the configuration options for a chef client +type Config struct { + Name string + Key string + SkipSSL bool } // NewClient is the client generator used to instantiate a client for talking to a chef-server // It is a simple constructor for the Client struct intended as a easy interface for issuing // signed requests -func NewClient(name string, key string, skipSSL bool) (*Client, error) { - pk, err := privateKeyFromString([]byte(key)) +func NewClient(cfg *Config) (*Client, error) { + pk, err := privateKeyFromString([]byte(cfg.Key)) if err != nil { return nil, err } tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: skipSSL}, + TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.SkipSSL}, } c := &Client{ Auth: &AuthConfig{ privateKey: pk, - clientName: name, + clientName: cfg.Name, }, - client: &http.Client{Transport: tr}, - InsecureSSL: skipSSL, + client: &http.Client{Transport: tr}, } return c, nil } diff --git a/http_test.go b/http_test.go index efb93524c61..f6c13f5de69 100644 --- a/http_test.go +++ b/http_test.go @@ -7,8 +7,6 @@ import ( "encoding/pem" "errors" "fmt" - . "github.com/ctdk/goiardi/chefcrypto" - . "github.com/smartystreets/goconvey/convey" "io" "net/http" "net/http/httptest" @@ -16,6 +14,8 @@ import ( "strconv" "strings" "testing" + . "github.com/ctdk/goiardi/chefcrypto" + . "github.com/smartystreets/goconvey/convey" ) var testRequiredHeaders = []string{ @@ -451,7 +451,8 @@ func TestRequestError(t *testing.T) { } func TestNewClient(t *testing.T) { - c, err := NewClient("testclient", privateKey, false) + cfg := &Config{Name: "testclient", Key: privateKey, SkipSSL: false} + c, err := NewClient(cfg) if err != nil { t.Error("Couldn't make a valid client...\n", err) } @@ -461,13 +462,15 @@ func TestNewClient(t *testing.T) { } // Bad PEM should be an error - c, err = NewClient("blah", "not a key", false) + cfg = &Config{Name: "blah", Key: "not a key", SkipSSL: false} + c, err = NewClient(cfg) if err == nil { t.Error("Built a client from a bad key string") } // Not a proper key should be an error - c, err = NewClient("blah", badPrivateKey, false) + cfg = &Config{Name: "blah", Key: badPrivateKey, SkipSSL: false} + c, err = NewClient(cfg) if err == nil { t.Error("Built a client from a bad key string") } @@ -475,7 +478,8 @@ func TestNewClient(t *testing.T) { func TestMakeRequest(t *testing.T) { server := createServer() - c, _ := NewClient("testclient", privateKey, false) + cfg := &Config{Name: "testclient", Key: privateKey, SkipSSL: false} + c, _ := NewClient(cfg) defer server.Close() resp, err := c.MakeRequest("GET", server.URL, nil)