Skip to content

Commit ef04246

Browse files
authored
Merge pull request #57 from doraem-on/fix-3003-kscloudapi-context
KSCloudAPI client ignores caller context: GetExceptions and GetControlsInputs drop timeouts/cancellations
2 parents ff7bd6b + ed14080 commit ef04246

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

pkg/client/v1/kscloudapi.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"context"
45
"bytes"
56
"encoding/json"
67
"errors"
@@ -223,7 +224,13 @@ func (api *KSCloudAPI) ListFrameworks() ([]string, error) {
223224

224225
// GetExceptions returns exception policies.
225226
func (api *KSCloudAPI) GetExceptions(clusterName string) ([]PostureExceptionPolicy, error) {
226-
rdr, _, err := api.get(api.getExceptionsURL(clusterName))
227+
return api.GetExceptionsWithContext(context.Background(), clusterName)
228+
}
229+
230+
// GetExceptionsWithContext returns exception policies with the provided context.
231+
func (api *KSCloudAPI) GetExceptionsWithContext(ctx context.Context, clusterName string, opts ...RequestOption) ([]PostureExceptionPolicy, error) {
232+
opts = append([]RequestOption{WithContext(ctx)}, opts...)
233+
rdr, _, err := api.get(api.getExceptionsURL(clusterName), opts...)
227234
if err != nil {
228235
return nil, err
229236
}
@@ -249,12 +256,12 @@ func (api *KSCloudAPI) getExceptionsURL(clusterName string) string {
249256
}
250257

251258
// GetAccountConfig yields the account configuration.
252-
func (api *KSCloudAPI) GetAccountConfig(clusterName string) (*CustomerConfig, error) {
259+
func (api *KSCloudAPI) GetAccountConfig(clusterName string, opts ...RequestOption) (*CustomerConfig, error) {
253260
if api.accountID == "" {
254261
return &CustomerConfig{}, nil
255262
}
256263

257-
rdr, _, err := api.get(api.getAccountConfig(clusterName))
264+
rdr, _, err := api.get(api.getAccountConfig(clusterName), opts...)
258265
if err != nil {
259266
return nil, err
260267
}
@@ -263,7 +270,7 @@ func (api *KSCloudAPI) GetAccountConfig(clusterName string) (*CustomerConfig, er
263270
accountConfig, err := utils.Decode[CustomerConfig](rdr)
264271
if err != nil {
265272
// retry with default scope
266-
rdr, _, err = api.get(api.getAccountConfigDefault(clusterName))
273+
rdr, _, err = api.get(api.getAccountConfigDefault(clusterName), opts...)
267274
if err != nil {
268275
return nil, err
269276
}
@@ -315,7 +322,13 @@ func (api *KSCloudAPI) getAccountConfigDefault(clusterName string) string {
315322

316323
// GetControlsInputs returns the controls inputs configured in the account configuration.
317324
func (api *KSCloudAPI) GetControlsInputs(clusterName string) (map[string][]string, error) {
318-
accountConfig, err := api.GetAccountConfig(clusterName)
325+
return api.GetControlsInputsWithContext(context.Background(), clusterName)
326+
}
327+
328+
// GetControlsInputsWithContext returns the controls inputs configured in the account configuration with the provided context.
329+
func (api *KSCloudAPI) GetControlsInputsWithContext(ctx context.Context, clusterName string, opts ...RequestOption) (map[string][]string, error) {
330+
opts = append([]RequestOption{WithContext(ctx)}, opts...)
331+
accountConfig, err := api.GetAccountConfig(clusterName, opts...)
319332
if err != nil {
320333
return nil, err
321334
}

pkg/client/v1/kscloudoption.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ func WithHeaders(headers map[string]string) RequestOption {
101101
}
102102
}
103103

104+
// WithContext sets the context for the request
105+
func WithContext(ctx context.Context) RequestOption {
106+
return func(o *RequestOptions) {
107+
o.reqContext = ctx
108+
}
109+
}
110+
104111
// withTrace dumps requests for debugging
105112
func withTrace(enabled bool) RequestOption {
106113
return func(o *RequestOptions) {

pkg/client/v1/vulnerabilities.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package v1
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
7+
"io"
8+
"net/http"
9+
"net/url"
10+
611
"github.com/armosec/armoapi-go/armotypes"
712
"github.com/armosec/armoapi-go/identifiers"
8-
httputils "github.com/armosec/utils-go/httputils"
913
v1 "github.com/kubescape/backend/pkg/server/v1"
1014
"github.com/kubescape/backend/pkg/utils"
11-
"io"
12-
"net/http"
13-
"net/url"
1415
)
1516

1617
func constructCVEExceptionsURL(backendURL, customerGUID string, queryParams *url.Values) (*url.URL, error) {
@@ -40,13 +41,22 @@ func getCVEExceptionsURLByRawQuery(backendURL, customerGUID string, rawQuery *ur
4041
return constructCVEExceptionsURL(backendURL, customerGUID, rawQuery)
4142
}
4243

43-
func fetchCVEExceptions(url *url.URL, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
44+
func fetchCVEExceptions(ctx context.Context, url *url.URL, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
4445
var vulnerabilityExceptionPolicy []armotypes.VulnerabilityExceptionPolicy
4546

46-
resp, err := httputils.HttpGet(http.DefaultClient, url.String(), headers)
47+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
48+
if err != nil {
49+
return nil, err
50+
}
51+
for k, v := range headers {
52+
req.Header.Set(k, v)
53+
}
54+
55+
resp, err := http.DefaultClient.Do(req)
4756
if err != nil {
4857
return nil, err
4958
}
59+
defer resp.Body.Close()
5060

5161
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
5262
return nil, fmt.Errorf("fetchCVEExceptions: resp.StatusCode %d", resp.StatusCode)
@@ -65,20 +75,20 @@ func fetchCVEExceptions(url *url.URL, headers map[string]string) ([]armotypes.Vu
6575
return vulnerabilityExceptionPolicy, nil
6676
}
6777

68-
func GetCVEExceptionByDesignator(backendURL, customerGUID string, designators *identifiers.PortalDesignator, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
78+
func GetCVEExceptionByDesignator(ctx context.Context, backendURL, customerGUID string, designators *identifiers.PortalDesignator, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
6979
url, err := getCVEExceptionsURL(backendURL, customerGUID, designators)
7080
if err != nil {
7181
return nil, err
7282
}
73-
return fetchCVEExceptions(url, headers)
83+
return fetchCVEExceptions(ctx, url, headers)
7484
}
7585

76-
func GetCVEExceptionByRawQuery(backendURL, customerGUID string, rawQuery *url.Values, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
86+
func GetCVEExceptionByRawQuery(ctx context.Context, backendURL, customerGUID string, rawQuery *url.Values, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
7787
url, err := getCVEExceptionsURLByRawQuery(backendURL, customerGUID, rawQuery)
7888
if err != nil {
7989
return nil, err
8090
}
81-
return fetchCVEExceptions(url, headers)
91+
return fetchCVEExceptions(ctx, url, headers)
8292
}
8393

8494
func GetVulnerabilitiesReportURL(eventReceiverUrl, customerGUID string) (*url.URL, error) {

0 commit comments

Comments
 (0)