-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
aliases_test.go
303 lines (271 loc) · 8.03 KB
/
aliases_test.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package identity
import (
"testing"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/builtin/credential/github"
credLdap "github.com/hashicorp/vault/builtin/credential/ldap"
"github.com/hashicorp/vault/builtin/credential/userpass"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
)
func TestIdentityStore_EntityAliasLocalMount(t *testing.T) {
coreConfig := &vault.CoreConfig{
CredentialBackends: map[string]logical.Factory{
"ldap": credLdap.Factory,
},
}
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: vaulthttp.Handler,
})
cluster.Start()
defer cluster.Cleanup()
core := cluster.Cores[0].Core
vault.TestWaitActive(t, core)
client := cluster.Cores[0].Client
// Create a local auth mount
err := client.Sys().EnableAuthWithOptions("ldap", &api.EnableAuthOptions{
Type: "ldap",
Local: true,
})
if err != nil {
t.Fatal(err)
}
// Extract out the mount accessor for LDAP auth
auths, err := client.Sys().ListAuth()
if err != nil {
t.Fatal(err)
}
ldapMountAccessor := auths["ldap/"].Accessor
// Create an entity
secret, err := client.Logical().Write("identity/entity", nil)
if err != nil {
t.Fatal(err)
}
entityID := secret.Data["id"].(string)
// Attempt to create an entity alias against a local mount should fail
secret, err = client.Logical().Write("identity/entity-alias", map[string]interface{}{
"name": "testuser",
"mount_accessor": ldapMountAccessor,
"canonical_id": entityID,
})
if err == nil {
t.Fatalf("expected error since mount is local")
}
}
func TestIdentityStore_ListAlias(t *testing.T) {
coreConfig := &vault.CoreConfig{
CredentialBackends: map[string]logical.Factory{
"github": github.Factory,
},
}
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: vaulthttp.Handler,
})
cluster.Start()
defer cluster.Cleanup()
core := cluster.Cores[0].Core
vault.TestWaitActive(t, core)
client := cluster.Cores[0].Client
err := client.Sys().EnableAuthWithOptions("github", &api.EnableAuthOptions{
Type: "github",
})
if err != nil {
t.Fatal(err)
}
mounts, err := client.Sys().ListAuth()
if err != nil {
t.Fatal(err)
}
var githubAccessor string
for k, v := range mounts {
t.Logf("key: %v\nmount: %#v", k, *v)
if k == "github/" {
githubAccessor = v.Accessor
break
}
}
if githubAccessor == "" {
t.Fatal("did not find github accessor")
}
resp, err := client.Logical().Write("identity/entity", nil)
if err != nil {
t.Fatalf("err:%v resp:%#v", err, resp)
}
if resp == nil {
t.Fatalf("expected a non-nil response")
}
entityID := resp.Data["id"].(string)
// Create an alias
resp, err = client.Logical().Write("identity/entity-alias", map[string]interface{}{
"name": "testaliasname",
"mount_accessor": githubAccessor,
})
if err != nil {
t.Fatalf("err:%v resp:%#v", err, resp)
}
testAliasCanonicalID := resp.Data["canonical_id"].(string)
testAliasAliasID := resp.Data["id"].(string)
resp, err = client.Logical().Write("identity/entity-alias", map[string]interface{}{
"name": "entityalias",
"mount_accessor": githubAccessor,
"canonical_id": entityID,
})
if err != nil {
t.Fatalf("err:%v resp:%#v", err, resp)
}
entityAliasAliasID := resp.Data["id"].(string)
resp, err = client.Logical().List("identity/entity-alias/id")
if err != nil {
t.Fatalf("err:%v resp:%#v", err, resp)
}
keys := resp.Data["keys"].([]interface{})
if len(keys) != 2 {
t.Fatalf("bad: length of alias IDs listed; expected: 2, actual: %d", len(keys))
}
// Do some due diligence on the key info
aliasInfoRaw, ok := resp.Data["key_info"]
if !ok {
t.Fatal("expected key_info map in response")
}
aliasInfo := aliasInfoRaw.(map[string]interface{})
for _, keyRaw := range keys {
key := keyRaw.(string)
infoRaw, ok := aliasInfo[key]
if !ok {
t.Fatal("expected key info")
}
info := infoRaw.(map[string]interface{})
currName := "entityalias"
if info["canonical_id"].(string) == testAliasCanonicalID {
currName = "testaliasname"
}
t.Logf("alias info: %#v", info)
switch {
case info["name"].(string) != currName:
t.Fatalf("bad name: %v", info["name"].(string))
case info["mount_accessor"].(string) != githubAccessor:
t.Fatalf("bad mount_path: %v", info["mount_accessor"].(string))
}
}
// Now do the same with entity info
resp, err = client.Logical().List("identity/entity/id")
if err != nil {
t.Fatalf("err:%v resp:%#v", err, resp)
}
keys = resp.Data["keys"].([]interface{})
if len(keys) != 2 {
t.Fatalf("bad: length of entity IDs listed; expected: 2, actual: %d", len(keys))
}
entityInfoRaw, ok := resp.Data["key_info"]
if !ok {
t.Fatal("expected key_info map in response")
}
// This is basically verifying that the entity has the alias in key_info
// that we expect to be tied to it, plus tests a value further down in it
// for fun
entityInfo := entityInfoRaw.(map[string]interface{})
for _, keyRaw := range keys {
key := keyRaw.(string)
infoRaw, ok := entityInfo[key]
if !ok {
t.Fatal("expected key info")
}
info := infoRaw.(map[string]interface{})
t.Logf("entity info: %#v", info)
currAliasID := entityAliasAliasID
if key == testAliasCanonicalID {
currAliasID = testAliasAliasID
}
currAliases := info["aliases"].([]interface{})
if len(currAliases) != 1 {
t.Fatal("bad aliases length")
}
for _, v := range currAliases {
curr := v.(map[string]interface{})
switch {
case curr["id"].(string) != currAliasID:
t.Fatalf("bad alias id: %v", curr["id"])
case curr["mount_accessor"].(string) != githubAccessor:
t.Fatalf("bad mount accessor: %v", curr["mount_accessor"])
case curr["mount_path"].(string) != "auth/github/":
t.Fatalf("bad mount path: %v", curr["mount_path"])
case curr["mount_type"].(string) != "github":
t.Fatalf("bad mount type: %v", curr["mount_type"])
}
}
}
}
// TestIdentityStore_RenameAlias_CannotMergeEntity verifies that an error is
// returned on an attempt to rename an alias to match another alias with the
// same mount accessor. This used to result in a merge entity.
func TestIdentityStore_RenameAlias_CannotMergeEntity(t *testing.T) {
coreConfig := &vault.CoreConfig{
CredentialBackends: map[string]logical.Factory{
"userpass": userpass.Factory,
},
}
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: vaulthttp.Handler,
})
cluster.Start()
defer cluster.Cleanup()
client := cluster.Cores[0].Client
err := client.Sys().EnableAuthWithOptions("userpass", &api.EnableAuthOptions{
Type: "userpass",
})
if err != nil {
t.Fatal(err)
}
_, err = client.Logical().Write("auth/userpass/users/bsmith", map[string]interface{}{
"password": "training",
})
if err != nil {
t.Fatal(err)
}
_, err = client.Logical().Write("auth/userpass/login/bsmith", map[string]interface{}{
"password": "training",
})
if err != nil {
t.Fatal(err)
}
mounts, err := client.Sys().ListAuth()
if err != nil {
t.Fatal(err)
}
var mountAccessor string
for k, v := range mounts {
if k == "userpass/" {
mountAccessor = v.Accessor
break
}
}
if mountAccessor == "" {
t.Fatal("did not find userpass accessor")
}
// Now create a new unrelated entity and alias
entityResp, err := client.Logical().Write("identity/entity", map[string]interface{}{
"name": "bob-smith",
})
if err != nil {
t.Fatalf("err:%v resp:%#v", err, entityResp)
}
if entityResp == nil {
t.Fatalf("expected a non-nil response")
}
aliasResp, err := client.Logical().Write("identity/entity-alias", map[string]interface{}{
"name": "bob",
"mount_accessor": mountAccessor,
})
if err != nil {
t.Fatalf("err:%v resp:%#v", err, aliasResp)
}
aliasID2 := aliasResp.Data["id"].(string)
// Rename this new alias to have the same name as the one implicitly created by our login as bsmith
_, err = client.Logical().Write("identity/entity-alias/id/"+aliasID2, map[string]interface{}{
"name": "bsmith",
})
if err == nil {
t.Fatal("expected rename over existing entity to fail")
}
}