awsign

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2016 License: MIT Imports: 9 Imported by: 1

README

Golang awsign

Build Status Go Report Card

awsign is a Go library for signing AWS requests according to the specifications of the AWS Signature Version 4 Signing Process.

Why?

AWS provides a full featured Go SDK but this tiny library allows to sign just simple requests when you don't need to install the full SDK. Besides, Go is fun 😉

Install

$ go get github.com/fsaravia/awsign

Usage

Create the HTTP request you'd like to sign.

parsedUrl, _ := url.Parse("https://iam.amazonaws.com/")

query := url.Values{}
query.Add("Action", "ListUsers")
query.Add("Version", "2010-05-08")

parsedUrl.RawQuery = query.Encode()

request, _ := http.NewRequest(http.MethodGet, parsedUrl.String(), nil)

request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")

Instantiate a Signer and sign it!

signer := awsign.Signer{
	Region:          "us-east-1",
	Service:         "iam",
	AccessKeyID:     "<YOUR-ACCESS-KEY-ID>",
	AccessKeySecret: "<YOUR-ACCESS-KEY-SECRET>"}

signer.Sign(request, "")

If your request includes a body, pass its payload in the second parameter of Sign

// Payload to send an email via SES

form := url.Values{}
form.Add("Action", "SendEmail")
form.Add("Destination.ToAddresses.member.1", "destination@example.org")
form.Add("Message.Body.Text.Data", "Test email body")
form.Add("Message.Subject.Data", "Test email subject")
form.Add("Source", "noreply@example.org")

payload := form.Encode()

request, _ := http.NewRequest(requestMethod, parsedUrl.String(), strings.NewReader(payload))

signer := awsign.Signer{
	Region:          "us-east-1",
	Service:         "ses",
	AccessKeyID:     "<YOUR-ACCESS-KEY-ID>",
	AccessKeySecret: "<YOUR-ACCESS-KEY-SECRET>"}

signer.Sign(request, payload)

Documentation

Overview

Package aswign implments a simple library for signing HTTP requests made to Amazon AWS according to the specifications of the AWS Signature Version 4 Signing Process: http://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html.

It exposes a Signer struct which can be used to set the necessary configuration variables:

signer := awsign.Signer{
	Region:          "us-east-1",
	Service:         "iam",
	AccessKeyID:     "<YOUR-ACCESS-KEY-ID>",
	AccessKeySecret: "<YOUR-ACCESS-KEY-SECRET>"}

Signer exposes a Sign method which accepts an http.Request object and an optional payload with the request body:

request, _ := http.NewRequest(http.MethodGet, "http://example.org", nil)

payload = "Sample request body"

signer.Sign(request, payload)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Signature

func Signature(request *http.Request, payload string, timestamp time.Time, region, service, key string) string

Signature generates the request signature that has to be added to the Authorization header of a requests according to AWS documentation. This method can be used to manually generate the signature without using the Signer.Sign method.

Direct Callers of this method should handle the creation of the Authorization header manually.

Types

type Signer

type Signer struct {
	Region          string
	Service         string
	AccessKeyID     string
	AccessKeySecret string
}

Signer is a convenience mechanism for storing the configuration variables that are necessary for signing requests made to AWS, it allows users to instantiate it once and reuse it over several requests.

func (*Signer) Sign

func (signer *Signer) Sign(request *http.Request, payload string)

Sign accepts a request and an optional payload and signs the request by adding an Authorization header with the content required by Amazon AWS

http://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html

Jump to

Keyboard shortcuts

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