export.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package commands
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "unicode"
  8. "github.com/pkg/errors"
  9. "github.com/urfave/cli"
  10. "google.golang.org/protobuf/encoding/protojson"
  11. "github.com/smallstep/cli-utils/command"
  12. "github.com/smallstep/cli-utils/errs"
  13. "github.com/smallstep/certificates/authority"
  14. "github.com/smallstep/certificates/authority/config"
  15. )
  16. func init() {
  17. command.Register(cli.Command{
  18. Name: "export",
  19. Usage: "export the current configuration of step-ca",
  20. UsageText: "**step-ca export** <config>",
  21. Action: exportAction,
  22. Description: `**step-ca export** exports the current configuration of step-ca.
  23. Note that neither the PKI password nor the certificate issuer password will be
  24. included in the export file.
  25. For linked CAs, use the **--token** flag to authenticate with the linked CA
  26. service and export provisioners and admins stored in the cloud.
  27. ## POSITIONAL ARGUMENTS
  28. <config>
  29. : The ca.json that contains the step-ca configuration.
  30. ## EXAMPLES
  31. Export the current configuration:
  32. '''
  33. $ step-ca export $(step path)/config/ca.json
  34. '''
  35. Export the configuration of a linked CA:
  36. '''
  37. $ step-ca export $(step path)/config/ca.json --token $STEP_CA_TOKEN
  38. '''`,
  39. Flags: []cli.Flag{
  40. cli.StringFlag{
  41. Name: "password-file",
  42. Usage: `path to the <file> containing the password to decrypt the
  43. intermediate private key.`,
  44. },
  45. cli.StringFlag{
  46. Name: "issuer-password-file",
  47. Usage: `path to the <file> containing the password to decrypt the
  48. certificate issuer private key used in the RA mode.`,
  49. },
  50. cli.StringFlag{
  51. Name: "token",
  52. Usage: "token used to enable the linked CA.",
  53. EnvVar: "STEP_CA_TOKEN",
  54. },
  55. },
  56. })
  57. }
  58. func exportAction(ctx *cli.Context) error {
  59. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  60. return err
  61. }
  62. configFile := ctx.Args().Get(0)
  63. passwordFile := ctx.String("password-file")
  64. issuerPasswordFile := ctx.String("issuer-password-file")
  65. token := ctx.String("token")
  66. cfg, err := config.LoadConfiguration(configFile)
  67. if err != nil {
  68. return err
  69. }
  70. if err := cfg.Validate(); err != nil {
  71. return err
  72. }
  73. if passwordFile != "" {
  74. b, err := os.ReadFile(passwordFile)
  75. if err != nil {
  76. return errors.Wrapf(err, "error reading %s", passwordFile)
  77. }
  78. cfg.Password = string(bytes.TrimRightFunc(b, unicode.IsSpace))
  79. }
  80. if issuerPasswordFile != "" {
  81. b, err := os.ReadFile(issuerPasswordFile)
  82. if err != nil {
  83. return errors.Wrapf(err, "error reading %s", issuerPasswordFile)
  84. }
  85. if cfg.AuthorityConfig.CertificateIssuer != nil {
  86. cfg.AuthorityConfig.CertificateIssuer.Password = string(bytes.TrimRightFunc(b, unicode.IsSpace))
  87. }
  88. }
  89. auth, err := authority.New(cfg, authority.WithLinkedCAToken(token))
  90. if err != nil {
  91. return err
  92. }
  93. export, err := auth.Export()
  94. if err != nil {
  95. return err
  96. }
  97. b, err := protojson.Marshal(export)
  98. if err != nil {
  99. return errors.Wrap(err, "error marshaling export")
  100. }
  101. var buf bytes.Buffer
  102. if err := json.Indent(&buf, b, "", "\t"); err != nil {
  103. return errors.Wrap(err, "error indenting export")
  104. }
  105. fmt.Println(buf.String())
  106. return nil
  107. }