Skip to content

Commit

Permalink
Merge pull request #75 from theckman/no_hashed_path
Browse files Browse the repository at this point in the history
make go-chef HTTP headers compliant with RFC 2616 (HTTP/1.1) and go1.6
  • Loading branch information
AJ Christensen committed Feb 19, 2016
2 parents 849dab5 + 6c77fc6 commit 9ecc09c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,25 @@ func (ac AuthConfig) SignRequest(request *http.Request) error {
endpoint = request.URL.Path
}

request.Header.Set("Method", request.Method)
request.Header.Set("Hashed Path", HashStr(endpoint))
request.Header.Set("Accept", "application/json")
request.Header.Set("X-Chef-Version", ChefVersion)
request.Header.Set("X-Ops-Timestamp", time.Now().UTC().Format(time.RFC3339))
request.Header.Set("X-Ops-UserId", ac.ClientName)
request.Header.Set("X-Ops-Sign", "algorithm=sha1;version=1.0")
vals := map[string]string{
"Method": request.Method,
"Hashed Path": HashStr(endpoint),
"Accept": "application/json",
"X-Chef-Version": ChefVersion,
"X-Ops-Timestamp": time.Now().UTC().Format(time.RFC3339),
"X-Ops-UserId": ac.ClientName,
"X-Ops-Sign": "algorithm=sha1;version=1.0",
"X-Ops-Content-Hash": request.Header.Get("X-Ops-Content-Hash"),
}

for _, key := range []string{"Method", "Accept", "X-Chef-Version", "X-Ops-Timestamp", "X-Ops-UserId", "X-Ops-Sign"} {
request.Header.Set(key, vals[key])
}

// To validate the signature it seems to be very particular
var content string
for _, key := range []string{"Method", "Hashed Path", "X-Ops-Content-Hash", "X-Ops-Timestamp", "X-Ops-UserId"} {
content += fmt.Sprintf("%s:%s\n", key, request.Header.Get(key))
content += fmt.Sprintf("%s:%s\n", key, vals[key])
}
content = strings.TrimSuffix(content, "\n")
// generate signed string of headers
Expand Down

0 comments on commit 9ecc09c

Please sign in to comment.