Skip to content

Commit 5ab45c7

Browse files
committed
Add a snapshot JSON format for uploads
Signed-off-by: Richard Wall <[email protected]>
1 parent 80a73e7 commit 5ab45c7

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ const (
2929
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
3030
)
3131

32+
// Snapshot is the JSON that the CyberArk Discovery and Context API expects to
33+
// be uploaded to the AWS presigned URL.
34+
type Snapshot struct {
35+
AgentVersion string `json:"agent_version"`
36+
ClusterID string `json:"cluster_id"`
37+
K8SVersion string `json:"k8s_version"`
38+
Secrets []interface{} `json:"secrets"`
39+
ServiceAccounts []interface{} `json:"service_accounts"`
40+
Roles []interface{} `json:"roles"`
41+
RoleBindings []interface{} `json:"role_bindings"`
42+
}
43+
3244
type CyberArkClient struct {
3345
baseURL string
3446
client *http.Client
@@ -63,9 +75,14 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
6375
return fmt.Errorf("programmer mistake: the cluster name (aka `cluster_id` in the config file) cannot be left empty")
6476
}
6577

78+
snapshot := Snapshot{
79+
ClusterID: payload.AgentMetadata.ClusterID,
80+
AgentVersion: version.PreflightVersion,
81+
}
82+
6683
encodedBody := &bytes.Buffer{}
6784
checksum := sha256.New()
68-
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(payload); err != nil {
85+
if err := json.NewEncoder(io.MultiWriter(encodedBody, checksum)).Encode(snapshot); err != nil {
6986
return err
7087
}
7188

pkg/internal/cyberark/dataupload/dataupload_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,16 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
187187
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
188188
require.NoError(t, err)
189189

190-
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, dataupload.Options{
191-
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
192-
})
190+
err = cyberArkClient.PostDataReadingsWithOptions(
191+
ctx,
192+
api.DataReadingsPost{
193+
AgentMetadata: &api.AgentMetadata{
194+
ClusterID: "bb068932-c80d-460d-88df-34bc7f3f3297",
195+
},
196+
},
197+
dataupload.Options{
198+
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
199+
},
200+
)
193201
require.NoError(t, err)
194202
}

pkg/internal/cyberark/dataupload/mock.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dataupload
22

33
import (
4+
"bytes"
45
"crypto/sha256"
56
"encoding/hex"
67
"encoding/json"
@@ -140,15 +141,26 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req
140141
return
141142
}
142143

143-
checksum := sha256.New()
144-
_, err := io.Copy(checksum, r.Body)
144+
body, err := io.ReadAll(r.Body)
145145
if err != nil {
146146
panic(err)
147147
}
148148

149+
checksum := sha256.New()
150+
_, err = checksum.Write(body)
151+
if err != nil {
152+
panic(err)
153+
}
149154
if r.URL.Query().Get("checksum") != hex.EncodeToString(checksum.Sum(nil)) {
150155
http.Error(w, "checksum is invalid", http.StatusInternalServerError)
151156
}
152157

158+
var snapshot Snapshot
159+
d := json.NewDecoder(bytes.NewBuffer(body))
160+
d.DisallowUnknownFields()
161+
if err := d.Decode(&snapshot); err != nil {
162+
panic(err)
163+
}
164+
153165
w.WriteHeader(http.StatusOK)
154166
}

0 commit comments

Comments
 (0)