Skip to content

Commit d5dadf8

Browse files
committed
Update and cleanup
1 parent 6c58328 commit d5dadf8

22 files changed

+174
-114
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ fabric.properties
3838
go.work
3939
tmp/
4040
bin/
41+
/coverage.html
42+
/coverage.out
43+
.envrc

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ through Vault.
1717
- Gitlab Group Access Tokens - https://docs.gitlab.com/ee/api/group_access_tokens.html
1818
- Gitlab User Service Account Tokens - https://docs.gitlab.com/ee/api/users.html#create-service-account-user
1919
- Gitlab Group Service Account Tokens - https://docs.gitlab.com/ee/api/group_service_accounts.html
20+
- Gitlab Pipeline Project Trigger Tokens - https://docs.gitlab.com/ee/api/pipeline_triggers.html
2021

2122
## Getting Started
2223

@@ -92,7 +93,7 @@ The current authentication model requires providing Vault with a Gitlab Token.
9293
| base_url | yes | n/a | no | The address to access Gitlab |
9394
| auto_rotate_token | no | no | no | Should we autorotate the token when it's close to expiry? (Experimental) |
9495
| auto_rotate_before | no | 24h | no | How much time should be remaining on the token validity before we should rotate it? Minimum can be set to 24h and maximum to 730h |
95-
| type | yes | n/a | no | The type of gitlab instance that we use can be one of saas, self-managed or dedicated |
96+
| type | yes | n/a | no | The type of gitlab instance that we use can be one of saas, self-managed or dedicated |
9697
9798
### Role
9899
@@ -105,7 +106,7 @@ The current authentication model requires providing Vault with a Gitlab Token.
105106
| scopes | no | [] | no | List of scopes |
106107
| token_type | yes | n/a | no | Access token type |
107108
| gitlab_revokes_token | no | no | no | Gitlab revokes the token when it's time. Vault will not revoke the token when the lease expires |
108-
| config_name | no | default | no | The configuration to use for the role |
109+
| config_name | no | default | no | The configuration to use for the role |
109110
110111
#### path
111112
@@ -154,12 +155,14 @@ Depending on `gitlab_revokes_token` the TTL will change.
154155
155156
#### access_level
156157
157-
It's not required if `token_type` is set to `personal`.
158+
It's not required if `token_type` is set to `personal` or `pipeline-project-trigger`.
158159
159160
For a list of available roles check https://docs.gitlab.com/ee/user/permissions.html
160161
161162
#### scopes
162163
164+
It's not required if `token_type` is set to `pipeline-project-trigger`.
165+
163166
Depending on the type of token you have different scopes:
164167
165168
* `Personal` - https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#personal-access-token-scopes
@@ -175,6 +178,7 @@ Can be
175178
* group
176179
* user-service-account
177180
* group-service-account
181+
* pipeline-project-trigger
178182
179183
#### gitlab_revokes_token
180184
@@ -326,7 +330,7 @@ token_sha1_hash 91a91bb30f816770081c570504c5e2723bcb1f38
326330
type self-managed
327331
```
328332
329-
**Important**: Token will be showed after rotation, it will not be shown again.
333+
**Important**: Token will be shown only after rotation, and it will not be shown again.
330334
331335
## Upgrading
332336

gitlab_client.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/hashicorp/go-hclog"
1313
"github.com/hashicorp/vault/sdk/helper/logging"
14-
g "github.com/xanzy/go-gitlab"
14+
g "gitlab.com/gitlab-org/api/client-go"
1515
"golang.org/x/time/rate"
1616
)
1717

@@ -37,6 +37,8 @@ type Client interface {
3737
CreateUserServiceAccountAccessToken(ctx context.Context, username string, userId int, name string, expiresAt time.Time, scopes []string) (*EntryToken, error)
3838
RevokeUserServiceAccountAccessToken(ctx context.Context, token string) error
3939
RevokeGroupServiceAccountAccessToken(ctx context.Context, token string) error
40+
CreatePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, description string) error
41+
RevokePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, tokenId int) error
4042
}
4143

4244
type gitlabClient struct {
@@ -46,6 +48,22 @@ type gitlabClient struct {
4648
logger hclog.Logger
4749
}
4850

51+
func (gc *gitlabClient) CreatePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, description string) (err error) {
52+
defer func() {
53+
gc.logger.Debug("Created a pipeline project trigger access token", "projectId", description, "description", "error", err)
54+
}()
55+
56+
return err
57+
}
58+
59+
func (gc *gitlabClient) RevokePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, tokenId int) (err error) {
60+
defer func() {
61+
gc.logger.Debug("Revoked pipeline project trigger access token", "projectId", projectId, "tokenId", tokenId, "error", err)
62+
}()
63+
64+
return err
65+
}
66+
4967
func (gc *gitlabClient) GetGroupIdByPath(ctx context.Context, path string) (groupId int, err error) {
5068
defer func() {
5169
gc.logger.Debug("Get group id by path", "path", path, "groupId", groupId, "error", err)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/vault/api v1.15.0
99
github.com/hashicorp/vault/sdk v0.14.0
1010
github.com/stretchr/testify v1.10.0
11-
github.com/xanzy/go-gitlab v0.114.0
11+
gitlab.com/gitlab-org/api/client-go v0.116.0
1212
golang.org/x/time v0.8.0
1313
google.golang.org/protobuf v1.35.2
1414
gopkg.in/dnaeon/go-vcr.v4 v4.0.2
@@ -83,7 +83,7 @@ require (
8383
golang.org/x/crypto v0.28.0 // indirect
8484
golang.org/x/mod v0.20.0 // indirect
8585
golang.org/x/net v0.30.0 // indirect
86-
golang.org/x/oauth2 v0.23.0 // indirect
86+
golang.org/x/oauth2 v0.24.0 // indirect
8787
golang.org/x/sync v0.8.0 // indirect
8888
golang.org/x/sys v0.26.0 // indirect
8989
golang.org/x/text v0.19.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
239239
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
240240
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
241241
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
242-
github.com/xanzy/go-gitlab v0.114.0 h1:0wQr/KBckwrZPfEMjRqpUz0HmsKKON9UhCYv9KDy19M=
243-
github.com/xanzy/go-gitlab v0.114.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY=
244242
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
245243
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
244+
gitlab.com/gitlab-org/api/client-go v0.116.0 h1:Dy534gtZPMrnm3fAcmQRMadrcoUyFO4FQ4rXlSAdHAw=
245+
gitlab.com/gitlab-org/api/client-go v0.116.0/go.mod h1:B29OfnZklmaoiR7uHANh9jTyfWEgmXvZLVEnosw2Dx0=
246246
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 h1:Xs2Ncz0gNihqu9iosIZ5SkBbWo5T8JhhLJFMQL1qmLI=
247247
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0/go.mod h1:vy+2G/6NvVMpwGX/NyLqcC41fxepnuKHk16E6IZUcJc=
248248
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
@@ -279,8 +279,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
279279
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
280280
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
281281
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
282-
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
283-
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
282+
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
283+
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
284284
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
285285
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
286286
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

helpers_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/hashicorp/vault/sdk/helper/logging"
2020
"github.com/hashicorp/vault/sdk/logical"
2121
"github.com/stretchr/testify/require"
22-
g "github.com/xanzy/go-gitlab"
22+
g "gitlab.com/gitlab-org/api/client-go"
2323

2424
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
2525
)
@@ -164,6 +164,8 @@ type inMemoryClient struct {
164164
revokeGroupServiceAccountPersonalAccessTokenError bool
165165
createUserServiceAccountAccessTokenError bool
166166
createGroupServiceAccountAccessTokenError bool
167+
createPipelineProjectTriggerAccessTokenError bool
168+
revokePipelineProjectTriggerAccessTokenError bool
167169

168170
calledMainToken int
169171
calledRotateMainToken int
@@ -175,6 +177,24 @@ type inMemoryClient struct {
175177
accessTokens map[string]gitlab.EntryToken
176178
}
177179

180+
func (i *inMemoryClient) CreatePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, description string) error {
181+
i.muLock.Lock()
182+
defer i.muLock.Unlock()
183+
if i.createGroupServiceAccountAccessTokenError {
184+
return fmt.Errorf("CreatePipelineProjectTriggerAccessToken")
185+
}
186+
return nil
187+
}
188+
189+
func (i *inMemoryClient) RevokePipelineProjectTriggerAccessToken(ctx context.Context, projectId int, tokenId int) error {
190+
i.muLock.Lock()
191+
defer i.muLock.Unlock()
192+
if i.createGroupServiceAccountAccessTokenError {
193+
return fmt.Errorf("RevokePipelineProjectTriggerAccessToken")
194+
}
195+
return nil
196+
}
197+
178198
func (i *inMemoryClient) GetGroupIdByPath(ctx context.Context, path string) (int, error) {
179199
idx := slices.Index(i.groups, path)
180200
if idx == -1 {

path_role.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func (b *Backend) pathRolesWrite(ctx context.Context, req *logical.Request, data
266266
case TokenTypeGroupServiceAccount:
267267
validAccessLevels = ValidGroupServiceAccountAccessLevels
268268
skipFields = append(skipFields, "access_level")
269+
case TokenPipelineProjectTrigger:
270+
validAccessLevels = ValidPipelineProjectTriggerAccessLevels
271+
skipFields = append(skipFields, "access_level", "scopes")
269272
}
270273

271274
// check if all required fields are set
@@ -313,6 +316,10 @@ func (b *Backend) pathRolesWrite(ctx context.Context, req *logical.Request, data
313316
if tokenType == TokenTypeGroupServiceAccount {
314317
validScopes = append(validScopes, ValidGroupServiceAccountTokenScopes...)
315318
}
319+
if tokenType == TokenPipelineProjectTrigger {
320+
validScopes = []string{}
321+
}
322+
316323
for _, scope := range role.Scopes {
317324
if !slices.Contains(validScopes, scope) {
318325
invalidScopes = append(invalidScopes, scope)

path_token_role_multiple_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/hashicorp/vault/sdk/logical"
99
"github.com/stretchr/testify/require"
10-
g "github.com/xanzy/go-gitlab"
10+
g "gitlab.com/gitlab-org/api/client-go"
1111

1212
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
1313
)

0 commit comments

Comments
 (0)