-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcredentials_service.go
168 lines (143 loc) · 4.04 KB
/
credentials_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
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
)
// credentialService handles key-auth credentials in Kong.
type credentialService service
var (
credPath = map[string]string{
"key-auth": "key-auth",
"basic-auth": "basic-auth",
"hmac-auth": "hmac-auth",
"jwt-auth": "jwt",
"acl": "acls",
"oauth2": "oauth2",
"mtls-auth": "mtls-auth",
}
)
// Create creates a credential in Kong of type credType.
// If an ID is specified in the credential, it will be used to
// create a credential in Kong, otherwise an ID
// is auto-generated.
func (s *credentialService) Create(ctx context.Context, credType string,
consumerUsernameOrID *string,
credential interface{}) (json.RawMessage, error) {
if isEmptyString(consumerUsernameOrID) {
return nil, errors.New("consumerUsernameOrID cannot be nil")
}
subPath, ok := credPath[credType]
if !ok {
return nil, fmt.Errorf("unknown credential type: %v", credType)
}
endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath
method := "POST"
if credential != nil {
if id, ok := credential.(id); ok {
if !reflect.ValueOf(id).IsNil() {
uuid := id.id()
if !isEmptyString(uuid) {
endpoint = endpoint + "/" + *uuid
method = "PUT"
}
}
}
}
req, err := s.client.NewRequest(method, endpoint, nil, credential)
if err != nil {
return nil, err
}
var createdCredential json.RawMessage
_, err = s.client.Do(ctx, req, &createdCredential)
if err != nil {
return nil, err
}
return createdCredential, nil
}
// Get fetches a credential of credType with credIdentifier from Kong.
func (s *credentialService) Get(ctx context.Context, credType string,
consumerUsernameOrID *string,
credIdentifier *string) (json.RawMessage, error) {
if isEmptyString(credIdentifier) {
return nil, errors.New("credIdentifier cannot be nil for Get operation")
}
if isEmptyString(consumerUsernameOrID) {
return nil, errors.New("consumerUsernameOrID cannot be nil")
}
subPath, ok := credPath[credType]
if !ok {
return nil, fmt.Errorf("unknown credential type: %v", credType)
}
endpoint := "/consumers/" + *consumerUsernameOrID + "/" +
subPath + "/" + *credIdentifier
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var cred json.RawMessage
_, err = s.client.Do(ctx, req, &cred)
if err != nil {
return nil, err
}
return cred, nil
}
// Update updates credential in Kong
func (s *credentialService) Update(ctx context.Context, credType string,
consumerUsernameOrID *string,
credential interface{}) (json.RawMessage, error) {
if isEmptyString(consumerUsernameOrID) {
return nil, errors.New("consumerUsernameOrID cannot be nil")
}
subPath, ok := credPath[credType]
if !ok {
return nil, fmt.Errorf("unknown credential type: %v", credType)
}
endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath + "/"
credID := ""
if credential != nil {
if id, ok := credential.(id); ok {
if !reflect.ValueOf(id).IsNil() {
uuid := id.id()
if !isEmptyString(uuid) {
credID = *uuid
}
}
}
}
if credID == "" {
return nil, errors.New("cannot update a credential without an ID")
}
endpoint = endpoint + credID
req, err := s.client.NewRequest("PATCH", endpoint, nil, credential)
if err != nil {
return nil, err
}
var updatedCred json.RawMessage
_, err = s.client.Do(ctx, req, &updatedCred)
if err != nil {
return nil, err
}
return updatedCred, nil
}
// Delete deletes a credential in Kong
func (s *credentialService) Delete(ctx context.Context, credType string,
consumerUsernameOrID, credIdentifier *string) error {
if isEmptyString(credIdentifier) {
return errors.New("credIdentifier cannot be nil for Delete operation")
}
subPath, ok := credPath[credType]
if !ok {
return fmt.Errorf("unknown credential type: %v", credType)
}
endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath + "/" +
*credIdentifier
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}