This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
path_ejson_keys.go
135 lines (116 loc) · 3.46 KB
/
path_ejson_keys.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
package secretsejson
import (
"context"
"fmt"
"github.com/Shopify/ejson"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func ejsonKeysPaths(b *backend) []*framework.Path {
return []*framework.Path{
{
Pattern: "keys/.*",
Fields: map[string]*framework.FieldSchema{
"public": &framework.FieldSchema{
Type: framework.TypeString,
Description: "EJSON Public key",
},
"private": &framework.FieldSchema{
Type: framework.TypeString,
Description: "EJSON Private key",
},
},
ExistenceCheck: b.pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.keyRead,
logical.CreateOperation: b.keyCreateUpdate,
logical.UpdateOperation: b.keyCreateUpdate,
logical.DeleteOperation: b.keyDelete,
logical.ListOperation: b.keyList,
},
},
{
Pattern: "keypair",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.keyPairCreate,
logical.UpdateOperation: b.keyPairCreate,
},
},
}
}
func (b *backend) pathExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
out, err := req.Storage.Get(ctx, req.Path)
if err != nil {
return false, errwrap.Wrapf("existence check failed: {{err}}", err)
}
return out != nil, nil
}
func (b *backend) keyRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(ctx, req.Path)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
b.Logger().Info("reading value at", "path", req.Path)
// Return the secret
resp := &logical.Response{
Data: map[string]interface{}{
"private": nil,
},
}
resp.Data["private"] = string(entry.Value)
return resp, nil
}
func (b *backend) keyCreateUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
private := data.Get("private").(string)
b.Logger().Info("storing value at", "path", req.Path)
entry := &logical.StorageEntry{
Key: req.Path,
Value: []byte(private),
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"private": private,
},
}, nil
}
func (b *backend) keyDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.Logger().Info("deleting value at", "path", req.Path)
if err := req.Storage.Delete(ctx, req.Path); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) keyList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
vals, err := req.Storage.List(ctx, req.Path)
if err != nil {
return nil, err
}
return logical.ListResponse(vals), nil
}
func (b *backend) keyPairCreate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
public, private, err := ejson.GenerateKeypair()
if err != nil {
return nil, errwrap.Wrapf("failed to generate keypair ejson: {{err}}", err)
}
path := fmt.Sprintf("keys/%s", public)
b.Logger().Info(fmt.Sprintf("New key pair at %s", path))
entry := &logical.StorageEntry{
Key: path,
Value: []byte(private),
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"public": public,
},
}, nil
}