v4

package
v0.0.0-...-6eb7966 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2023 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package v4 implements signing for AWS V4 signer

Provides request signing for request that need to be signed with AWS V4 Signatures.

Standalone Signer

Generally using the signer outside of the SDK should not require any additional logic when using Go v1.5 or higher. The signer does this by taking advantage of the URL.EscapedPath method. If your request URI requires additional escaping you many need to use the URL.Opaque to define what the raw URI should be sent to the service as.

The signer will first check the URL.Opaque field, and use its value if set. The signer does require the URL.Opaque field to be set in the form of:

"//<hostname>/<path>"

// e.g.
"//example.com/some/path"

The leading "//" and hostname are required or the URL.Opaque escaping will not work correctly.

If URL.Opaque is not set the signer will fallback to the URL.EscapedPath() method and using the returned value. If you're using Go v1.4 you must set URL.Opaque if the URI path needs escaping. If URL.Opaque is not set with Go v1.5 the signer will fallback to URL.Path.

AWS v4 signature validation requires that the canonical string's URI path element must be the URI escaped form of the HTTP request's path. http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

The Go HTTP client will perform escaping automatically on the request. Some of these escaping may cause signature validation errors because the HTTP request differs from the URI path or query that the signature was generated. https://golang.org/pkg/net/url/#URL.EscapedPath

Because of this, it is recommended that when using the signer outside of the SDK that explicitly escaping the request prior to being signed is preferable, and will help prevent signature validation errors. This can be done by setting the URL.Opaque or URL.RawPath. The SDK will use URL.Opaque first and then call URL.EscapedPath() if Opaque is not set.

If signing a request intended for HTTP2 server, and you're using Go 1.6.2 through 1.7.4 you should use the URL.RawPath as the pre-escaped form of the request URL. https://github.com/golang/go/issues/16847 points to a bug in Go pre 1.8 that fails to make HTTP2 requests using absolute URL in the HTTP message. URL.Opaque generally will force Go to make requests with absolute URL. URL.RawPath does not do this, but RawPath must be a valid escaping of Path or url.EscapedPath will ignore the RawPath escaping.

Test `TestStandaloneSign` provides a complete example of using the signer outside of the SDK and pre-escaping the URI path.

Index

Constants

This section is empty.

Variables

View Source
var SignRequestHandler = request.NamedHandler{
	Name: "v4.SignRequestHandler", Fn: SignSDKRequest,
}

SignRequestHandler is a named request handler the SDK will use to sign service client request with using the V4 signature.

Functions

func BuildNamedHandler

func BuildNamedHandler(name string, opts ...func(*Signer)) request.NamedHandler

BuildNamedHandler will build a generic handler for signing.

func GetSignedRequestSignature

func GetSignedRequestSignature(r *http.Request) ([]byte, error)

GetSignedRequestSignature attempts to extract the signature of the request. Returning an error if the request is unsigned, or unable to extract the signature.

func HasPrefixFold

func HasPrefixFold(s, prefix string) bool

HasPrefixFold tests whether the string s begins with prefix, interpreted as UTF-8 strings, under Unicode case-folding.

func SignSDKRequest

func SignSDKRequest(req *request.Request)

SignSDKRequest signs an AWS request with the V4 signature. This request handler should only be used with the SDK's built in service client's API operation requests.

This function should not be used on its on its own, but in conjunction with an AWS service client's API operation call. To sign a standalone request not created by a service client's API operation method use the "Sign" or "Presign" functions of the "Signer" type.

If the credentials of the request's config are set to credentials.AnonymousCredentials the request will not be signed.

func SignSDKRequestWithCurrentTime

func SignSDKRequestWithCurrentTime(req *request.Request, curTimeFn func() time.Time, opts ...func(*Signer))

SignSDKRequestWithCurrentTime will sign the SDK's request using the time function passed in. Behaves the same as SignSDKRequest with the exception the request is signed with the value returned by the current time function.

func WithUnsignedPayload

func WithUnsignedPayload(v4 *Signer)

WithUnsignedPayload will enable and set the UnsignedPayload field to true of the signer.

Types

type Signer

type Signer struct {
	// The authentication credentials the request will be signed against.
	// This value must be set to sign requests.
	Credentials *credentials.Credentials

	// Sets the log level the signer should use when reporting information to
	// the logger. If the logger is nil nothing will be logged. See
	// aws.LogLevelType for more information on available logging levels
	//
	// By default nothing will be logged.
	Debug aws.LogLevelType

	// The logger loging information will be written to. If there the logger
	// is nil, nothing will be logged.
	Logger aws.Logger

	// Disables the Signer's moving HTTP header key/value pairs from the HTTP
	// request header to the request's query string. This is most commonly used
	// with pre-signed requests preventing headers from being added to the
	// request's query string.
	DisableHeaderHoisting bool

	// Disables the automatic escaping of the URI path of the request for the
	// siganture's canonical string's path. For services that do not need additional
	// escaping then use this to disable the signer escaping the path.
	//
	// S3 is an example of a service that does not need additional escaping.
	//
	// http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
	DisableURIPathEscaping bool

	// Disables the automatical setting of the HTTP request's Body field with the
	// io.ReadSeeker passed in to the signer. This is useful if you're using a
	// custom wrapper around the body for the io.ReadSeeker and want to preserve
	// the Body value on the Request.Body.
	//
	// This does run the risk of signing a request with a body that will not be
	// sent in the request. Need to ensure that the underlying data of the Body
	// values are the same.
	DisableRequestBodyOverwrite bool

	// UnsignedPayload will prevent signing of the payload. This will only
	// work for services that have support for this.
	UnsignedPayload bool
	// contains filtered or unexported fields
}

Signer applies AWS v4 signing to given request. Use this to sign requests that need to be signed with AWS V4 Signatures.

func NewSigner

func NewSigner(credentials *credentials.Credentials, options ...func(*Signer)) *Signer

NewSigner returns a Signer pointer configured with the credentials and optional option values provided. If not options are provided the Signer will use its default configuration.

func (Signer) Presign

func (v4 Signer) Presign(r *http.Request, body io.ReadSeeker, service, region string, exp time.Duration, signTime time.Time) (http.Header, error)

Presign signs AWS v4 requests with the provided body, service name, region the request is made to, and time the request is signed at. The signTime allows you to specify that a request is signed for the future, and cannot be used until then.

Returns a list of HTTP headers that were included in the signature or an error if signing the request failed. For presigned requests these headers and their values must be included on the HTTP request when it is made. This is helpful to know what header values need to be shared with the party the presigned request will be distributed to.

Presign differs from Sign in that it will sign the request using query string instead of header values. This allows you to share the Presigned Request's URL with third parties, or distribute it throughout your system with minimal dependencies.

Presign also takes an exp value which is the duration the signed request will be valid after the signing time. This is allows you to set when the request will expire.

The requests body is an io.ReadSeeker so the SHA256 of the body can be generated. To bypass the signer computing the hash you can set the "X-Amz-Content-Sha256" header with a precomputed value. The signer will only compute the hash if the request header value is empty.

Presigning a S3 request will not compute the body's SHA256 hash by default. This is done due to the general use case for S3 presigned URLs is to share PUT/GET capabilities. If you would like to include the body's SHA256 in the presigned request's signature you can set the "X-Amz-Content-Sha256" HTTP header and that will be included in the request's signature.

func (Signer) Sign

func (v4 Signer) Sign(r *http.Request, body io.ReadSeeker, service, region string, signTime time.Time) (http.Header, error)

Sign signs AWS v4 requests with the provided body, service name, region the request is made to, and time the request is signed at. The signTime allows you to specify that a request is signed for the future, and cannot be used until then.

Returns a list of HTTP headers that were included in the signature or an error if signing the request failed. Generally for signed requests this value is not needed as the full request context will be captured by the http.Request value. It is included for reference though.

Sign will set the request's Body to be the `body` parameter passed in. If the body is not already an io.ReadCloser, it will be wrapped within one. If a `nil` body parameter passed to Sign, the request's Body field will be also set to nil. Its important to note that this functionality will not change the request's ContentLength of the request.

Sign differs from Presign in that it will sign the request using HTTP header values. This type of signing is intended for http.Request values that will not be shared, or are shared in a way the header values on the request will not be lost.

The requests body is an io.ReadSeeker so the SHA256 of the body can be generated. To bypass the signer computing the hash you can set the "X-Amz-Content-Sha256" header with a precomputed value. The signer will only compute the hash if the request header value is empty.

type StreamSigner

type StreamSigner struct {
	// contains filtered or unexported fields
}

StreamSigner implements signing of event stream encoded payloads.

func NewStreamSigner

func NewStreamSigner(region, service string, seedSignature []byte, credentials *credentials.Credentials) *StreamSigner

NewStreamSigner creates a SigV4 signer used to sign Event Stream encoded messages.

func (*StreamSigner) GetSignature

func (s *StreamSigner) GetSignature(headers, payload []byte, date time.Time) ([]byte, error)

GetSignature takes an event stream encoded headers and payload and returns a signature.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL