This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiam.go
80 lines (64 loc) · 1.95 KB
/
iam.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
package vault
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"strings"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
hashivault "github.com/hashicorp/vault/api"
)
type IAM struct {
client *hashivault.Client
}
type IAMLoginOptions struct {
Role string
MountPath string
}
func (i *IAM) Login(options IAMLoginOptions) (*hashivault.Secret, error) {
authSecret, err := i.iamLogin(options)
if err != nil {
return nil, err
}
if authSecret.Auth == nil {
return nil, errors.New("Vault IAM Auth returned nil")
}
i.client.SetToken(authSecret.Auth.ClientToken)
return authSecret, nil
}
// This is based on https://github.com/daveadams/onthelambda/blob/7eb4dc8a8cb58b8a17ba19ad5c68bb4affcbdd22/onthelambda.go#L118-L158
// Credit to @daveadams
func (i *IAM) iamLogin(options IAMLoginOptions) (*hashivault.Secret, error) {
stsClient := sts.New(session.Must(session.NewSession()))
req, _ := stsClient.GetCallerIdentityRequest(&sts.GetCallerIdentityInput{})
if err := req.Sign(); err != nil {
return nil, err
}
headers, err := json.Marshal(req.HTTPRequest.Header)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(req.HTTPRequest.Body)
if err != nil {
return nil, err
}
data := map[string]interface{}{
"iam_http_request_method": req.HTTPRequest.Method,
"iam_request_url": base64.StdEncoding.EncodeToString([]byte(req.HTTPRequest.URL.String())),
"iam_request_headers": base64.StdEncoding.EncodeToString(headers),
"iam_request_body": base64.StdEncoding.EncodeToString(body),
"role": options.Role}
authPath := "auth/aws/login"
if options.MountPath != "" {
authPath = "auth/" + strings.Trim(options.MountPath, "/") + "/login"
}
resp, err := i.client.Logical().Write(authPath, data)
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("Got no response from the aws authentication provider")
}
return resp, nil
}