4
0

export.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. ## POSITIONAL ARGUMENTS
  26. <config>
  27. : The ca.json that contains the step-ca configuration.
  28. ## EXAMPLES
  29. Export the current configuration:
  30. '''
  31. $ step-ca export $(step path)/config/ca.json
  32. '''`,
  33. Flags: []cli.Flag{
  34. cli.StringFlag{
  35. Name: "password-file",
  36. Usage: `path to the <file> containing the password to decrypt the
  37. intermediate private key.`,
  38. },
  39. cli.StringFlag{
  40. Name: "issuer-password-file",
  41. Usage: `path to the <file> containing the password to decrypt the
  42. certificate issuer private key used in the RA mode.`,
  43. },
  44. },
  45. })
  46. }
  47. func exportAction(ctx *cli.Context) error {
  48. if err := errs.NumberOfArguments(ctx, 1); err != nil {
  49. return err
  50. }
  51. configFile := ctx.Args().Get(0)
  52. passwordFile := ctx.String("password-file")
  53. issuerPasswordFile := ctx.String("issuer-password-file")
  54. cfg, err := config.LoadConfiguration(configFile)
  55. if err != nil {
  56. return err
  57. }
  58. if err := cfg.Validate(); err != nil {
  59. return err
  60. }
  61. if passwordFile != "" {
  62. b, err := os.ReadFile(passwordFile)
  63. if err != nil {
  64. return errors.Wrapf(err, "error reading %s", passwordFile)
  65. }
  66. cfg.Password = string(bytes.TrimRightFunc(b, unicode.IsSpace))
  67. }
  68. if issuerPasswordFile != "" {
  69. b, err := os.ReadFile(issuerPasswordFile)
  70. if err != nil {
  71. return errors.Wrapf(err, "error reading %s", issuerPasswordFile)
  72. }
  73. if cfg.AuthorityConfig.CertificateIssuer != nil {
  74. cfg.AuthorityConfig.CertificateIssuer.Password = string(bytes.TrimRightFunc(b, unicode.IsSpace))
  75. }
  76. }
  77. auth, err := authority.New(cfg)
  78. if err != nil {
  79. return err
  80. }
  81. export, err := auth.Export()
  82. if err != nil {
  83. return err
  84. }
  85. b, err := protojson.Marshal(export)
  86. if err != nil {
  87. return errors.Wrap(err, "error marshaling export")
  88. }
  89. var buf bytes.Buffer
  90. if err := json.Indent(&buf, b, "", "\t"); err != nil {
  91. return errors.Wrap(err, "error indenting export")
  92. }
  93. fmt.Println(buf.String())
  94. return nil
  95. }