-
Notifications
You must be signed in to change notification settings - Fork 21
/
path_config.go
315 lines (259 loc) · 10.2 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package artifactory
import (
"context"
"crypto/sha256"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const configAdminPath = "config/admin"
func (b *backend) pathConfig() *framework.Path {
return &framework.Path{
Pattern: configAdminPath,
Fields: map[string]*framework.FieldSchema{
"access_token": {
Type: framework.TypeString,
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,
Default: false,
Description: "Optional. If Artifactory version >= 7.50.3, set expires_in to max_ttl and force_revocable.",
},
"force_revocable": {
Type: framework.TypeBool,
Default: false,
Description: "Optional. When set to true, we will add the 'force_revocable' flag to the token's extension. In addition, a new configuration has been added that sets the default for setting the 'force_revocable' default when creating a new token - the default of this configuration will be 'false' to ensure that the Circle of Trust remains in place.",
},
"bypass_artifactory_tls_verification": {
Type: framework.TypeBool,
Default: false,
Description: "Optional. Bypass certification verification for TLS connection with Artifactory. Default to `false`.",
},
"allow_scope_override": {
Type: framework.TypeBool,
Default: false,
Description: "Optional. Determine if scoped tokens should be allowed. This is an advanced configuration option. Default to `false`.",
},
"revoke_on_delete": {
Type: framework.TypeBool,
Default: false,
Description: "Optional. Revoke Administrator access token when this configuration is deleted. Default to `false`. Will be set to `true` if token is rotated.",
},
},
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.
An optional "allow_scope_override" parameter will enable issuing scoped tokens with Artifactory. This is an advanced option that must
have more sophisticated Vault policies. Please see README for an example.
No renewals or new tokens will be issued if the backend configuration (config/admin) is deleted.
`,
}
}
type adminConfiguration struct {
baseConfiguration
UsernameTemplate string `json:"username_template,omitempty"`
BypassArtifactoryTLSVerification bool `json:"bypass_artifactory_tls_verification,omitempty"`
AllowScopeOverride bool `json:"allow_scope_override,omitempty"`
RevokeOnDelete bool `json:"revoke_on_delete,omitempty"`
}
// fetchAdminConfiguration will return nil,nil if there's no configuration
func (b *backend) fetchAdminConfiguration(ctx context.Context, storage logical.Storage) (*adminConfiguration, error) {
var config adminConfiguration
// Read in the backend configuration
entry, err := storage.Get(ctx, configAdminPath)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
if err := entry.DecodeJSON(&config); err != nil {
return nil, err
}
return &config, nil
}
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{
baseConfiguration: baseConfiguration{
UseNewAccessAPI: true,
},
}
}
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("force_revocable"); ok {
temp := val.(bool)
config.ForceRevocable = &temp
} else {
config.ForceRevocable = nil
}
if val, ok := data.GetOk("bypass_artifactory_tls_verification"); ok {
config.BypassArtifactoryTLSVerification = val.(bool)
}
if val, ok := data.GetOk("allow_scope_override"); ok {
config.AllowScopeOverride = val.(bool)
}
if val, ok := data.GetOk("revoke_on_delete"); ok {
config.RevokeOnDelete = val.(bool)
}
if config.ArtifactoryURL == "" {
return logical.ErrorResponse("url is required"), nil
}
b.InitializeHttpClient(config)
go b.sendUsage(config.baseConfiguration, "pathConfigRotateUpdate")
config.UseNewAccessAPI = b.useNewAccessAPI(config.baseConfiguration)
entry, err := logical.StorageEntryJSON(configAdminPath, 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.baseConfiguration, "pathConfigDelete")
if err := req.Storage.Delete(ctx, configAdminPath); err != nil {
return nil, err
}
if config.RevokeOnDelete {
b.Logger().Info("config.RevokeOnDelete is 'true'. Attempt to revoke access token.")
token, err := b.getTokenInfo(config.baseConfiguration, config.AccessToken)
if err != nil {
b.Logger().Warn("error parsing existing access token", "err", err)
return nil, nil
}
err = b.RevokeToken(config.baseConfiguration, token.TokenID)
if err != nil {
b.Logger().Warn("error revoking existing access token", "tokenId", token.TokenID, "err", err)
return nil, nil
}
}
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()
logger := b.Logger().With("func", "pathConfigRead")
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.baseConfiguration, "pathConfigRead")
version, err := b.getVersion(config.baseConfiguration)
if err != nil {
logger.Error("failed to get system version", "err", err)
return nil, err
}
// 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": version,
"use_expiring_tokens": config.UseExpiringTokens,
"force_revocable": config.ForceRevocable,
"bypass_artifactory_tls_verification": config.BypassArtifactoryTLSVerification,
"allow_scope_override": config.AllowScopeOverride,
"revoke_on_delete": config.RevokeOnDelete,
}
// 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.baseConfiguration, config.AccessToken)
if err != nil {
logger.Warn("Error parsing AccessToken", "err", 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()
}
}
supportForceRevocable, err := b.supportForceRevocable(config.baseConfiguration)
if err != nil {
logger.Warn("failed to determine if force_revocable is supported. Set 'supportForceRevocable' to 'false'.", "err", err)
supportForceRevocable = false
}
if supportForceRevocable {
configMap["use_expiring_tokens"] = config.UseExpiringTokens
}
return &logical.Response{
Data: configMap,
}, nil
}