Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,55 @@ type AuthConfig struct {

// Client is vessel for public methods used against the chef-server
type Client struct {
Auth *AuthConfig
client *http.Client
*AuthConfig
*http.Client
}

// 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
// It is a custom constructor wrapper aound the http Client struct intended as a easy interface for issuing signed requests
func NewClient(name string, key string) (*Client, error) {
pk, err := privateKeyFromString([]byte(key))
if err != nil {
return nil, err
}

c := &Client{
Auth: &AuthConfig{
&AuthConfig{
privateKey: pk,
clientName: name,
cryptoHash: crypto.SHA1,
},
client: &http.Client{},
&http.Client{},
}
return c, nil
}

// NewRequest is used internally by Get,Put,Head,Post,Do
func (c *Client) NewRequest(method string, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return nil, err
}
// BUG(fujin): Make a non-mutating version of this that returns a new request.
c.SignRequest(req, c.cryptoHash)
return req, nil
}

// Client Get shadows http.Get, with requset signing
// func (c *Client) Get(url string) (*http.Response, error) {
// return nil, nil
// }

// MakeRequest performs a signed request for the chef client
func (c *Client) MakeRequest(method string, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
// don't have to check this works, signRequest only emits error when signing hash is not valid, and we baked that in
c.Auth.SignRequest(req, c.Auth.cryptoHash)
c.SignRequest(req, c.cryptoHash)

res, err := c.client.Do(req)
res, err := c.Do(req)
if err != nil {
return nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ func TestNewClient(t *testing.T) {
t.Error("Couldn't make a valid client...\n", err)
}
// simple validation on the created client
if c.Auth.clientName != "testclient" {
t.Error("unexpected client name: ", c.Auth.clientName)
if c.clientName != "testclient" {
t.Error("unexpected client name: ", c.clientName)
}

// Bad PEM should be an error
Expand All @@ -443,17 +443,22 @@ func TestNewClient(t *testing.T) {
}
}

func TestMakeRequest(t *testing.T) {
func TestClient(t *testing.T) {
server := createServer()
c, _ := NewClient("testclient", privateKey)
defer server.Close()

resp, err := c.MakeRequest("GET", server.URL, nil)
req, err := c.NewRequest("GET", server.URL, nil)
if err != nil {
t.Error("HRRRM! we tried to make a request but it failed :`( ", err)
t.Error("could not create request to:", server.URL)
}

resp, err := c.Do(req)
if err != nil {
t.Error("do req failed:", err)
}
if resp.StatusCode != 200 {
t.Error("Non 200 return code: ", resp.Status)
t.Error("non 200 return code: ", resp.Status)
}

// This should fail because we've got an invalid URI
Expand Down