forked from UKHomeOffice/vault-sidekick
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_aws_iam.go
152 lines (124 loc) · 4.17 KB
/
auth_aws_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
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
/*
Copyright 2015 Home Office All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/hashicorp/vault/api"
)
// aws iam authentication plugin
type authAWSIAMPlugin struct {
// the vault client
client *api.Client
}
// NewUserTokenPlugin creates a new User Token plugin
func NewAWSIAMPlugin(client *api.Client) AuthInterface {
return &authAWSIAMPlugin{
client: client,
}
}
// Create retrieves the token from an environment variable or file
func (r authAWSIAMPlugin) Create(cfg *vaultAuthOptions) (string, error) {
role := os.Getenv("VAULT_SIDEKICK_ROLE_ID")
if cfg.FileName != "" {
content, err := readConfigFile(cfg.FileName, cfg.FileFormat)
if err != nil {
return "", err
}
role = content.RoleID
}
creds, err := generateCredentialChain()
if err != nil {
return "", err
}
loginData, err := generateLoginData(creds)
if err != nil {
return "", err
}
if loginData == nil {
return "", fmt.Errorf("got nil response from GenerateLoginData")
}
loginData["role"] = role
path := fmt.Sprintf("auth/aws/login")
resp, err := r.client.Logical().Write(path, loginData)
if err != nil {
return "", err
}
if resp == nil {
return "", fmt.Errorf("empty response from credential provider")
}
return resp.Auth.ClientToken, nil
}
// generateLoginData populates the necessary data to send to the Vault server for generating a token
// from github.com/hashicorp/vault/builtin/credential/aws/cli.go
func generateLoginData(creds *credentials.Credentials) (map[string]interface{}, error) {
loginData := make(map[string]interface{})
// Use the credentials we've found to construct an STS session
stsSession, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Credentials: creds,
},
})
if err != nil {
return nil, err
}
var params *sts.GetCallerIdentityInput
svc := sts.New(stsSession)
stsRequest, _ := svc.GetCallerIdentityRequest(params)
if err := stsRequest.Sign(); err != nil {
return nil, err
}
// Now extract out the relevant parts of the request
headersJson, err := json.Marshal(stsRequest.HTTPRequest.Header)
if err != nil {
return nil, err
}
requestBody, err := ioutil.ReadAll(stsRequest.HTTPRequest.Body)
if err != nil {
return nil, err
}
loginData["iam_http_request_method"] = stsRequest.HTTPRequest.Method
loginData["iam_request_url"] = base64.StdEncoding.EncodeToString([]byte(stsRequest.HTTPRequest.URL.String()))
loginData["iam_request_headers"] = base64.StdEncoding.EncodeToString(headersJson)
loginData["iam_request_body"] = base64.StdEncoding.EncodeToString(requestBody)
return loginData, nil
}
// from github.com/hashicorp/vault/helper/awsutil/generate_credentials.go
func generateCredentialChain() (*credentials.Credentials, error) {
var providers []credentials.Provider
// Add the environment credential provider
providers = append(providers, &credentials.EnvProvider{})
// Add the shared credentials provider
providers = append(providers, &credentials.SharedCredentialsProvider{})
// Add the remote provider
def := defaults.Get()
providers = append(providers, defaults.RemoteCredProvider(*def.Config, def.Handlers))
// Create the credentials required to access the API.
creds := credentials.NewChainCredentials(providers)
if creds == nil {
return nil, fmt.Errorf("could not compile valid credential providers from environment, shared, or instance metadata")
}
_, err := creds.Get()
if err != nil {
return nil, err
}
return creds, nil
}