forked from jfrog/vault-plugin-secrets-artifactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config_rotate.go
114 lines (95 loc) · 2.91 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
114
package artifactory
import (
"context"
"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 admin token.",
},
"description": {
Type: framework.TypeString,
Description: "Optional. Set Artifactory token description on new admin token.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigRotateWrite,
Summary: "Rotate the Artifactory Admin Token.",
},
},
HelpSynopsis: `Rotate the Artifactory Admin Token.`,
HelpDescription: `
This will rotate the "access_token" used to access artifactory from this plugin, and revoke the old admin 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
}
go b.sendUsage(*config, "pathConfigRotateWrite")
oldAccessToken := config.AccessToken
// Parse Current Token (to get tokenID/scope)
token, err := b.getTokenInfo(*config, oldAccessToken)
if err != nil {
return logical.ErrorResponse("error parsing existing AccessToken: " + 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 Admin token for artifactory-secrets plugin in Vault"
}
// Create a new token
resp, err := b.CreateToken(*config, *role)
if err != nil {
return logical.ErrorResponse("error creating new token"), err
}
// Set new token
config.AccessToken = resp.AccessToken
// Save new config
entry, err := logical.StorageEntryJSON("config/admin", config)
if err != nil {
return nil, err
}
err = req.Storage.Put(ctx, entry)
if err != nil {
return nil, err
}
// Invalidate Old Token
oldSecret := logical.Secret{
InternalData: map[string]interface{}{
"access_token": oldAccessToken,
"token_id": token.TokenID,
},
}
err = b.RevokeToken(*config, oldSecret)
if err != nil {
return logical.ErrorResponse("error revoking existing AccessToken"), err
}
return nil, nil
}