secret.go 4.0 KB

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