secret.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package commands
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. "text/tabwriter"
  10. "github.com/docker/cli/cli/command"
  11. amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
  12. "github.com/docker/ecs-plugin/pkg/compose"
  13. "github.com/docker/ecs-plugin/pkg/docker"
  14. "github.com/spf13/cobra"
  15. )
  16. type createSecretOptions struct {
  17. Label string
  18. Username string
  19. Password string
  20. Description string
  21. }
  22. type deleteSecretOptions struct {
  23. recover bool
  24. }
  25. func SecretCommand(dockerCli command.Cli) *cobra.Command {
  26. cmd := &cobra.Command{
  27. Use: "secret",
  28. Short: "Manages secrets",
  29. }
  30. cmd.AddCommand(
  31. CreateSecret(dockerCli),
  32. InspectSecret(dockerCli),
  33. ListSecrets(dockerCli),
  34. DeleteSecret(dockerCli),
  35. )
  36. return cmd
  37. }
  38. func CreateSecret(dockerCli command.Cli) *cobra.Command {
  39. opts := createSecretOptions{}
  40. cmd := &cobra.Command{
  41. Use: "create NAME",
  42. Short: "Creates a secret.",
  43. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  44. if len(args) == 0 {
  45. return errors.New("Missing mandatory parameter: NAME")
  46. }
  47. name := args[0]
  48. secret := compose.NewSecret(name, opts.Username, opts.Password, opts.Description)
  49. id, err := backend.CreateSecret(context.Background(), secret)
  50. fmt.Println(id)
  51. return err
  52. }),
  53. }
  54. cmd.Flags().StringVarP(&opts.Username, "username", "u", "", "username")
  55. cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "password")
  56. cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Secret description")
  57. return cmd
  58. }
  59. func InspectSecret(dockerCli command.Cli) *cobra.Command {
  60. cmd := &cobra.Command{
  61. Use: "inspect ID",
  62. Short: "Displays secret details",
  63. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  64. if len(args) == 0 {
  65. return errors.New("Missing mandatory parameter: ID")
  66. }
  67. id := args[0]
  68. secret, err := backend.InspectSecret(context.Background(), id)
  69. if err != nil {
  70. return err
  71. }
  72. out, err := secret.ToJSON()
  73. if err != nil {
  74. return err
  75. }
  76. fmt.Println(out)
  77. return nil
  78. }),
  79. }
  80. return cmd
  81. }
  82. func ListSecrets(dockerCli command.Cli) *cobra.Command {
  83. cmd := &cobra.Command{
  84. Use: "list",
  85. Aliases: []string{"ls"},
  86. Short: "List secrets stored for the existing account.",
  87. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  88. secrets, err := backend.ListSecrets(context.Background())
  89. if err != nil {
  90. return err
  91. }
  92. printList(os.Stdout, secrets)
  93. return nil
  94. }),
  95. }
  96. return cmd
  97. }
  98. func DeleteSecret(dockerCli command.Cli) *cobra.Command {
  99. opts := deleteSecretOptions{}
  100. cmd := &cobra.Command{
  101. Use: "delete NAME",
  102. Aliases: []string{"rm", "remove"},
  103. Short: "Removes a secret.",
  104. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  105. if len(args) == 0 {
  106. return errors.New("Missing mandatory parameter: [NAME]")
  107. }
  108. return backend.DeleteSecret(context.Background(), args[0], opts.recover)
  109. }),
  110. }
  111. cmd.Flags().BoolVar(&opts.recover, "recover", false, "Enable recovery.")
  112. return cmd
  113. }
  114. func printList(out io.Writer, secrets []compose.Secret) {
  115. printSection(out, len(secrets), func(w io.Writer) {
  116. for _, secret := range secrets {
  117. fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description)
  118. }
  119. }, "ID", "NAME", "DESCRIPTION")
  120. }
  121. func printSection(out io.Writer, len int, printer func(io.Writer), headers ...string) {
  122. w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
  123. fmt.Fprintln(w, strings.Join(headers, "\t"))
  124. printer(w)
  125. w.Flush()
  126. }