forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
306 lines (265 loc) · 7.72 KB
/
rest.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
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package rest implements a REST client for communicating with remote services.
package rest
import (
"bytes"
"context"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/open-policy-agent/opa/util"
)
// Config represents configuration for a REST client.
type Config struct {
Name string `json:"name"`
URL string `json:"url"`
Headers map[string]string `json:"headers"`
AllowInsureTLS bool `json:"allow_insecure_tls,omitempty"`
Credentials struct {
Bearer *struct {
Scheme string `json:"scheme,omitempty"`
Token string `json:"token"`
} `json:"bearer,omitempty"`
ClientTLS *struct {
Cert string `json:"cert"`
PrivateKey string `json:"private_key"`
PrivateKeyPassphrase string `json:"private_key_passphrase,omitempty"`
} `json:"client_tls,omitempty"`
} `json:"credentials"`
}
func (c *Config) validateAndInjectDefaults() (*tls.Config, error) {
c.URL = strings.TrimRight(c.URL, "/")
url, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
if c.Credentials.Bearer != nil {
if c.Credentials.Bearer.Scheme == "" {
c.Credentials.Bearer.Scheme = "Bearer"
}
}
tlsConfig := &tls.Config{}
if url.Scheme == "https" {
tlsConfig.InsecureSkipVerify = c.AllowInsureTLS
}
if c.Credentials.ClientTLS != nil {
if err := c.readCertificate(tlsConfig); err != nil {
return nil, err
}
}
return tlsConfig, err
}
func (c *Config) readCertificate(t *tls.Config) error {
if c.Credentials.ClientTLS.Cert == "" {
return errors.New("client certificate is needed when client TLS is enabled")
}
if c.Credentials.ClientTLS.PrivateKey == "" {
return errors.New("private key is needed when client TLS is enabled")
}
var keyPEMBlock []byte
data, err := ioutil.ReadFile(c.Credentials.ClientTLS.PrivateKey)
if err != nil {
return err
}
block, _ := pem.Decode(data)
if block == nil {
return errors.New("PEM data could not be found")
}
if x509.IsEncryptedPEMBlock(block) {
if c.Credentials.ClientTLS.PrivateKeyPassphrase == "" {
return errors.New("client certificate passphrase is need, because the certificate is password encrypted")
}
block, err := x509.DecryptPEMBlock(block, []byte(c.Credentials.ClientTLS.PrivateKeyPassphrase))
if err != nil {
return err
}
key, err := x509.ParsePKCS8PrivateKey(block)
if err != nil {
key, err = x509.ParsePKCS1PrivateKey(block)
if err != nil {
return fmt.Errorf("private key should be a PEM or plain PKCS1 or PKCS8; parse error: %v", err)
}
}
rsa, ok := key.(*rsa.PrivateKey)
if !ok {
return errors.New("private key is invalid")
}
keyPEMBlock = pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(rsa),
},
)
} else {
keyPEMBlock = data
}
certPEMBlock, err := ioutil.ReadFile(c.Credentials.ClientTLS.Cert)
if err != nil {
return err
}
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return err
}
t.Certificates = []tls.Certificate{cert}
return nil
}
// Client implements an HTTP/REST client for communicating with remote
// services.
type Client struct {
Client http.Client
bytes *[]byte
json *interface{}
config Config
headers map[string]string
}
// Name returns an option that overrides the service name on the client.
func Name(s string) func(*Client) {
return func(c *Client) {
c.config.Name = s
}
}
// New returns a new Client for config.
func New(config []byte, opts ...func(*Client)) (Client, error) {
var parsedConfig Config
if err := util.Unmarshal(config, &parsedConfig); err != nil {
return Client{}, err
}
tlsConfig, err := parsedConfig.validateAndInjectDefaults()
if err != nil {
return Client{}, err
}
// Ensure we use a http.Transport with proper settings: the zero values are not
// a good choice, as they cause leaking connections:
// https://github.com/golang/go/issues/19620
//
// Also, there's no simple way to copy the values from http.DefaultTransport,
// see https://github.com/golang/go/issues/26013. Hence, we copy the settings
// used in the golang sources,
// https://github.com/golang/go/blob/5fae09b7386de26db59a1184f62fc7b22ec7667b/src/net/http/transport.go#L42-L53
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
var tr http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
// copy, we don't want to alter the default client's Transport
c := *http.DefaultClient
c.Transport = tr
client := Client{
config: parsedConfig,
Client: c,
}
for _, f := range opts {
f(&client)
}
return client, nil
}
// Service returns the name of the service this Client is configured for.
func (c Client) Service() string {
return c.config.Name
}
// WithHeader returns a shallow copy of the client with a header to include the
// requests.
func (c Client) WithHeader(k, v string) Client {
if v == "" {
return c
}
if c.headers == nil {
c.headers = map[string]string{}
}
c.headers[k] = v
return c
}
// WithJSON returns a shallow copy of the client with the JSON value set as the
// message body to include the requests. This function sets the Content-Type
// header.
func (c Client) WithJSON(body interface{}) Client {
c = c.WithHeader("Content-Type", "application/json")
c.json = &body
return c
}
// WithBytes returns a shallow copy of the client with the bytes set as the
// message body to include in the requests.
func (c Client) WithBytes(body []byte) Client {
c.bytes = &body
return c
}
// Do executes a request using the client.
func (c Client) Do(ctx context.Context, method, path string) (*http.Response, error) {
path = strings.Trim(path, "/")
var body io.Reader
if c.bytes != nil {
buf := bytes.NewBuffer(*c.bytes)
body = buf
} else if c.json != nil {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(*c.json); err != nil {
return nil, err
}
body = &buf
}
url := c.config.URL + "/" + path
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
headers := map[string]string{}
// Set authorization header for credentials.
if c.config.Credentials.Bearer != nil {
req.Header.Add("Authorization", fmt.Sprintf("%v %v", c.config.Credentials.Bearer.Scheme, c.config.Credentials.Bearer.Token))
}
// Copy custom headers from config.
for key, value := range c.config.Headers {
headers[key] = value
}
// Overwrite with headers set directly on client.
for key, value := range c.headers {
headers[key] = value
}
for key, value := range headers {
req.Header.Add(key, value)
}
req = req.WithContext(ctx)
logrus.WithFields(logrus.Fields{
"method": method,
"url": url,
"headers": req.Header,
}).Debug("Sending request.")
resp, err := c.Client.Do(req)
if resp != nil {
// Only log for debug purposes. If an error occurred, the caller should handle
// that. In the non-error case, the caller may not do anything.
logrus.WithFields(logrus.Fields{
"method": method,
"url": url,
"status": resp.Status,
"headers": resp.Header,
}).Debug("Received response.")
}
return resp, err
}