-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
path_token_role_multiple_config_test.go
153 lines (135 loc) · 4.58 KB
/
path_token_role_multiple_config_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
package gitlab_test
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
g "github.com/xanzy/go-gitlab"
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
)
func TestPathTokenRolesMultipleConfigs(t *testing.T) {
httpClient, gitlabUrl := getClient(t)
ctx := gitlab.HttpClientNewContext(context.Background(), httpClient)
b, l, events, err := getBackendWithEvents(ctx)
require.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, events)
require.NotNil(t, b)
require.NotNil(t, l)
var configs = map[string]string{"root": "glpat-secret-random-token", "admin": "glpat-secret-admin-token", "normal": "glpat-secret-normal-token"}
for name, token := range configs {
require.NoError(t,
writeBackendConfigWithName(ctx, b, l,
map[string]any{
"token": token,
"base_url": gitlabUrl,
"type": gitlab.TypeSelfManaged.String(),
},
name,
),
)
}
type roleData struct {
rn, path string
tt gitlab.TokenType
al gitlab.AccessLevel
scopes []string
}
var roles = map[string][]roleData{
"root": {
{rn: "root-root", path: "root", tt: gitlab.TokenTypePersonal, scopes: gitlab.ValidPersonalTokenScopes},
{rn: "root-normal-user", path: "normal-user", tt: gitlab.TokenTypePersonal, scopes: gitlab.ValidPersonalTokenScopes},
},
"admin": {
{rn: "admin-example-example", path: "example/example", tt: gitlab.TokenTypeProject, al: gitlab.AccessLevelGuestPermissions, scopes: []string{gitlab.TokenScopeApi.String()}},
},
"normal": {
{rn: "normal-example", path: "example", tt: gitlab.TokenTypeGroup, al: gitlab.AccessLevelGuestPermissions, scopes: []string{gitlab.TokenScopeApi.String()}},
},
}
for cfg, rds := range roles {
for _, rd := range rds {
var data = map[string]any{
"name": fmt.Sprintf("%s-{{ .role_name }}-{{ .config_name }}-{{ .token_type }}", rd.path),
"token_type": rd.tt.String(), "path": rd.path, "config_name": cfg, "ttl": gitlab.DefaultAccessTokenMinTTL,
}
switch rd.tt {
case gitlab.TokenTypePersonal:
data["access_level"] = rd.al.String()
data["scopes"] = rd.scopes
case gitlab.TokenTypeGroup:
data["access_level"] = rd.al.String()
data["scopes"] = rd.scopes
case gitlab.TokenTypeProject:
data["access_level"] = rd.al.String()
data["scopes"] = rd.scopes
}
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathRoleStorage, rd.rn), Storage: l,
Data: data,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.Empty(t, resp.Warnings)
require.EqualValues(t, cfg, resp.Data["config_name"])
ctxIssueToken, _ := ctxTestTime(ctx, t.Name())
resp, err = b.HandleRequest(ctxIssueToken, &logical.Request{
Operation: logical.ReadOperation, Storage: l,
Path: fmt.Sprintf("%s/%s", gitlab.PathTokenRoleStorage, rd.rn),
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.Secret)
require.NoError(t, resp.Error())
var token = resp.Data["token"].(string)
require.NotEmpty(t, token)
var secret = resp.Secret
require.NotNil(t, secret)
// verify token that it works
var c *g.Client
c, err = g.NewClient(token, g.WithHTTPClient(httpClient), g.WithBaseURL(gitlabUrl))
require.NoError(t, err)
require.NotNil(t, c)
pat, r, err := c.PersonalAccessTokens.GetSinglePersonalAccessToken()
require.NoError(t, err)
require.NotNil(t, r)
require.NotNil(t, pat)
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.RevokeOperation,
Path: "/",
Storage: l,
Secret: secret,
})
require.NoError(t, err)
require.Nil(t, resp)
}
}
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ListOperation,
Path: gitlab.PathRoleStorage, Storage: l,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.NotEmpty(t, resp.Data)
events.expectEvents(t, []expectedEvent{
{eventType: "gitlab/config-write"},
{eventType: "gitlab/config-write"},
{eventType: "gitlab/config-write"},
{eventType: "gitlab/role-write"},
{eventType: "gitlab/token-write"},
{eventType: "gitlab/token-revoke"},
{eventType: "gitlab/role-write"},
{eventType: "gitlab/token-write"},
{eventType: "gitlab/token-revoke"},
{eventType: "gitlab/role-write"},
{eventType: "gitlab/token-write"},
{eventType: "gitlab/token-revoke"},
{eventType: "gitlab/role-write"},
{eventType: "gitlab/token-write"},
{eventType: "gitlab/token-revoke"},
})
}