forked from jfrog/vault-plugin-secrets-artifactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_access_token.go
92 lines (74 loc) · 2.3 KB
/
secret_access_token.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
package artifactory
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const SecretArtifactoryAccessTokenType = "artifactory_access_token"
func (b *backend) secretAccessToken() *framework.Secret {
return &framework.Secret{
Type: SecretArtifactoryAccessTokenType,
Fields: map[string]*framework.FieldSchema{
"access_token": {
Type: framework.TypeString,
Description: `Artifactory Access Token`,
},
"token_id": {
Type: framework.TypeString,
Description: `Artifactory Access Token Id`,
},
"username": {
Type: framework.TypeString,
Description: `Artifactory Username for Token ID`,
},
},
Renew: b.secretAccessTokenRenew,
Revoke: b.secretAccessTokenRevoke,
}
}
func (b *backend) secretAccessTokenRenew(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
resp := &logical.Response{Secret: req.Secret}
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
if !req.Secret.Renewable {
return nil, fmt.Errorf("lease cannot be renewed")
}
role, err := b.Role(ctx, req.Storage, req.Secret.InternalData["role"].(string))
if err != nil {
return nil, fmt.Errorf("error during renew: could not get role: %q", req.Secret.InternalData["role"])
}
if role == nil {
return nil, fmt.Errorf("error during renew: could not find role with name: %q", req.Secret.InternalData["role"])
}
ttl, warnings, err :=
framework.CalculateTTL(b.System(), req.Secret.Increment, role.DefaultTTL, 0, role.MaxTTL, req.Secret.MaxTTL, req.Secret.IssueTime)
if err != nil {
return nil, err
}
if len(warnings) > 0 {
for _, warning := range warnings {
resp.AddWarning(warning)
}
}
resp.Secret.TTL = ttl
return resp, nil
}
func (b *backend) secretAccessTokenRevoke(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
if err := b.RevokeToken(*config, *req.Secret); err != nil {
return nil, err
}
return nil, nil
}