secrets.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "strings"
  19. "text/tabwriter"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose-cli/client"
  22. "github.com/docker/compose-cli/secrets"
  23. )
  24. type createSecretOptions struct {
  25. Label string
  26. Username string
  27. Password string
  28. Description string
  29. }
  30. // SecretCommand manage secrets
  31. func SecretCommand() *cobra.Command {
  32. cmd := &cobra.Command{
  33. Use: "secret",
  34. Short: "Manages secrets",
  35. }
  36. cmd.AddCommand(
  37. createSecret(),
  38. inspectSecret(),
  39. listSecrets(),
  40. deleteSecret(),
  41. )
  42. return cmd
  43. }
  44. func createSecret() *cobra.Command {
  45. opts := createSecretOptions{}
  46. cmd := &cobra.Command{
  47. Use: "create NAME",
  48. Short: "Creates a secret.",
  49. Args: cobra.ExactArgs(1),
  50. RunE: func(cmd *cobra.Command, args []string) error {
  51. c, err := client.New(cmd.Context())
  52. if err != nil {
  53. return err
  54. }
  55. name := args[0]
  56. secret := secrets.NewSecret(name, opts.Username, opts.Password, opts.Description)
  57. id, err := c.SecretsService().CreateSecret(cmd.Context(), secret)
  58. if err != nil {
  59. return err
  60. }
  61. fmt.Println(id)
  62. return nil
  63. },
  64. }
  65. cmd.Flags().StringVarP(&opts.Username, "username", "u", "", "username")
  66. cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "password")
  67. cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Secret description")
  68. return cmd
  69. }
  70. func inspectSecret() *cobra.Command {
  71. cmd := &cobra.Command{
  72. Use: "inspect ID",
  73. Short: "Displays secret details",
  74. Args: cobra.ExactArgs(1),
  75. RunE: func(cmd *cobra.Command, args []string) error {
  76. c, err := client.New(cmd.Context())
  77. if err != nil {
  78. return err
  79. }
  80. secret, err := c.SecretsService().InspectSecret(cmd.Context(), args[0])
  81. if err != nil {
  82. return err
  83. }
  84. out, err := secret.ToJSON()
  85. if err != nil {
  86. return err
  87. }
  88. fmt.Println(out)
  89. return nil
  90. },
  91. }
  92. return cmd
  93. }
  94. func listSecrets() *cobra.Command {
  95. cmd := &cobra.Command{
  96. Use: "list",
  97. Aliases: []string{"ls"},
  98. Short: "List secrets stored for the existing account.",
  99. RunE: func(cmd *cobra.Command, args []string) error {
  100. c, err := client.New(cmd.Context())
  101. if err != nil {
  102. return err
  103. }
  104. list, err := c.SecretsService().ListSecrets(cmd.Context())
  105. if err != nil {
  106. return err
  107. }
  108. printList(os.Stdout, list)
  109. return nil
  110. },
  111. }
  112. return cmd
  113. }
  114. type deleteSecretOptions struct {
  115. recover bool
  116. }
  117. func deleteSecret() *cobra.Command {
  118. opts := deleteSecretOptions{}
  119. cmd := &cobra.Command{
  120. Use: "delete NAME",
  121. Aliases: []string{"rm", "remove"},
  122. Short: "Removes a secret.",
  123. Args: cobra.ExactArgs(1),
  124. RunE: func(cmd *cobra.Command, args []string) error {
  125. c, err := client.New(cmd.Context())
  126. if err != nil {
  127. return err
  128. }
  129. return c.SecretsService().DeleteSecret(cmd.Context(), args[0], opts.recover)
  130. },
  131. }
  132. cmd.Flags().BoolVar(&opts.recover, "recover", false, "Enable recovery.")
  133. return cmd
  134. }
  135. func printList(out io.Writer, secrets []secrets.Secret) {
  136. printSection(out, func(w io.Writer) {
  137. for _, secret := range secrets {
  138. fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description) // nolint:errcheck
  139. }
  140. }, "ID", "NAME", "DESCRIPTION")
  141. }
  142. func printSection(out io.Writer, printer func(io.Writer), headers ...string) {
  143. w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
  144. fmt.Fprintln(w, strings.Join(headers, "\t")) // nolint:errcheck
  145. printer(w)
  146. w.Flush() // nolint:errcheck
  147. }