-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest_service.go
322 lines (274 loc) · 8.41 KB
/
rest_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
/**
* Copyright 2022 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schemaregistry
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"path"
"strings"
"time"
)
// Relative Confluent Schema Registry REST API endpoints as described in the Confluent documentation
// https://docs.confluent.io/current/schema-registry/docs/api.html
const (
base = ".."
schemas = "/schemas/ids/%d"
getSubject = "/schemas/ids/%d/subjects"
schemasBySubject = "/schemas/ids/%d?subject=%s"
subject = "/subjects"
subjects = subject + "/%s"
subjectsNormalize = subject + "/%s?normalize=%t"
subjectsDelete = subjects + "?permanent=%t"
version = subjects + "/versions"
versionNormalize = subjects + "/versions?normalize=%t"
versions = version + "/%v"
versionsDelete = versions + "?permanent=%t"
compatibility = "/compatibility" + versions
config = "/config"
subjectConfig = config + "/%s"
mode = "/mode"
modeConfig = mode + "/%s"
)
// REST API request
type api struct {
method string
endpoint string
arguments []interface{}
body interface{}
}
// newRequest returns new Confluent Schema Registry API request */
func newRequest(method string, endpoint string, body interface{}, arguments ...interface{}) *api {
// log.Println("rest_service.go - handleRequest - request.body: ", body)
return &api{
method: method,
endpoint: endpoint,
arguments: arguments,
body: body,
}
}
/*
* HTTP error codes/ SR int:error_code:
* 402: Invalid {resource}
* 404: {resource} not found
* - 40401 - Subject not found
* - 40402 - SchemaMetadata not found
* - 40403 - Schema not found
* 422: Invalid {resource}
* - 42201 - Invalid Schema
* - 42202 - Invalid SchemaMetadata
* 500: Internal Server Error (something broke between SR and Kafka)
* - 50001 - Error in backend(kafka)
* - 50002 - Operation timed out
* - 50003 - Error forwarding request to SR leader
*/
// RestError represents a Schema Registry HTTP Error response
type RestError struct {
Code int `json:"error_code"`
Message string `json:"message"`
}
// Error implements the errors.Error interface
func (err *RestError) Error() string {
return fmt.Sprintf("schema registry request failed error code: %d: %s", err.Code, err.Message)
}
type restService struct {
url *url.URL
headers http.Header
*http.Client
}
// newRestService returns a new REST client for the Confluent Schema Registry
func newRestService(conf *Config) (*restService, error) {
urlConf := conf.SchemaRegistryURL
u, err := url.Parse(urlConf)
if err != nil {
return nil, err
}
headers, err := newAuthHeader(u, conf)
if err != nil {
return nil, err
}
headers.Add("Content-Type", "application/vnd.schemaregistry.v1+json")
if err != nil {
return nil, err
}
transport, err := configureTransport(conf)
if err != nil {
return nil, err
}
timeout := conf.RequestTimeoutMs
return &restService{
url: u,
headers: headers,
Client: &http.Client{
Transport: transport,
Timeout: time.Duration(timeout) * time.Millisecond,
},
}, nil
}
// encodeBasicAuth adds a basic http authentication header to the provided header
func encodeBasicAuth(userinfo string) string {
return base64.StdEncoding.EncodeToString([]byte(userinfo))
}
// configureTLS populates tlsConf
func configureTLS(conf *Config, tlsConf *tls.Config) error {
certFile := conf.SslCertificateLocation
keyFile := conf.SslKeyLocation
caFile := conf.SslCaLocation
unsafe := conf.SslDisableEndpointVerification
var err error
if certFile != "" {
if keyFile == "" {
return errors.New(
"SslKeyLocation needs to be provided if using SslCertificateLocation")
}
var cert tls.Certificate
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
tlsConf.Certificates = []tls.Certificate{cert}
}
if caFile != "" {
if unsafe {
log.Println("WARN: endpoint verification is currently disabled. " +
"This feature should be configured for development purposes only")
}
var caCert []byte
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
return err
}
tlsConf.RootCAs = x509.NewCertPool()
if !tlsConf.RootCAs.AppendCertsFromPEM(caCert) {
return fmt.Errorf("could not parse certificate from %s", caFile)
}
}
tlsConf.BuildNameToCertificate()
return err
}
// configureTransport returns a new Transport for use by the Confluent Schema Registry REST client
func configureTransport(conf *Config) (*http.Transport, error) {
// Exposed for testing purposes only. In production properly formed certificates should be used
// https://tools.ietf.org/html/rfc2818#section-3
tlsConfig := &tls.Config{}
if err := configureTLS(conf, tlsConfig); err != nil {
return nil, err
}
timeout := conf.ConnectionTimeoutMs
return &http.Transport{
Dial: (&net.Dialer{
Timeout: time.Duration(timeout) * time.Millisecond,
}).Dial,
TLSClientConfig: tlsConfig,
}, nil
}
// configureURLAuth copies the url userinfo into a basic HTTP auth authorization header
func configureURLAuth(service *url.URL, header http.Header) error {
header.Add("Authorization", fmt.Sprintf("Basic %s", encodeBasicAuth(service.User.String())))
return nil
}
// configureSASLAuth copies the sasl username and password into a HTTP basic authorization header
func configureSASLAuth(conf *Config, header http.Header) error {
mech := conf.SaslMechanism
if strings.ToUpper(mech) == "GSSAPI" {
return fmt.Errorf("SASL_INHERIT support PLAIN and SCRAM SASL mechanisms only")
}
user := conf.SaslUsername
pass := conf.SaslPassword
if user == "" || pass == "" {
return fmt.Errorf("SASL_INHERIT requires both sasl.username and sasl.password be set")
}
header.Add("Authorization", fmt.Sprintf("Basic %s", encodeBasicAuth(fmt.Sprintf("%s:%s", user, pass))))
return nil
}
// configureUSERINFOAuth copies basic.auth.user.info
func configureUSERINFOAuth(conf *Config, header http.Header) error {
auth := conf.BasicAuthUserInfo
if auth == "" {
return fmt.Errorf("USER_INFO source configured without basic.auth.user.info ")
}
header.Add("Authorization", fmt.Sprintf("Basic %s", encodeBasicAuth(auth)))
return nil
}
// newAuthHeader returns a base64 encoded userinfo string identified on the configured credentials source
func newAuthHeader(service *url.URL, conf *Config) (http.Header, error) {
// Remove userinfo from url regardless of source to avoid confusion/conflicts
defer func() {
service.User = nil
}()
source := conf.BasicAuthCredentialsSource
header := http.Header{}
var err error
switch strings.ToUpper(source) {
case "URL":
err = configureURLAuth(service, header)
case "SASL_INHERIT":
err = configureSASLAuth(conf, header)
case "USER_INFO":
err = configureUSERINFOAuth(conf, header)
default:
err = fmt.Errorf("unrecognized value for basic.auth.credentials.source %s", source)
}
return header, err
}
// handleRequest sends a HTTP(S) request to the Schema Registry, placing results into the response object
func (rs *restService) handleRequest(request *api, response interface{}) error {
urlPath := path.Join(rs.url.Path, fmt.Sprintf(request.endpoint, request.arguments...))
endpoint, err := rs.url.Parse(urlPath)
if err != nil {
return err
}
var readCloser io.ReadCloser
if request.body != nil {
outbuf, err := json.Marshal(request.body)
if err != nil {
return err
}
readCloser = ioutil.NopCloser(bytes.NewBuffer(outbuf))
}
req := &http.Request{
Method: request.method,
URL: endpoint,
Body: readCloser,
Header: rs.headers,
}
resp, err := rs.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
if err = json.NewDecoder(resp.Body).Decode(response); err != nil {
return err
}
return nil
}
var failure RestError
if err := json.NewDecoder(resp.Body).Decode(&failure); err != nil {
return err
}
return &failure
}