Debian Codegraph @

command/ca/provisioner/remove.go

raw symbols SCIP
1 package provisioner
2
3 import (
4 "strings"
5
6 "github.com/pkg/errors"
7 "github.com/smallstep/certificates/authority"
8 "github.com/smallstep/certificates/authority/provisioner"
9 "github.com/smallstep/cli/errs"
10 "github.com/smallstep/cli/ui"
11 "github.com/urfave/cli"
12 )
13
14 func removeCommand() cli.Command {
15 return cli.Command{
16 Name: "remove",
17 Action: cli.ActionFunc(removeAction),
18 Usage: "remove one, or more, provisioners from the CA configuration",
19 UsageText: `**step ca provisioner remove** <name>
20 [**--kid**=<kid>] [**--config**=<file>] [**--all**]`,
21 Flags: []cli.Flag{
22 cli.StringFlag{
23 Name: "ca-config",
24 Usage: "The <file> containing the CA configuration.",
25 },
26 cli.StringFlag{
27 Name: "kid",
28 Usage: "The <kid> (Key ID) of the JWK provisioner key to be removed.",
29 },
30 cli.StringFlag{
31 Name: "client-id",
32 Usage: "The <id> (Client ID) of the OIDC provisioner to be removed.",
33 },
34 cli.BoolFlag{
35 Name: "all",
36 Usage: `Remove all provisioners with a given name. Cannot be
37 used in combination w/ the **--kid** or **--client-id** flag.`,
38 },
39 cli.StringFlag{
40 Name: "type",
41 Usage: `The <type> of provisioner to remove. Type is a case-insensitive string
42 and must be one of:
43 **JWK**
44 : Uses an JWK key pair to sign provisioning tokens.
45
46 **OIDC**
47 : Uses an OpenID Connect provider to sign provisioning tokens.
48
49 **AWS**
50 : Uses Amazon AWS instance identity documents.
51
52 **GCP**
53 : Use Google instance identity tokens.
54
55 **Azure**
56 : Uses Microsoft Azure identity tokens.
57
58 **ACME**
59 : Uses ACME protocol.
60
61 **X5C**
62 : Uses an X509 Certificate / private key pair to sign provisioning tokens.
63
64 **K8sSA**
65 : Uses Kubernetes Service Account tokens.`,
66 },
67 },
68 Description: `**step ca provisioner remove** removes one or more provisioners
69 from the configuration and writes the new configuration back to the CA config.
70
71 To pick up the new configuration you must SIGHUP (kill -1 <pid>) or restart the
72 step-ca process.
73
74 ## POSITIONAL ARGUMENTS
75
76 <name>
77 : The name field of the provisioner(s) to be removed.
78
79 ## EXAMPLES
80
81 Remove all provisioners associated with a given name ([email protected]):
82 '''
83 $ step ca provisioner remove [email protected] --all --ca-config ca.json
84 '''
85
86 Remove the provisioner matching a given name and kid:
87 '''
88 $ step ca provisioner remove max@smallstep. --kid 1234 --ca-config ca.json
89 '''
90
91 Remove the provisioner matching a given name and a client id:
92 '''
93 $ step ca provisioner remove Google --ca-config ca.json \
94 --client-id 1087160488420-8qt7bavg3qesdhs6it824mhnfgcfe8il.apps.googleusercontent.com
95 '''
96
97 Remove the cloud identity provisioner given name and a type:
98 '''
99 $ step ca provisioner remove Amazon --ca-config ca.json --type AWS
100 '''
101
102 Remove the ACME provisioner by name:
103 '''
104 $ step ca provisioner remove my-acme-provisioner --type acme
105 '''
106
107 Remove an X5C provisioner by name:
108 '''
109 $ step ca provisioner remove my-x5c-provisioner --type x5c
110 '''
111
112 Remove a K8sSA provisioner by name:
113 '''
114 $ step ca provisioner remove k8sSA-default --type k8sSA
115 '''`,
116 }
117 }
118
119 func removeAction(ctx *cli.Context) error {
120 if err := errs.NumberOfArguments(ctx, 1); err != nil {
121 return err
122 }
123
124 name := ctx.Args().Get(0)
125 config := ctx.String("ca-config")
126 all := ctx.Bool("all")
127 kid := ctx.String("kid")
128 clientID := ctx.String("client-id")
129 typ := ctx.String("type")
130
131 if len(config) == 0 {
132 return errs.RequiredFlag(ctx, "ca-config")
133 }
134
135 if len(kid) > 0 && len(clientID) > 0 {
136 return errs.MutuallyExclusiveFlags(ctx, "kid", "client-id")
137 }
138
139 if all {
140 if len(kid) != 0 {
141 return errs.MutuallyExclusiveFlags(ctx, "all", "kid")
142 }
143 if len(clientID) != 0 {
144 return errs.MutuallyExclusiveFlags(ctx, "all", "client-id")
145 }
146 } else {
147 if len(kid) == 0 && len(clientID) == 0 && len(typ) == 0 {
148 return errs.RequiredOrFlag(ctx, "all", "kid", "client-id", "type")
149 }
150 }
151
152 c, err := authority.LoadConfiguration(config)
153 if err != nil {
154 return errors.Wrapf(err, "error loading configuration")
155 }
156
157 var (
158 provisioners provisioner.List
159 found = false
160 )
161 for _, p := range c.AuthorityConfig.Provisioners {
162 if p.GetName() != name || !isProvisionerType(p, typ) {
163 provisioners = append(provisioners, p)
164 continue
165 }
166 if !all {
167 switch pp := p.(type) {
168 case *provisioner.JWK:
169 if kid != "" && pp.Key.KeyID != kid {
170 provisioners = append(provisioners, p)
171 }
172 case *provisioner.OIDC:
173 if clientID != "" && pp.ClientID != clientID {
174 provisioners = append(provisioners, p)
175 }
176 case *provisioner.AWS, *provisioner.Azure, *provisioner.GCP,
177 *provisioner.ACME, *provisioner.X5C, *provisioner.K8sSA:
178 // they are filtered by type and name.
179 default:
180 continue
181 }
182 }
183 found = true
184 }
185
186 if !found {
187 switch {
188 case kid != "":
189 return errors.Errorf("no provisioners with name=%s and kid=%s found", name, kid)
190 case clientID != "":
191 return errors.Errorf("no provisioners with name=%s and client-id=%s found", name, clientID)
192 case typ != "":
193 return errors.Errorf("no provisioners with name=%s and type=%s found", name, typ)
194 default:
195 return errors.Errorf("no provisioners with name %s found", name)
196 }
197 }
198
199 c.AuthorityConfig.Provisioners = provisioners
200 if err = c.Save(config); err != nil {
201 return err
202 }
203
204 ui.Println("Success! Your `step-ca` config has been updated. To pick up the new configuration SIGHUP (kill -1 <pid>) or restart the step-ca process.")
205
206 return nil
207 }
208
209 // isProvisionerType returns true if p.GetType() is equal to typ. If typ is
210 // empty it will always return true.
211 func isProvisionerType(p provisioner.Interface, typ string) bool {
212 return typ == "" || strings.EqualFold(typ, p.GetType().String())
213 }