@@ -35,6 +35,9 @@ import (
3535 "github.com/jetstack/preflight/pkg/clusteruid"
3636 "github.com/jetstack/preflight/pkg/datagatherer"
3737 "github.com/jetstack/preflight/pkg/datagatherer/k8s"
38+ "github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
39+ "github.com/jetstack/preflight/pkg/internal/cyberark/identity"
40+ "github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
3841 "github.com/jetstack/preflight/pkg/kubeconfig"
3942 "github.com/jetstack/preflight/pkg/logs"
4043 "github.com/jetstack/preflight/pkg/version"
@@ -79,8 +82,46 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7982 return fmt .Errorf ("While evaluating configuration: %v" , err )
8083 }
8184
82- // We need the cluster UID before we progress further so it can be sent along with other data readings
85+ var caClient * dataupload.CyberArkClient
86+ {
87+ platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
88+ subdomain := os .Getenv ("ARK_SUBDOMAIN" )
89+ username := os .Getenv ("ARK_USERNAME" )
90+ password := []byte (os .Getenv ("ARK_SECRET" ))
91+
92+ const (
93+ discoveryContextServiceName = "inventory"
94+ separator = "."
95+ )
96+
97+ // TODO(wallrj): Maybe get this URL via the service discovery API.
98+ // https://platform-discovery.integration-cyberark.cloud/api/public/tenant-discovery?allEndpoints=true&bySubdomain=tlskp-test
99+ serviceURL := fmt .Sprintf ("https://%s%s%s.%s" , subdomain , separator , discoveryContextServiceName , platformDomain )
100+
101+ var (
102+ identityClient * identity.Client
103+ err error
104+ )
105+ if platformDomain == "cyberark.cloud" {
106+ identityClient , err = identity .New (ctx , subdomain )
107+ } else {
108+ discoveryClient := servicediscovery .New (servicediscovery .WithIntegrationEndpoint ())
109+ identityClient , err = identity .NewWithDiscoveryClient (ctx , discoveryClient , subdomain )
110+ }
111+ if err != nil {
112+ return fmt .Errorf ("while creating the CyberArk identity client: %v" , err )
113+ }
114+ if err := identityClient .LoginUsernamePassword (ctx , username , password ); err != nil {
115+ return fmt .Errorf ("while logging in: %v" , err )
116+ }
117+ caClient , err = dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
118+ if err != nil {
119+ return fmt .Errorf ("while creating the CyberArk dataupload client: %v" , err )
120+ }
121+ }
83122
123+ // We need the cluster UID before we progress further so it can be sent along with other data readings
124+ // TODO(wallrj): Use the k8s-discovery gatherer to get clusterID
84125 {
85126 restCfg , err := kubeconfig .LoadRESTConfig ("" )
86127 if err != nil {
@@ -262,7 +303,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
262303 // be cancelled, which will cause this blocking loop to exit
263304 // instead of waiting for the time period.
264305 for {
265- if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , dataGatherers ); err != nil {
306+ if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , caClient , dataGatherers ); err != nil {
266307 return err
267308 }
268309
@@ -316,7 +357,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
316357// Like Printf but for sending events to the agent's Pod object.
317358type Eventf func (eventType , reason , msg string , args ... interface {})
318359
319- func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
360+ func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , caClient * dataupload. CyberArkClient , dataGatherers map [string ]datagatherer.DataGatherer ) error {
320361 log := klog .FromContext (ctx ).WithName ("gatherAndOutputData" )
321362 var readings []* api.DataReading
322363
@@ -362,8 +403,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
362403
363404 if config .MachineHubMode {
364405 post := func () (any , error ) {
365- log .Info ("machine hub mode not yet implemented" )
366- return struct {}{}, nil
406+ return struct {}{}, caClient .PostDataReadingsWithOptions (ctx , readings , dataupload.Options {})
367407 }
368408
369409 group .Go (func () error {
0 commit comments