@@ -34,6 +34,9 @@ import (
34
34
"github.com/jetstack/preflight/pkg/client"
35
35
"github.com/jetstack/preflight/pkg/datagatherer"
36
36
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
37
+ "github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
38
+ "github.com/jetstack/preflight/pkg/internal/cyberark/identity"
39
+ "github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
37
40
"github.com/jetstack/preflight/pkg/kubeconfig"
38
41
"github.com/jetstack/preflight/pkg/logs"
39
42
"github.com/jetstack/preflight/pkg/version"
@@ -78,6 +81,44 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
78
81
return fmt .Errorf ("While evaluating configuration: %v" , err )
79
82
}
80
83
84
+ var caClient * dataupload.CyberArkClient
85
+ if config .MachineHubMode {
86
+ platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
87
+ subdomain := os .Getenv ("ARK_SUBDOMAIN" )
88
+ username := os .Getenv ("ARK_USERNAME" )
89
+ password := []byte (os .Getenv ("ARK_SECRET" ))
90
+
91
+ const (
92
+ discoveryContextServiceName = "inventory"
93
+ separator = "."
94
+ )
95
+
96
+ // TODO(wallrj): Maybe get this URL via the service discovery API.
97
+ // https://platform-discovery.integration-cyberark.cloud/api/public/tenant-discovery?allEndpoints=true&bySubdomain=tlskp-test
98
+ serviceURL := fmt .Sprintf ("https://%s%s%s.%s" , subdomain , separator , discoveryContextServiceName , platformDomain )
99
+
100
+ var (
101
+ identityClient * identity.Client
102
+ err error
103
+ )
104
+ if platformDomain == "cyberark.cloud" {
105
+ identityClient , err = identity .New (ctx , subdomain )
106
+ } else {
107
+ discoveryClient := servicediscovery .New (servicediscovery .WithIntegrationEndpoint ())
108
+ identityClient , err = identity .NewWithDiscoveryClient (ctx , discoveryClient , subdomain )
109
+ }
110
+ if err != nil {
111
+ return fmt .Errorf ("while creating the CyberArk identity client: %v" , err )
112
+ }
113
+ if err := identityClient .LoginUsernamePassword (ctx , username , password ); err != nil {
114
+ return fmt .Errorf ("while logging in: %v" , err )
115
+ }
116
+ caClient , err = dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
117
+ if err != nil {
118
+ return fmt .Errorf ("while creating the CyberArk dataupload client: %v" , err )
119
+ }
120
+ }
121
+
81
122
group , gctx := errgroup .WithContext (ctx )
82
123
defer func () {
83
124
cancel ()
@@ -239,7 +280,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
239
280
// be cancelled, which will cause this blocking loop to exit
240
281
// instead of waiting for the time period.
241
282
for {
242
- if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , dataGatherers ); err != nil {
283
+ if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , caClient , dataGatherers ); err != nil {
243
284
return err
244
285
}
245
286
@@ -293,7 +334,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
293
334
// Like Printf but for sending events to the agent's Pod object.
294
335
type Eventf func (eventType , reason , msg string , args ... interface {})
295
336
296
- func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
337
+ func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , caClient * dataupload. CyberArkClient , dataGatherers map [string ]datagatherer.DataGatherer ) error {
297
338
log := klog .FromContext (ctx ).WithName ("gatherAndOutputData" )
298
339
var readings []* api.DataReading
299
340
@@ -347,8 +388,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
347
388
348
389
if config .MachineHubMode {
349
390
post := func () (any , error ) {
350
- log .Info ("machine hub mode not yet implemented" )
351
- return struct {}{}, nil
391
+ return struct {}{}, caClient .PostDataReadingsWithOptions (ctx , readings , dataupload.Options {})
352
392
}
353
393
354
394
group .Go (func () error {
0 commit comments