Skip to content

Commit 261d8bc

Browse files
committed
trust: add internal utility for checking DOCKER_CONTENT_TRUST
Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 1bae6aa) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 3755161 commit 261d8bc

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
@@ -96,7 +96,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
9696
addPlatformFlag(flags, &options.platform)
9797
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
9898

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

102102
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
@@ -10,6 +10,7 @@ import (
1010
"github.com/docker/cli/cli"
1111
"github.com/docker/cli/cli/command"
1212
"github.com/docker/cli/cli/command/completion"
13+
"github.com/docker/cli/cli/trust"
1314
"github.com/docker/cli/opts"
1415
"github.com/docker/docker/api/types/container"
1516
"github.com/moby/sys/signal"
@@ -74,7 +75,7 @@ func newRunCommand(dockerCLI command.Cli) *cobra.Command {
7475

7576
// TODO(thaJeztah): consider adding platform as "image create option" on containerOptions
7677
addPlatformFlag(flags, &options.platform)
77-
flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image verification")
78+
flags.BoolVar(&options.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image verification")
7879
copts = addFlags(flags)
7980

8081
_ = 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
@@ -56,7 +56,7 @@ func newPullCommand(dockerCLI command.Cli) *cobra.Command {
5656
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
5757

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

6161
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
6262

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 image.PullOptions) (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
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/cli/cli/command"
1616
"github.com/docker/cli/cli/command/completion"
1717
"github.com/docker/cli/cli/streams"
18+
"github.com/docker/cli/cli/trust"
1819
"github.com/docker/cli/internal/jsonstream"
1920
"github.com/docker/cli/internal/registry"
2021
"github.com/docker/cli/internal/tui"
@@ -64,7 +65,7 @@ func newPushCommand(dockerCLI command.Cli) *cobra.Command {
6465
flags := cmd.Flags()
6566
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Push all tags of an image to the repository")
6667
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
67-
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image signing")
68+
flags.BoolVar(&opts.untrusted, "disable-content-trust", !trust.Enabled(), "Skip image signing")
6869

6970
// Don't default to DOCKER_DEFAULT_PLATFORM env variable, always default to
7071
// 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
@@ -15,7 +15,7 @@ import (
1515
)
1616

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

cli/trust/trust.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13+
"strconv"
1314
"time"
1415

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

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

internal/test/cli.go

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

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

0 commit comments

Comments
 (0)