Skip to content

Instantly share code, notes, and snippets.

@slav123
Last active August 29, 2022 07:43
Show Gist options
  • Save slav123/ceb704ee99ccc1f2d2d6e3b1e8eccb26 to your computer and use it in GitHub Desktop.
Save slav123/ceb704ee99ccc1f2d2d6e3b1e8eccb26 to your computer and use it in GitHub Desktop.
AWS Signed request in GO
package main
import (
"compress/gzip"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
const (
apiUrl = ""
region = `eu-central-1`
service = ``
// TimeFormat is the time format to be used in the X-Amz-Date header or query parameter
TimeFormat = "20060102T150405Z"
EmptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`
)
type credentails struct {
AccessKeyID string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
}
var canned = map[string]credentails{
"": {
AccessKeyID: "",
SecretAccessKey: "",
},
}
func main() {
result, err := getQuery(false)
if err != nil {
fmt.Printf("%s\n", err)
}
fmt.Printf("%s\n", result)
}
func getQuery(debug bool) (string, error) {
var credentials = aws.Credentials{AccessKeyID: canned[clientUUID].AccessKeyID, SecretAccessKey: canned[clientUUID].SecretAccessKey}
body := ""
reader := strings.NewReader(body)
req, body, err := buildRequest(fmt.Sprintf("%s/v1/configuration/clients/%s/experiments", apiUrl, clientUUID), reader, "GET")
query := req.URL.Query()
req.URL.RawQuery = query.Encode()
signer := v4.NewSigner()
err = signer.SignHTTP(context.Background(), credentials, req, body, service, region, time.Now())
if err != nil {
fmt.Println(err)
return "", err
}
if debug {
fmt.Printf("%s\n", req.URL.String())
}
// An HTTP client for sending the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Print(err)
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
// Check if server sent gzipped response. Decompress if yes.
var respReader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
respReader, err = gzip.NewReader(resp.Body)
defer respReader.Close()
default:
respReader = resp.Body
}
bodyString, err := ioutil.ReadAll(respReader)
if err != nil {
fmt.Print(err)
return "", err
}
fmt.Printf("%s\n", string(bodyString))
return string(bodyString), nil
}
return "", nil
}
// buildRequest builds an http.Request with the given body and method
func buildRequest(url string, body io.Reader, requestType string) (*http.Request, string, error) {
var bodyLen int
type lenner interface {
Len() int
}
if lr, ok := body.(lenner); ok {
bodyLen = lr.Len()
}
req, err := http.NewRequest(requestType, url, body)
if err != nil {
return nil, "", err
}
if bodyLen > 0 {
req.ContentLength = int64(bodyLen)
}
req.Header.Add("X-Amz-Date", time.Now().UTC().Format(TimeFormat))
//req.Header.Add("x-api-key", "")
req.Header.Add("date", time.Now().UTC().Format(TimeFormat))
var payloadHash string
if bodyLen == 0 {
payloadHash = EmptyStringSHA256
} else {
h := sha256.New()
_, _ = io.Copy(h, body)
payloadHash = hex.EncodeToString(h.Sum(nil))
}
return req, payloadHash, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment