-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_creds_create.go
executable file
·71 lines (61 loc) · 1.92 KB
/
path_creds_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
package openstack
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathCreateCreds(b *backend) *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the credential",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathTokenRead,
},
}
}
func (b *backend) pathTokenRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
// Determine if we have a lease configuration
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
if err != nil {
b.Logger().Warn("get leaseconfig", "error", err)
return nil, err
}
if leaseConfig == nil {
leaseConfig = &configLease{}
}
role, err := b.Role(ctx, req.Storage, name)
if err != nil {
return nil, fmt.Errorf("error retrieving role: %w", err)
}
if role == nil {
return logical.ErrorResponse(fmt.Sprintf("role %q not found", name)), nil
}
c, err := b.client(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error retrieving appCredentialClient: %w", err)
}
// Create it
tokenName := fmt.Sprintf("vault-%s-%s-%d", name, req.DisplayName, time.Now().UnixNano()/int64(time.Millisecond))
id, secret, err := c.Create(tokenName, role.Roles, leaseConfig.TTL) //, role.AccessRules)
if err != nil {
b.Logger().Warn("Create applicationcredential", "error", err)
return nil, err
}
// Use the helper to create the secret
resp := b.Secret(SecretTokenType).Response(map[string]interface{}{
"application_credential_id": id,
"application_credential_secret": secret,
}, map[string]interface{}{
"application_credential_id": id,
})
resp.Secret.TTL = leaseConfig.TTL
return resp, nil
}