Skip to content

Commit 57d3435

Browse files
Merge pull request #692 from jetstack/VC-43403-fix-output-path-mode
[VC-43403] Add Local FIle output path mode
2 parents 4026993 + 4e5623d commit 57d3435

File tree

8 files changed

+236
-99
lines changed

8 files changed

+236
-99
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ To build and run a version from master:
2020
go run main.go agent --agent-config-file ./path/to/agent/config/file.yaml -p 0h1m0s
2121
```
2222

23-
You can find an example agent file [here](https://github.com/jetstack/preflight/blob/master/agent.yaml).
23+
You can configure the agent to perform one data gathering loop and output the data to a local file:
24+
25+
```bash
26+
go run . agent \
27+
--agent-config-file examples/one-shot-secret.yaml \
28+
--one-shot \
29+
--output-path output.json
30+
```
31+
32+
> Some examples of agent configuration files:
33+
> - [./agent.yaml](./agent.yaml).
34+
> - [./examples/one-shot-secret.yaml](./examples/one-shot-secret.yaml).
35+
> - [./examples/cert-manager-agent.yaml](./examples/cert-manager-agent.yaml).
2436
2537
You might also want to run a local echo server to monitor requests sent by the agent:
2638

cmd/agent_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func TestAgentRunOneShot(t *testing.T) {
1919
"preflight",
2020
"agent",
2121
"--one-shot",
22-
// TODO(wallrj): This should not be required when an `--input-file` has been supplied.
23-
"--api-token=should-not-be-required",
2422
"--agent-config-file=testdata/agent/one-shot/success/config.yaml",
2523
"--input-path=testdata/agent/one-shot/success/input.json",
2624
"--output-path=/dev/null",

examples/one-shot-secret.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# It gathers only secrets and it does not attempt to upload to Venafi.
55
# For example:
66
#
7-
# builds/preflight agent \
7+
# go run . agent \
88
# --agent-config-file examples/one-shot-secret.yaml \
99
# --one-shot \
1010
# --output-path output.json

pkg/agent/config.go

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,16 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
324324

325325
}
326326

327-
// TLSPKMode controls how to authenticate to TLSPK / Jetstack Secure. Only one
328-
// TLSPKMode may be provided if using those backends.
329-
type TLSPKMode string
327+
// OutputMode controls how the collected data is published.
328+
// Only one OutputMode may be provided.
329+
type OutputMode string
330330

331331
const (
332-
JetstackSecureOAuth TLSPKMode = "Jetstack Secure OAuth"
333-
JetstackSecureAPIToken TLSPKMode = "Jetstack Secure API Token"
334-
VenafiCloudKeypair TLSPKMode = "Venafi Cloud Key Pair Service Account"
335-
VenafiCloudVenafiConnection TLSPKMode = "Venafi Cloud VenafiConnection"
332+
JetstackSecureOAuth OutputMode = "Jetstack Secure OAuth"
333+
JetstackSecureAPIToken OutputMode = "Jetstack Secure API Token"
334+
VenafiCloudKeypair OutputMode = "Venafi Cloud Key Pair Service Account"
335+
VenafiCloudVenafiConnection OutputMode = "Venafi Cloud VenafiConnection"
336+
LocalFile OutputMode = "Local File"
336337
)
337338

338339
// The command-line flags and the config file are combined into this struct by
@@ -345,7 +346,7 @@ type CombinedConfig struct {
345346
StrictMode bool
346347
OneShot bool
347348

348-
TLSPKMode TLSPKMode
349+
OutputMode OutputMode
349350

350351
// Used by all TLSPK modes.
351352
ClusterID string
@@ -389,7 +390,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
389390

390391
{
391392
var (
392-
mode TLSPKMode
393+
mode OutputMode
393394
reason string
394395
keysAndValues []any
395396
)
@@ -419,18 +420,25 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
419420
case !flags.VenafiCloudMode && flags.CredentialsPath != "":
420421
mode = JetstackSecureOAuth
421422
reason = "--credentials-file was specified without --venafi-cloud"
423+
case flags.OutputPath != "":
424+
mode = LocalFile
425+
reason = "--output-path was specified"
426+
case cfg.OutputPath != "":
427+
mode = LocalFile
428+
reason = "output-path was specified in the config file"
422429
default:
423-
return CombinedConfig{}, nil, fmt.Errorf("no TLSPK mode specified. " +
424-
"To enable one of the TLSPK modes, you can:\n" +
430+
return CombinedConfig{}, nil, fmt.Errorf("no output mode specified. " +
431+
"To enable one of the output modes, you can:\n" +
425432
" - Use (--venafi-cloud with --credentials-file) or (--client-id with --private-key-path) to use the " + string(VenafiCloudKeypair) + " mode.\n" +
426433
" - Use --venafi-connection for the " + string(VenafiCloudVenafiConnection) + " mode.\n" +
427434
" - Use --credentials-file alone if you want to use the " + string(JetstackSecureOAuth) + " mode.\n" +
428-
" - Use --api-token if you want to use the " + string(JetstackSecureAPIToken) + " mode.")
435+
" - Use --api-token if you want to use the " + string(JetstackSecureAPIToken) + " mode.\n" +
436+
" - Use --output-path or output-path in the config file for " + string(LocalFile) + " mode.")
429437
}
430438

431439
keysAndValues = append(keysAndValues, "mode", mode, "reason", reason)
432-
log.V(logs.Debug).Info("Configured to push to Venafi", keysAndValues...)
433-
res.TLSPKMode = mode
440+
log.V(logs.Debug).Info("Output mode selected", keysAndValues...)
441+
res.OutputMode = mode
434442
}
435443

436444
var errs error
@@ -459,7 +467,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
459467
endpointPath = cfg.Endpoint.Path
460468
case !hasServerField && !hasEndpointField:
461469
server = "https://preflight.jetstack.io"
462-
if res.TLSPKMode == VenafiCloudKeypair {
470+
if res.OutputMode == VenafiCloudKeypair {
463471
// The VenafiCloudVenafiConnection mode doesn't need a server.
464472
server = client.VenafiCloudProdURL
465473
}
@@ -468,7 +476,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
468476
if urlErr != nil || url.Hostname() == "" {
469477
errs = multierror.Append(errs, fmt.Errorf("server %q is not a valid URL", server))
470478
}
471-
if res.TLSPKMode == VenafiCloudVenafiConnection && server != "" {
479+
if res.OutputMode == VenafiCloudVenafiConnection && server != "" {
472480
log.Info(fmt.Sprintf("ignoring the server field specified in the config file. In %s mode, this field is not needed.", VenafiCloudVenafiConnection))
473481
server = ""
474482
}
@@ -479,10 +487,10 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
479487
// Validation of `venafi-cloud.upload_path`.
480488
{
481489
var uploadPath string
482-
switch res.TLSPKMode { // nolint:exhaustive
490+
switch res.OutputMode { // nolint:exhaustive
483491
case VenafiCloudKeypair:
484492
if cfg.VenafiCloud == nil || cfg.VenafiCloud.UploadPath == "" {
485-
errs = multierror.Append(errs, fmt.Errorf("the venafi-cloud.upload_path field is required when using the %s mode", res.TLSPKMode))
493+
errs = multierror.Append(errs, fmt.Errorf("the venafi-cloud.upload_path field is required when using the %s mode", res.OutputMode))
486494
break // Skip to the end of the switch statement.
487495
}
488496
_, urlErr := url.Parse(cfg.VenafiCloud.UploadPath)
@@ -499,7 +507,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
499507
// change this value with the new --venafi-connection flag, and this
500508
// field is simply ignored.
501509
if cfg.VenafiCloud != nil && cfg.VenafiCloud.UploadPath != "" {
502-
log.Info(fmt.Sprintf(`ignoring the venafi-cloud.upload_path field in the config file. In %s mode, this field is not needed.`, res.TLSPKMode))
510+
log.Info(fmt.Sprintf(`ignoring the venafi-cloud.upload_path field in the config file. In %s mode, this field is not needed.`, res.OutputMode))
503511
}
504512
uploadPath = ""
505513
}
@@ -517,18 +525,18 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
517525
// https://venafi.atlassian.net/browse/VC-35385 is done.
518526
{
519527
if cfg.VenafiCloud != nil && cfg.VenafiCloud.UploaderID != "" {
520-
log.Info(fmt.Sprintf(`ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in %s mode.`, res.TLSPKMode))
528+
log.Info(fmt.Sprintf(`ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in %s mode.`, res.OutputMode))
521529
}
522530
}
523531

524532
// Validation of `cluster_id` and `organization_id`.
525533
{
526534
var clusterID string
527535
var organizationID string // Only used by the old jetstack-secure mode.
528-
switch res.TLSPKMode { // nolint:exhaustive
536+
switch res.OutputMode { // nolint:exhaustive
529537
case VenafiCloudKeypair, VenafiCloudVenafiConnection:
530538
if cfg.ClusterID == "" {
531-
errs = multierror.Append(errs, fmt.Errorf("cluster_id is required in %s mode", res.TLSPKMode))
539+
errs = multierror.Append(errs, fmt.Errorf("cluster_id is required in %s mode", res.OutputMode))
532540
}
533541
clusterID = cfg.ClusterID
534542
case JetstackSecureOAuth, JetstackSecureAPIToken:
@@ -587,7 +595,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
587595
var err error
588596
installNS, err = getInClusterNamespace()
589597
if err != nil {
590-
if res.TLSPKMode == VenafiCloudVenafiConnection {
598+
if res.OutputMode == VenafiCloudVenafiConnection {
591599
errs = multierror.Append(errs, fmt.Errorf("could not guess which namespace the agent is running in: %w", err))
592600
}
593601
}
@@ -596,7 +604,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
596604
}
597605

598606
// Validation of --venafi-connection and --venafi-connection-namespace.
599-
if res.TLSPKMode == VenafiCloudVenafiConnection {
607+
if res.OutputMode == VenafiCloudVenafiConnection {
600608
res.VenConnName = flags.VenConnName
601609
venConnNS := flags.VenConnNS
602610
if flags.VenConnNS == "" {
@@ -643,12 +651,12 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
643651
return CombinedConfig{}, nil, errs
644652
}
645653

646-
preflightClient, err := validateCredsAndCreateClient(log, flags.CredentialsPath, flags.ClientID, flags.PrivateKeyPath, flags.APIToken, res)
654+
outputClient, err := validateCredsAndCreateClient(log, flags.CredentialsPath, flags.ClientID, flags.PrivateKeyPath, flags.APIToken, res)
647655
if err != nil {
648656
return CombinedConfig{}, nil, multierror.Prefix(err, "validating creds:")
649657
}
650658

651-
return res, preflightClient, nil
659+
return res, outputClient, nil
652660
}
653661

654662
// Validation of --credentials-file/-k, --client-id, and --private-key-path,
@@ -660,9 +668,9 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
660668
func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClientID, flagPrivateKeyPath, flagAPIToken string, cfg CombinedConfig) (client.Client, error) {
661669
var errs error
662670

663-
var preflightClient client.Client
671+
var outputClient client.Client
664672
metadata := &api.AgentMetadata{Version: version.PreflightVersion, ClusterID: cfg.ClusterID}
665-
switch cfg.TLSPKMode {
673+
switch cfg.OutputMode {
666674
case JetstackSecureOAuth:
667675
// Note that there are no command line flags to configure the
668676
// JetstackSecureOAuth mode.
@@ -678,7 +686,7 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
678686
break // Don't continue with the client if credentials file invalid.
679687
}
680688

681-
preflightClient, err = client.NewOAuthClient(metadata, creds, cfg.Server)
689+
outputClient, err = client.NewOAuthClient(metadata, creds, cfg.Server)
682690
if err != nil {
683691
errs = multierror.Append(errs, err)
684692
}
@@ -730,7 +738,7 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
730738
log.Info("Loading upload_path from \"venafi-cloud\" configuration.")
731739

732740
var err error
733-
preflightClient, err = client.NewVenafiCloudClient(metadata, creds, cfg.Server, uploaderID, cfg.UploadPath)
741+
outputClient, err = client.NewVenafiCloudClient(metadata, creds, cfg.Server, uploaderID, cfg.UploadPath)
734742
if err != nil {
735743
errs = multierror.Append(errs, err)
736744
}
@@ -742,25 +750,27 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
742750
break // Don't continue with the client if kubeconfig wasn't loaded.
743751
}
744752

745-
preflightClient, err = client.NewVenConnClient(restCfg, metadata, cfg.InstallNS, cfg.VenConnName, cfg.VenConnNS, nil)
753+
outputClient, err = client.NewVenConnClient(restCfg, metadata, cfg.InstallNS, cfg.VenConnName, cfg.VenConnNS, nil)
746754
if err != nil {
747755
errs = multierror.Append(errs, err)
748756
}
749757
case JetstackSecureAPIToken:
750758
var err error
751-
preflightClient, err = client.NewAPITokenClient(metadata, flagAPIToken, cfg.Server)
759+
outputClient, err = client.NewAPITokenClient(metadata, flagAPIToken, cfg.Server)
752760
if err != nil {
753761
errs = multierror.Append(errs, err)
754762
}
763+
case LocalFile:
764+
outputClient = client.NewFileClient(cfg.OutputPath)
755765
default:
756-
panic(fmt.Errorf("programmer mistake: auth mode not implemented: %s", cfg.TLSPKMode))
766+
panic(fmt.Errorf("programmer mistake: output mode not implemented: %s", cfg.OutputMode))
757767
}
758768

759769
if errs != nil {
760-
return nil, fmt.Errorf("failed loading config using the %s mode: %w", cfg.TLSPKMode, errs)
770+
return nil, fmt.Errorf("failed loading config using the %s mode: %w", cfg.OutputMode, errs)
761771
}
762772

763-
return preflightClient, nil
773+
return outputClient, nil
764774
}
765775

766776
// Same as ValidateAndCombineConfig but just for validating the data gatherers.

0 commit comments

Comments
 (0)