This repository was archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets_service.go
412 lines (380 loc) · 9.21 KB
/
secrets_service.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package databricks
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// SecretsService is a service for interacting with the DBFS.
type SecretsService struct {
client Client
}
// CreateSecretScope Creates a new secret scope.
//
// The scope name must consist of alphanumeric characters,
// dashes, underscores, and periods, and may not exceed 128
// characters. The maximum number of scopes in a workspace
// is 100.
func (s *SecretsService) CreateSecretScope(
ctx context.Context,
scope, initialManagePrincipal string,
) error {
raw, err := json.Marshal(struct {
Scope string
InitialManagePrincipal string
}{
scope,
initialManagePrincipal,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/scopes/create",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// DeleteSecretScope removes a secret scope.
func (s *SecretsService) DeleteSecretScope(
ctx context.Context,
scope string,
) error {
raw, err := json.Marshal(struct {
Scope string
}{
scope,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/scopes/delete",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// ListSecretScopes returns all secret scopes available in the workspace.
func (s *SecretsService) ListSecretScopes(
ctx context.Context,
) ([]SecretScope, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/secrets/scopes/list",
nil,
)
if err != nil {
return []SecretScope{}, err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return []SecretScope{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []SecretScope{}, fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
listRes := struct {
Scopes []SecretScope
}{[]SecretScope{}}
err = decoder.Decode(&listRes)
return listRes.Scopes, err
}
// PutSecret inserts a secret under the provided scope with the given name. If
// a secret already exists with the same name, this command overwrites the
// existing secret’s value. The server encrypts the secret using the secret
// scope’s encryption settings before storing it. You must have WRITE or MANAGE
// permission on the secret scope.
//
// The secret key must consist of alphanumeric characters, dashes, underscores,
// and periods, and cannot exceed 128 characters. The maximum allowed secret
// value size is 128 KB. The maximum number of secrets in a given scope is
// 1000.
func (s *SecretsService) PutSecret(
ctx context.Context,
scope, key, value string,
) error {
raw, err := json.Marshal(struct {
Scope string
Key string
StringValue string
}{
scope,
key,
value,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/put",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// DeleteSecret removes a secret.
func (s *SecretsService) DeleteSecret(
ctx context.Context,
scope, key string,
) error {
raw, err := json.Marshal(struct {
Scope string
Key string
}{
scope,
key,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/delete",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// ListSecrets returns the secret keys that are stored at this scope. This is a
// metadata-only operation; secret data cannot be retrieved using this API.
// Users need READ permission to make this call.
func (s *SecretsService) ListSecrets(
ctx context.Context,
) ([]SecretMetadata, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/secrets/list",
nil,
)
if err != nil {
return []SecretMetadata{}, err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return []SecretMetadata{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []SecretMetadata{}, fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
listRes := struct {
Secrets []SecretMetadata
}{[]SecretMetadata{}}
err = decoder.Decode(&listRes)
return listRes.Secrets, err
}
// PutSecretACL Creates or overwrites the ACL associated with the given
// principal (user or group) on the specified scope point. In general, a user
// or group will use the most powerful permission available to them, and
// permissions are ordered as follows:
//
// MANAGE - Allowed to change ACLs, and read and write to this secret scope.
// WRITE - Allowed to read and write to this secret scope. READ - Allowed to
// read this secret scope and list what secrets are available. Note that in
// general, secret values can only be read from within a command on a cluster
// (for example, through a notebook). There is no API to read the actual secret
// value material outside of a cluster. However, the user’s permission will be
// applied based on who is executing the command, and they must have at least
// READ permission.
//
// Users must have the MANAGE permission to invoke this API.
func (s *SecretsService) PutSecretACL(
ctx context.Context,
scope, principal string,
permission string,
) error {
raw, err := json.Marshal(struct {
Scope string
Principal string
Permission string
}{
scope,
principal,
permission,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/acls/put",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// DeleteSecretACL removes a Secret ACL.
func (s *SecretsService) DeleteSecretACL(
ctx context.Context,
scope, principal string,
) error {
raw, err := json.Marshal(struct {
Scope string
Principal string
}{
scope,
principal,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/secrets/acls/delete",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
return nil
}
// GetSecretACL Describes the details about the given ACL, such as the group
// and permission.
//
// Users must have the MANAGE permission to invoke this API.
func (s *SecretsService) GetSecretACL(
ctx context.Context,
scope, principal string,
) (string, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/secrets/acls/get",
nil,
)
if err != nil {
return "", err
}
req = req.WithContext(ctx)
q := req.URL.Query()
q.Add("scope", scope)
q.Add("principal", principal)
req.URL.RawQuery = q.Encode()
res, err := s.client.client.Do(req)
if err != nil {
return "", err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return "", fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
getRes := struct {
Principal string
Permission string
}{}
err = decoder.Decode(&getRes)
return getRes.Permission, err
}
// ListSecretACLs Lists the ACLs set on the given scope.
//
// Users must have the MANAGE permission to invoke this API.
func (s *SecretsService) ListSecretACLs(
ctx context.Context,
scope string,
) ([]ACLItem, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/secrets/acls/list",
nil,
)
if err != nil {
return []ACLItem{}, err
}
req = req.WithContext(ctx)
q := req.URL.Query()
q.Add("scope", scope)
req.URL.RawQuery = q.Encode()
res, err := s.client.client.Do(req)
if err != nil {
return []ACLItem{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []ACLItem{}, fmt.Errorf(
"Failed to return a 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
listRes := struct {
Items []ACLItem
}{[]ACLItem{}}
err = decoder.Decode(&listRes)
return listRes.Items, err
}