Skip to content

Fix some provisioner and policy prompt issues #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions command/ca/policy/actions/cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
commonNamesAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
flags.EABKeyID,
flags.EABReference,
cli.BoolFlag{
Expand All @@ -76,9 +76,12 @@
}

func commonNamesAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 83 in command/ca/policy/actions/cn.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/cn.go#L79-L83

Added lines #L79 - L83 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -88,7 +91,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 94 in command/ca/policy/actions/cn.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/cn.go#L94

Added line #L94 was not covered by tests
if err != nil {
return fmt.Errorf("error retrieving policy: %w", err)
}
Expand All @@ -113,7 +116,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 119 in command/ca/policy/actions/cn.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/cn.go#L119

Added line #L119 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
13 changes: 8 additions & 5 deletions command/ca/policy/actions/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
dnsAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
flags.EABKeyID,
flags.EABReference,
cli.BoolFlag{
Expand All @@ -94,9 +94,12 @@
}

func dnsAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 101 in command/ca/policy/actions/dns.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/dns.go#L97-L101

Added lines #L97 - L101 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -106,7 +109,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 112 in command/ca/policy/actions/dns.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/dns.go#L112

Added line #L112 was not covered by tests
if err != nil {
return fmt.Errorf("error retrieving policy: %w", err)
}
Expand Down Expand Up @@ -138,7 +141,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 144 in command/ca/policy/actions/dns.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/dns.go#L144

Added line #L144 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
13 changes: 8 additions & 5 deletions command/ca/policy/actions/emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
emailAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
cli.BoolFlag{
Name: "remove",
Usage: `removes the provided emails from the policy instead of adding them`,
Expand All @@ -81,9 +81,12 @@
}

func emailAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 88 in command/ca/policy/actions/emails.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/emails.go#L84-L88

Added lines #L84 - L88 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -93,7 +96,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 99 in command/ca/policy/actions/emails.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/emails.go#L99

Added line #L99 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -125,7 +128,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 131 in command/ca/policy/actions/emails.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/emails.go#L131

Added line #L131 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
13 changes: 8 additions & 5 deletions command/ca/policy/actions/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
ipAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
flags.EABKeyID,
flags.EABReference,
cli.BoolFlag{
Expand All @@ -114,9 +114,12 @@
}

func ipAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 121 in command/ca/policy/actions/ips.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/ips.go#L117-L121

Added lines #L117 - L121 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -126,7 +129,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 132 in command/ca/policy/actions/ips.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/ips.go#L132

Added line #L132 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -158,7 +161,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 164 in command/ca/policy/actions/ips.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/ips.go#L164

Added line #L164 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
48 changes: 30 additions & 18 deletions command/ca/policy/actions/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"errors"
"fmt"

"github.com/urfave/cli"
"google.golang.org/protobuf/encoding/protojson"

"github.com/smallstep/certificates/ca"
Expand All @@ -18,22 +17,37 @@
"github.com/smallstep/cli/internal/command"
)

var provisionerFilterFlag = cli.StringFlag{
Name: "provisioner",
Usage: `The provisioner <name>`,
func retrieveAndUnsetProvisionerFlagIfRequired(ctx context.Context) string {
// when managing policies on the authority level there's no need
// to select a provisioner, so the flag does not need to be unset.
if policycontext.IsAuthorityPolicyLevel(ctx) {
return ""
}

Check warning on line 25 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L20-L25

Added lines #L20 - L25 were not covered by tests

clictx := command.CLIContextFromContext(ctx)
provisioner := clictx.String("provisioner")

// unset the provisioner and issuer flag values, so that they're not used
// automatically in token flows.
if err := clictx.Set("provisioner", ""); err != nil {
panic(fmt.Errorf("failed unsetting provisioner flag: %w", err))

Check warning on line 33 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L27-L33

Added lines #L27 - L33 were not covered by tests
}
if err := clictx.Set("issuer", ""); err != nil {
panic(fmt.Errorf("failed unsetting issuer flag: %w", err))

Check warning on line 36 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L35-L36

Added lines #L35 - L36 were not covered by tests
}

return provisioner

Check warning on line 39 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L39

Added line #L39 was not covered by tests
}

func retrieveAndInitializePolicy(ctx context.Context, client *ca.AdminClient) (*linkedca.Policy, error) {
func retrieveAndInitializePolicy(ctx context.Context, client *ca.AdminClient, provisioner string) (*linkedca.Policy, error) {

Check warning on line 42 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L42

Added line #L42 was not covered by tests
var (
policy *linkedca.Policy
err error
clictx = command.CLIContextFromContext(ctx)
reference = clictx.String("eab-key-reference")
keyID = clictx.String("eab-key-id")
policy *linkedca.Policy
err error

Check warning on line 48 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L44-L48

Added lines #L44 - L48 were not covered by tests
)

clictx := command.CLIContextFromContext(ctx)
provisioner := clictx.String("provisioner")
reference := clictx.String("eab-key-reference")
keyID := clictx.String("eab-key-id")

switch {
case policycontext.IsAuthorityPolicyLevel(ctx):
policy, err = client.GetAuthorityPolicy()
Expand Down Expand Up @@ -147,13 +161,11 @@
return p
}

func updatePolicy(ctx context.Context, client *ca.AdminClient, policy *linkedca.Policy) (*linkedca.Policy, error) {
clictx := command.CLIContextFromContext(ctx)
provisioner := clictx.String("provisioner")
reference := clictx.String("eab-key-reference")
keyID := clictx.String("eab-key-id")

func updatePolicy(ctx context.Context, client *ca.AdminClient, policy *linkedca.Policy, provisioner string) (*linkedca.Policy, error) {

Check warning on line 164 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L164

Added line #L164 was not covered by tests
var (
clictx = command.CLIContextFromContext(ctx)
reference = clictx.String("eab-key-reference")
keyID = clictx.String("eab-key-id")

Check warning on line 168 in command/ca/policy/actions/policy.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/policy.go#L166-L168

Added lines #L166 - L168 were not covered by tests
updatedPolicy *linkedca.Policy
err error
)
Expand Down
13 changes: 8 additions & 5 deletions command/ca/policy/actions/principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
principalAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
cli.BoolFlag{
Name: "remove",
Usage: `removes the provided Principals from the policy instead of adding them`,
Expand All @@ -76,9 +76,12 @@
}

func principalAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 83 in command/ca/policy/actions/principals.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/principals.go#L79-L83

Added lines #L79 - L83 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -88,7 +91,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 94 in command/ca/policy/actions/principals.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/principals.go#L94

Added line #L94 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -120,7 +123,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 126 in command/ca/policy/actions/principals.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/principals.go#L126

Added line #L126 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
12 changes: 7 additions & 5 deletions command/ca/policy/actions/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
removeAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
flags.EABKeyID,
flags.EABReference,
flags.AdminCert,
Expand All @@ -71,10 +71,12 @@
}

func removeAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
provisioner := clictx.String("provisioner")
reference := clictx.String("eab-key-reference")
keyID := clictx.String("eab-key-id")
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
reference = clictx.String("eab-key-reference")
keyID = clictx.String("eab-key-id")
)

Check warning on line 79 in command/ca/policy/actions/remove.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/remove.go#L74-L79

Added lines #L74 - L79 were not covered by tests

client, err := cautils.NewAdminClient(clictx)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions command/ca/policy/actions/uris.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
uriAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
cli.BoolFlag{
Name: "remove",
Usage: `removes the provided URIs from the policy instead of adding them`,
Expand All @@ -71,9 +71,12 @@
}

func uriAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
args = clictx.Args()
)

Check warning on line 78 in command/ca/policy/actions/uris.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/uris.go#L74-L78

Added lines #L74 - L78 were not covered by tests

args := clictx.Args()
if len(args) == 0 {
return errs.TooFewArguments(clictx)
}
Expand All @@ -83,7 +86,7 @@
return fmt.Errorf("error creating admin client: %w", err)
}

policy, err := retrieveAndInitializePolicy(ctx, client)
policy, err := retrieveAndInitializePolicy(ctx, client, provisioner)

Check warning on line 89 in command/ca/policy/actions/uris.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/uris.go#L89

Added line #L89 was not covered by tests
if err != nil {
return fmt.Errorf("error retrieving policy: %w", err)
}
Expand All @@ -108,7 +111,7 @@
panic("no SSH nor X.509 context set")
}

updatedPolicy, err := updatePolicy(ctx, client, policy)
updatedPolicy, err := updatePolicy(ctx, client, policy, provisioner)

Check warning on line 114 in command/ca/policy/actions/uris.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/uris.go#L114

Added line #L114 was not covered by tests
if err != nil {
return fmt.Errorf("error updating policy: %w", err)
}
Expand Down
17 changes: 8 additions & 9 deletions command/ca/policy/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
viewAction,
),
Flags: []cli.Flag{
provisionerFilterFlag,
flags.Provisioner,
flags.EABKeyID,
flags.EABReference,
flags.AdminCert,
Expand All @@ -72,20 +72,19 @@
}

func viewAction(ctx context.Context) (err error) {
clictx := command.CLIContextFromContext(ctx)
provisioner := clictx.String("provisioner")
reference := clictx.String("eab-key-reference")
keyID := clictx.String("eab-key-id")
var (
provisioner = retrieveAndUnsetProvisionerFlagIfRequired(ctx)
clictx = command.CLIContextFromContext(ctx)
reference = clictx.String("eab-key-reference")
keyID = clictx.String("eab-key-id")
policy *linkedca.Policy
)

Check warning on line 81 in command/ca/policy/actions/view.go

View check run for this annotation

Codecov / codecov/patch

command/ca/policy/actions/view.go#L75-L81

Added lines #L75 - L81 were not covered by tests

client, err := cautils.NewAdminClient(clictx)
if err != nil {
return fmt.Errorf("error creating admin client: %w", err)
}

var (
policy *linkedca.Policy
)

switch {
case policycontext.IsAuthorityPolicyLevel(ctx):
policy, err = client.GetAuthorityPolicy()
Expand Down
Loading