@@ -5,13 +5,18 @@ import (
5
5
"encoding/pem"
6
6
"fmt"
7
7
"net/http"
8
+ "os"
8
9
"testing"
9
10
"time"
10
11
11
12
"github.com/stretchr/testify/require"
13
+ "k8s.io/klog/v2"
14
+ "k8s.io/klog/v2/ktesting"
12
15
13
16
"github.com/jetstack/preflight/api"
14
17
"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"
15
20
)
16
21
17
22
func TestCyberArkClient_PostDataReadingsWithOptions (t * testing.T ) {
@@ -75,7 +80,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
75
80
opts : defaultOpts ,
76
81
authenticate : setToken ("fail-token" ),
77
82
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" )
79
84
},
80
85
},
81
86
{
@@ -84,16 +89,16 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
84
89
opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" , ClusterDescription : defaultOpts .ClusterDescription },
85
90
authenticate : setToken ("success-token" ),
86
91
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" )
88
93
},
89
94
},
90
95
{
91
- name : "500 from server (PostData step)" ,
96
+ name : "500 from server (RetrievePresignedUploadURL step)" ,
92
97
payload : defaultPayload ,
93
98
opts : dataupload.Options {ClusterName : "invalid-response-post-data" , ClusterDescription : defaultOpts .ClusterDescription },
94
99
authenticate : setToken ("success-token" ),
95
100
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" )
97
102
},
98
103
},
99
104
}
@@ -117,3 +122,52 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
117
122
})
118
123
}
119
124
}
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