api.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 secrets
  14. import (
  15. "context"
  16. "encoding/json"
  17. )
  18. // Service interacts with the underlying secrets backend
  19. type Service interface {
  20. CreateSecret(ctx context.Context, secret Secret) (string, error)
  21. InspectSecret(ctx context.Context, id string) (Secret, error)
  22. ListSecrets(ctx context.Context) ([]Secret, error)
  23. DeleteSecret(ctx context.Context, id string, recover bool) error
  24. }
  25. // Secret hold sensitive data
  26. type Secret struct {
  27. ID string `json:"ID"`
  28. Name string `json:"Name"`
  29. Labels map[string]string `json:"Labels"`
  30. Description string `json:"Description"`
  31. username string
  32. password string
  33. }
  34. // NewSecret builds a secret
  35. func NewSecret(name, username, password, description string) Secret {
  36. return Secret{
  37. Name: name,
  38. username: username,
  39. password: password,
  40. Description: description,
  41. }
  42. }
  43. // ToJSON marshall a Secret into JSON string
  44. func (s Secret) ToJSON() (string, error) {
  45. b, err := json.MarshalIndent(&s, "", "\t")
  46. if err != nil {
  47. return "", err
  48. }
  49. return string(b), nil
  50. }
  51. // GetCredString marshall a Secret's sensitive data into JSON string
  52. func (s Secret) GetCredString() (string, error) {
  53. creds := map[string]string{
  54. "username": s.username,
  55. "password": s.password,
  56. }
  57. b, err := json.Marshal(&creds)
  58. if err != nil {
  59. return "", err
  60. }
  61. return string(b), nil
  62. }