forked from Serviceware/vault-plugin-secrets-keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config_connection.go
282 lines (243 loc) · 7.99 KB
/
path_config_connection.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package keycloak
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
storageKey = "config/connection"
storagePerRealmKey = "config/realms/%s/connection"
)
func pathConfigConnection(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/connection",
Fields: map[string]*framework.FieldSchema{
"server_url": {
Type: framework.TypeString,
Description: "Base Keycloak Url http://auth.example.org",
},
"realm": {
Type: framework.TypeString,
Description: "Name of the realm where the clients are stored",
},
"client_id": {
Type: framework.TypeString,
Description: "Client to be used to access keycloak",
},
"client_secret": {
Type: framework.TypeString,
Description: `The secret that is used to get an access token`,
},
"ignore_connectivity_check": {
Type: framework.TypeBool,
Description: `Ignore connectivity check`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathConnectionUpdate,
logical.ReadOperation: b.pathConnectionRead,
logical.DeleteOperation: b.pathConnectionDelete,
},
}
}
func pathConfigConnectionOfRealm(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/realms/" + framework.GenericNameRegex("realm") + "/connection",
Fields: map[string]*framework.FieldSchema{
"server_url": {
Type: framework.TypeString,
Description: "Base Keycloak Url http://auth.example.org",
},
"realm": {
Type: framework.TypeString,
Description: "Name of the realm where the clients are stored",
},
"client_id": {
Type: framework.TypeString,
Description: "Client to be used to access keycloak",
},
"client_secret": {
Type: framework.TypeString,
Description: `The secret that is used to get an access token`,
},
"ignore_connectivity_check": {
Type: framework.TypeBool,
Description: `Ignore connectivity check`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathConnectionUpdateOfRealm,
logical.DeleteOperation: b.pathConnectionDeleteForRealm,
logical.ReadOperation: b.pathConnectionReadForRealm,
},
}
}
func (b *backend) pathConnectionUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
server_url := data.Get("server_url").(string)
if server_url == "" {
return logical.ErrorResponse("missing server_url"), nil
}
realm := data.Get("realm").(string)
if realm == "" {
return logical.ErrorResponse("missing realm"), nil
}
clientId := data.Get("client_id").(string)
if clientId == "" {
return logical.ErrorResponse("missing client_id"), nil
}
clientSecret := data.Get("client_secret").(string)
if clientSecret == "" {
return logical.ErrorResponse("missing client_secret"), nil
}
ignore_connectivity_check := data.Get("ignore_connectivity_check").(bool)
config := ConnectionConfig{
ServerUrl: server_url,
Realm: realm,
ClientId: clientId,
ClientSecret: clientSecret,
}
if !ignore_connectivity_check {
if _, _, err := b.getClientAndAccessToken(ctx, config); err != nil {
b.logger.Warn("failed to access keycloak", "error", err)
return logical.ErrorResponse("failed to access keycloak"), err
}
}
if err := writeConfig(ctx, req.Storage, config); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConnectionUpdateOfRealm(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
server_url := data.Get("server_url").(string)
if server_url == "" {
return logical.ErrorResponse("missing server_url"), nil
}
realm := data.Get("realm").(string)
if realm == "" {
return logical.ErrorResponse("missing realm"), nil
}
clientId := data.Get("client_id").(string)
if clientId == "" {
return logical.ErrorResponse("missing client_id"), nil
}
clientSecret := data.Get("client_secret").(string)
if clientSecret == "" {
return logical.ErrorResponse("missing client_secret"), nil
}
ignore_connectivity_check := data.Get("ignore_connectivity_check").(bool)
config := ConnectionConfig{
ServerUrl: server_url,
Realm: realm,
ClientId: clientId,
ClientSecret: clientSecret,
}
if !ignore_connectivity_check {
if _, _, err := b.getClientAndAccessToken(ctx, config); err != nil {
b.logger.Info("failed to access keycloak", "error", err)
return logical.ErrorResponse("failed to access keycloak"), err
}
}
if err := writeConfigForKey(ctx, req.Storage, config, fmt.Sprintf(storagePerRealmKey, realm)); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConnectionDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := deleteConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConnectionDeleteForRealm(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
realm := data.Get("realm").(string)
if realm == "" {
return logical.ErrorResponse("missing realm"), nil
}
err := deleteConfigForKey(ctx, req.Storage, fmt.Sprintf(storagePerRealmKey, realm))
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConnectionRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
response := &logical.Response{
Data: map[string]interface{}{
"client_id": config.ClientId,
"client_secret": config.ClientSecret,
"server_url": config.ServerUrl,
"realm": config.Realm,
},
}
return response, nil
}
func (b *backend) pathConnectionReadForRealm(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
realm := data.Get("realm").(string)
if realm == "" {
return logical.ErrorResponse("missing realm"), nil
}
config, err := readConfigForKey(ctx, req.Storage, fmt.Sprintf(storagePerRealmKey, realm))
if err != nil {
return nil, err
}
response := &logical.Response{
Data: map[string]interface{}{
"client_id": config.ClientId,
"client_secret": config.ClientSecret,
"server_url": config.ServerUrl,
"realm": config.Realm,
},
}
return response, nil
}
func readConfig(ctx context.Context, storage logical.Storage) (ConnectionConfig, error) {
return readConfigForKey(ctx, storage, storageKey)
}
func readConfigForKey(ctx context.Context, storage logical.Storage, storageKey string) (ConnectionConfig, error) {
entry, err := storage.Get(ctx, storageKey)
if err != nil {
return ConnectionConfig{}, err
}
if entry == nil {
return ConnectionConfig{}, nil
}
var connConfig ConnectionConfig
if err := entry.DecodeJSON(&connConfig); err != nil {
return ConnectionConfig{}, err
}
return connConfig, nil
}
func writeConfig(ctx context.Context, storage logical.Storage, config ConnectionConfig) error {
return writeConfigForKey(ctx, storage, config, storageKey)
}
func writeConfigForKey(ctx context.Context, storage logical.Storage, config ConnectionConfig, storageKey string) error {
entry, err := logical.StorageEntryJSON(storageKey, config)
if err != nil {
return err
}
if err := storage.Put(ctx, entry); err != nil {
return err
}
return nil
}
func deleteConfig(ctx context.Context, storage logical.Storage) error {
return deleteConfigForKey(ctx, storage, storageKey)
}
func deleteConfigForKey(ctx context.Context, storage logical.Storage, storageKey string) error {
if err := storage.Delete(ctx, storageKey); err != nil {
return err
}
return nil
}
// ConnectionConfig contains the information required to make a connection to a RabbitMQ node
type ConnectionConfig struct {
ServerUrl string `json:"server_url"`
Realm string `json:"realm"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}