secrets.go 4.3 KB

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