trust.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package command
  2. import (
  3. "os"
  4. "strconv"
  5. "github.com/spf13/pflag"
  6. )
  7. var (
  8. // TODO: make this not global
  9. untrusted bool
  10. )
  11. // AddTrustVerificationFlags adds content trust flags to the provided flagset
  12. func AddTrustVerificationFlags(fs *pflag.FlagSet) {
  13. trusted := getDefaultTrustState()
  14. fs.BoolVar(&untrusted, "disable-content-trust", !trusted, "Skip image verification")
  15. }
  16. // AddTrustSigningFlags adds "signing" flags to the provided flagset
  17. func AddTrustSigningFlags(fs *pflag.FlagSet) {
  18. trusted := getDefaultTrustState()
  19. fs.BoolVar(&untrusted, "disable-content-trust", !trusted, "Skip image signing")
  20. }
  21. // getDefaultTrustState returns true if content trust is enabled through the $DOCKER_CONTENT_TRUST environment variable.
  22. func getDefaultTrustState() bool {
  23. var trusted bool
  24. if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
  25. if t, err := strconv.ParseBool(e); t || err != nil {
  26. // treat any other value as true
  27. trusted = true
  28. }
  29. }
  30. return trusted
  31. }
  32. // IsTrusted returns true if content trust is enabled, either through the $DOCKER_CONTENT_TRUST environment variable,
  33. // or through `--disabled-content-trust=false` on a command.
  34. func IsTrusted() bool {
  35. return !untrusted
  36. }