forked from hashicorp/vault
/
path_config.go
97 lines (79 loc) · 2.23 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
package transit
import (
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func (b *backend) pathConfig() *framework.Path {
return &framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
"allow_upsert": &framework.FieldSchema{
Type: framework.TypeBool,
Default: false,
Description: `Whether to allow upserting keys on first use,
rather than requiring them to be manually
specified through the keys endpoint`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConfigRead,
logical.UpdateOperation: b.pathConfigWrite,
},
HelpSynopsis: pathConfigHelpSyn,
HelpDescription: pathConfigHelpDesc,
}
}
type transitConfig struct {
AllowUpsert bool `json:"allow_upsert" structs:"allow_upsert" mapstructure:"allow_upsert"`
}
func (b *backend) getConfig(s logical.Storage) (*transitConfig, error) {
entry, err := s.Get("config")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result transitConfig
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
func (b *backend) pathConfigRead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
config, err := b.getConfig(req.Storage)
if err != nil {
return nil, err
}
if config == nil {
config = &transitConfig{}
}
resp := &logical.Response{
Data: structs.New(config).Map(),
}
return resp, nil
}
func (b *backend) pathConfigWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
allowUpsertInt, ok := d.GetOk("allow_upsert")
if !ok {
return logical.ErrorResponse("no known configuration parameters supplied"), nil
}
config := &transitConfig{
AllowUpsert: allowUpsertInt.(bool),
}
jsonEntry, err := logical.StorageEntryJSON("config", config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(jsonEntry); err != nil {
return nil, err
}
return nil, nil
}
const pathConfigHelpSyn = `Configure the backend`
const pathConfigHelpDesc = `
This path is used to configure the backend. Currently, this allows configuring
whether or not keys can be created via upsert from the encryption endpoint.`