-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
319 lines (268 loc) · 7.71 KB
/
client.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
// Package cfdns is a non-official GO CloudFlare DNS API client for go.
//
// All requests sent by this library use configurable exponential back-off
// for retrying failed requests and implements a soft-limiter to avoid
// exhausting CloudFlare's client request quota.
package cfdns
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/simplesurance/cfdns/log"
"github.com/simplesurance/cfdns/retry"
)
const (
retryFirstDelay = 2 * time.Second
retryMaxDelay = 30 * time.Second
retryFactor = 2
retryMaxAttempts = 6
itemsPerPage = 500
maxResponseLength = 1024 * 1024
)
var errResponseTooLarge = retry.PermanentError{
Cause: errors.New("Response from CloudFlare is too large"),
}
func NewClient(creds Credentials, options ...Option) *Client {
ret := Client{
settings: applyOptions(options...),
creds: creds,
}
return &ret
}
type Client struct {
*settings
creds Credentials
}
// sendRequestRetry tries sending the request until it succeeds, fail to
// many times of fails once with a permanent error. Wait between retries
// use exponential backoff.
//
// This is not a method of Client because go allows using a type parameter
// on a method, but not declaring them.
func sendRequestRetry[TRESP commonResponseSetter](
ctx context.Context,
client *Client,
logger *log.Logger,
req *request,
) (
*response[TRESP],
error,
) {
var resp *response[TRESP]
reterr := retry.ExpBackoff(ctx, logger, retryFirstDelay, retryMaxDelay,
retryFactor, retryMaxAttempts, func() error {
var err error
resp, err = sendRequest[TRESP](ctx, client, logger, req)
return err
})
return resp, reterr
}
// sendRequest sends an HTTP request, parses and returns the response.
// Permanent errors are wrapped with retry.PermanentError. Any error returned
// from the server is wrapped with HTTPError. If the error is a valid
// CloudFlare error, it is also wrapped with CloudFlareError.
func sendRequest[TRESP commonResponseSetter](
ctx context.Context,
client *Client,
logger *log.Logger,
treq *request,
) (
*response[TRESP],
error,
) {
err := client.ratelim.Wait(ctx)
if err != nil {
return nil, err
}
// request body
var reqBody []byte
if treq.body != nil {
reqBody, err = json.Marshal(treq.body)
if err != nil {
return nil, retry.PermanentError{Cause: err}
}
}
// request timeout
reqCtx := ctx
if client.requestTimeout > 0 {
var reqCtxCancel func()
reqCtx, reqCtxCancel = context.WithTimeout(reqCtx, client.requestTimeout)
defer reqCtxCancel()
}
// create an http request
req, err := http.NewRequestWithContext(reqCtx, treq.method, requestURL(treq),
bytes.NewReader(reqBody))
if err != nil {
return nil, retry.PermanentError{Cause: err}
}
// headers
req.Header.Set("Content-Type", "application/json")
// credentials
reqNoAuth := req.Clone(ctx)
client.creds.configure(req)
// send the request
resp, err := client.httpClient.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return nil, retry.PermanentError{Cause: err}
}
// errors from Do() may be permanent or not, it is not possible to
// determine precisely
return nil, err // allow retry
}
defer func() {
_ = resp.Body.Close()
}()
// handle response
if resp.StatusCode >= 400 {
err = handleErrorResponse(resp, logger)
logFullRequestResponse(logger, reqNoAuth, reqBody, resp, rawResponseFromErr(err))
return nil, err
}
tresp, err := handleSuccessResponse[TRESP](resp, logger)
if err != nil {
logFullRequestResponse(logger, reqNoAuth, reqBody, resp, rawResponseFromErr(err))
return nil, err
}
if client.logSuccess {
logFullRequestResponse(logger, reqNoAuth, reqBody, resp, tresp.rawBody)
}
return tresp, err
}
func handleSuccessResponse[TRESP commonResponseSetter](httpResp *http.Response, logger *log.Logger) (
*response[TRESP],
error,
) {
var ret response[TRESP]
ret.code = httpResp.StatusCode
ret.headers = httpResp.Header
var err error
ret.rawBody, err = readResponseBody(httpResp.Body)
if err != nil {
// error response already specifies is can retry or not
return nil, errors.Join(err, HTTPError{
Code: httpResp.StatusCode,
RawBody: ret.rawBody,
Headers: ret.headers,
})
}
if len(ret.rawBody) == maxResponseLength {
logger.W(fmt.Sprintf("Response from CloudFlare rejected because is bigger than %d", maxResponseLength))
return nil, retry.PermanentError{
Cause: errors.Join(err, HTTPError{
Code: httpResp.StatusCode,
RawBody: ret.rawBody,
Headers: ret.headers,
}),
}
}
err = json.Unmarshal(ret.rawBody, &ret.body)
if err != nil {
// error response already specifies is can retry or not
return nil, errors.Join(err, HTTPError{
Code: httpResp.StatusCode,
RawBody: ret.rawBody,
Headers: ret.headers,
})
}
return &ret, nil
}
func handleErrorResponse(resp *http.Response, _ *log.Logger) error {
// the error response must always support errors.As(err, HTTPError)
httpErr := HTTPError{
Code: resp.StatusCode,
Headers: resp.Header,
}
respBody, err := readResponseBody(resp.Body)
if err != nil {
err := fmt.Errorf("CloudFlare returned an error, but failed to read the error body: %w; %w", err, httpErr)
if errors.Is(err, errResponseTooLarge) {
return retry.PermanentError{Cause: err}
}
return err
}
httpErr.RawBody = respBody
// try to parse the CloudFlare error objects
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("content-type"))
if err != nil {
return retry.PermanentError{Cause: fmt.Errorf("CloudFlare returned an error, and the content-type of the response is invalid %q (%w): %w",
resp.Header.Get("content-type"), err, httpErr)}
}
if mediaType != "application/json" {
return retry.PermanentError{Cause: fmt.Errorf("CloudFlare returned an error, and the content-type of the response is invalid %q: %w",
resp.Header.Get("content-type"), httpErr)}
}
var cfcommon cfResponseCommon
err = json.Unmarshal(respBody, &cfcommon)
if err != nil {
return retry.PermanentError{Cause: fmt.Errorf("CloudFlare returned an error, unmarshaling the error body as json failed: %w; %w", err, httpErr)}
}
ret := CloudFlareError{
cfResponseCommon: cfcommon,
HTTPError: httpErr,
}
if httpErr.IsPermanent() {
return retry.PermanentError{Cause: ret}
}
return ret
}
func logFullRequestResponse(
logger *log.Logger,
req *http.Request,
reqBody []byte,
resp *http.Response,
respBody []byte,
) {
logger.D(func(log log.DebugFn) {
msg := &strings.Builder{}
// request
reqDump, _ := httputil.DumpRequestOut(req, false)
_, _ = msg.Write(reqDump)
_, _ = msg.Write(reqBody)
fmt.Fprintln(msg)
fmt.Fprintln(msg)
// response
respDump, _ := httputil.DumpResponse(resp, false)
_, _ = msg.Write(respDump)
_, _ = msg.Write(respBody)
log("Successful request to CloudFlare:\n" + msg.String())
})
}
func requestURL(treq *request) string {
urlstring := baseURL + "/" + treq.path
theurl, err := url.Parse(urlstring)
if err != nil {
// this only happens in case of coding error on cfapi
panic(fmt.Sprintf("URL %q is invalid; created from request %v",
urlstring, treq))
}
theurl.RawQuery = treq.queryParams.Encode()
return theurl.String()
}
func readResponseBody(body io.Reader) ([]byte, error) {
ret, err := io.ReadAll(io.LimitReader(body, maxResponseLength+1))
if err != nil {
return nil, err // allow retry
}
if len(ret) > maxResponseLength {
return nil, errResponseTooLarge // permanent error
}
return ret, nil
}
func rawResponseFromErr(err error) []byte {
var httpErr HTTPError
if errors.As(err, &httpErr) {
return httpErr.RawBody
}
// if the error is not an HTTPError return instead the error's details
return []byte(fmt.Sprintf("%v", err))
}