Skip to content

Commit 19b71a1

Browse files
committed
Add DockerCompatAuthFile options to ...Options, and --compat-auth-file
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
1 parent de06ef8 commit 19b71a1

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

pkg/auth/auth.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,25 @@ func CheckAuthFile(pathOption string) error {
7575
}
7676

7777
// systemContextWithOptions returns a version of sys
78-
// updated with authFile and certDir values (if they are not "").
78+
// updated with authFile, dockerCompatAuthFile and certDir values (if they are not "").
7979
// NOTE: this is a shallow copy that can be used and updated, but may share
8080
// data with the original parameter.
81-
func systemContextWithOptions(sys *types.SystemContext, authFile, certDir string) *types.SystemContext {
81+
func systemContextWithOptions(sys *types.SystemContext, authFile, dockerCompatAuthFile, certDir string) (*types.SystemContext, error) {
8282
if sys != nil {
8383
sysCopy := *sys
8484
sys = &sysCopy
8585
} else {
8686
sys = &types.SystemContext{}
8787
}
8888

89-
if authFile != "" {
89+
switch {
90+
case authFile != "" && dockerCompatAuthFile != "":
91+
return nil, errors.New("options for paths to the credential file and to the Docker-compatible credential file can not be set simultaneously")
92+
case authFile != "":
9093
sys.AuthFilePath = authFile
91-
} else {
94+
case dockerCompatAuthFile != "":
95+
sys.DockerCompatAuthFilePath = dockerCompatAuthFile
96+
default:
9297
// Keep this in sync with GetDefaultAuthFile()!
9398
//
9499
// Note that c/image does not natively implement the REGISTRY_AUTH_FILE
@@ -100,24 +105,24 @@ func systemContextWithOptions(sys *types.SystemContext, authFile, certDir string
100105
// If the Docker configuration exists in the default ~/.docker/config.json location,
101106
// we DO NOT write to it; instead, we update auth.json in the default path.
102107
// Only if the user explicitly sets DOCKER_CONFIG, we write to that config.json.
103-
sys.AuthFilePath = filepath.Join(dockerConfig, "config.json")
108+
sys.DockerCompatAuthFilePath = filepath.Join(dockerConfig, "config.json")
104109
}
105110
}
106111
if certDir != "" {
107112
sys.DockerCertPath = certDir
108113
}
109-
return sys
114+
return sys, nil
110115
}
111116

112117
// Login implements a “log in” command with the provided opts and args
113118
// reading the password from opts.Stdin or the options in opts.
114119
func Login(ctx context.Context, systemContext *types.SystemContext, opts *LoginOptions, args []string) error {
115-
systemContext = systemContextWithOptions(systemContext, opts.AuthFile, opts.CertDir)
120+
systemContext, err := systemContextWithOptions(systemContext, opts.AuthFile, opts.DockerCompatAuthFile, opts.CertDir)
121+
if err != nil {
122+
return err
123+
}
116124

117-
var (
118-
key, registry string
119-
err error
120-
)
125+
var key, registry string
121126
switch len(args) {
122127
case 0:
123128
if !opts.AcceptUnspecifiedRegistry {
@@ -311,7 +316,13 @@ func Logout(systemContext *types.SystemContext, opts *LogoutOptions, args []stri
311316
if err := CheckAuthFile(opts.AuthFile); err != nil {
312317
return err
313318
}
314-
systemContext = systemContextWithOptions(systemContext, opts.AuthFile, "")
319+
if err := CheckAuthFile(opts.DockerCompatAuthFile); err != nil {
320+
return err
321+
}
322+
systemContext, err := systemContextWithOptions(systemContext, opts.AuthFile, opts.DockerCompatAuthFile, "")
323+
if err != nil {
324+
return err
325+
}
315326

316327
if opts.All {
317328
if len(args) != 0 {
@@ -324,10 +335,7 @@ func Logout(systemContext *types.SystemContext, opts *LogoutOptions, args []stri
324335
return nil
325336
}
326337

327-
var (
328-
key, registry string
329-
err error
330-
)
338+
var key, registry string
331339
switch len(args) {
332340
case 0:
333341
if !opts.AcceptUnspecifiedRegistry {

pkg/auth/cli.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ type LoginOptions struct {
1414
// CLI flags managed by the FlagSet returned by GetLoginFlags
1515
// Callers that use GetLoginFlags should not need to touch these values at all; callers that use
1616
// other CLI frameworks should set them based on user input.
17-
AuthFile string
18-
CertDir string
19-
Password string
20-
Username string
21-
StdinPassword bool
22-
GetLoginSet bool
23-
Verbose bool // set to true for verbose output
24-
AcceptRepositories bool // set to true to allow namespaces or repositories rather than just registries
17+
AuthFile string
18+
DockerCompatAuthFile string
19+
CertDir string
20+
Password string
21+
Username string
22+
StdinPassword bool
23+
GetLoginSet bool
24+
Verbose bool // set to true for verbose output
25+
AcceptRepositories bool // set to true to allow namespaces or repositories rather than just registries
2526
// Options caller can set
2627
Stdin io.Reader // set to os.Stdin
2728
Stdout io.Writer // set to os.Stdout
@@ -34,9 +35,10 @@ type LogoutOptions struct {
3435
// CLI flags managed by the FlagSet returned by GetLogoutFlags
3536
// Callers that use GetLogoutFlags should not need to touch these values at all; callers that use
3637
// other CLI frameworks should set them based on user input.
37-
AuthFile string
38-
All bool
39-
AcceptRepositories bool // set to true to allow namespaces or repositories rather than just registries
38+
AuthFile string
39+
DockerCompatAuthFile string
40+
All bool
41+
AcceptRepositories bool // set to true to allow namespaces or repositories rather than just registries
4042
// Options caller can set
4143
Stdout io.Writer // set to os.Stdout
4244
AcceptUnspecifiedRegistry bool // set to true if allows logout with unspecified registry
@@ -46,6 +48,7 @@ type LogoutOptions struct {
4648
func GetLoginFlags(flags *LoginOptions) *pflag.FlagSet {
4749
fs := pflag.FlagSet{}
4850
fs.StringVar(&flags.AuthFile, "authfile", "", "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
51+
fs.StringVar(&flags.DockerCompatAuthFile, "compat-auth-file", "", "path of a Docker-compatible config file to update instead")
4952
fs.StringVar(&flags.CertDir, "cert-dir", "", "use certificates at the specified path to access the registry")
5053
fs.StringVarP(&flags.Password, "password", "p", "", "Password for registry")
5154
fs.StringVarP(&flags.Username, "username", "u", "", "Username for registry")
@@ -59,6 +62,7 @@ func GetLoginFlags(flags *LoginOptions) *pflag.FlagSet {
5962
func GetLoginFlagsCompletions() completion.FlagCompletions {
6063
flagCompletion := completion.FlagCompletions{}
6164
flagCompletion["authfile"] = completion.AutocompleteDefault
65+
flagCompletion["compat-auth-file"] = completion.AutocompleteDefault
6266
flagCompletion["cert-dir"] = completion.AutocompleteDefault
6367
flagCompletion["password"] = completion.AutocompleteNone
6468
flagCompletion["username"] = completion.AutocompleteNone
@@ -69,6 +73,7 @@ func GetLoginFlagsCompletions() completion.FlagCompletions {
6973
func GetLogoutFlags(flags *LogoutOptions) *pflag.FlagSet {
7074
fs := pflag.FlagSet{}
7175
fs.StringVar(&flags.AuthFile, "authfile", "", "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
76+
fs.StringVar(&flags.DockerCompatAuthFile, "compat-auth-file", "", "path of a Docker-compatible config file to update instead")
7277
fs.BoolVarP(&flags.All, "all", "a", false, "Remove the cached credentials for all registries in the auth file")
7378
return &fs
7479
}
@@ -77,5 +82,6 @@ func GetLogoutFlags(flags *LogoutOptions) *pflag.FlagSet {
7782
func GetLogoutFlagsCompletions() completion.FlagCompletions {
7883
flagCompletion := completion.FlagCompletions{}
7984
flagCompletion["authfile"] = completion.AutocompleteDefault
85+
flagCompletion["compat-auth-file"] = completion.AutocompleteDefault
8086
return flagCompletion
8187
}

0 commit comments

Comments
 (0)