@@ -35,6 +35,8 @@ 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/identity"
39+ "github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
3840 "github.com/jetstack/preflight/pkg/kubeconfig"
3941 "github.com/jetstack/preflight/pkg/logs"
4042 "github.com/jetstack/preflight/pkg/version"
@@ -79,6 +81,44 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7981 return fmt .Errorf ("While evaluating configuration: %v" , err )
8082 }
8183
84+ var caClient * client.CyberArkClient
85+ {
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 = client .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
117+ if err != nil {
118+ return fmt .Errorf ("while creating the CyberArk dataupload client: %v" , err )
119+ }
120+ }
121+
82122 // We need the cluster UID before we progress further so it can be sent along with other data readings
83123
84124 {
@@ -185,7 +225,6 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
185225 }
186226
187227 dataGatherers := map [string ]datagatherer.DataGatherer {}
188-
189228 // load datagatherer config and boot each one
190229 for _ , dgConfig := range config .DataGatherers {
191230 kind := dgConfig .Kind
@@ -262,7 +301,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
262301 // be cancelled, which will cause this blocking loop to exit
263302 // instead of waiting for the time period.
264303 for {
265- if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , dataGatherers ); err != nil {
304+ if err := gatherAndOutputData (klog .NewContext (ctx , log ), eventf , config , preflightClient , caClient , dataGatherers ); err != nil {
266305 return err
267306 }
268307
@@ -316,7 +355,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
316355// Like Printf but for sending events to the agent's Pod object.
317356type Eventf func (eventType , reason , msg string , args ... interface {})
318357
319- func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
358+ func gatherAndOutputData (ctx context.Context , eventf Eventf , config CombinedConfig , preflightClient client.Client , caClient * client. CyberArkClient , dataGatherers map [string ]datagatherer.DataGatherer ) error {
320359 log := klog .FromContext (ctx ).WithName ("gatherAndOutputData" )
321360 var readings []* api.DataReading
322361
@@ -338,8 +377,18 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
338377 }
339378 }
340379
380+ clusterID := clusteruid .ClusterUIDFromContext (ctx )
381+ payload := api.DataReadingsPost {
382+ AgentMetadata : & api.AgentMetadata {
383+ Version : version .PreflightVersion ,
384+ ClusterID : clusterID ,
385+ },
386+ DataGatherTime : time .Now (),
387+ DataReadings : readings ,
388+ }
341389 if config .OutputPath != "" {
342- data , err := json .MarshalIndent (readings , "" , " " )
390+
391+ data , err := json .MarshalIndent (payload , "" , " " )
343392 if err != nil {
344393 return fmt .Errorf ("failed to marshal JSON: %s" , err )
345394 }
@@ -359,11 +408,11 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
359408 eventf ("Warning" , "PushingErr" , "retrying in %v after error: %s" , t , err )
360409 log .Info ("Warning: PushingErr: retrying" , "in" , t , "reason" , err )
361410 })
362-
363411 if config .MachineHubMode {
364412 post := func () (any , error ) {
365- log .Info ("machine hub mode not yet implemented" )
366- return struct {}{}, nil
413+ return struct {}{}, caClient .PostDataReadingsWithOptions (ctx , payload , client.CyberArkClientOptions {
414+ ClusterName : clusterID ,
415+ })
367416 }
368417
369418 group .Go (func () error {
0 commit comments