Skip to content

Commit e44907d

Browse files
committed
Added path parameter to token creation to allow for generic roles
Fixes #202
1 parent 651adfd commit e44907d

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

path_token_role.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ var (
3737
Description: "Role name",
3838
Required: true,
3939
},
40+
"path": {
41+
Type: framework.TypeString,
42+
Description: "Overwrites the role path",
43+
Required: false,
44+
},
4045
}
4146
)
4247

@@ -58,6 +63,10 @@ func (b *Backend) pathTokenRoleCreate(ctx context.Context, req *logical.Request,
5863
return nil, fmt.Errorf("%s: %w", roleName, ErrRoleNotFound)
5964
}
6065

66+
if role.Path == "*" {
67+
role.Path = data.Get("path").(string)
68+
}
69+
6170
b.Logger().Debug("Creating token for role", "role_name", roleName, "token_type", role.TokenType.String())
6271
defer b.Logger().Debug("Created token for role", "role_name", roleName, "token_type", role.TokenType.String())
6372

@@ -168,7 +177,7 @@ func pathTokenRoles(b *Backend) *framework.Path {
168177
return &framework.Path{
169178
HelpSynopsis: strings.TrimSpace(pathTokenRolesHelpSyn),
170179
HelpDescription: strings.TrimSpace(pathTokenRolesHelpDesc),
171-
Pattern: fmt.Sprintf("%s/%s", PathTokenRoleStorage, framework.GenericNameRegex("role_name")),
180+
Pattern: fmt.Sprintf("%s/%s%s", PathTokenRoleStorage, framework.GenericNameRegex("role_name"), framework.OptionalParamRegex("path")),
172181
Fields: FieldSchemaTokenRole,
173182
DisplayAttrs: &framework.DisplayAttributes{
174183
OperationPrefix: operationPrefixGitlabAccessTokens,

path_token_role_test.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
1616
gitlab2 "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/gitlab"
17+
modelToken "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/model/token"
1718
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
1819
)
1920

@@ -37,8 +38,8 @@ func TestPathTokenRoles(t *testing.T) {
3738
require.ErrorIs(t, err, gitlab.ErrRoleNotFound)
3839
})
3940

40-
var generalTokenCreation = func(t *testing.T, tokenType token.Type, level token.AccessLevel, gitlabRevokesToken bool) {
41-
t.Logf("token creation, token type: %s, level: %s, gitlab revokes token: %t", tokenType, level, gitlabRevokesToken)
41+
var generalTokenCreation = func(t *testing.T, tokenType token.Type, level token.AccessLevel, gitlabRevokesToken bool, path string, pathOverride string) {
42+
t.Logf("token creation, token type: %s, level: %s, gitlab revokes token: %t, path: %s, path override: %s", tokenType, level, gitlabRevokesToken, path, pathOverride)
4243
ctx := getCtxGitlabClient(t, "unit")
4344
client := newInMemoryClient(true)
4445
ctx = gitlab2.ClientNewContext(ctx, client)
@@ -56,16 +57,6 @@ func TestPathTokenRoles(t *testing.T) {
5657
ttl = "48h"
5758
}
5859

59-
var path string
60-
switch tokenType {
61-
case token.TypeProject:
62-
path = "example/example"
63-
case token.TypePersonal:
64-
path = "admin-user"
65-
case token.TypeGroup:
66-
path = "example"
67-
}
68-
6960
// create a role
7061
resp, err := b.HandleRequest(ctx, &logical.Request{
7162
Operation: logical.CreateOperation,
@@ -84,10 +75,18 @@ func TestPathTokenRoles(t *testing.T) {
8475
require.NoError(t, resp.Error())
8576

8677
// read an access token
87-
resp, err = b.HandleRequest(ctx, &logical.Request{
78+
reqPath := fmt.Sprintf("%s/test", gitlab.PathTokenRoleStorage)
79+
if pathOverride != "" {
80+
reqPath += fmt.Sprintf("/%s", pathOverride)
81+
}
82+
83+
req := &logical.Request{
8884
Operation: logical.ReadOperation,
89-
Path: fmt.Sprintf("%s/test", gitlab.PathTokenRoleStorage), Storage: l,
90-
})
85+
Path: reqPath,
86+
Storage: l,
87+
}
88+
89+
resp, err = b.HandleRequest(ctx, req)
9190
require.NoError(t, err)
9291
require.NotNil(t, resp)
9392
require.NotNil(t, resp.Secret)
@@ -99,6 +98,24 @@ func TestPathTokenRoles(t *testing.T) {
9998

10099
require.Contains(t, client.accessTokens, fmt.Sprintf("%s_%v", tokenType.String(), tokenId))
101100

101+
// Check path correctness
102+
expectedPath := path
103+
if path == "*" && pathOverride != "" {
104+
expectedPath = pathOverride
105+
}
106+
107+
var createdToken = client.accessTokens[fmt.Sprintf("%s_%v", tokenType.String(), tokenId)]
108+
var actualPath string
109+
switch v := createdToken.(type) {
110+
case *modelToken.TokenProject:
111+
actualPath = v.Path
112+
case *modelToken.TokenPersonal:
113+
actualPath = v.Path
114+
case *modelToken.TokenGroup:
115+
actualPath = v.Path
116+
}
117+
require.Equal(t, expectedPath, actualPath, "Token path mismatch")
118+
102119
// revoke the access token
103120
resp, err = b.HandleRequest(ctx, &logical.Request{
104121
Operation: logical.RevokeOperation,
@@ -152,17 +169,18 @@ func TestPathTokenRoles(t *testing.T) {
152169
}
153170

154171
t.Run("personal access token", func(t *testing.T) {
155-
generalTokenCreation(t, token.TypePersonal, token.AccessLevelUnknown, false)
156-
generalTokenCreation(t, token.TypePersonal, token.AccessLevelUnknown, true)
172+
generalTokenCreation(t, token.TypePersonal, token.AccessLevelUnknown, false, "admin-user", "")
173+
generalTokenCreation(t, token.TypePersonal, token.AccessLevelUnknown, true, "admin-user", "")
174+
generalTokenCreation(t, token.TypeProject, token.AccessLevelGuestPermissions, false, "*", "some-user")
157175
})
158176

159177
t.Run("project access token", func(t *testing.T) {
160-
generalTokenCreation(t, token.TypeProject, token.AccessLevelGuestPermissions, false)
161-
generalTokenCreation(t, token.TypeProject, token.AccessLevelGuestPermissions, true)
178+
generalTokenCreation(t, token.TypeProject, token.AccessLevelGuestPermissions, false, "example/example", "")
179+
generalTokenCreation(t, token.TypeProject, token.AccessLevelGuestPermissions, true, "example/example", "")
162180
})
163181

164182
t.Run("group access token", func(t *testing.T) {
165-
generalTokenCreation(t, token.TypeGroup, token.AccessLevelGuestPermissions, false)
166-
generalTokenCreation(t, token.TypeGroup, token.AccessLevelGuestPermissions, true)
183+
generalTokenCreation(t, token.TypeGroup, token.AccessLevelGuestPermissions, false, "example", "")
184+
generalTokenCreation(t, token.TypeGroup, token.AccessLevelGuestPermissions, true, "example", "")
167185
})
168186
}

0 commit comments

Comments
 (0)