Skip to content

Commit 1bae6aa

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

File tree

10 files changed

+28
-19
lines changed

10 files changed

+28
-19
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/create_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
249249
}
250250
for _, tc := range testCases {
251251
t.Run(tc.name, func(t *testing.T) {
252+
t.Setenv("DOCKER_CONTENT_TRUST", "true")
252253
fakeCLI := test.NewFakeCli(&fakeClient{
253254
createContainerFunc: func(config *container.Config,
254255
hostConfig *container.HostConfig,
@@ -258,7 +259,7 @@ func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
258259
) (container.CreateResponse, error) {
259260
return container.CreateResponse{}, errors.New("shouldn't try to pull image")
260261
},
261-
}, test.EnableContentTrust)
262+
})
262263
fakeCLI.SetNotaryClient(tc.notaryFunc)
263264
cmd := newCreateCommand(fakeCLI)
264265
cmd.SetOut(io.Discard)

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/container/run_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func TestRunCommandWithContentTrustErrors(t *testing.T) {
323323
}
324324
for _, tc := range testCases {
325325
t.Run(tc.name, func(t *testing.T) {
326+
t.Setenv("DOCKER_CONTENT_TRUST", "true")
326327
fakeCLI := test.NewFakeCli(&fakeClient{
327328
createContainerFunc: func(config *container.Config,
328329
hostConfig *container.HostConfig,
@@ -332,7 +333,7 @@ func TestRunCommandWithContentTrustErrors(t *testing.T) {
332333
) (container.CreateResponse, error) {
333334
return container.CreateResponse{}, errors.New("shouldn't try to pull image")
334335
},
335-
}, test.EnableContentTrust)
336+
})
336337
fakeCLI.SetNotaryClient(tc.notaryFunc)
337338
cmd := newRunCommand(fakeCLI)
338339
cmd.SetArgs(tc.args)

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/pull_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
118118
}
119119
for _, tc := range testCases {
120120
t.Run(tc.name, func(t *testing.T) {
121+
t.Setenv("DOCKER_CONTENT_TRUST", "true")
121122
cli := test.NewFakeCli(&fakeClient{
122123
imagePullFunc: func(ref string, options client.ImagePullOptions) (io.ReadCloser, error) {
123124
return io.NopCloser(strings.NewReader("")), errors.New("shouldn't try to pull image")
124125
},
125-
}, test.EnableContentTrust)
126+
})
126127
cli.SetNotaryClient(tc.notaryFunc)
127128
cmd := newPullCommand(cli)
128129
cmd.SetOut(io.Discard)

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

internal/test/cli.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ type FakeCli struct {
3535
notaryClientFunc NotaryClientFuncType
3636
manifestStore manifeststore.Store
3737
registryClient registryclient.RegistryClient
38-
contentTrust bool
3938
contextStore store.Store
4039
currentContext string
4140
dockerEndpoint docker.Endpoint
@@ -197,16 +196,6 @@ func (c *FakeCli) SetRegistryClient(registryClient registryclient.RegistryClient
197196
c.registryClient = registryClient
198197
}
199198

200-
// ContentTrustEnabled on the fake cli
201-
func (c *FakeCli) ContentTrustEnabled() bool {
202-
return c.contentTrust
203-
}
204-
205-
// EnableContentTrust on the fake cli
206-
func EnableContentTrust(c *FakeCli) {
207-
c.contentTrust = true
208-
}
209-
210199
// BuildKitEnabled on the fake cli
211200
func (*FakeCli) BuildKitEnabled() (bool, error) {
212201
return true, nil

0 commit comments

Comments
 (0)