forked from immutability-io/vault-btc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_config.go
163 lines (146 loc) · 5.32 KB
/
path_config.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright © 2018 Immutability, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
const (
// Bitcoin is Bitcoin
Bitcoin string = "btc"
// MainNet is the main Bitcoin network.
MainNet string = "mainnet"
// RegressionNet is the regression test Bitcoin network
RegressionNet string = "regtest"
// TestNet is the test Bitcoin network (version 3).
TestNet string = "testnet"
// SimNet is the simulation network.
SimNet string = "simnet"
)
// Config contains required information for this plugin to operate
type Config struct {
BoundCIDRList []string `json:"bound_cidr_list_list" structs:"bound_cidr_list" mapstructure:"bound_cidr_list"`
Network string `json:"network" structs:"network" mapstructure:"network"`
CompressPublicKeys bool `json:"compress_public_keys" structs:"compress_public_keys" mapstructure:"compress_public_keys"`
}
func configPaths(b *backend) []*framework.Path {
return []*framework.Path{
&framework.Path{
Pattern: "config",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathCreateConfig,
logical.UpdateOperation: b.pathCreateConfig,
logical.ReadOperation: b.pathReadConfig,
},
HelpSynopsis: "Configure the trustee plugin.",
HelpDescription: `
Configure the trustee plugin.
`,
Fields: map[string]*framework.FieldSchema{
"network": &framework.FieldSchema{
Type: framework.TypeString,
Description: `Bitcoin network - can be one of the following values:
mainnet - The main Bitcoin network.
regtest - The regression test Bitcoin network. Not to be confused with the
test Bitcoin network (version 3).
testnet - The test Bitcoin network (version 3). Not to be confused with the
regression test network, this network is sometimes simply called "testnet".
simnet - This network is similar to the normal test network except it is
intended for private use within a group of individuals doing simulation
testing. The functionality is intended to differ in that the only nodes
which are specifically specified are used to create the network rather than
following normal discovery rules.`,
Default: TestNet,
},
"compress_public_keys": &framework.FieldSchema{
Type: framework.TypeBool,
Description: `Determines whether addresses are generated from compressed or uncompressed public keys.`,
Default: false,
},
"bound_cidr_list": &framework.FieldSchema{
Type: framework.TypeCommaStringSlice,
Description: `Comma separated string or list of CIDR blocks. If set, specifies the blocks of
IP addresses which can perform the login operation.`,
},
},
},
}
}
func (b *backend) pathCreateConfig(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
network := data.Get("network").(string)
if !ValidNetwork(network) {
return nil, fmt.Errorf("Invalid network: %s", network)
}
compress := data.Get("compress_public_keys").(bool)
var boundCIDRList []string
if boundCIDRListRaw, ok := data.GetOk("bound_cidr_list"); ok {
boundCIDRList = boundCIDRListRaw.([]string)
}
configBundle := Config{
BoundCIDRList: boundCIDRList,
Network: network,
CompressPublicKeys: compress,
}
entry, err := logical.StorageEntryJSON("config", configBundle)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// Return the secret
return &logical.Response{
Data: map[string]interface{}{
"bound_cidr_list": configBundle.BoundCIDRList,
"network": configBundle.Network,
"compress_public_keys": configBundle.CompressPublicKeys,
},
}, nil
}
func (b *backend) pathReadConfig(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
configBundle, err := b.readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if configBundle == nil {
return nil, nil
}
// Return the secret
return &logical.Response{
Data: map[string]interface{}{
"bound_cidr_list": configBundle.BoundCIDRList,
"network": configBundle.Network,
"compress_public_keys": configBundle.CompressPublicKeys,
},
}, nil
}
// Config returns the configuration for this backend.
func (b *backend) readConfig(ctx context.Context, s logical.Storage) (*Config, error) {
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, err
}
if entry == nil {
return nil, fmt.Errorf("the backend is not configured properly")
}
var result Config
if entry != nil {
if err := entry.DecodeJSON(&result); err != nil {
return nil, fmt.Errorf("error reading configuration: %s", err)
}
}
return &result, nil
}