diff --git a/http.go b/http.go index 5f7c87162bc..9a75bef5c35 100644 --- a/http.go +++ b/http.go @@ -3,6 +3,7 @@ package chef import ( "bytes" "crypto/rsa" + "crypto/tls" "crypto/x509" "encoding/pem" "fmt" @@ -28,21 +29,32 @@ type Client struct { 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) (*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: cfg.SkipSSL}, + } + c := &Client{ Auth: &AuthConfig{ privateKey: pk, - clientName: name, + clientName: cfg.Name, }, - client: &http.Client{}, + client: &http.Client{Transport: tr}, } return c, nil } diff --git a/http_test.go b/http_test.go index 867e5a97c69..e59b97b65d9 100644 --- a/http_test.go +++ b/http_test.go @@ -475,7 +475,8 @@ func TestRequestError(t *testing.T) { } func TestNewClient(t *testing.T) { - c, err := NewClient("testclient", privateKey) + 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) } @@ -485,13 +486,15 @@ func TestNewClient(t *testing.T) { } // Bad PEM should be an error - c, err = NewClient("blah", "not a key") + 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) + 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") } @@ -499,7 +502,8 @@ func TestNewClient(t *testing.T) { func TestMakeRequest(t *testing.T) { server := createServer() - c, _ := NewClient("testclient", privateKey) + cfg := &Config{Name: "testclient", Key: privateKey, SkipSSL: false} + c, _ := NewClient(cfg) defer server.Close() resp, err := c.MakeRequest("GET", server.URL, nil)