secrets.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "github.com/pkg/errors"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose-cli/api/client"
  22. "github.com/docker/compose-cli/api/secrets"
  23. "github.com/docker/compose-cli/errdefs"
  24. "github.com/docker/compose-cli/formatter"
  25. )
  26. type createSecretOptions struct {
  27. Label string
  28. Username string
  29. Password string
  30. Description string
  31. }
  32. // SecretCommand manage secrets
  33. func SecretCommand() *cobra.Command {
  34. cmd := &cobra.Command{
  35. Use: "secret",
  36. Short: "Manages secrets",
  37. }
  38. cmd.AddCommand(
  39. createSecret(),
  40. inspectSecret(),
  41. listSecrets(),
  42. deleteSecret(),
  43. )
  44. return cmd
  45. }
  46. func createSecret() *cobra.Command {
  47. opts := createSecretOptions{}
  48. cmd := &cobra.Command{
  49. Use: "create NAME",
  50. Short: "Creates a secret.",
  51. Args: cobra.ExactArgs(1),
  52. RunE: func(cmd *cobra.Command, args []string) error {
  53. c, err := client.New(cmd.Context())
  54. if err != nil {
  55. return err
  56. }
  57. name := args[0]
  58. secret := secrets.NewSecret(name, opts.Username, opts.Password, opts.Description)
  59. id, err := c.SecretsService().CreateSecret(cmd.Context(), secret)
  60. if err != nil {
  61. return err
  62. }
  63. fmt.Println(id)
  64. return nil
  65. },
  66. }
  67. cmd.Flags().StringVarP(&opts.Username, "username", "u", "", "username")
  68. cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "password")
  69. cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Secret description")
  70. return cmd
  71. }
  72. func inspectSecret() *cobra.Command {
  73. cmd := &cobra.Command{
  74. Use: "inspect ID",
  75. Short: "Displays secret details",
  76. Args: cobra.ExactArgs(1),
  77. RunE: func(cmd *cobra.Command, args []string) error {
  78. c, err := client.New(cmd.Context())
  79. if err != nil {
  80. return err
  81. }
  82. secret, err := c.SecretsService().InspectSecret(cmd.Context(), args[0])
  83. if err != nil {
  84. return err
  85. }
  86. out, err := secret.ToJSON()
  87. if err != nil {
  88. return err
  89. }
  90. fmt.Println(out)
  91. return nil
  92. },
  93. }
  94. return cmd
  95. }
  96. type listSecretsOpts struct {
  97. format string
  98. }
  99. func listSecrets() *cobra.Command {
  100. var opts listSecretsOpts
  101. cmd := &cobra.Command{
  102. Use: "list",
  103. Aliases: []string{"ls"},
  104. Short: "List secrets stored for the existing account.",
  105. RunE: func(cmd *cobra.Command, args []string) error {
  106. c, err := client.New(cmd.Context())
  107. if err != nil {
  108. return err
  109. }
  110. list, err := c.SecretsService().ListSecrets(cmd.Context())
  111. if err != nil {
  112. return err
  113. }
  114. return printSecretList(opts.format, os.Stdout, list)
  115. },
  116. }
  117. cmd.Flags().StringVar(&opts.format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
  118. return cmd
  119. }
  120. type deleteSecretOptions struct {
  121. recover bool
  122. }
  123. func deleteSecret() *cobra.Command {
  124. opts := deleteSecretOptions{}
  125. cmd := &cobra.Command{
  126. Use: "delete NAME",
  127. Aliases: []string{"rm", "remove"},
  128. Short: "Removes a secret.",
  129. Args: cobra.ExactArgs(1),
  130. RunE: func(cmd *cobra.Command, args []string) error {
  131. c, err := client.New(cmd.Context())
  132. if err != nil {
  133. return err
  134. }
  135. return c.SecretsService().DeleteSecret(cmd.Context(), args[0], opts.recover)
  136. },
  137. }
  138. cmd.Flags().BoolVar(&opts.recover, "recover", false, "Enable recovery.")
  139. return cmd
  140. }
  141. func printSecretList(format string, out io.Writer, secrets []secrets.Secret) error {
  142. var err error
  143. switch strings.ToLower(format) {
  144. case formatter.PRETTY, "":
  145. err = formatter.PrintPrettySection(out, func(w io.Writer) {
  146. for _, secret := range secrets {
  147. fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description) // nolint:errcheck
  148. }
  149. }, "ID", "NAME", "DESCRIPTION")
  150. case formatter.JSON:
  151. outJSON, err := formatter.ToStandardJSON(secrets)
  152. if err != nil {
  153. return err
  154. }
  155. _, _ = fmt.Fprint(out, outJSON)
  156. default:
  157. err = errors.Wrapf(errdefs.ErrParsingFailed, "format value %q could not be parsed", format)
  158. }
  159. return err
  160. }