Skip to content

Commit ed75fe8

Browse files
committed
Update the dataupload package to be compatible with the implementated inventory API
Signed-off-by: Richard Wall <[email protected]>
1 parent 9d25e21 commit ed75fe8

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const (
2222
// maxRetrievePresignedUploadURLBodySize is the maximum allowed size for a response body from the
2323
// Retrieve Presigned Upload URL service.
2424
maxRetrievePresignedUploadURLBodySize = 10 * 1024
25+
26+
// apiPathSnapshotLinks is the URL path of the snapshot-links endpoint of the inventory API.
27+
// This endpoint returns an AWS presigned URL.
28+
// TODO(wallrj): Link to CyberArk API documentation when it is published.
29+
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
2530
)
2631

2732
type CyberArkClient struct {
@@ -51,6 +56,9 @@ func NewCyberArkClient(trustedCAs *x509.CertPool, baseURL string, authenticateRe
5156
}, nil
5257
}
5358

59+
// PostDataReadingsWithOptions PUTs the supplied payload to an [AWS presigned URL] which it obtains via the CyberArk inventory API.
60+
//
61+
// [AWS presigned URL]: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
5462
func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payload api.DataReadingsPost, opts Options) error {
5563
if opts.ClusterName == "" {
5664
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty")
@@ -64,15 +72,15 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
6472

6573
presignedUploadURL, err := c.retrievePresignedUploadURL(ctx, hex.EncodeToString(checksum.Sum(nil)), opts)
6674
if err != nil {
67-
return err
75+
return fmt.Errorf("while retrieving snapshot upload URL: %s", err)
6876
}
6977

70-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, presignedUploadURL, encodedBody)
78+
// The snapshot-links endpoint returns an AWS presigned URL which only supports the PUT verb.
79+
req, err := http.NewRequestWithContext(ctx, http.MethodPut, presignedUploadURL, encodedBody)
7180
if err != nil {
7281
return err
7382
}
7483

75-
req.Header.Set("Content-Type", "application/json")
7684
version.SetUserAgent(req)
7785

7886
res, err := c.client.Do(req)
@@ -93,7 +101,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
93101
}
94102

95103
func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksum string, opts Options) (string, error) {
96-
uploadURL, err := url.JoinPath(c.baseURL, "/api/data/kubernetes/upload")
104+
uploadURL, err := url.JoinPath(c.baseURL, apiPathSnapshotLinks)
97105
if err != nil {
98106
return "", err
99107
}
@@ -102,10 +110,12 @@ func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksu
102110
ClusterID string `json:"cluster_id"`
103111
ClusterDescription string `json:"cluster_description"`
104112
Checksum string `json:"checksum_sha256"`
113+
AgentVersion string `json:"agent_version"`
105114
}{
106115
ClusterID: opts.ClusterName,
107116
ClusterDescription: opts.ClusterDescription,
108117
Checksum: checksum,
118+
AgentVersion: version.PreflightVersion,
109119
}
110120

111121
encodedBody := &bytes.Buffer{}
@@ -120,7 +130,7 @@ func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksu
120130

121131
req.Header.Set("Content-Type", "application/json")
122132
if err := c.authenticateRequest(req); err != nil {
123-
return "", fmt.Errorf("failed to authenticate request")
133+
return "", fmt.Errorf("failed to authenticate request: %s", err)
124134
}
125135
version.SetUserAgent(req)
126136

pkg/internal/cyberark/dataupload/dataupload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
7575
opts: defaultOpts,
7676
authenticate: setToken("fail-token"),
7777
requireFn: func(t *testing.T, err error) {
78-
require.ErrorContains(t, err, "received response with status code 500: should authenticate using the correct bearer token")
78+
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token")
7979
},
8080
},
8181
{
@@ -84,16 +84,16 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
8484
opts: dataupload.Options{ClusterName: "invalid-json-retrieve-presigned", ClusterDescription: defaultOpts.ClusterDescription},
8585
authenticate: setToken("success-token"),
8686
requireFn: func(t *testing.T, err error) {
87-
require.ErrorContains(t, err, "rejecting JSON response from server as it was too large or was truncated")
87+
require.ErrorContains(t, err, "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated")
8888
},
8989
},
9090
{
91-
name: "500 from server (PostData step)",
91+
name: "500 from server (RetrievePresignedUploadURL step)",
9292
payload: defaultPayload,
9393
opts: dataupload.Options{ClusterName: "invalid-response-post-data", ClusterDescription: defaultOpts.ClusterDescription},
9494
authenticate: setToken("success-token"),
9595
requireFn: func(t *testing.T, err error) {
96-
require.ErrorContains(t, err, "received response with status code 500: mock error")
96+
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: mock error")
9797
},
9898
},
9999
}

pkg/internal/cyberark/dataupload/mock.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (mds *mockDataUploadServer) Close() {
3737

3838
func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3939
switch r.URL.Path {
40-
case "/api/data/kubernetes/upload":
40+
case apiPathSnapshotLinks:
4141
mds.handlePresignedUpload(w, r)
4242
return
4343
case "/presigned-upload":
@@ -123,7 +123,7 @@ func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r
123123
}
124124

125125
func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Request, invalidJSON bool) {
126-
if r.Method != http.MethodPost {
126+
if r.Method != http.MethodPut {
127127
w.WriteHeader(http.StatusMethodNotAllowed)
128128
_, _ = w.Write([]byte(`{"message":"method not allowed"}`))
129129
return
@@ -134,11 +134,6 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req
134134
return
135135
}
136136

137-
if r.Header.Get("Content-Type") != "application/json" {
138-
http.Error(w, "should send JSON on all requests", http.StatusInternalServerError)
139-
return
140-
}
141-
142137
if invalidJSON {
143138
w.WriteHeader(http.StatusOK)
144139
w.Header().Set("Content-Type", "application/json")

0 commit comments

Comments
 (0)