@@ -5,13 +5,18 @@ import (
55 "encoding/pem"
66 "fmt"
77 "net/http"
8+ "os"
89 "testing"
910 "time"
1011
1112 "github.com/stretchr/testify/require"
13+ "k8s.io/klog/v2"
14+ "k8s.io/klog/v2/ktesting"
1215
1316 "github.com/jetstack/preflight/api"
1417 "github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
18+ "github.com/jetstack/preflight/pkg/internal/cyberark/identity"
19+ "github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
1520)
1621
1722func TestCyberArkClient_PostDataReadingsWithOptions (t * testing.T ) {
@@ -75,7 +80,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
7580 opts : defaultOpts ,
7681 authenticate : setToken ("fail-token" ),
7782 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" )
83+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
7984 },
8085 },
8186 {
@@ -84,16 +89,16 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
8489 opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" , ClusterDescription : defaultOpts .ClusterDescription },
8590 authenticate : setToken ("success-token" ),
8691 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" )
92+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated" )
8893 },
8994 },
9095 {
91- name : "500 from server (PostData step)" ,
96+ name : "500 from server (RetrievePresignedUploadURL step)" ,
9297 payload : defaultPayload ,
9398 opts : dataupload.Options {ClusterName : "invalid-response-post-data" , ClusterDescription : defaultOpts .ClusterDescription },
9499 authenticate : setToken ("success-token" ),
95100 requireFn : func (t * testing.T , err error ) {
96- require .ErrorContains (t , err , "received response with status code 500: mock error" )
101+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: mock error" )
97102 },
98103 },
99104 }
@@ -117,3 +122,52 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
117122 })
118123 }
119124}
125+
126+ // TestPostDataReadingsWithOptionsWithRealAPI demonstrates that the dataupload code works with the real inventory API.
127+ // An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
128+ // ARK_SUBDOMAIN should be your tenant subdomain.
129+ // ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
130+ func TestPostDataReadingsWithOptionsWithRealAPI (t * testing.T ) {
131+ platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
132+ subdomain := os .Getenv ("ARK_SUBDOMAIN" )
133+ username := os .Getenv ("ARK_USERNAME" )
134+ secret := os .Getenv ("ARK_SECRET" )
135+
136+ if platformDomain == "" || subdomain == "" || username == "" || secret == "" {
137+ t .Skip ("Skipping because one of the following environment variables is unset or empty: ARK_PLATFORM_DOMAIN, ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET" )
138+ return
139+ }
140+
141+ logger := ktesting .NewLogger (t , ktesting .NewConfig ())
142+ ctx := klog .NewContext (t .Context (), logger )
143+
144+ const (
145+ discoveryContextServiceName = "inventory"
146+ separator = "."
147+ )
148+
149+ serviceURL := fmt .Sprintf ("https://%s%s%s.%s" , subdomain , separator , discoveryContextServiceName , platformDomain )
150+
151+ var (
152+ identityClient * identity.Client
153+ err error
154+ )
155+ if platformDomain == "cyberark.cloud" {
156+ identityClient , err = identity .New (ctx , subdomain )
157+ } else {
158+ discoveryClient := servicediscovery .New (servicediscovery .WithIntegrationEndpoint ())
159+ identityClient , err = identity .NewWithDiscoveryClient (ctx , discoveryClient , subdomain )
160+ }
161+ require .NoError (t , err )
162+
163+ err = identityClient .LoginUsernamePassword (ctx , username , []byte (secret ))
164+ require .NoError (t , err )
165+
166+ cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
167+ require .NoError (t , err )
168+
169+ err = cyberArkClient .PostDataReadingsWithOptions (t .Context (), api.DataReadingsPost {}, dataupload.Options {
170+ ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
171+ })
172+ require .NoError (t , err )
173+ }
0 commit comments