forked from kula/vault-plugin-secrets-backblazeb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config.go
160 lines (132 loc) · 4.21 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
package vault_plugin_secrets_backblazeb2
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const configStoragePath = "config"
type backblazeB2Config struct {
ApplicationKeyId string `json:"application_key_id"`
ApplicationKey string `json:"application_key"`
}
// Define the CRU functions for the config path
func (b *backblazeB2Backend) pathConfigCRUD() *framework.Path {
return &framework.Path{
Pattern: "config",
HelpSynopsis: "Configure the Backblaze B2 connection.",
HelpDescription: "Use this endpoint to set the Backblaze B2 key id and key.",
Fields: map[string]*framework.FieldSchema{
"application_key_id": {
Type: framework.TypeString,
Description: "The Backblaze B2 application key id",
Required: true,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Application Key ID",
Sensitive: false,
},
},
"application_key": {
Type: framework.TypeString,
Description: "The Backblaze B2 application key",
Required: true,
DisplayAttrs: &framework.DisplayAttributes{
Name: "Application Key",
Sensitive: true,
},
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
},
},
ExistenceCheck: b.pathConfigExistenceCheck,
}
}
func (b *backblazeB2Backend) pathConfigExistenceCheck(ctx context.Context, req *logical.Request, _ *framework.FieldData) (bool, error) {
out, err := req.Storage.Get(ctx, req.Path)
if err != nil {
return false, fmt.Errorf("existence check failed: %w", err)
}
return out != nil, nil
}
// Read the current configuration
func (b *backblazeB2Backend) pathConfigRead(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
config, err := b.getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
return &logical.Response{
Data: map[string]interface{}{
"application_key_id": config.ApplicationKeyId,
},
}, nil
}
// Update the configuration
func (b *backblazeB2Backend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := b.getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
createOperation := req.Operation == logical.CreateOperation
if config == nil {
if !createOperation {
return nil, errors.New("config not found during update operation")
}
config = new(backblazeB2Config)
}
if applicationKeyID, ok := data.GetOk("application_key_id"); ok {
config.ApplicationKeyId = applicationKeyID.(string)
}
if applicationKey, ok := data.GetOk("application_key"); ok {
config.ApplicationKey = applicationKey.(string)
}
if config.ApplicationKeyId == "" || config.ApplicationKey == "" {
return nil, errors.New("both application_key_id and application_key must be set")
}
entry, err := logical.StorageEntryJSON(configStoragePath, config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// reset the client so the next invocation will pick up the new configuration
b.reset()
return nil, nil
}
func (b *backblazeB2Backend) pathConfigDelete(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete(ctx, configStoragePath)
if err == nil {
b.reset()
}
return nil, err
}
func (b *backblazeB2Backend) getConfig(ctx context.Context, s logical.Storage) (*backblazeB2Config, error) {
entry, err := s.Get(ctx, configStoragePath)
if err != nil {
return nil, fmt.Errorf("error reading mount configuration: %w", err)
}
if entry == nil {
return nil, nil
}
config := new(backblazeB2Config)
if err := entry.DecodeJSON(&config); err != nil {
return nil, fmt.Errorf("error reading root configuration: %w", err)
}
return config, nil
}