api.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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:"Tags"`
  30. content []byte
  31. }
  32. // NewSecret builds a secret
  33. func NewSecret(name string, content []byte) Secret {
  34. return Secret{
  35. Name: name,
  36. content: content,
  37. }
  38. }
  39. // ToJSON marshall a Secret into JSON string
  40. func (s Secret) ToJSON() (string, error) {
  41. b, err := json.MarshalIndent(&s, "", "\t")
  42. if err != nil {
  43. return "", err
  44. }
  45. return string(b), nil
  46. }
  47. // GetContent returns a Secret's sensitive data
  48. func (s Secret) GetContent() []byte {
  49. return s.content
  50. }