@@ -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,6 +82,44 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7982 return fmt .Errorf ("While evaluating configuration: %v" , err )
8083 }
8184
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+ }
122+
82123 // We need the cluster UID before we progress further so it can be sent along with other data readings
83124
84125 {
@@ -185,7 +226,6 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
185226 }
186227
187228 dataGatherers := map [string ]datagatherer.DataGatherer {}
188-
189229 // load datagatherer config and boot each one
190230 for _ , dgConfig := range config .DataGatherers {
191231 kind := dgConfig .Kind
@@ -262,7 +302,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
262302 // be cancelled, which will cause this blocking loop to exit
263303 // instead of waiting for the time period.
264304 for {
265- if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , dataGatherers ); err != nil {
305+ if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , caClient , dataGatherers ); err != nil {
266306 return err
267307 }
268308
@@ -316,7 +356,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
316356// Like Printf but for sending events to the agent's Pod object.
317357type Eventf func (eventType , reason , msg string , args ... interface {})
318358
319- func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
359+ func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , caClient * dataupload. CyberArkClient , dataGatherers map [string ]datagatherer.DataGatherer ) error {
320360 log := klog .FromContext (ctx ).WithName ("gatherAndOutputData" )
321361 var readings []* api.DataReading
322362
@@ -338,8 +378,18 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
338378 }
339379 }
340380
381+ clusterID := clusteruid .ClusterUIDFromContext (ctx )
382+ payload := api.DataReadingsPost {
383+ AgentMetadata : & api.AgentMetadata {
384+ Version : version .PreflightVersion ,
385+ ClusterID : clusterID ,
386+ },
387+ DataGatherTime : time .Now (),
388+ DataReadings : readings ,
389+ }
341390 if config .OutputPath != "" {
342- data , err := json .MarshalIndent (readings , "" , " " )
391+
392+ data , err := json .MarshalIndent (payload , "" , " " )
343393 if err != nil {
344394 return fmt .Errorf ("failed to marshal JSON: %s" , err )
345395 }
@@ -359,11 +409,11 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
359409 eventf ("Warning" , "PushingErr" , "retrying in %v after error: %s" , t , err )
360410 log .Info ("Warning: PushingErr: retrying" , "in" , t , "reason" , err )
361411 })
362-
363412 if config .MachineHubMode {
364413 post := func () (any , error ) {
365- log .Info ("machine hub mode not yet implemented" )
366- return struct {}{}, nil
414+ return struct {}{}, caClient .PostDataReadingsWithOptions (ctx , payload , dataupload.Options {
415+ ClusterName : clusterID ,
416+ })
367417 }
368418
369419 group .Go (func () error {
0 commit comments