Skip to content

Instantly share code, notes, and snippets.

@lynsei
Created December 19, 2015 03:41
Show Gist options
  • Save lynsei/c0cb21dc7b4e032ae9c4 to your computer and use it in GitHub Desktop.
Save lynsei/c0cb21dc7b4e032ae9c4 to your computer and use it in GitHub Desktop.
gist copied code that I can reference in the future for creating go-language compiled endpoints for web socket apps
// this shows a client implementation for HTTP & HMAC hashing using AES256bit encryption
// it also shows date functions and other things that might be useful as I start to move all my gateway code to compiled go-lang interfaces
// copied from: https://github.com/tav/dynamodb/blob/master/dynamodb.go#L436
// looking forward towards implementing this kind of code for ubernetes and beanstalk containers
func (c *Client) RawRequest(method string, payload []byte) ([]byte, error) {
req, err := http.NewRequest("POST", c.endpoint.url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
hasher := sha256.New()
hasher.Write(payload)
datetime := time.Now().UTC().Format(iso8601)
date := datetime[:8]
method = "DynamoDB_20120810." + method
canonicalReq := "POST\n/\n\ncontent-type:application/x-amz-json-1.0\nhost:" + c.endpoint.host + "\nx-amz-date:" + datetime + "\nx-amz-target:" + method + "\n\ncontent-type;host;x-amz-date;x-amz-target\n" + hex.EncodeToString(hasher.Sum(nil))
hasher.Reset()
hasher.Write([]byte(canonicalReq))
post := "AWS4-HMAC-SHA256\n" + datetime + "\n" + date + "/" + c.endpoint.region + "/dynamodb/aws4_request\n" + hex.EncodeToString(hasher.Sum(nil))
sig := hex.EncodeToString(doHMAC(doHMAC(doHMAC(doHMAC(doHMAC(c.auth.secretKey, date), c.endpoint.region), "dynamodb"), "aws4_request"), post))
credential := "AWS4-HMAC-SHA256 Credential=" + c.auth.accessKey + "/" + date + "/" + c.endpoint.region + "/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=" + sig
req.Header.Set("Authorization", credential)
req.Header.Set("Content-Type", "application/x-amz-json-1.0")
req.Header.Set("Host", c.endpoint.host)
req.Header.Set("X-Amz-Date", datetime)
req.Header.Set("X-Amz-Target", method)
resp, err := c.web.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, Error{
Body: body,
StatusCode: resp.StatusCode,
}
}
return body, nil
}
func doHMAC(key []byte, data string) []byte {
h := hmac.New(sha256.New, key)
h.Write([]byte(data))
return h.Sum(nil)
}
func Dial(region endpoint, creds auth, transport http.RoundTripper) *Client {
if transport == nil {
transport = &http.Transport{TLSClientConfig: tlsconf.Config}
}
return &Client{
auth: creds,
endpoint: region,
web: &http.Client{Transport: transport},
}
}
/*
"Software as disruptive as our attitude"
|8 888 d8888 8888888b. .d8888b. 8888888888
~+ad88RR88 |8 888 d88888 888 Y88b d88P Y88b 888
,8P'8 |8 888 d88P888 888 888 888 888 888
d8" D8 |8 888 d88P 888 888 d88P 888 8888888
,8P' 'I |8 888 d88P 888 8888888P" 888 88888 888
cd8" ' |8 888 d88P 888 888 T88b 888 888 888
.d8" |8 888 d8888888888 888 T88b Y88b d88P 888
dB" |8 88888888 d88P 888 888 T88b "Y8888P88 8888888888
__ ___ __________ __ ___ ____ _ ________
_____________________ ( (`/ / \| |_ | |\ \ // /\ | |_)| |_ | | | || | \ __
888888888888888888888 _)_)\_\_/|_| |_| \_\/\//_/--\|_| \|_|__ |_|__|_||_|_/(_()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment