forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config_sts.go
248 lines (204 loc) · 7.38 KB
/
path_config_sts.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package awsauth
import (
"fmt"
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
// awsStsEntry is used to store details of an STS role for assumption
type awsStsEntry struct {
StsRole string `json:"sts_role" structs:"sts_role" mapstructure:"sts_role"`
}
func pathListSts(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/sts/?",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathStsList,
},
HelpSynopsis: pathListStsHelpSyn,
HelpDescription: pathListStsHelpDesc,
}
}
func pathConfigSts(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/sts/" + framework.GenericNameRegex("account_id"),
Fields: map[string]*framework.FieldSchema{
"account_id": {
Type: framework.TypeString,
Description: `AWS account ID to be associated with STS role. If set,
Vault will use assumed credentials to verify any login attempts from EC2
instances in this account.`,
},
"sts_role": {
Type: framework.TypeString,
Description: `AWS ARN for STS role to be assumed when interacting with the account specified.
The Vault server must have permissions to assume this role.`,
},
},
ExistenceCheck: b.pathConfigStsExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathConfigStsCreateUpdate,
logical.UpdateOperation: b.pathConfigStsCreateUpdate,
logical.ReadOperation: b.pathConfigStsRead,
logical.DeleteOperation: b.pathConfigStsDelete,
},
HelpSynopsis: pathConfigStsSyn,
HelpDescription: pathConfigStsDesc,
}
}
// Establishes dichotomy of request operation between CreateOperation and UpdateOperation.
// Returning 'true' forces an UpdateOperation, CreateOperation otherwise.
func (b *backend) pathConfigStsExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
accountID := data.Get("account_id").(string)
if accountID == "" {
return false, fmt.Errorf("missing account_id")
}
entry, err := b.lockedAwsStsEntry(req.Storage, accountID)
if err != nil {
return false, err
}
return entry != nil, nil
}
// pathStsList is used to list all the AWS STS role configurations
func (b *backend) pathStsList(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.configMutex.RLock()
defer b.configMutex.RUnlock()
sts, err := req.Storage.List("config/sts/")
if err != nil {
return nil, err
}
return logical.ListResponse(sts), nil
}
// nonLockedSetAwsStsEntry creates or updates an STS role association with the given accountID
// This method does not acquire the write lock before creating or updating. If locking is
// desired, use lockedSetAwsStsEntry instead
func (b *backend) nonLockedSetAwsStsEntry(s logical.Storage, accountID string, stsEntry *awsStsEntry) error {
if accountID == "" {
return fmt.Errorf("missing AWS account ID")
}
if stsEntry == nil {
return fmt.Errorf("missing AWS STS Role ARN")
}
entry, err := logical.StorageEntryJSON("config/sts/"+accountID, stsEntry)
if err != nil {
return err
}
if entry == nil {
return fmt.Errorf("failed to create storage entry for AWS STS configuration")
}
return s.Put(entry)
}
// lockedSetAwsStsEntry creates or updates an STS role association with the given accountID
// This method acquires the write lock before creating or updating the STS entry.
func (b *backend) lockedSetAwsStsEntry(s logical.Storage, accountID string, stsEntry *awsStsEntry) error {
if accountID == "" {
return fmt.Errorf("missing AWS account ID")
}
if stsEntry == nil {
return fmt.Errorf("missing sts entry")
}
b.configMutex.Lock()
defer b.configMutex.Unlock()
return b.nonLockedSetAwsStsEntry(s, accountID, stsEntry)
}
// nonLockedAwsStsEntry returns the STS role associated with the given accountID.
// This method does not acquire the read lock before returning information. If locking is
// desired, use lockedAwsStsEntry instead
func (b *backend) nonLockedAwsStsEntry(s logical.Storage, accountID string) (*awsStsEntry, error) {
entry, err := s.Get("config/sts/" + accountID)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var stsEntry awsStsEntry
if err := entry.DecodeJSON(&stsEntry); err != nil {
return nil, err
}
return &stsEntry, nil
}
// lockedAwsStsEntry returns the STS role associated with the given accountID.
// This method acquires the read lock before returning the association.
func (b *backend) lockedAwsStsEntry(s logical.Storage, accountID string) (*awsStsEntry, error) {
b.configMutex.RLock()
defer b.configMutex.RUnlock()
return b.nonLockedAwsStsEntry(s, accountID)
}
// pathConfigStsRead is used to return information about an STS role/AWS accountID association
func (b *backend) pathConfigStsRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
accountID := data.Get("account_id").(string)
if accountID == "" {
return logical.ErrorResponse("missing account id"), nil
}
stsEntry, err := b.lockedAwsStsEntry(req.Storage, accountID)
if err != nil {
return nil, err
}
if stsEntry == nil {
return nil, nil
}
return &logical.Response{
Data: structs.New(stsEntry).Map(),
}, nil
}
// pathConfigStsCreateUpdate is used to associate an STS role with a given AWS accountID
func (b *backend) pathConfigStsCreateUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
accountID := data.Get("account_id").(string)
if accountID == "" {
return logical.ErrorResponse("missing AWS account ID"), nil
}
b.configMutex.Lock()
defer b.configMutex.Unlock()
// Check if an STS role is already registered
stsEntry, err := b.nonLockedAwsStsEntry(req.Storage, accountID)
if err != nil {
return nil, err
}
if stsEntry == nil {
stsEntry = &awsStsEntry{}
}
// Check that an STS role has actually been provided
stsRole, ok := data.GetOk("sts_role")
if ok {
stsEntry.StsRole = stsRole.(string)
} else if req.Operation == logical.CreateOperation {
return logical.ErrorResponse("missing sts role"), nil
}
if stsEntry.StsRole == "" {
return logical.ErrorResponse("sts role cannot be empty"), nil
}
// save the provided STS role
if err := b.nonLockedSetAwsStsEntry(req.Storage, accountID, stsEntry); err != nil {
return nil, err
}
return nil, nil
}
// pathConfigStsDelete is used to delete a previously configured STS configuration
func (b *backend) pathConfigStsDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.configMutex.Lock()
defer b.configMutex.Unlock()
accountID := data.Get("account_id").(string)
if accountID == "" {
return logical.ErrorResponse("missing account id"), nil
}
return nil, req.Storage.Delete("config/sts/" + accountID)
}
const pathConfigStsSyn = `
Specify STS roles to be assumed for certain AWS accounts.
`
const pathConfigStsDesc = `
Allows the explicit association of STS roles to satellite AWS accounts (i.e. those
which are not the account in which the Vault server is running.) Login attempts from
EC2 instances running in these accounts will be verified using credentials obtained
by assumption of these STS roles.
The environment in which the Vault server resides must have access to assume the
given STS roles.
`
const pathListStsHelpSyn = `
List all the AWS account/STS role relationships registered with Vault.
`
const pathListStsHelpDesc = `
AWS accounts will be listed by account ID, along with their respective role names.
`