forked from jfrog/vault-plugin-secrets-artifactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config.go
227 lines (187 loc) · 7.24 KB
/
path_config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package artifactory
import (
"context"
"crypto/sha256"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathConfig() *framework.Path {
return &framework.Path{
Pattern: "config/admin",
Fields: map[string]*framework.FieldSchema{
"access_token": {
Type: framework.TypeString,
Required: true,
Description: "Administrator token to access Artifactory",
},
"url": {
Type: framework.TypeString,
Required: true,
Description: "Address of the Artifactory instance",
},
"username_template": {
Type: framework.TypeString,
Description: "Optional. Vault Username Template for dynamically generating usernames.",
},
"use_expiring_tokens": {
Type: framework.TypeBool,
Description: "Optional. If Artifactory version >= 7.50.3, set expires_in to max_ttl and force_revocable.",
},
"bypass_artifactory_tls_verification": {
Type: framework.TypeBool,
Default: false,
Description: "Optional. Bypass certification verification for TLS connection with Artifactory. Default to `false`.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigUpdate,
Summary: "Configure the Artifactory secrets backend.",
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
Summary: "Delete the Artifactory secrets configuration.",
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
Summary: "Examine the Artifactory secrets configuration.",
},
},
HelpSynopsis: `Interact with the Artifactory secrets configuration.`,
HelpDescription: `
Configure the parameters used to connect to the Artifactory server integrated with this backend. The two main
parameters are "url" which is the absolute URL to the Artifactory server. Note that "/artifactory/api" is prepended by the
individual calls, so do not include it in the URL here.
The second is "access_token" which must be an access token powerful enough to generate the other access tokens you'll
be using. This value is stored seal wrapped when available. Once set, the access token cannot be retrieved, but the backend
will send a sha256 hash of the token so you can compare it to your notes. If the token is a JWT Access Token, it will return
additional information such as jfrog_token_id, username and scope.
An optional "username_template" parameter will override the built-in default username_template for dynamically generating
usernames if a static one is not provided.
An optional "bypass_artifactory_tls_verification" parameter will enable bypassing the TLS connection verification with Artifactory.
No renewals or new tokens will be issued if the backend configuration (config/admin) is deleted.
`,
}
}
type adminConfiguration struct {
AccessToken string `json:"access_token"`
ArtifactoryURL string `json:"artifactory_url"`
UsernameTemplate string `json:"username_template,omitempty"`
UseExpiringTokens bool `json:"use_expiring_tokens,omitempty"`
BypassArtifactoryTLSVerification bool `json:"bypass_artifactory_tls_verification,omitempty"`
}
func (b *backend) pathConfigUpdate(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 {
config = &adminConfiguration{}
}
if val, ok := data.GetOk("url"); ok {
config.ArtifactoryURL = val.(string)
config.AccessToken = "" // clear access token if URL changes, requires setting access_token and url together for security reasons
}
if val, ok := data.GetOk("access_token"); ok {
config.AccessToken = val.(string)
}
if val, ok := data.GetOk("username_template"); ok {
config.UsernameTemplate = val.(string)
up, err := testUsernameTemplate(config.UsernameTemplate)
if err != nil {
return logical.ErrorResponse("username_template error"), err
}
b.usernameProducer = up
}
if val, ok := data.GetOk("use_expiring_tokens"); ok {
config.UseExpiringTokens = val.(bool)
}
if val, ok := data.GetOk("bypass_artifactory_tls_verification"); ok {
config.BypassArtifactoryTLSVerification = val.(bool)
}
if config.AccessToken == "" {
return logical.ErrorResponse("access_token is required"), nil
}
if config.ArtifactoryURL == "" {
return logical.ErrorResponse("url is required"), nil
}
b.InitializeHttpClient(config)
go b.sendUsage(*config, "pathConfigRotateUpdate")
err = b.getVersion(*config)
if err != nil {
return logical.ErrorResponse("Unable to get Artifactory Version. Check url and access_token fields. TLS connection verification with Artifactory can be skipped by setting bypass_artifactory_tls_verification field to 'true'"), err
}
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
}
return nil, nil
}
func (b *backend) pathConfigDelete(ctx context.Context, req *logical.Request, _ *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, "pathConfigDelete")
if err := req.Storage.Delete(ctx, "config/admin"); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
b.configMutex.RLock()
defer b.configMutex.RUnlock()
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, "pathConfigRead")
// I'm not sure if I should be returning the access token, so I'll hash it.
accessTokenHash := sha256.Sum256([]byte(config.AccessToken))
configMap := map[string]interface{}{
"access_token_sha256": fmt.Sprintf("%x", accessTokenHash[:]),
"url": config.ArtifactoryURL,
"version": b.version,
"bypass_artifactory_tls_verification": config.BypassArtifactoryTLSVerification,
}
// Optionally include username_template
if len(config.UsernameTemplate) > 0 {
configMap["username_template"] = config.UsernameTemplate
}
// Optionally include token info if it parses properly
token, err := b.getTokenInfo(*config, config.AccessToken)
if err != nil {
b.Logger().Warn("Error parsing AccessToken: " + err.Error())
} else {
configMap["token_id"] = token.TokenID
configMap["username"] = token.Username
configMap["scope"] = token.Scope
if token.Expires > 0 {
configMap["exp"] = token.Expires
tm := time.Unix(token.Expires, 0)
configMap["expires"] = tm.Local()
}
}
if b.supportForceRevocable() {
configMap["use_expiring_tokens"] = config.UseExpiringTokens
}
return &logical.Response{
Data: configMap,
}, nil
}