forked from openshift/insights-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
293 lines (251 loc) · 9.22 KB
/
requests.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
package insightsclient
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"strconv"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/authorizer"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
// Send uploads archives to Ingress service
func (c *Client) Send(ctx context.Context, endpoint string, source Source) error {
cv, err := c.GetClusterVersion()
if apierrors.IsNotFound(err) {
return ErrWaitingForVersion
}
if err != nil {
return err
}
req, err := c.prepareRequest(ctx, http.MethodPost, endpoint, cv)
if err != nil {
return err
}
bytesRead := make(chan int64, 1)
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
req.Header.Set("Content-Type", mw.FormDataContentType())
go c.createAndWriteMIMEHeader(&source, mw, pw, bytesRead)
req.Body = pr
// dynamically set the proxy environment
c.client.Transport = clientTransport(c.authorizer)
klog.V(4).Infof("Uploading %s to %s", source.Type, req.URL.String())
resp, err := c.client.Do(req)
if err != nil {
klog.V(4).Infof("Unable to build a request, possible invalid token: %v", err)
// if the request is not build, for example because of invalid endpoint,(maybe some problem with DNS), we want to have record about it in metrics as well.
counterRequestSend.WithLabelValues(c.metricsName, "0").Inc()
return fmt.Errorf("unable to build request to connect to Insights server: %v", err)
}
requestID := resp.Header.Get(insightsReqId)
defer func() {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
klog.Warningf("error copying body: %v", err)
}
if err := resp.Body.Close(); err != nil {
klog.Warningf("Failed to close response body: %v", err)
}
}()
counterRequestSend.WithLabelValues(c.metricsName, strconv.Itoa(resp.StatusCode)).Inc()
if resp.StatusCode == http.StatusUnauthorized {
klog.V(2).Infof("gateway server %s returned 401, %s=%s", resp.Request.URL, insightsReqId, requestID)
return authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support or your token has expired: %s", responseBody(resp))}
}
if resp.StatusCode == http.StatusForbidden {
klog.V(2).Infof("gateway server %s returned 403, %s=%s", resp.Request.URL, insightsReqId, requestID)
return authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support")}
}
if resp.StatusCode == http.StatusBadRequest {
return fmt.Errorf("gateway server bad request: %s (request=%s): %s", resp.Request.URL, requestID, responseBody(resp))
}
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return fmt.Errorf("gateway server reported unexpected error code: %d (request=%s): %s", resp.StatusCode, requestID, responseBody(resp))
}
if len(requestID) > 0 {
klog.V(2).Infof("Successfully reported id=%s %s=%s, wrote=%d", source.ID, insightsReqId, requestID, <-bytesRead)
}
return nil
}
// RecvReport performs a request to Insights Results Smart Proxy endpoint
func (c *Client) RecvReport(ctx context.Context, endpoint string) (*http.Response, error) {
cv, err := c.GetClusterVersion()
if apierrors.IsNotFound(err) {
return nil, ErrWaitingForVersion
}
if err != nil {
return nil, err
}
endpoint = fmt.Sprintf(endpoint, cv.Spec.ClusterID)
klog.Infof("Retrieving report for cluster: %s", cv.Spec.ClusterID)
klog.Infof("Endpoint: %s", endpoint)
req, err := c.prepareRequest(ctx, http.MethodGet, endpoint, cv)
if err != nil {
return nil, err
}
// dynamically set the proxy environment
c.client.Transport = clientTransport(c.authorizer)
klog.V(4).Infof("Retrieving report from %s", req.URL.String())
resp, err := c.client.Do(req)
if err != nil {
klog.Errorf("Unable to retrieve latest report for %s: %v", cv.Spec.ClusterID, err)
counterRequestRecvReport.WithLabelValues(c.metricsName, "0").Inc()
return nil, err
}
requestID := resp.Header.Get("x-rh-insights-request-id")
if resp.StatusCode == http.StatusUnauthorized {
klog.V(2).Infof("gateway server %s returned 401, x-rh-insights-request-id=%s", resp.Request.URL, requestID)
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support or your token has expired")}
}
if resp.StatusCode == http.StatusForbidden {
klog.V(2).Infof("gateway server %s returned 403, x-rh-insights-request-id=%s", resp.Request.URL, requestID)
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support")}
}
if resp.StatusCode == http.StatusBadRequest {
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, fmt.Errorf("gateway server bad request: %s (request=%s): %s", resp.Request.URL, requestID, string(body))
}
if resp.StatusCode == http.StatusNotFound {
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
notFoundErr := HttpError{
StatusCode: resp.StatusCode,
Err: fmt.Errorf("not found: %s (request=%s): %s", resp.Request.URL, requestID, string(body)),
}
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, notFoundErr
}
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
body, _ := io.ReadAll(resp.Body)
if len(body) > 1024 {
body = body[:1024]
}
c.IncrementRecvReportMetric(resp.StatusCode)
return nil, fmt.Errorf("gateway server reported unexpected error code: %d (request=%s): %s", resp.StatusCode, requestID, string(body))
}
if resp.StatusCode == http.StatusOK {
return resp, nil
}
klog.Warningf("Report response status code: %d", resp.StatusCode)
return nil, fmt.Errorf("Report response status code: %d", resp.StatusCode)
}
func (c *Client) RecvSCACerts(_ context.Context, endpoint string) ([]byte, error) {
cv, err := c.GetClusterVersion()
if apierrors.IsNotFound(err) {
return nil, ErrWaitingForVersion
}
if err != nil {
return nil, err
}
token, err := c.authorizer.Token()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer([]byte(scaArchPayload)))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
c.client.Transport = clientTransport(c.authorizer)
authHeader := fmt.Sprintf("AccessToken %s:%s", cv.Spec.ClusterID, token)
req.Header.Set("Authorization", authHeader)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to retrieve SCA certs data from %s: %v", endpoint, err)
}
if resp.StatusCode > 399 || resp.StatusCode < 200 {
return nil, ocmErrorMessage(resp)
}
defer func() {
if err := resp.Body.Close(); err != nil {
klog.Warningf("Failed to close response body: %v", err)
}
}()
return io.ReadAll(resp.Body)
}
// RecvGatheringRules performs a request to Insights Operator Gathering Conditions Service
// https://github.com/RedHatInsights/insights-operator-gathering-conditions-service
// and returns the response body or an error
func (c *Client) RecvGatheringRules(ctx context.Context, endpoint string) ([]byte, error) {
klog.Infof(
`Preparing a request to Insights Operator Gathering Conditions Service at the endpoint "%v"`, endpoint,
)
cv, err := c.GetClusterVersion()
if apierrors.IsNotFound(err) {
return nil, ErrWaitingForVersion
}
if err != nil {
return nil, err
}
req, err := c.prepareRequest(ctx, http.MethodGet, endpoint, cv)
if err != nil {
return nil, err
}
// dynamically set the proxy environment and authentication
c.client.Transport = clientTransport(c.authorizer)
klog.Infof("Performing a request to Insights Operator Gathering Conditions Service")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newHTTPErrorFromResponse(resp)
}
defer func() {
if err := resp.Body.Close(); err != nil {
klog.Warningf("failed to close response body: %v", err)
}
}()
return io.ReadAll(resp.Body)
}
// RecvClusterTransfer performs a request to the OCM cluster transfer API.
// It is a HTTP GET request with the `search` query parameter limiting the result
// only for the one cluster and only for the `accepted` cluster transfers.
func (c *Client) RecvClusterTransfer(endpoint string) ([]byte, error) {
cv, err := c.GetClusterVersion()
if apierrors.IsNotFound(err) {
return nil, ErrWaitingForVersion
}
if err != nil {
return nil, err
}
token, err := c.authorizer.Token()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, endpoint, http.NoBody)
if err != nil {
return nil, err
}
q := req.URL.Query()
searchQuery := fmt.Sprintf("cluster_uuid is '%s' and status is 'accepted'", cv.Spec.ClusterID)
q.Add("search", searchQuery)
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/json")
c.client.Transport = clientTransport(c.authorizer)
authHeader := fmt.Sprintf("AccessToken %s:%s", cv.Spec.ClusterID, token)
req.Header.Set("Authorization", authHeader)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to retrieve cluster transfer data from %s: %v", endpoint, err)
}
if resp.StatusCode > 399 || resp.StatusCode < 200 {
return nil, ocmErrorMessage(resp)
}
defer func() {
if err := resp.Body.Close(); err != nil {
klog.Warningf("Failed to close response body: %v", err)
}
}()
return io.ReadAll(resp.Body)
}