-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsat.go
324 lines (271 loc) · 9.29 KB
/
rsat.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
323
324
// Copyright 2023 Adam Chalkley
//
// https://github.com/atc0005/check-rsat
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package rsat
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/rs/zerolog"
)
// JSONNullKeyword is the keyword used in JSON to represent null.
const JSONNullKeyword string = "null"
// Red Hat Satellite API endpoints URL templates.
const (
// OrganizationsAPIEndPointURLTemplate provides a template for a fully
// qualified API endpoint URL for retrieving Organizations from a Red Hat
// Satellite instance.
OrganizationsAPIEndPointURLTemplate string = "https://%s:%d/api/v2/organizations?full_result=1"
// SubscriptionsAPIEndPointURLTemplate provides a template for a fully
// qualified API endpoint URL for retrieving Subscriptions associated with
// a Red Hat Satellite Organization.
SubscriptionsAPIEndPointURLTemplate string = "https://%s:%d/katello/api/v2/organizations/%d/subscriptions?full_result=1"
// SyncPlansAPIEndPointURLTemplate provides a template for a fully
// qualified API endpoint URL for retrieving Sync Plans associated with a
// Red Hat Satellite Organization.
SyncPlansAPIEndPointURLTemplate string = "https://%s:%d/katello/api/v2/organizations/%d/sync_plans?full_result=1"
// ProductsAPIEndPointURLTemplate provides a template for a fully
// qualified API endpoint URL for retrieving Products associated with a
// Red Hat Satellite Organization.
ProductsAPIEndPointURLTemplate string = "https://%s:%d/katello/api/v2/products?organization_id=%d&full_result=1"
)
// Prep tasks for processing of Red Hat Satellite API endpoints.
const (
PrepTaskParseURL string = "parse URL"
PrepTaskPrepareRequest string = "prepare request"
PrepTaskDecode string = "decode JSON data"
PrepTaskSubmitRequest string = "submit request"
PrepTaskValidateResponse string = "validate response"
)
// APIAuthInfo represents the settings needed to access Red Hat Satellite
// server API endpoints.
type APIAuthInfo struct {
// ReadLimit is the size in bytes used to help prevent abuse when reading
// input that could be larger than expected.
ReadLimit int64
// Port is the TCP/IP port associated with the Red Hat Satellite server's
// API endpoints.
Port int
// Server is the FQDN or IP Address of the Red Hat Satellite server.
Server string
// Username is the valid user for the specified Red Hat Satellite server.
Username string
// Password is the valid password for the specified Red Hat Satellite
// Server user account.
Password string
// UserAgent is an optional custom user agent string used to override the
// default Go user agent ("Go-http-client/1.1").
UserAgent string
// NetworkType indicates whether an attempt should be made to connect to
// only IPv4, only IPv6 or Red Hat Satellite API endpoints listening on
// either of IPv4 or IPv6 addresses ("auto").
NetworkType string
// CACert is the optional certificate authority certificate used to
// validate the certificate chain used by the Red Hat Satellite server.
CACert []byte
// PermitTLSRenegotiation controls whether the server is allowed to
// request TLS renegotiation.
PermitTLSRenegotiation bool
// TrustCert indicates whether the certificate should be trusted as-is
// without validation.
TrustCert bool
}
// SortOptions is the optional sorting criteria for API query responses.
type SortOptions struct {
By NullString `json:"by"`
Order NullString `json:"order"`
}
// decode is a helper function intended to handle the core JSON decoding tasks
// for various JSON sources (file, http body, etc.).
func decode(dst interface{}, reader io.Reader, logger zerolog.Logger, sourceName string, limit int64) error {
if reader == nil {
return &PrepError{
Task: PrepTaskDecode,
Message: "failed to decode JSON data",
Source: sourceName,
Cause: fmt.Errorf(
"required JSON source was not provided: %w",
ErrMissingValue,
),
}
}
logger.Debug().Msgf(
"Setting up JSON decoder for source %s with a limit of %d bytes",
sourceName,
limit,
)
dec := json.NewDecoder(io.LimitReader(reader, limit))
// This project does not use all fields from Red Hat Satellite API
// responses so we do not attempt to assert that we've accounted for all
// of them.
logger.Debug().Msg("Allowing unknown JSON feed fields")
logger.Debug().Msg("Decoding JSON input")
// Decode the first JSON object.
if err := dec.Decode(dst); err != nil {
return &PrepError{
Task: PrepTaskDecode,
Message: "failed to decode JSON data",
Source: sourceName,
Cause: err,
}
}
logger.Debug().Msg("Successfully decoded JSON input")
// If there is more than one object, something is off.
if dec.More() {
return &PrepError{
Task: PrepTaskDecode,
Message: "failed to decode JSON data",
Source: sourceName,
Cause: fmt.Errorf(
"source %s contains multiple JSON objects; only one JSON object is supported: %w",
sourceName,
ErrJSONUnexpectedObjectCount,
),
}
}
return nil
}
// validateResponse is a helper function responsible for validating a response
// from an endpoint after submitting a message.
func validateResponse(ctx context.Context, response *http.Response, logger zerolog.Logger, limit int64) error {
if response == nil {
return &PrepError{
Task: PrepTaskValidateResponse,
Message: "error validating HTTP request",
Source: "missing",
Cause: fmt.Errorf(
"required HTTP response was not provided: %w",
ErrMissingValue,
),
}
}
feedSource := response.Request.URL.RequestURI()
if err := ctx.Err(); err != nil {
logger.Debug().Msg("context has expired")
return &PrepError{
Task: PrepTaskValidateResponse,
Message: "timeout reached",
Source: feedSource,
Cause: err,
}
}
switch {
case response.ContentLength == -1:
logger.Debug().Msgf("Response indicates unknown length of content from %q", feedSource)
default:
logger.Debug().Msgf(
"Response indicates %d bytes available to be read from %q",
response.ContentLength,
feedSource,
)
}
// TODO: Refactor this block
switch {
// Successful / expected response.
case response.StatusCode == http.StatusOK:
logger.Debug().Msgf("Status code %d received as expected", response.StatusCode)
return nil
// Success status range, but not expected value.
case response.StatusCode > 200 && response.StatusCode <= 299:
logger.Debug().Msgf(
"Status code %d (%s) received; expected %d (%s), but received value within success range",
response.StatusCode,
http.StatusText(response.StatusCode),
http.StatusOK,
http.StatusText(http.StatusOK),
)
return nil
// Everything else is assumed to be an error (outside of success range).
default:
// Get the response body, then convert to string for use with extended
// error messages
responseData, readErr := io.ReadAll(io.LimitReader(response.Body, limit))
if readErr != nil {
return &PrepError{
Task: PrepTaskValidateResponse,
Message: "error reading response data",
Source: feedSource,
Cause: readErr,
}
}
responseString := string(responseData)
statusCodeErr := fmt.Errorf(
"response %v (%s) from API: %w",
response.Status,
responseString,
ErrHTTPResponseOutsideRange,
)
return &PrepError{
Task: PrepTaskValidateResponse,
Message: "unexpected response",
Source: feedSource,
Cause: statusCodeErr,
}
}
}
// prepareRequest is a helper function that prepares a http.Request (including
// all desired headers) for submission to an endpoint.
func prepareRequest(ctx context.Context, client *APIClient, apiURL string) (*http.Request, error) {
if client == nil {
return nil, &PrepError{
Task: PrepTaskPrepareRequest,
Message: "error preparing HTTP request",
Source: apiURL,
Cause: fmt.Errorf(
"required API client was not provided: %w",
ErrMissingValue,
),
}
}
if apiURL == "" {
return nil, &PrepError{
Task: PrepTaskPrepareRequest,
Message: "error preparing HTTP request",
Source: apiURL,
Cause: fmt.Errorf(
"required API URL was not provided: %w",
ErrMissingValue,
),
}
}
logger := client.Logger
logger.Debug().Msgf("Parsing %q as URL", apiURL)
parsedURL, parseErr := url.Parse(apiURL)
if parseErr != nil {
return nil, &PrepError{
Task: PrepTaskParseURL,
Message: "error parsing URL",
Source: apiURL,
Cause: parseErr,
}
}
logger.Debug().Msgf("Successfully parsed %q as URL", apiURL)
logger.Debug().Msg("Preparing HTTP request")
request, reqErr := http.NewRequestWithContext(ctx, http.MethodGet, parsedURL.String(), nil)
if reqErr != nil {
return nil, &PrepError{
Task: PrepTaskPrepareRequest,
Source: parsedURL.String(),
Message: "error preparing request for URL",
Cause: reqErr,
}
}
// Explicitly note that we want JSON content.
request.Header.Add("Content-Type", "application/json;charset=utf-8")
// Provide API authentication credentials.
// https://stackoverflow.com/questions/16673766/basic-http-auth-in-go
request.SetBasicAuth(client.AuthInfo.Username, client.AuthInfo.Password)
// If provided, override the default Go user agent ("Go-http-client/1.1")
// with custom value.
if client.AuthInfo.UserAgent != "" {
logger.Debug().Msg("Setting custom user agent")
request.Header.Set("User-Agent", client.AuthInfo.UserAgent)
}
return request, nil
}