This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_token_create.go
116 lines (96 loc) · 2.94 KB
/
path_token_create.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
package artifactory
import (
"context"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathTokenCreate() *framework.Path {
return &framework.Path{
Pattern: "token/" + framework.GenericNameWithAtRegex("role"),
Fields: map[string]*framework.FieldSchema{
"role": {
Type: framework.TypeString,
Description: `Use the configuration of the specified role.`,
},
"ttl": {
Type: framework.TypeDurationSecond,
Description: `Override the default TTL when issuing this access token. Cannot exceed smallest (system, backend, role, this request) maximum TTL.`,
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: `Override the maximum TTL for this access token. Cannot exceed smallest (system, backend) maximum TTL.`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathTokenCreatePerform,
},
},
HelpSynopsis: `Create an Artifactory access token for the specified role.`,
}
}
type createTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
func (b *backend) pathTokenCreatePerform(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.rolesMutex.RLock()
b.configMutex.RLock()
defer b.configMutex.RUnlock()
defer b.rolesMutex.RUnlock()
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
// Read in the requested role
roleName := data.Get("role").(string)
role, err := b.Role(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse("no such role"), nil
}
var ttl time.Duration
if value, ok := data.GetOk("ttl"); ok {
ttl = time.Second * time.Duration(value.(int))
} else {
ttl = role.DefaultTTL
}
maxLeaseTTL := b.Backend.System().MaxLeaseTTL()
maxTTL := role.MaxTTL
if maxTTL == 0 {
maxTTL = maxLeaseTTL
} else if maxTTL > maxLeaseTTL {
maxTTL = maxLeaseTTL
}
if maxTTL > 0 && ttl > maxTTL {
ttl = maxTTL
}
resp, err := b.createToken(*config, *role)
if err != nil {
return nil, err
}
if resp.ExpiresIn > 0 {
b.Backend.Logger().Warn("Artifactory access token created, but cannot be revoked as it has an expiry. See RTFACT-15293.",
"role", roleName,
"expires_in", resp.ExpiresIn)
}
response := b.Secret(SecretArtifactoryAccessTokenType).Response(map[string]interface{}{
"access_token": resp.AccessToken,
"role": roleName,
"scope": resp.Scope,
}, map[string]interface{}{
"role": roleName,
"access_token": resp.AccessToken,
})
response.Secret.TTL = ttl
response.Secret.MaxTTL = maxTTL
return response, nil
}