@@ -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 ) {
@@ -33,8 +38,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
3338 },
3439 }
3540 defaultOpts := dataupload.Options {
36- ClusterName : "success-cluster-id" ,
37- ClusterDescription : "success-cluster-description" ,
41+ ClusterName : "success-cluster-id" ,
3842 }
3943
4044 setToken := func (token string ) func (* http.Request ) error {
@@ -75,25 +79,25 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
7579 opts : defaultOpts ,
7680 authenticate : setToken ("fail-token" ),
7781 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" )
82+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
7983 },
8084 },
8185 {
8286 name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
8387 payload : defaultPayload ,
84- opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" , ClusterDescription : defaultOpts . ClusterDescription },
88+ opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
8589 authenticate : setToken ("success-token" ),
8690 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" )
91+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated" )
8892 },
8993 },
9094 {
91- name : "500 from server (PostData step)" ,
95+ name : "500 from server (RetrievePresignedUploadURL step)" ,
9296 payload : defaultPayload ,
93- opts : dataupload.Options {ClusterName : "invalid-response-post-data" , ClusterDescription : defaultOpts . ClusterDescription },
97+ opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
9498 authenticate : setToken ("success-token" ),
9599 requireFn : func (t * testing.T , err error ) {
96- require .ErrorContains (t , err , "received response with status code 500: mock error" )
100+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: mock error" )
97101 },
98102 },
99103 }
@@ -117,3 +121,52 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
117121 })
118122 }
119123}
124+
125+ // TestPostDataReadingsWithOptionsWithRealAPI demonstrates that the dataupload code works with the real inventory API.
126+ // An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
127+ // ARK_SUBDOMAIN should be your tenant subdomain.
128+ // ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
129+ func TestPostDataReadingsWithOptionsWithRealAPI (t * testing.T ) {
130+ platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
131+ subdomain := os .Getenv ("ARK_SUBDOMAIN" )
132+ username := os .Getenv ("ARK_USERNAME" )
133+ secret := os .Getenv ("ARK_SECRET" )
134+
135+ if platformDomain == "" || subdomain == "" || username == "" || secret == "" {
136+ t .Skip ("Skipping because one of the following environment variables is unset or empty: ARK_PLATFORM_DOMAIN, ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET" )
137+ return
138+ }
139+
140+ logger := ktesting .NewLogger (t , ktesting .NewConfig ())
141+ ctx := klog .NewContext (t .Context (), logger )
142+
143+ const (
144+ discoveryContextServiceName = "inventory"
145+ separator = "."
146+ )
147+
148+ serviceURL := fmt .Sprintf ("https://%s%s%s.%s" , subdomain , separator , discoveryContextServiceName , platformDomain )
149+
150+ var (
151+ identityClient * identity.Client
152+ err error
153+ )
154+ if platformDomain == "cyberark.cloud" {
155+ identityClient , err = identity .New (ctx , subdomain )
156+ } else {
157+ discoveryClient := servicediscovery .New (servicediscovery .WithIntegrationEndpoint ())
158+ identityClient , err = identity .NewWithDiscoveryClient (ctx , discoveryClient , subdomain )
159+ }
160+ require .NoError (t , err )
161+
162+ err = identityClient .LoginUsernamePassword (ctx , username , []byte (secret ))
163+ require .NoError (t , err )
164+
165+ cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
166+ require .NoError (t , err )
167+
168+ err = cyberArkClient .PostDataReadingsWithOptions (t .Context (), api.DataReadingsPost {}, dataupload.Options {
169+ ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
170+ })
171+ require .NoError (t , err )
172+ }
0 commit comments