@@ -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 ) {
@@ -33,8 +38,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
33
38
},
34
39
}
35
40
defaultOpts := dataupload.Options {
36
- ClusterName : "success-cluster-id" ,
37
- ClusterDescription : "success-cluster-description" ,
41
+ ClusterName : "success-cluster-id" ,
38
42
}
39
43
40
44
setToken := func (token string ) func (* http.Request ) error {
@@ -75,25 +79,25 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
75
79
opts : defaultOpts ,
76
80
authenticate : setToken ("fail-token" ),
77
81
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" )
79
83
},
80
84
},
81
85
{
82
86
name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
83
87
payload : defaultPayload ,
84
- opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" , ClusterDescription : defaultOpts . ClusterDescription },
88
+ opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
85
89
authenticate : setToken ("success-token" ),
86
90
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" )
88
92
},
89
93
},
90
94
{
91
- name : "500 from server (PostData step)" ,
95
+ name : "500 from server (RetrievePresignedUploadURL step)" ,
92
96
payload : defaultPayload ,
93
- opts : dataupload.Options {ClusterName : "invalid-response-post-data" , ClusterDescription : defaultOpts . ClusterDescription },
97
+ opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
94
98
authenticate : setToken ("success-token" ),
95
99
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" )
97
101
},
98
102
},
99
103
}
@@ -117,3 +121,52 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
117
121
})
118
122
}
119
123
}
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