Skip to content

Commit a2aa345

Browse files
committed
move access level to token package
1 parent 125fecd commit a2aa345

19 files changed

+76
-66
lines changed

entry_role.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
)
1212

1313
type EntryRole struct {
14-
RoleName string `json:"role_name" structs:"role_name" mapstructure:"role_name"`
15-
TTL time.Duration `json:"ttl" structs:"ttl" mapstructure:"ttl"`
16-
Path string `json:"path" structs:"path" mapstructure:"path"`
17-
Name string `json:"name" structs:"name" mapstructure:"name"`
18-
Scopes []string `json:"scopes" structs:"scopes" mapstructure:"scopes"`
19-
AccessLevel AccessLevel `json:"access_level" structs:"access_level" mapstructure:"access_level,omitempty"`
20-
TokenType token.Type `json:"token_type" structs:"token_type" mapstructure:"token_type"`
21-
GitlabRevokesTokens bool `json:"gitlab_revokes_token" structs:"gitlab_revokes_token" mapstructure:"gitlab_revokes_token"`
22-
ConfigName string `json:"config_name" structs:"config_name" mapstructure:"config_name"`
14+
RoleName string `json:"role_name" structs:"role_name" mapstructure:"role_name"`
15+
TTL time.Duration `json:"ttl" structs:"ttl" mapstructure:"ttl"`
16+
Path string `json:"path" structs:"path" mapstructure:"path"`
17+
Name string `json:"name" structs:"name" mapstructure:"name"`
18+
Scopes []string `json:"scopes" structs:"scopes" mapstructure:"scopes"`
19+
AccessLevel token.AccessLevel `json:"access_level" structs:"access_level" mapstructure:"access_level,omitempty"`
20+
TokenType token.Type `json:"token_type" structs:"token_type" mapstructure:"token_type"`
21+
GitlabRevokesTokens bool `json:"gitlab_revokes_token" structs:"gitlab_revokes_token" mapstructure:"gitlab_revokes_token"`
22+
ConfigName string `json:"config_name" structs:"config_name" mapstructure:"config_name"`
2323
}
2424

2525
func (e EntryRole) LogicalResponseData() map[string]any {

gitlab_client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type Client interface {
3232
CurrentTokenInfo(ctx context.Context) (*TokenConfig, error)
3333
RotateCurrentToken(ctx context.Context) (newToken *TokenConfig, oldToken *TokenConfig, err error)
3434
CreatePersonalAccessToken(ctx context.Context, username string, userId int, name string, expiresAt time.Time, scopes []string) (*TokenPersonal, error)
35-
CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel AccessLevel) (*TokenGroup, error)
36-
CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel AccessLevel) (*TokenProject, error)
35+
CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (*TokenGroup, error)
36+
CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (*TokenProject, error)
3737
RevokePersonalAccessToken(ctx context.Context, tokenId int) error
3838
RevokeProjectAccessToken(ctx context.Context, tokenId int, projectId string) error
3939
RevokeGroupAccessToken(ctx context.Context, tokenId int, groupId string) error
@@ -460,7 +460,7 @@ func (gc *gitlabClient) CreatePersonalAccessToken(ctx context.Context, username
460460
return et, err
461461
}
462462

463-
func (gc *gitlabClient) CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel AccessLevel) (et *TokenGroup, err error) {
463+
func (gc *gitlabClient) CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (et *TokenGroup, err error) {
464464
var at *g.GroupAccessToken
465465
defer func() {
466466
gc.logger.Debug("Create group access token", "gat", at, "et", et, "groupId", groupId, "name", name, "expiresAt", expiresAt, "scopes", scopes, "accessLevel", accessLevel, "error", err)
@@ -493,7 +493,7 @@ func (gc *gitlabClient) CreateGroupAccessToken(ctx context.Context, groupId stri
493493
return et, err
494494
}
495495

496-
func (gc *gitlabClient) CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel AccessLevel) (et *TokenProject, err error) {
496+
func (gc *gitlabClient) CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (et *TokenProject, err error) {
497497
var at *g.ProjectAccessToken
498498
defer func() {
499499
gc.logger.Debug("Create project access token", "gat", at, "et", et, "projectId", projectId, "name", name, "expiresAt", expiresAt, "scopes", scopes, "accessLevel", accessLevel, "error", err)

gitlab_client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func TestGitlabClient_InvalidToken(t *testing.T) {
8989
_, err = client.GetUserIdByUsername(ctx, "username")
9090
require.Error(t, err)
9191

92-
gatToken, err := client.CreateGroupAccessToken(ctx, "groupId", "name", timeExpiresAt, []string{"scope"}, gitlab.AccessLevelUnknown)
92+
gatToken, err := client.CreateGroupAccessToken(ctx, "groupId", "name", timeExpiresAt, []string{"scope"}, token2.AccessLevelUnknown)
9393
require.Error(t, err)
9494
require.Nil(t, gatToken)
9595

96-
prjAtToken, err := client.CreateProjectAccessToken(ctx, "projectId", "name", timeExpiresAt, []string{"scope"}, gitlab.AccessLevelUnknown)
96+
prjAtToken, err := client.CreateProjectAccessToken(ctx, "projectId", "name", timeExpiresAt, []string{"scope"}, token2.AccessLevelUnknown)
9797
require.Error(t, err)
9898
require.Nil(t, prjAtToken)
9999

@@ -258,7 +258,7 @@ func TestGitlabClient_CreateAccessToken_And_Revoke(t *testing.T) {
258258
"name",
259259
timeExpiresAt,
260260
[]string{token2.ScopeReadApi.String()},
261-
gitlab.AccessLevelGuestPermissions,
261+
token2.AccessLevelGuestPermissions,
262262
)
263263
require.NoError(t, err)
264264
require.NotNil(t, gatToken)
@@ -272,7 +272,7 @@ func TestGitlabClient_CreateAccessToken_And_Revoke(t *testing.T) {
272272
"name",
273273
timeExpiresAt,
274274
[]string{token2.ScopeReadApi.String()},
275-
gitlab.AccessLevelDeveloperPermissions,
275+
token2.AccessLevelDeveloperPermissions,
276276
)
277277
require.NoError(t, err)
278278
require.NotNil(t, prjatToken)

helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (i *inMemoryClient) CreatePersonalAccessToken(ctx context.Context, username
467467
return entryToken, nil
468468
}
469469

470-
func (i *inMemoryClient) CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel gitlab.AccessLevel) (*gitlab.TokenGroup, error) {
470+
func (i *inMemoryClient) CreateGroupAccessToken(ctx context.Context, groupId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (*gitlab.TokenGroup, error) {
471471
i.muLock.Lock()
472472
defer i.muLock.Unlock()
473473
if i.groupAccessTokenCreateError {
@@ -495,7 +495,7 @@ func (i *inMemoryClient) CreateGroupAccessToken(ctx context.Context, groupId str
495495
return entryToken, nil
496496
}
497497

498-
func (i *inMemoryClient) CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel gitlab.AccessLevel) (*gitlab.TokenProject, error) {
498+
func (i *inMemoryClient) CreateProjectAccessToken(ctx context.Context, projectId string, name string, expiresAt time.Time, scopes []string, accessLevel t.AccessLevel) (*gitlab.TokenProject, error) {
499499
i.muLock.Lock()
500500
defer i.muLock.Unlock()
501501
if i.projectAccessTokenCreateError {

internal/errs/errs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ var (
1818
// ErrBackendNotConfigured represents an error when trying to use a backend that hasn't been properly configured
1919
ErrBackendNotConfigured = errors.New("backend not configured")
2020

21+
// ErrUnknown represents an error indicating an unknown or unspecified condition occurred.
22+
ErrUnknown = errors.New("unknown")
23+
24+
// ErrUnknownTokenType indicates an error when an undefined or unrecognized token type is encountered.
2125
ErrUnknownTokenType = errors.New("unknown token type")
2226

27+
// ErrUnknownTokenScope is returned when an unrecognized or undefined token scope is encountered.
2328
ErrUnknownTokenScope = errors.New("unknown token scope")
29+
30+
// ErrUnknownAccessLevel indicates an error caused by encountering an undefined or unrecognized access level.
31+
ErrUnknownAccessLevel = errors.New("unknown access level")
2432
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package gitlab
1+
package token
22

33
import (
44
"errors"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//go:build unit
22

3-
package gitlab_test
3+
package token_test
44

55
import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
99

10-
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
10+
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
1111
)
1212

1313
func TestAccessLevel(t *testing.T) {

name_tpl_rand_string_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestTokenNameGenerator_RandString(t *testing.T) {
2020
Path: "/path",
2121
Name: "{{ randHexString 8 }}",
2222
Scopes: []string{token.ScopeApi.String()},
23-
AccessLevel: g.AccessLevelNoPermissions,
23+
AccessLevel: token.AccessLevelNoPermissions,
2424
TokenType: token.TypePersonal,
2525
GitlabRevokesTokens: false,
2626
},

name_tpl_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestTokenNameGenerator(t *testing.T) {
2929
Path: "/path",
3030
Name: "{{ .role_name",
3131
Scopes: []string{token.ScopeApi.String()},
32-
AccessLevel: g.AccessLevelNoPermissions,
32+
AccessLevel: token.AccessLevelNoPermissions,
3333
TokenType: token.TypePersonal,
3434
GitlabRevokesTokens: true,
3535
},
@@ -45,7 +45,7 @@ func TestTokenNameGenerator(t *testing.T) {
4545
Path: "/path",
4646
Name: "{{ .role_name }}-{{ .token_type }}-access-token-{{ yesNoBool .gitlab_revokes_token }}",
4747
Scopes: []string{token.ScopeApi.String()},
48-
AccessLevel: g.AccessLevelNoPermissions,
48+
AccessLevel: token.AccessLevelNoPermissions,
4949
TokenType: token.TypePersonal,
5050
GitlabRevokesTokens: true,
5151
},
@@ -61,7 +61,7 @@ func TestTokenNameGenerator(t *testing.T) {
6161
Path: "/path",
6262
Name: "{{ .role_name }}-{{ .token_type }}-{{ stringsJoin .scopes \"-\" }}-{{ yesNoBool .gitlab_revokes_token }}",
6363
Scopes: []string{token.ScopeApi.String(), token.ScopeSudo.String()},
64-
AccessLevel: g.AccessLevelNoPermissions,
64+
AccessLevel: token.AccessLevelNoPermissions,
6565
TokenType: token.TypePersonal,
6666
GitlabRevokesTokens: false,
6767
},
@@ -77,7 +77,7 @@ func TestTokenNameGenerator(t *testing.T) {
7777
Path: "/path",
7878
Name: "{{ .role_name }}-{{ .token_type }}-{{ timeNowFormat \"2006-01\" }}",
7979
Scopes: []string{token.ScopeApi.String(), token.ScopeSudo.String()},
80-
AccessLevel: g.AccessLevelNoPermissions,
80+
AccessLevel: token.AccessLevelNoPermissions,
8181
TokenType: token.TypePersonal,
8282
GitlabRevokesTokens: false,
8383
},

name_tpl_unix_timestamp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestTokenNameGenerator_UnixTimeStamp(t *testing.T) {
2222
Path: "/path",
2323
Name: "{{ .unix_timestamp_utc }}",
2424
Scopes: []string{token.ScopeApi.String()},
25-
AccessLevel: g.AccessLevelNoPermissions,
25+
AccessLevel: token.AccessLevelNoPermissions,
2626
TokenType: token.TypePersonal,
2727
GitlabRevokesTokens: false,
2828
},

0 commit comments

Comments
 (0)