-
Notifications
You must be signed in to change notification settings - Fork 7
/
backend.go
106 lines (93 loc) · 2.84 KB
/
backend.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
package internal
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
acctPath = "accounts"
signPath = "sign"
keyPath = "keys"
)
type backend struct {
*framework.Backend
}
func BackendFactory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := new(backend)
b.Backend = &framework.Backend{
Help: strings.TrimSpace("Creates and stores Quorum accounts. Signs data using those accounts.\n"),
BackendType: logical.TypeLogical,
Paths: []*framework.Path{
b.accountIDPath(),
b.signPath(),
},
PathsSpecial: &logical.Paths{
// paths to encrypt when sealed
SealWrapStorage: []string{
fmt.Sprintf("%s/", acctPath),
fmt.Sprintf("%s/", keyPath),
},
},
}
if err := b.Backend.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func (b *backend) accountIDPath() *framework.Path {
return &framework.Path{
Pattern: fmt.Sprintf("%s/%s", acctPath, framework.MatchAllRegex("acctID")),
Fields: map[string]*framework.FieldSchema{
"acctID": {
Type: framework.TypeString,
Description: "Specifies the path of the account.",
},
"import": {
Type: framework.TypeString,
Description: "(optional) A hex-encoded private key to imported and store at the specified path.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.readAccount,
Summary: "Read account address",
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.createAccount,
Summary: "Generate and store new Quorum account, or import existing account by using the 'import' field.",
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.updateAccount,
Summary: "Generate and store new Quorum account, or import existing account by using the 'import' field.",
},
logical.ListOperation: &framework.PathOperation{
Callback: b.listAccountIDs,
Summary: "List account IDs",
},
},
ExistenceCheck: b.accountExistenceCheck, // determines whether create or update operation is called
}
}
func (b *backend) signPath() *framework.Path {
return &framework.Path{
Pattern: fmt.Sprintf("%s/%s", signPath, framework.MatchAllRegex("acctID")),
Fields: map[string]*framework.FieldSchema{
"acctID": {
Type: framework.TypeString,
Description: "Specifies the path of the account.",
},
"sign": {
Type: framework.TypeString,
Description: "Hex-encoded payload to be signed.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.sign,
Summary: "Sign data with account, returns hex-encoded signature in r,s,v format where v is 0 or 1",
},
},
}
}