Skip to content

Commit 064866f

Browse files
committed
Refactor ignoring of provisioner flag to not rely on package globals
1 parent 9e63618 commit 064866f

File tree

18 files changed

+371
-20
lines changed

18 files changed

+371
-20
lines changed

cmd/step/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/smallstep/cli/command/version"
2424
"github.com/smallstep/cli/internal/plugin"
25+
"github.com/smallstep/cli/internal/provisionerflag"
2526
"github.com/smallstep/cli/utils"
2627

2728
// Enabled cas interfaces.
@@ -126,11 +127,17 @@ func newApp(stdout, stderr io.Writer) *cli.App {
126127
app.Copyright = fmt.Sprintf("(c) 2018-%d Smallstep Labs, Inc.", time.Now().Year())
127128

128129
// Flag of custom configuration flag
129-
app.Flags = append(app.Flags, cli.StringFlag{
130+
app.Flags = append(app.Flags, cli.StringFlag{ //nolint:gocritic // intentionally split for documentation
130131
Name: "config",
131132
Usage: "path to the config file to use for CLI flags",
132133
})
133134

135+
// add a hidden flag that can be used to signal that the provisioner
136+
// flag should be ignored in certain commands. By defining it on the
137+
// app level it can be ignored in multiple (sub)commands without having
138+
// to specify the flag in each command.
139+
app.Flags = append(app.Flags, provisionerflag.DisabledSentinelFlag)
140+
134141
// Action runs on `step` or `step <command>` if the command is not enabled.
135142
app.Action = func(ctx *cli.Context) error {
136143
args := ctx.Args()

cmd/step/main_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ package main
33
import (
44
"bytes"
55
"regexp"
6+
"slices"
67
"testing"
78

89
"github.com/stretchr/testify/require"
10+
"github.com/urfave/cli"
11+
12+
"github.com/smallstep/cli/internal/provisionerflag"
913
)
1014

1115
func TestAppHasAllCommands(t *testing.T) {
@@ -44,3 +48,15 @@ func TestAppRuns(t *testing.T) {
4448
output := ansiRegex.ReplaceAllString(stdout.String(), "")
4549
require.Contains(t, output, "step -- plumbing for distributed systems")
4650
}
51+
52+
func TestAppHasSentinelFlagForIgnoringProvisionersFlag(t *testing.T) {
53+
app := newApp(nil, nil)
54+
require.NotNil(t, app)
55+
56+
// this test only checks if the flag is present when an app is created
57+
// through [getApp]. This is sufficient for now to proof that the flag
58+
// exists in the actual released CLI binary.
59+
require.True(t, slices.ContainsFunc(app.Flags, func(f cli.Flag) bool {
60+
return f.GetName() == provisionerflag.DisabledSentinelFlagName()
61+
}))
62+
}

command/ca/policy/acme/acme.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/smallstep/cli/command/ca/policy/actions"
99
"github.com/smallstep/cli/command/ca/policy/policycontext"
1010
"github.com/smallstep/cli/command/ca/policy/x509"
11-
"github.com/smallstep/cli/internal/provisionerflag"
1211
)
1312

1413
// Command returns the ACME account policy subcommand.
@@ -28,9 +27,5 @@ Please note that certificate issuance policies for ACME accounts are currently o
2827
actions.RemoveCommand(ctx),
2928
x509.Command(ctx),
3029
},
31-
Before: func(ctx *cli.Context) error {
32-
provisionerflag.Ignore()
33-
return nil
34-
},
3530
}
3631
}

command/ca/policy/actions/cn.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ $ step ca policy authority x509 deny cn "My Bad CA Name"
7676
}
7777

7878
func commonNamesAction(ctx context.Context) (err error) {
79+
ignoreProvisionerFlagIfRequired(ctx)
80+
7981
clictx := command.CLIContextFromContext(ctx)
8082

8183
args := clictx.Args()

command/ca/policy/actions/dns.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ $ step ca policy authority ssh host allow dns "badsshhost.local"
9494
}
9595

9696
func dnsAction(ctx context.Context) (err error) {
97+
ignoreProvisionerFlagIfRequired(ctx)
98+
9799
clictx := command.CLIContextFromContext(ctx)
98100

99101
args := clictx.Args()

command/ca/policy/actions/emails.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ $ step ca policy provisioner ssh user deny email @example.com --provisioner my_p
8181
}
8282

8383
func emailAction(ctx context.Context) (err error) {
84+
ignoreProvisionerFlagIfRequired(ctx)
85+
8486
clictx := command.CLIContextFromContext(ctx)
8587

8688
args := clictx.Args()

command/ca/policy/actions/ips.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ $ step ca policy authority ssh host deny ip 192.168.0.40
114114
}
115115

116116
func ipAction(ctx context.Context) (err error) {
117+
ignoreProvisionerFlagIfRequired(ctx)
118+
117119
clictx := command.CLIContextFromContext(ctx)
118120

119121
args := clictx.Args()

command/ca/policy/actions/policy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@ import (
1616

1717
"github.com/smallstep/cli/command/ca/policy/policycontext"
1818
"github.com/smallstep/cli/internal/command"
19+
"github.com/smallstep/cli/internal/provisionerflag"
1920
)
2021

2122
var provisionerFilterFlag = cli.StringFlag{
2223
Name: "provisioner",
2324
Usage: `The provisioner <name>`,
2425
}
2526

27+
// ignoreProvisionerFlagIfRequired is a helper function that marks the provisioner
28+
// flag to be ignored when managing a provisioner or ACME account level policy. In
29+
// those cases the provisioner flag is used to filter which provisioner the policy
30+
// applies to, as opposed to its normal usage, where it can be used to select the
31+
// (admin) provisioner to use for authentication.
32+
func ignoreProvisionerFlagIfRequired(ctx context.Context) {
33+
clictx := command.CLIContextFromContext(ctx)
34+
if policycontext.IsProvisionerPolicyLevel(ctx) || policycontext.IsACMEPolicyLevel(ctx) {
35+
provisionerflag.Ignore(clictx)
36+
}
37+
}
38+
2639
func retrieveAndInitializePolicy(ctx context.Context, client *ca.AdminClient) (*linkedca.Policy, error) {
2740
var (
2841
policy *linkedca.Policy

command/ca/policy/actions/principals.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ $ step ca policy provisioner ssh host deny principal root --provisioner my_ssh_u
7676
}
7777

7878
func principalAction(ctx context.Context) (err error) {
79+
ignoreProvisionerFlagIfRequired(ctx)
80+
7981
clictx := command.CLIContextFromContext(ctx)
8082

8183
args := clictx.Args()

command/ca/policy/actions/remove.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ $ step ca policy acme remove --provisioner my_acme_provisioner --eab-key-id "lUO
7171
}
7272

7373
func removeAction(ctx context.Context) (err error) {
74+
ignoreProvisionerFlagIfRequired(ctx)
75+
7476
clictx := command.CLIContextFromContext(ctx)
7577
provisioner := clictx.String("provisioner")
7678
reference := clictx.String("eab-key-reference")

0 commit comments

Comments
 (0)