| 1 | package provisioner |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/pkg/errors" |
| 8 | "github.com/smallstep/certificates/pki" |
| 9 | "github.com/smallstep/cli/errs" |
| 10 | "github.com/smallstep/cli/flags" |
| 11 | "github.com/urfave/cli" |
| 12 | ) |
| 13 | |
| 14 | func listCommand() cli.Command { |
| 15 | return cli.Command{ |
| 16 | Name: "list", |
| 17 | Action: cli.ActionFunc(listAction), |
| 18 | Usage: "list provisioners configured in the CA", |
| 19 | UsageText: `**step ca provisioner list** |
| 20 | [**--ca-url**=<uri>] [**--root**=<file>]`, |
| 21 | Flags: []cli.Flag{ |
| 22 | cli.StringFlag{ |
| 23 | Name: "ca-url", |
| 24 | Usage: "<URI> of the targeted Step Certificate Authority.", |
| 25 | }, |
| 26 | cli.StringFlag{ |
| 27 | Name: "root", |
| 28 | Usage: "The path to the PEM <file> used as the root certificate authority.", |
| 29 | }, |
| 30 | }, |
| 31 | Description: `**step ca provisioner list** lists the provisioners configured |
| 32 | in the CA. |
| 33 | |
| 34 | ## EXAMPLES |
| 35 | |
| 36 | Prints a JSON list with active provisioners: |
| 37 | ''' |
| 38 | $ step ca provisioner list |
| 39 | '''`, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func listAction(ctx *cli.Context) error { |
| 44 | if err := errs.NumberOfArguments(ctx, 0); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | root := ctx.String("root") |
| 49 | caURL, err := flags.ParseCaURL(ctx) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | provisioners, err := pki.GetProvisioners(caURL, root) |
| 55 | if err != nil { |
| 56 | return errors.Wrap(err, "error getting the provisioners") |
| 57 | } |
| 58 | |
| 59 | b, err := json.MarshalIndent(provisioners, "", " ") |
| 60 | if err != nil { |
| 61 | return errors.Wrap(err, "error marshaling provisioners") |
| 62 | } |
| 63 | |
| 64 | fmt.Println(string(b)) |
| 65 | return nil |
| 66 | } |