Skip to content

Commit

Permalink
cli: add --cert-principal-map to cert list command
Browse files Browse the repository at this point in the history
Remove the use of `base.Config.GetCertificateManager()` from the `cert
list` implementation as a first step in limiting use of that method, and
possibly removing it in the future.

Fixes cockroachdb#48011

Release note (cli change): Support `list cert` with certificates which
require `--cert-principal-map` to pass validation.
  • Loading branch information
petermattis committed May 2, 2020
1 parent 283315f commit 003f7fd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/cli/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ List certificates and keys found in the certificate directory.

// runListCerts loads and lists all certs.
func runListCerts(cmd *cobra.Command, args []string) error {
cm, err := baseCfg.GetCertificateManager()
if err := security.SetCertPrincipalMap(certCtx.certPrincipalMap); err != nil {
return err
}
cm, err := security.NewCertificateManager(baseCfg.SSLCertsDir)
if err != nil {
return errors.Wrap(err, "could not get certificate manager")
return errors.Wrap(err, "cannot load certificates")
}

fmt.Fprintf(os.Stdout, "Certificate directory: %s\n", baseCfg.SSLCertsDir)
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func initCLIDefaults() {

authCtx.validityPeriod = 1 * time.Hour

certCtx.certPrincipalMap = nil

initPreFlagsDefaults()

// Clear the "Changed" state of all the registered command-line flags.
Expand Down Expand Up @@ -394,3 +396,9 @@ var demoCtx struct {
transientCluster *transientCluster
insecure bool
}

// certCtx captures the command-line parameters of the `cert` command.
// Defaults set by InitCLIDefaults() above.
var certCtx struct {
certPrincipalMap []string
}
4 changes: 4 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ func init() {
StringFlag(f, &baseCfg.SSLCertsDir, cliflags.CertsDir, baseCfg.SSLCertsDir)
}

// The list certs command needs the certificate principal map.
StringSlice(listCertsCmd.Flags(), &certCtx.certPrincipalMap,
cliflags.CertPrincipalMap, certCtx.certPrincipalMap)

for _, cmd := range []*cobra.Command{createCACertCmd, createClientCACertCmd} {
f := cmd.Flags()
// CA certificates have a longer expiration time.
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/interactive_tests/test_cert_advisory_validation.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ interrupt
eexpect "interrupted"
expect $prompt
end_test

start_test "Check that 'cert list' can utilize cert principal map."
send "$argv cert list --certs-dir=$certs_dir --cert-principal-map=foo.bar:node\r"
eexpect "Certificate directory:"
expect $prompt
end_test

0 comments on commit 003f7fd

Please sign in to comment.