Skip to content

Commit 846ecf5

Browse files
committed
login: add oauth escape hatch
Signed-off-by: Laura Brehm <[email protected]>
1 parent bbce5a0 commit 846ecf5

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

‎cli/command/registry/login.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"os"
8+
"strconv"
79
"strings"
810

911
"github.com/docker/cli/cli"
@@ -141,9 +143,22 @@ func loginWithStoredCredentials(ctx context.Context, dockerCli command.Cli, auth
141143
return &response, err
142144
}
143145

146+
const OauthLoginEscapeHatchEnvVar = "DOCKER_CLI_DISABLE_OAUTH_LOGIN"
147+
148+
func isOauthLoginDisabled() bool {
149+
if v := os.Getenv(OauthLoginEscapeHatchEnvVar); v != "" {
150+
enabled, err := strconv.ParseBool(v)
151+
if err != nil {
152+
return false
153+
}
154+
return enabled
155+
}
156+
return false
157+
}
158+
144159
func loginUser(ctx context.Context, dockerCli command.Cli, opts loginOptions, defaultUsername, serverAddress string) (*registrytypes.AuthenticateOKBody, error) {
145160
// If we're logging into the index server and the user didn't provide a username or password, use the device flow
146-
if serverAddress == registry.IndexServer && opts.user == "" && opts.password == "" {
161+
if serverAddress == registry.IndexServer && opts.user == "" && opts.password == "" && !isOauthLoginDisabled() {
147162
response, err := loginWithDeviceCodeFlow(ctx, dockerCli)
148163
// if the error represents a failure to initiate the device-code flow,
149164
// then we fallback to regular cli credentials login

‎cli/command/registry/login_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,47 @@ func TestLoginTermination(t *testing.T) {
228228
assert.ErrorIs(t, err, command.ErrPromptTerminated)
229229
}
230230
}
231+
232+
func TestIsOauthLoginDisabled(t *testing.T) {
233+
testCases := []struct {
234+
envVar string
235+
disabled bool
236+
}{
237+
{
238+
envVar: "",
239+
disabled: false,
240+
},
241+
{
242+
envVar: "bork",
243+
disabled: false,
244+
},
245+
{
246+
envVar: "0",
247+
disabled: false,
248+
},
249+
{
250+
envVar: "false",
251+
disabled: false,
252+
},
253+
{
254+
envVar: "true",
255+
disabled: true,
256+
},
257+
{
258+
envVar: "TRUE",
259+
disabled: true,
260+
},
261+
{
262+
envVar: "1",
263+
disabled: true,
264+
},
265+
}
266+
267+
for _, tc := range testCases {
268+
t.Setenv(OauthLoginEscapeHatchEnvVar, tc.envVar)
269+
270+
disabled := isOauthLoginDisabled()
271+
272+
assert.Equal(t, disabled, tc.disabled)
273+
}
274+
}

0 commit comments

Comments
 (0)