Skip to content

Commit 432e16f

Browse files
committed
nats server check credential --context-credential
A common use-case for checking credentials is "check my current credentials". Rather than force users to manually grab the path from their context, provide a flag `--context-credential` to just use the credential from that. Changes in existing behavior: a different error flow for missing `--credential` with this new flag is not given. I couldn't see a way in the fisk library to have a mandatory mutually-exclusive flag group, so coded up the checks manually when dropping `Required()` from `--credential`.
1 parent 7c8f9bc commit 432e16f

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

cli/server_check_command.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type SrvCheckCmd struct {
116116
credentialValidityCrit time.Duration
117117
credentialValidityWarn time.Duration
118118
credentialRequiresExpire bool
119+
credentialFromContext bool
119120
credential string
120121

121122
exporterConfigFile string
@@ -237,10 +238,11 @@ When set these settings will be used, but can be overridden using --waiting-crit
237238
kv.Flag("key", "Requires a key to have any non-delete value set").StringVar(&c.kvKey)
238239

239240
cred := check.Command("credential", "Checks the validity of a NATS credential file").Action(c.checkCredentialAction)
240-
cred.Flag("credential", "The file holding the NATS credential").Required().StringVar(&c.credential)
241+
cred.Flag("credential", "The file holding the NATS credential").StringVar(&c.credential)
241242
cred.Flag("validity-warn", "Warning threshold for time before expiry").DurationVar(&c.credentialValidityWarn)
242243
cred.Flag("validity-critical", "Critical threshold for time before expiry").DurationVar(&c.credentialValidityCrit)
243244
cred.Flag("require-expiry", "Requires the credential to have expiry set").Default("true").BoolVar(&c.credentialRequiresExpire)
245+
cred.Flag("context-credential", "Use the credential file from the context").BoolVar(&c.credentialFromContext)
244246

245247
exporter := check.Command("exporter", "Prometheus exporter for server checks").Hidden().Action(c.exporterAction)
246248
exporter.Flag("config", "Exporter configuration").Required().ExistingFileVar(&c.exporterConfigFile)
@@ -489,6 +491,25 @@ func (c *SrvCheckCmd) checkCredentialAction(_ *fisk.ParseContext) error {
489491
check := &monitor.Result{Name: "Credential", Check: "credential", OutFile: checkRenderOutFile, NameSpace: opts().PrometheusNamespace, RenderFormat: checkRenderFormat, Trace: opts().Trace}
490492
defer check.GenericExit()
491493

494+
if c.credentialFromContext {
495+
if c.credential == "" {
496+
err := loadContext(false)
497+
if check.CriticalIfErr(err, "loading context failed: %v", err) {
498+
return nil
499+
}
500+
c.credential = opts().Config.Creds()
501+
if c.credential == "" {
502+
check.CriticalExit("--context-credential failed to load a credential")
503+
return nil
504+
}
505+
}
506+
} else {
507+
if c.credential == "" {
508+
check.CriticalExit("neither --credential nor --context-credential given")
509+
return nil
510+
}
511+
}
512+
492513
return monitor.CheckCredential(check, monitor.CheckCredentialOptions{
493514
File: c.credential,
494515
ValidityWarning: c.credentialValidityWarn.Seconds(),

0 commit comments

Comments
 (0)