Skip to content

Commit

Permalink
Merge pull request chef#23 from go-chef/toggleSSL
Browse files Browse the repository at this point in the history
Toggle ssl
  • Loading branch information
spheromak committed Jul 9, 2014
2 parents c8177c6 + 1eb127e commit 3c69d84
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 16 additions & 4 deletions http.go
Expand Up @@ -3,6 +3,7 @@ package chef
import (
"bytes"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
Expand All @@ -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
}
Expand Down
12 changes: 8 additions & 4 deletions http_test.go
Expand Up @@ -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)
}
Expand All @@ -485,21 +486,24 @@ 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")
}
}

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)
Expand Down

0 comments on commit 3c69d84

Please sign in to comment.