secret.go 3.5 KB

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