-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #509 from smallstep/herman/attestation-client-requ…
…est-id Add `X-Request-Id` and `User-Agent` headers to attestation requests
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package attestation | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"go.step.sm/crypto/randutil" | ||
) | ||
|
||
type requestIDContextKey struct{} | ||
|
||
// NewRequestIDContext returns a new context with the given request ID added to the | ||
// context. | ||
func NewRequestIDContext(ctx context.Context, requestID string) context.Context { | ||
return context.WithValue(ctx, requestIDContextKey{}, requestID) | ||
} | ||
|
||
// RequestIDFromContext returns the request ID from the context if it exists. | ||
// and is not empty. | ||
func RequestIDFromContext(ctx context.Context) (string, bool) { | ||
v, ok := ctx.Value(requestIDContextKey{}).(string) | ||
return v, ok && v != "" | ||
} | ||
|
||
// requestIDHeader is the header name used for propagating request IDs from | ||
// the attestation client to the attestation CA and back again. | ||
const requestIDHeader = "X-Request-Id" | ||
|
||
// newRequestID generates a new random UUIDv4 request ID. If it fails, | ||
// the request ID will be the empty string. | ||
func newRequestID() string { | ||
requestID, err := randutil.UUIDv4() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return requestID | ||
} | ||
|
||
// enforceRequestID checks if the X-Request-Id HTTP header is filled. If it's | ||
// empty, the context is searched for a request ID. If that's also empty, a new | ||
// request ID is generated. | ||
func enforceRequestID(r *http.Request) { | ||
if requestID := r.Header.Get(requestIDHeader); requestID == "" { | ||
if reqID, ok := RequestIDFromContext(r.Context()); ok { | ||
requestID = reqID | ||
} else { | ||
requestID = newRequestID() | ||
} | ||
r.Header.Set(requestIDHeader, requestID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package attestation | ||
|
||
import "net/http" | ||
|
||
// UserAgent is the value of the User-Agent HTTP header that will | ||
// be set in HTTP requests to the attestation CA. | ||
var UserAgent = "step-attestation-http-client/1.0" | ||
|
||
// setUserAgent sets the User-Agent header in HTTP requests. | ||
func setUserAgent(r *http.Request) { | ||
r.Header.Set("User-Agent", UserAgent) | ||
} |