Debian Codegraph @

kong/credentials_service.go

raw symbols SCIP
1 package kongscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src go1.12 context/"
5 "encoding/jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/"
6 "errorsscip-go gomod github.com/golang/go/src go1.12 errors/"
7 "fmtscip-go gomod github.com/golang/go/src go1.12 fmt/"
8 "reflectscip-go gomod github.com/golang/go/src go1.12 reflect/"
9 )
10
11 // credentialService handles key-auth credentials in Kong.
12 type credentialServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService# servicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#
13
14 var (
15 credPathscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credPath. = map[string]string{
16 "key-auth": "key-auth",
17 "basic-auth": "basic-auth",
18 "hmac-auth": "hmac-auth",
19 "jwt-auth": "jwt",
20 "acl": "acls",
21 "oauth2": "oauth2",
22 "mtls-auth": "mtls-auth",
23 }
24 )
25
26 // Create creates a credential in Kong of type credType.
27 // If an ID is specified in the credential, it will be used to
28 // create a credential in Kong, otherwise an ID
29 // is auto-generated.
30 func (s *credentialServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#) Createscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Create().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, credType string,
31 consumerUsernameOrID *string,
32 credential interface{}) (jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#, error) {
33
34 if isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(consumerUsernameOrID) {
35 return nil, errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("consumerUsernameOrID cannot be nil")
36 }
37
38 subPath, ok := credPathscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credPath.[credType]
39 if !ok {
40 return nil, fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.12 fmt/Errorf().("unknown credential type: %v", credType)
41 }
42 endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath
43 method := "POST"
44 if credential != nil {
45 if id, ok := credential.(idscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/id#); ok {
46 if !reflectscip-go gomod github.com/golang/go/src go1.12 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.12 reflect/ValueOf().(id).IsNilscip-go gomod github.com/golang/go/src go1.12 reflect/Value#IsNil().() {
47 uuid := id.idscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/id#id.()
48 if !isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(uuid) {
49 endpoint = endpoint + "/" + *uuid
50 method = "PUT"
51 }
52 }
53 }
54 }
55
56 req, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..NewRequestscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#NewRequest().(method, endpoint, nil, credential)
57 if err != nil {
58 return nil, err
59 }
60
61 var createdCredential jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#
62 _, err = s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..Doscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#Do().(ctx, req, &createdCredential)
63 if err != nil {
64 return nil, err
65 }
66 return createdCredential, nil
67 }
68
69 // Get fetches a credential of credType with credIdentifier from Kong.
70 func (s *credentialServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#) Getscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Get().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, credType string,
71 consumerUsernameOrID *string,
72 credIdentifier *string) (jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#, error) {
73
74 if isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(credIdentifier) {
75 return nil, errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("credIdentifier cannot be nil for Get operation")
76 }
77 if isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(consumerUsernameOrID) {
78 return nil, errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("consumerUsernameOrID cannot be nil")
79 }
80
81 subPath, ok := credPathscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credPath.[credType]
82 if !ok {
83 return nil, fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.12 fmt/Errorf().("unknown credential type: %v", credType)
84 }
85 endpoint := "/consumers/" + *consumerUsernameOrID + "/" +
86 subPath + "/" + *credIdentifier
87 req, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..NewRequestscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#NewRequest().("GET", endpoint, nil, nil)
88 if err != nil {
89 return nil, err
90 }
91
92 var cred jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#
93 _, err = s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..Doscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#Do().(ctx, req, &cred)
94 if err != nil {
95 return nil, err
96 }
97 return cred, nil
98 }
99
100 // Update updates credential in Kong
101 func (s *credentialServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#) Updatescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Update().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, credType string,
102 consumerUsernameOrID *string,
103 credential interface{}) (jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#, error) {
104
105 if isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(consumerUsernameOrID) {
106 return nil, errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("consumerUsernameOrID cannot be nil")
107 }
108
109 subPath, ok := credPathscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credPath.[credType]
110 if !ok {
111 return nil, fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.12 fmt/Errorf().("unknown credential type: %v", credType)
112 }
113
114 endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath + "/"
115
116 credID := ""
117 if credential != nil {
118 if id, ok := credential.(idscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/id#); ok {
119 if !reflectscip-go gomod github.com/golang/go/src go1.12 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.12 reflect/ValueOf().(id).IsNilscip-go gomod github.com/golang/go/src go1.12 reflect/Value#IsNil().() {
120 uuid := id.idscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/id#id.()
121 if !isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(uuid) {
122 credID = *uuid
123 }
124 }
125 }
126 }
127 if credID == "" {
128 return nil, errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("cannot update a credential without an ID")
129 }
130
131 endpoint = endpoint + credID
132
133 req, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..NewRequestscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#NewRequest().("PATCH", endpoint, nil, credential)
134 if err != nil {
135 return nil, err
136 }
137
138 var updatedCred jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#
139 _, err = s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..Doscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#Do().(ctx, req, &updatedCred)
140 if err != nil {
141 return nil, err
142 }
143 return updatedCred, nil
144 }
145
146 // Delete deletes a credential in Kong
147 func (s *credentialServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#) Deletescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Delete().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, credType string,
148 consumerUsernameOrID, credIdentifier *string) error {
149
150 if isEmptyStringscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/isEmptyString().(credIdentifier) {
151 return errorsscip-go gomod github.com/golang/go/src go1.12 errors/.Newscip-go gomod github.com/golang/go/src go1.12 errors/New().("credIdentifier cannot be nil for Delete operation")
152 }
153
154 subPath, ok := credPathscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credPath.[credType]
155 if !ok {
156 return fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.12 fmt/Errorf().("unknown credential type: %v", credType)
157 }
158 endpoint := "/consumers/" + *consumerUsernameOrID + "/" + subPath + "/" +
159 *credIdentifier
160
161 req, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..NewRequestscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#NewRequest().("DELETE", endpoint, nil, nil)
162 if err != nil {
163 return err
164 }
165
166 _, err = s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..Doscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#Do().(ctx, req, nil)
167 return err
168 }