-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsecret_access_token.go
132 lines (107 loc) · 3.63 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
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
package artifactory
import (
"context"
"fmt"
"strings"
"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`,
},
"refresh_token": {
Type: framework.TypeString,
Description: `Artifactory Refresh Token`,
},
"reference_token": {
Type: framework.TypeString,
Description: `Artifactory Reference 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) {
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
}
resp := &logical.Response{Secret: req.Secret}
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) {
logger := b.Logger().With("func", "secretAccessTokenRevoke")
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
logger.Debug("failed to fetch admin config", "err", err)
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
// logger.Debug("req", "Path", req.Path, "Secret.InternalData", req.Secret.InternalData)
if config.AccessToken == "" {
// check if this is admin token
if strings.HasPrefix(req.Path, "token/") {
return logical.ErrorResponse("admin access_token is not configured"), nil
}
// try to use user token
if strings.HasPrefix(req.Path, "user_token/") {
logger.Debug("admin access token is empty and request path is user_token")
username := req.Secret.InternalData["username"].(string)
userTokenConfig, err := b.fetchUserTokenConfiguration(ctx, req.Storage, username)
if err != nil {
logger.Debug("failed to fetch user config", "err", err)
return nil, err
}
if userTokenConfig.AccessToken == "" {
return logical.ErrorResponse("user access_token is not configured"), nil
}
config.AccessToken = userTokenConfig.AccessToken
}
}
tokenId := req.Secret.InternalData["token_id"].(string)
if err := b.RevokeToken(config.baseConfiguration, tokenId); err != nil {
return logical.ErrorResponse("failed to revoke access token"), err
}
return nil, nil
}