-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_credentials.go
103 lines (81 loc) · 2.74 KB
/
path_credentials.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
package secretsengine
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathCredentials(b *apigeeBackend) *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeLowerCaseString,
Description: "Name of the role",
Required: true,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathCredentialsRead,
logical.UpdateOperation: b.pathCredentialsRead,
},
HelpSynopsis: pathCredentialsHelpSyn,
HelpDescription: pathCredentialsHelpDesc,
}
}
func (b *apigeeBackend) pathCredentialsRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleName := d.Get("name").(string)
roleEntry, err := b.getRole(ctx, req.Storage, roleName)
if err != nil {
return nil, fmt.Errorf("error retrieving role: %w", err)
}
if roleEntry == nil {
return nil, errors.New("error retrieving role: role is nil")
}
return b.createCreds(ctx, req, roleEntry)
}
func (b *apigeeBackend) createCreds(ctx context.Context, req *logical.Request, role *apigeeRole) (*logical.Response, error) {
token, err := b.createCredentials(ctx, req.Storage, role)
if err != nil {
return nil, err
}
resp := b.Secret(apigeeSecretType).Response(map[string]interface{}{
"org_name": token.OrgName,
"developer_email": token.DeveloperEmail,
"app_name": token.AppName,
"api_products": token.ApiProducts,
"key": token.Key,
"secret": token.Secret,
"credentials": token.Credentials,
}, map[string]interface{}{
"org_name": token.OrgName,
"developer_email": token.DeveloperEmail,
"app_name": token.AppName,
"api_products": token.ApiProducts,
"key": token.Key,
"secret": token.Secret,
"credentials": token.Credentials,
})
if role.TTL > 0 {
resp.Secret.TTL = role.TTL
}
return resp, nil
}
func (b *apigeeBackend) createCredentials(ctx context.Context, s logical.Storage, role *apigeeRole) (*apigeeToken, error) {
client, err := b.getClient(ctx, s)
if err != nil {
return nil, err
}
var token *apigeeToken
token, err = createCredentials(ctx, client, role.OrgName, role.DeveloperEmail, role.AppName, role.ApiProducts, int(role.TTL.Seconds()))
if err != nil {
return nil, fmt.Errorf("error creating credentials: %w", err)
}
if token == nil {
return nil, errors.New("error creating credentials")
}
return token, nil
}
const pathCredentialsHelpSyn = `Generate Apigee credentials from Vault role.`
const pathCredentialsHelpDesc = `Generate Apigee credentials from Vault role.`