@@ -34,6 +34,9 @@ import (
3434 "github.com/jetstack/preflight/pkg/client"
3535 "github.com/jetstack/preflight/pkg/datagatherer"
3636 "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"
3740 "github.com/jetstack/preflight/pkg/kubeconfig"
3841 "github.com/jetstack/preflight/pkg/logs"
3942 "github.com/jetstack/preflight/pkg/version"
@@ -78,6 +81,44 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7881 return fmt .Errorf ("While evaluating configuration: %v" , err )
7982 }
8083
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+
81122 group , gctx := errgroup .WithContext (ctx )
82123 defer func () {
83124 cancel ()
@@ -239,7 +280,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
239280 // be cancelled, which will cause this blocking loop to exit
240281 // instead of waiting for the time period.
241282 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 {
243284 return err
244285 }
245286
@@ -293,7 +334,7 @@ func newEventf(log logr.Logger, installNS string) (Eventf, error) {
293334// Like Printf but for sending events to the agent's Pod object.
294335type Eventf func (eventType , reason , msg string , args ... interface {})
295336
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 {
297338 log := klog .FromContext (ctx ).WithName ("gatherAndOutputData" )
298339 var readings []* api.DataReading
299340
@@ -347,8 +388,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
347388
348389 if config .MachineHubMode {
349390 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 {})
352392 }
353393
354394 group .Go (func () error {
0 commit comments