forked from ceph/go-ceph
/
keys.go
67 lines (58 loc) · 1.64 KB
/
keys.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
package admin
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// CreateKey will generate new keys or add specified to keyring
// https://docs.ceph.com/en/latest/radosgw/adminops/#create-key
func (api *API) CreateKey(ctx context.Context, key UserKeySpec) (*[]UserKeySpec, error) {
switch key.KeyType {
case "swift":
if key.SubUser == "" {
return nil, errMissingSubuserID
}
case "s3", "": /* s3 key-type is regarded as default */
if key.UID == "" {
return nil, errMissingUserID
}
default:
return nil, errUnsupportedKeyType
}
body, err := api.call(ctx, http.MethodPut, "/user?key", valueToURLParams(key, []string{"uid", "subuser", "access-key", "secret-key", "key-type", "generate-key"}))
if err != nil {
return nil, err
}
ref := []UserKeySpec{}
err = json.Unmarshal(body, &ref)
if err != nil {
return nil, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
}
return &ref, nil
}
// RemoveKey will remove an existing key
// https://docs.ceph.com/en/latest/radosgw/adminops/#remove-key
// KeySpec.SecretKey parameter shouldn't be provided and will be ignored
func (api *API) RemoveKey(ctx context.Context, key UserKeySpec) error {
switch key.KeyType {
case "swift":
if key.SubUser == "" {
return errMissingSubuserID
}
case "s3", "": /* s3 key-type is regarded as default */
if key.UID == "" {
return errMissingUserID
}
if key.AccessKey == "" {
return errMissingUserAccessKey
}
default:
return errUnsupportedKeyType
}
_, err := api.call(ctx, http.MethodDelete, "/user?key", valueToURLParams(key, []string{"uid", "subuser", "access-key", "key-type"}))
if err != nil {
return err
}
return nil
}