forked from kula/vault-plugin-secrets-backblazeb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_credentials.go
73 lines (60 loc) · 1.75 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
package vault_plugin_secrets_backblazeb2
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// Define the R functions for the keys path
func (b *backblazeB2Backend) pathCredentials() *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("role"),
HelpSynopsis: "Provision an application key for this role.",
Fields: map[string]*framework.FieldSchema{
"role": {
Type: framework.TypeString,
Description: "Name of role",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathKeyRead,
},
},
}
}
// Read a new key
func (b *backblazeB2Backend) pathKeyRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleName := d.Get("role").(string)
role, err := b.getRole(ctx, req.Storage, roleName)
if err != nil {
return nil, fmt.Errorf("error fetching role: %w", err)
}
if role == nil {
return nil, errors.New("error retrieving role: role is nil")
}
name := uuid.New().String()
newKeyName := fmt.Sprintf("%s%s", role.KeyNamePrefix, name)
// Generate key
newKey, err := b.b2ApplicationKeyCreate(ctx, req.Storage, newKeyName, *role)
if err != nil {
return nil, err
}
// Gin up response
resp := b.Secret(b2KeyType).Response(map[string]interface{}{
"application_key_id": newKey.ID(),
"application_key": newKey.Secret(),
}, map[string]interface{}{
"application_key_id": newKey.ID(),
"role": roleName,
})
if role.TTL > 0 {
resp.Secret.TTL = role.TTL
}
if role.MaxTTL > 0 {
resp.Secret.MaxTTL = role.MaxTTL
}
return resp, nil
}