-
Notifications
You must be signed in to change notification settings - Fork 25
[VC-43753] CyberArk Discovery and Context: Upload data in the JSON format required by the API #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ var SecretSelectedFields = []FieldPath{ | |
{"metadata", "ownerReferences"}, | ||
{"metadata", "selfLink"}, | ||
{"metadata", "uid"}, | ||
{"metadata", "creationTimestamp"}, | ||
{"metadata", "deletionTimestamp"}, | ||
{"metadata", "resourceVersion"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Cyberark backend needs this extra metadata to produce its reports. |
||
|
||
{"type"}, | ||
{"data", "tls.crt"}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package dataupload_test | |
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
@@ -17,28 +18,23 @@ import ( | |
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload" | ||
"github.com/jetstack/preflight/pkg/internal/cyberark/identity" | ||
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery" | ||
"github.com/jetstack/preflight/pkg/testutil" | ||
|
||
_ "k8s.io/klog/v2/ktesting/init" | ||
) | ||
|
||
func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | ||
func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) { | ||
fakeTime := time.Unix(123, 0) | ||
defaultPayload := api.DataReadingsPost{ | ||
AgentMetadata: &api.AgentMetadata{ | ||
Version: "test-version", | ||
ClusterID: "test", | ||
}, | ||
DataGatherTime: fakeTime, | ||
DataReadings: []*api.DataReading{ | ||
{ | ||
ClusterID: "success-cluster-id", | ||
DataGatherer: "test-gatherer", | ||
Timestamp: api.Time{Time: fakeTime}, | ||
Data: map[string]interface{}{"test": "data"}, | ||
SchemaVersion: "v1", | ||
}, | ||
defaultDataReadings := []*api.DataReading{ | ||
{ | ||
ClusterID: "success-cluster-id", | ||
DataGatherer: "test-gatherer", | ||
Timestamp: api.Time{Time: fakeTime}, | ||
Data: map[string]interface{}{"test": "data"}, | ||
SchemaVersion: "v1", | ||
}, | ||
} | ||
|
||
defaultOpts := dataupload.Options{ | ||
ClusterName: "success-cluster-id", | ||
} | ||
|
@@ -52,14 +48,14 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
|
||
tests := []struct { | ||
name string | ||
payload api.DataReadingsPost | ||
readings []*api.DataReading | ||
authenticate func(req *http.Request) error | ||
opts dataupload.Options | ||
requireFn func(t *testing.T, err error) | ||
}{ | ||
{ | ||
name: "successful upload", | ||
payload: defaultPayload, | ||
readings: defaultDataReadings, | ||
opts: defaultOpts, | ||
authenticate: setToken("success-token"), | ||
requireFn: func(t *testing.T, err error) { | ||
|
@@ -68,7 +64,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
}, | ||
{ | ||
name: "error when cluster name is empty", | ||
payload: defaultPayload, | ||
readings: defaultDataReadings, | ||
opts: dataupload.Options{ClusterName: ""}, | ||
authenticate: setToken("success-token"), | ||
requireFn: func(t *testing.T, err error) { | ||
|
@@ -77,16 +73,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
}, | ||
{ | ||
name: "error when bearer token is incorrect", | ||
payload: defaultPayload, | ||
readings: defaultDataReadings, | ||
opts: defaultOpts, | ||
authenticate: setToken("fail-token"), | ||
requireFn: func(t *testing.T, err error) { | ||
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token") | ||
}, | ||
}, | ||
{ | ||
name: "error contains authenticate error", | ||
readings: defaultDataReadings, | ||
opts: defaultOpts, | ||
authenticate: func(_ *http.Request) error { | ||
return errors.New("simulated-authenticate-error") | ||
}, | ||
requireFn: func(t *testing.T, err error) { | ||
require.ErrorContains(t, err, "while retrieving snapshot upload URL: failed to authenticate request: simulated-authenticate-error") | ||
}, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a drive-by additional test for when |
||
{ | ||
name: "invalid JSON from server (RetrievePresignedUploadURL step)", | ||
payload: defaultPayload, | ||
readings: defaultDataReadings, | ||
opts: dataupload.Options{ClusterName: "invalid-json-retrieve-presigned"}, | ||
authenticate: setToken("success-token"), | ||
requireFn: func(t *testing.T, err error) { | ||
|
@@ -95,7 +102,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
}, | ||
{ | ||
name: "500 from server (RetrievePresignedUploadURL step)", | ||
payload: defaultPayload, | ||
readings: defaultDataReadings, | ||
opts: dataupload.Options{ClusterName: "invalid-response-post-data"}, | ||
authenticate: setToken("success-token"), | ||
requireFn: func(t *testing.T, err error) { | ||
|
@@ -106,6 +113,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
logger := ktesting.NewLogger(t, ktesting.DefaultConfig) | ||
ctx := klog.NewContext(t.Context(), logger) | ||
|
||
server := dataupload.MockDataUploadServer() | ||
defer server.Close() | ||
|
||
|
@@ -118,22 +128,22 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) { | |
cyberArkClient, err := dataupload.NewCyberArkClient(certPool, server.Server.URL, tc.authenticate) | ||
require.NoError(t, err) | ||
|
||
err = cyberArkClient.PostDataReadingsWithOptions(t.Context(), tc.payload, tc.opts) | ||
err = cyberArkClient.PostDataReadingsWithOptions(ctx, tc.readings, tc.opts) | ||
tc.requireFn(t, err) | ||
}) | ||
} | ||
} | ||
|
||
// TestPostDataReadingsWithOptionsWithRealAPI demonstrates that the dataupload code works with the real inventory API. | ||
// TestCyberArkClient_PostDataReadingsWithOptions_RealAPI demonstrates that the dataupload code works with the real inventory API. | ||
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment. | ||
// ARK_SUBDOMAIN should be your tenant subdomain. | ||
// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud | ||
// | ||
// To enable verbose request logging: | ||
// | ||
// go test ./pkg/internal/cyberark/dataupload/... \ | ||
// -v -count 1 -run TestPostDataReadingsWithOptionsWithRealAPI -args -testing.v 6 | ||
func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) { | ||
// -v -count 1 -run TestCyberArkClient_PostDataReadingsWithOptions_RealAPI -args -testing.v 6 | ||
func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) { | ||
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN") | ||
subdomain := os.Getenv("ARK_SUBDOMAIN") | ||
username := os.Getenv("ARK_USERNAME") | ||
|
@@ -172,8 +182,13 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) { | |
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest) | ||
require.NoError(t, err) | ||
|
||
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, dataupload.Options{ | ||
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297", | ||
}) | ||
dataReadings := testutil.ParseDataReadings(t, testutil.ReadGZIP(t, "testdata/example-1/datareadings.json.gz")) | ||
err = cyberArkClient.PostDataReadingsWithOptions( | ||
ctx, | ||
dataReadings, | ||
dataupload.Options{ | ||
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297", | ||
}, | ||
) | ||
require.NoError(t, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added json annotations here so that I can unmarshal date readings from a file, for testing.
The agent already has an
--input-file
option, but stops decoding the input atapi.DataReading.Data
, leaving the actual data asinterface{}
.In the test in this PR I need to decode the Data, so that it has the same types as the DataGatherer.Fetch return values.