@@ -36,6 +36,9 @@ var Period time.Duration
3636// OneShot flag causes agent to run once
3737var OneShot bool
3838
39+ // VenafiCloudMode flag determines which format to load for config and credential type
40+ var VenafiCloudMode bool
41+
3942// CredentialsPath is where the agent will try to loads the credentials. (Experimental)
4043var CredentialsPath string
4144
@@ -221,7 +224,7 @@ func getConfiguration() (Config, client.Client) {
221224
222225 log .Printf ("Loaded config: \n %s" , dump )
223226
224- var credentials * client.Credentials
227+ var credentials client.Credentials
225228 if CredentialsPath != "" {
226229 file , err = os .Open (CredentialsPath )
227230 if err != nil {
@@ -233,7 +236,11 @@ func getConfiguration() (Config, client.Client) {
233236 if err != nil {
234237 log .Fatalf ("Failed to read credentials file: %v" , err )
235238 }
236- credentials , err = client .ParseCredentials (b )
239+ if VenafiCloudMode {
240+ credentials , err = client .ParseVenafiCredentials (b )
241+ } else {
242+ credentials , err = client .ParseOAuthCredentials (b )
243+ }
237244 if err != nil {
238245 log .Fatalf ("Failed to parse credentials file: %s" , err )
239246 }
@@ -247,8 +254,7 @@ func getConfiguration() (Config, client.Client) {
247254 var preflightClient client.Client
248255 switch {
249256 case credentials != nil :
250- log .Println ("A credentials file was specified, using oauth authentication." )
251- preflightClient , err = client .NewOAuthClient (agentMetadata , credentials , baseURL )
257+ preflightClient , err = createCredentialClient (credentials , config , agentMetadata , baseURL )
252258 case APIToken != "" :
253259 log .Println ("An API token was specified, using API token authentication." )
254260 preflightClient , err = client .NewAPITokenClient (agentMetadata , APIToken , baseURL )
@@ -264,6 +270,24 @@ func getConfiguration() (Config, client.Client) {
264270 return config , preflightClient
265271}
266272
273+ func createCredentialClient (credentials client.Credentials , config Config , agentMetadata * api.AgentMetadata , baseURL string ) (client.Client , error ) {
274+ switch creds := credentials .(type ) {
275+ case * client.VenafiSvcAccountCredentials :
276+ log .Println ("Venafi Cloud mode was specified, using Venafi Service Account authentication." )
277+ // check if config has Venafi Cloud data
278+ if config .VenafiCloud == nil {
279+ log .Fatalf ("Failed to find config for venafi-cloud: required for Venafi Cloud mode" )
280+ }
281+ return client .NewVenafiCloudClient (agentMetadata , creds , baseURL , config .VenafiCloud .UploaderID , config .VenafiCloud .UploadPath )
282+
283+ case * client.OAuthCredentials :
284+ log .Println ("A credentials file was specified, using oauth authentication." )
285+ return client .NewOAuthClient (agentMetadata , creds , baseURL )
286+ default :
287+ return nil , errors .New ("credentials file is in unknown format" )
288+ }
289+ }
290+
267291func gatherAndOutputData (config Config , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) {
268292 var readings []* api.DataReading
269293
@@ -363,6 +387,18 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
363387
364388 log .Println ("Running Agent..." )
365389 log .Println ("Posting data to:" , baseURL )
390+
391+ if VenafiCloudMode {
392+ // orgID and clusterID are not required for Venafi Cloud auth
393+ err := preflightClient .PostDataReadings ("" , "" , readings )
394+ if err != nil {
395+ return fmt .Errorf ("post to server failed: %+v" , err )
396+ }
397+ log .Println ("Data sent successfully." )
398+
399+ return nil
400+ }
401+
366402 if config .OrganizationID == "" {
367403 data , err := json .Marshal (readings )
368404 if err != nil {
@@ -382,7 +418,7 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
382418 res , err := preflightClient .Post (path , bytes .NewBuffer (data ))
383419
384420 if err != nil {
385- return fmt .Errorf ("Failed to post data: %+v" , err )
421+ return fmt .Errorf ("failed to post data: %+v" , err )
386422 }
387423 if code := res .StatusCode ; code < 200 || code >= 300 {
388424 errorContent := ""
@@ -392,19 +428,19 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
392428 }
393429 defer res .Body .Close ()
394430
395- return fmt .Errorf ("Received response with status code %d. Body: %s" , code , errorContent )
431+ return fmt .Errorf ("received response with status code %d. Body: %s" , code , errorContent )
396432 }
397433 log .Println ("Data sent successfully." )
398434 return err
399435 }
400436
401437 if config .ClusterID == "" {
402- return fmt .Errorf ("Post to server failed: missing clusterID from agent configuration" )
438+ return fmt .Errorf ("post to server failed: missing clusterID from agent configuration" )
403439 }
404440
405441 err := preflightClient .PostDataReadings (config .OrganizationID , config .ClusterID , readings )
406442 if err != nil {
407- return fmt .Errorf ("Post to server failed: %+v" , err )
443+ return fmt .Errorf ("post to server failed: %+v" , err )
408444 }
409445 log .Println ("Data sent successfully." )
410446
0 commit comments