Skip to content

Commit 7625c06

Browse files
committed
trust: add internal utility for checking DOCKER_CONTENT_TRUST
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 30df782 commit 7625c06

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
9090
addPlatformFlag(flags, &options.platform)
9191
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms())
9292

93-
flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image verification")
93+
flags.BoolVar(&options.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image verification")
9494
copts = addFlags(flags)
9595

9696
addCompletions(cmd, dockerCLI)

cli/command/container/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/cli/cli"
1212
"github.com/docker/cli/cli/command"
1313
"github.com/docker/cli/cli/command/completion"
14+
"github.com/docker/cli/cli/trust"
1415
"github.com/docker/cli/opts"
1516
"github.com/moby/moby/api/types/container"
1617
"github.com/moby/moby/client"
@@ -70,7 +71,7 @@ func newRunCommand(dockerCLI command.Cli) *cobra.Command {
7071

7172
// TODO(thaJeztah): consider adding platform as "image create option" on containerOptions
7273
addPlatformFlag(flags, &options.platform)
73-
flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image verification")
74+
flags.BoolVar(&options.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image verification")
7475
copts = addFlags(flags)
7576

7677
_ = cmd.RegisterFlagCompletionFunc("detach-keys", completeDetachKeys)

cli/command/image/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newPullCommand(dockerCLI command.Cli) *cobra.Command {
5050
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
5151

5252
addPlatformFlag(flags, &opts.platform)
53-
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image verification")
53+
flags.BoolVar(&opts.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image verification")
5454

5555
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms())
5656

cli/command/image/push.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/cli/cli/command"
1717
"github.com/docker/cli/cli/command/completion"
1818
"github.com/docker/cli/cli/streams"
19+
"github.com/docker/cli/cli/trust"
1920
"github.com/docker/cli/internal/jsonstream"
2021
"github.com/docker/cli/internal/registry"
2122
"github.com/docker/cli/internal/tui"
@@ -58,7 +59,7 @@ func newPushCommand(dockerCLI command.Cli) *cobra.Command {
5859
flags := cmd.Flags()
5960
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Push all tags of an image to the repository")
6061
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
61-
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image signing")
62+
flags.BoolVar(&opts.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image signing")
6263

6364
// Don't default to DOCKER_DEFAULT_PLATFORM env variable, always default to
6465
// pushing the image as-is. This also avoids forcing the platform selection

cli/command/service/trust.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm.ServiceSpec) error {
19-
if !dockerCli.ContentTrustEnabled() {
19+
if !trust.Enabled() {
2020
// When not using content trust, digest resolution happens later when
2121
// contacting the registry to retrieve image information.
2222
return nil

cli/trust/trust.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path"
1414
"path/filepath"
15+
"strconv"
1516
"time"
1617

1718
"github.com/distribution/reference"
@@ -43,6 +44,20 @@ var (
4344
ActionsPushAndPull = []string{"pull", "push"}
4445
)
4546

47+
// Enabled returns whether content-trust is enabled through the DOCKER_CONTENT_TRUST env-var.
48+
//
49+
// IMPORTANT: this function is for internal use, and may be removed at any moment.
50+
func Enabled() bool {
51+
var enabled bool
52+
if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
53+
if t, err := strconv.ParseBool(e); t || err != nil {
54+
// treat any other value as true
55+
enabled = true
56+
}
57+
}
58+
return enabled
59+
}
60+
4661
// NotaryServer is the endpoint serving the Notary trust server
4762
const NotaryServer = "https://notary.docker.io"
4863

0 commit comments

Comments
 (0)