-
Notifications
You must be signed in to change notification settings - Fork 21
/
path_config_rotate.go
113 lines (93 loc) · 3.2 KB
/
path_config_rotate.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
package artifactory
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathConfigRotate() *framework.Path {
return &framework.Path{
Pattern: "config/rotate",
Fields: map[string]*framework.FieldSchema{
"username": {
Type: framework.TypeString,
Description: "Optional. Override Artifactory token username for new access token.",
},
"description": {
Type: framework.TypeString,
Description: "Optional. Set Artifactory token description on new access token.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigRotateWrite,
Summary: "Rotate the Artifactory Access Token.",
},
},
HelpSynopsis: `Rotate the Artifactory Access Token.`,
HelpDescription: `This will rotate the "access_token" used to access artifactory from this plugin. A new access token is created first then revokes the old access token.`,
}
}
func (b *backend) pathConfigRotateWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.configMutex.Lock()
defer b.configMutex.Unlock()
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
if config.AccessToken == "" {
return logical.ErrorResponse("missing access token"), errors.New("missing access token")
}
go b.sendUsage(config.baseConfiguration, "pathConfigRotateWrite")
oldAccessToken := config.AccessToken
// Parse Current Token (to get tokenID/scope)
token, err := b.getTokenInfo(config.baseConfiguration, oldAccessToken)
if err != nil {
return logical.ErrorResponse("error parsing existing access token: " + err.Error()), err
}
// Check for submitted username
if val, ok := data.GetOk("username"); ok {
token.Username = val.(string)
}
if len(token.Username) == 0 {
token.Username = "admin-vault-secrets-artifactory" // default username if empty
}
// Create admin role for the new token
role := &artifactoryRole{
Username: token.Username,
Scope: token.Scope,
}
// Check for new description
if val, ok := data.GetOk("description"); ok {
role.Description = val.(string)
} else {
role.Description = "Rotated access token for artifactory-secrets plugin in Vault"
}
// Create a new token
resp, err := b.CreateToken(config.baseConfiguration, *role)
if err != nil {
return logical.ErrorResponse("error creating new access token"), err
}
// Set new token and set revoke_on_delete to true
config.AccessToken = resp.AccessToken
b.Logger().With("func", "pathConfigRotateWrite").Info("set config.RevokeOnDelete to 'true'")
config.RevokeOnDelete = true
// Save new config
entry, err := logical.StorageEntryJSON(configAdminPath, config)
if err != nil {
return nil, err
}
err = req.Storage.Put(ctx, entry)
if err != nil {
return nil, err
}
// Invalidate Old Token
err = b.RevokeToken(config.baseConfiguration, token.TokenID)
if err != nil {
return logical.ErrorResponse("error revoking existing access token %s", token.TokenID), err
}
return nil, nil
}