secret.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/amazon/types"
  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: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
  44. backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  45. if err != nil {
  46. return err
  47. }
  48. if len(args) == 0 {
  49. return errors.New("Missing mandatory parameter: NAME")
  50. }
  51. name := args[0]
  52. secret := types.NewSecret(name, opts.Username, opts.Password, opts.Description)
  53. id, err := backend.CreateSecret(context.Background(), secret)
  54. fmt.Println(id)
  55. return err
  56. }),
  57. }
  58. cmd.Flags().StringVarP(&opts.Username, "username", "u", "", "username")
  59. cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "password")
  60. cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Secret description")
  61. return cmd
  62. }
  63. func InspectSecret(dockerCli command.Cli) *cobra.Command {
  64. cmd := &cobra.Command{
  65. Use: "inspect ID",
  66. Short: "Displays secret details",
  67. RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
  68. backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  69. if err != nil {
  70. return err
  71. }
  72. if len(args) == 0 {
  73. return errors.New("Missing mandatory parameter: ID")
  74. }
  75. id := args[0]
  76. secret, err := backend.InspectSecret(context.Background(), id)
  77. if err != nil {
  78. return err
  79. }
  80. out, err := secret.ToJSON()
  81. if err != nil {
  82. return err
  83. }
  84. fmt.Println(out)
  85. return nil
  86. }),
  87. }
  88. return cmd
  89. }
  90. func ListSecrets(dockerCli command.Cli) *cobra.Command {
  91. cmd := &cobra.Command{
  92. Use: "list",
  93. Aliases: []string{"ls"},
  94. Short: "List secrets stored for the existing account.",
  95. RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
  96. backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  97. if err != nil {
  98. return err
  99. }
  100. secrets, err := backend.ListSecrets(context.Background())
  101. if err != nil {
  102. return err
  103. }
  104. printList(os.Stdout, secrets)
  105. return nil
  106. }),
  107. }
  108. return cmd
  109. }
  110. func DeleteSecret(dockerCli command.Cli) *cobra.Command {
  111. opts := deleteSecretOptions{}
  112. cmd := &cobra.Command{
  113. Use: "delete NAME",
  114. Aliases: []string{"rm", "remove"},
  115. Short: "Removes a secret.",
  116. RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
  117. backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  118. if err != nil {
  119. return err
  120. }
  121. if len(args) == 0 {
  122. return errors.New("Missing mandatory parameter: [NAME]")
  123. }
  124. return backend.DeleteSecret(context.Background(), args[0], opts.recover)
  125. }),
  126. }
  127. cmd.Flags().BoolVar(&opts.recover, "recover", false, "Enable recovery.")
  128. return cmd
  129. }
  130. func printList(out io.Writer, secrets []types.Secret) {
  131. printSection(out, len(secrets), func(w io.Writer) {
  132. for _, secret := range secrets {
  133. fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description)
  134. }
  135. }, "ID", "NAME", "DESCRIPTION")
  136. }
  137. func printSection(out io.Writer, len int, printer func(io.Writer), headers ...string) {
  138. w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
  139. fmt.Fprintln(w, strings.Join(headers, "\t"))
  140. printer(w)
  141. w.Flush()
  142. }