forked from open-policy-agent/opa
/
aws.go
164 lines (136 loc) · 5.68 KB
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2022 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package providers
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"net/url"
"sort"
"strings"
"time"
"github.com/open-policy-agent/opa/ast"
)
func stringFromTerm(t *ast.Term) string {
if v, ok := t.Value.(ast.String); ok {
return string(v)
}
return ""
}
// Headers that may be mutated before reaching an aws service (eg by a proxy) should be added here to omit them from
// the sigv4 canonical request
// ref. https://github.com/aws/aws-sdk-go/blob/master/aws/signer/v4/v4.go#L92
var awsSigv4IgnoredHeaders = map[string]struct{}{
"authorization": {},
"user-agent": {},
"x-amzn-trace-id": {},
}
type AWSCredentials struct {
AccessKey string
SecretKey string
RegionName string
SessionToken string
}
func AWSCredentialsFromObject(v ast.Object) AWSCredentials {
var creds AWSCredentials
awsAccessKey := v.Get(ast.StringTerm("aws_access_key"))
awsSecretKey := v.Get(ast.StringTerm("aws_secret_access_key"))
awsRegion := v.Get(ast.StringTerm("aws_region"))
awsSessionToken := v.Get(ast.StringTerm("aws_session_token"))
creds.AccessKey = stringFromTerm(awsAccessKey)
creds.SecretKey = stringFromTerm(awsSecretKey)
creds.RegionName = stringFromTerm(awsRegion)
if awsSessionToken != nil {
creds.SessionToken = stringFromTerm(awsSessionToken)
}
return creds
}
func sha256MAC(message string, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(message))
return mac.Sum(nil)
}
func sortKeys(strMap map[string][]string) []string {
keys := make([]string, len(strMap))
i := 0
for k := range strMap {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
// AWSSignV4 modifies a map[string][]string of headers to generate an AWS V4 signature + headers based on the config/credentials provided.
func AWSSignV4(headers map[string][]string, method string, theURL *url.URL, body []byte, service string, awsCreds AWSCredentials, theTime time.Time) (string, map[string]string) {
// General ref. https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
// S3 ref. https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
// APIGateway ref. https://docs.aws.amazon.com/apigateway/api-reference/signing-requests/
bodyHexHash := fmt.Sprintf("%x", sha256.Sum256(body))
now := theTime.UTC()
// V4 signing has specific ideas of how it wants to see dates/times encoded
dateNow := now.Format("20060102")
iso8601Now := now.Format("20060102T150405Z")
awsHeaders := map[string]string{
"host": theURL.Host,
"x-amz-date": iso8601Now,
}
// s3 and glacier require the extra x-amz-content-sha256 header. other services do not.
if service == "s3" || service == "glacier" {
awsHeaders["x-amz-content-sha256"] = bodyHexHash
}
// the security token header is necessary for ephemeral credentials, e.g. from
// the EC2 metadata service
if awsCreds.SessionToken != "" {
awsHeaders["x-amz-security-token"] = awsCreds.SessionToken
}
headersToSign := map[string][]string{}
// sign all of the aws headers.
for k, v := range awsHeaders {
headersToSign[k] = []string{v}
}
// sign all of the request's headers, except for those in the ignore list
for k, v := range headers {
lowercaseHeader := strings.ToLower(k)
if _, ok := awsSigv4IgnoredHeaders[lowercaseHeader]; !ok {
headersToSign[lowercaseHeader] = v
}
}
// the "canonical request" is the normalized version of the AWS service access
// that we're attempting to perform; in this case, a GET from an S3 bucket
canonicalReq := method + "\n" // HTTP method
canonicalReq += theURL.EscapedPath() + "\n" // URI-escaped path
canonicalReq += theURL.RawQuery + "\n" // RAW Query String
// include the values for the signed headers
orderedKeys := sortKeys(headersToSign)
for _, k := range orderedKeys {
canonicalReq += k + ":" + strings.Join(headersToSign[k], ",") + "\n"
}
canonicalReq += "\n" // linefeed to terminate headers
// include the list of the signed headers
headerList := strings.Join(orderedKeys, ";")
canonicalReq += headerList + "\n"
canonicalReq += bodyHexHash
// the "string to sign" is a time-bounded, scoped request token which
// is linked to the "canonical request" by inclusion of its SHA-256 hash
strToSign := "AWS4-HMAC-SHA256\n" // V4 signing with SHA-256 HMAC
strToSign += iso8601Now + "\n" // ISO 8601 time
strToSign += dateNow + "/" + awsCreds.RegionName + "/" + service + "/aws4_request\n" // scoping for signature
strToSign += fmt.Sprintf("%x", sha256.Sum256([]byte(canonicalReq))) // SHA-256 of canonical request
// the "signing key" is generated by repeated HMAC-SHA256 based on the same
// scoping that's included in the "string to sign"; but including the secret key
// to allow AWS to validate it
signingKey := sha256MAC(dateNow, []byte("AWS4"+awsCreds.SecretKey))
signingKey = sha256MAC(awsCreds.RegionName, signingKey)
signingKey = sha256MAC(service, signingKey)
signingKey = sha256MAC("aws4_request", signingKey)
// the "signature" is finally the "string to sign" signed by the "signing key"
signature := sha256MAC(strToSign, signingKey)
// required format of Authorization header; n.b. the access key corresponding to
// the secret key is included here
authHeader := "AWS4-HMAC-SHA256 Credential=" + awsCreds.AccessKey + "/" + dateNow
authHeader += "/" + awsCreds.RegionName + "/" + service + "/aws4_request,"
authHeader += "SignedHeaders=" + headerList + ","
authHeader += "Signature=" + fmt.Sprintf("%x", signature)
return authHeader, awsHeaders
}