From 4c6b09caf49582542dbd1da5ff88ee6644403f83 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 13 May 2019 15:19:56 -0400 Subject: [PATCH] Default Authfile to proper path We should show the location of the default authfile in user help Signed-off-by: Daniel J Walsh Closes: #1591 Approved by: TomSweeneyRedHat --- cmd/buildah/commit.go | 2 +- cmd/buildah/from.go | 2 +- cmd/buildah/login.go | 3 ++- cmd/buildah/logout.go | 3 ++- cmd/buildah/pull.go | 2 +- cmd/buildah/push.go | 2 +- pkg/cli/common.go | 15 ++++++++++++++- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cmd/buildah/commit.go b/cmd/buildah/commit.go index 9240b1d4b49..939a275cd0f 100644 --- a/cmd/buildah/commit.go +++ b/cmd/buildah/commit.go @@ -55,7 +55,7 @@ func init() { flags := commitCommand.Flags() flags.SetInterspersed(false) - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.StringVar(&opts.blobCache, "blob-cache", "", "assume image blobs in the specified directory will be available for pushing") flags.MarkHidden("blob-cache") diff --git a/cmd/buildah/from.go b/cmd/buildah/from.go index 2c4d6ae1001..a2d14073091 100644 --- a/cmd/buildah/from.go +++ b/cmd/buildah/from.go @@ -58,7 +58,7 @@ func init() { flags := fromCommand.Flags() flags.SetInterspersed(false) - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") flags.StringVar(&opts.cidfile, "cidfile", "", "write the container ID to the file") flags.StringVar(&opts.creds, "creds", "", "use `[username[:password]]` for accessing the registry") diff --git a/cmd/buildah/login.go b/cmd/buildah/login.go index 0c4556676bd..10b305e7267 100644 --- a/cmd/buildah/login.go +++ b/cmd/buildah/login.go @@ -6,6 +6,7 @@ import ( "os" "strings" + buildahcli "github.com/containers/buildah/pkg/cli" "github.com/containers/buildah/pkg/parse" "github.com/containers/image/docker" "github.com/containers/image/pkg/docker/config" @@ -42,7 +43,7 @@ func init() { flags := loginCommand.Flags() flags.SetInterspersed(false) - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json. Use REGISTRY_AUTH_FILE environment variable to override") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") flags.StringVarP(&opts.password, "password", "p", "", "Password for registry") flags.BoolVar(&opts.tlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing the registry") diff --git a/cmd/buildah/logout.go b/cmd/buildah/logout.go index 64b3a9b5f06..825d34842b1 100644 --- a/cmd/buildah/logout.go +++ b/cmd/buildah/logout.go @@ -3,6 +3,7 @@ package main import ( "fmt" + buildahcli "github.com/containers/buildah/pkg/cli" "github.com/containers/buildah/pkg/parse" "github.com/containers/image/pkg/docker/config" "github.com/pkg/errors" @@ -32,7 +33,7 @@ func init() { flags := logoutCommand.Flags() flags.SetInterspersed(false) - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json. Use REGISTRY_AUTH_FILE environment variable to override") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.BoolVarP(&opts.all, "all", "a", false, "Remove the cached credentials for all registries in the auth file") rootCmd.AddCommand(logoutCommand) } diff --git a/cmd/buildah/pull.go b/cmd/buildah/pull.go index 0fa67495690..ac42e4427ef 100644 --- a/cmd/buildah/pull.go +++ b/cmd/buildah/pull.go @@ -46,7 +46,7 @@ func init() { flags := pullCommand.Flags() flags.SetInterspersed(false) flags.BoolVarP(&opts.allTags, "all-tags", "a", false, "download all tagged images in the repository") - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.StringVar(&opts.blobCache, "blob-cache", "", "store copies of pulled image blobs in the specified directory") flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") flags.StringVar(&opts.creds, "creds", "", "use `[username[:password]]` for accessing the registry") diff --git a/cmd/buildah/push.go b/cmd/buildah/push.go index 73f36018cfd..fb17628a800 100644 --- a/cmd/buildah/push.go +++ b/cmd/buildah/push.go @@ -61,7 +61,7 @@ func init() { flags := pushCommand.Flags() flags.SetInterspersed(false) - flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json") + flags.StringVar(&opts.authfile, "authfile", buildahcli.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") flags.StringVar(&opts.blobCache, "blob-cache", "", "assume image blobs in the specified directory will be available for pushing") flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") flags.StringVar(&opts.creds, "creds", "", "use `[username[:password]]` for accessing the registry") diff --git a/pkg/cli/common.go b/pkg/cli/common.go index e7a571db6c7..23bb696fcfe 100644 --- a/pkg/cli/common.go +++ b/pkg/cli/common.go @@ -7,6 +7,7 @@ package cli import ( "fmt" "os" + "path/filepath" "strings" "github.com/containers/buildah" @@ -137,7 +138,7 @@ func GetLayerFlags(flags *LayerResults) pflag.FlagSet { func GetBudFlags(flags *BudResults) pflag.FlagSet { fs := pflag.FlagSet{} fs.StringArrayVar(&flags.Annotation, "annotation", []string{}, "Set metadata for an image (default [])") - fs.StringVar(&flags.Authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json") + fs.StringVar(&flags.Authfile, "authfile", GetDefaultAuthFile(), "path of the authentication file.") fs.StringArrayVar(&flags.BuildArg, "build-arg", []string{}, "`argument=value` to supply to the builder") fs.StringVar(&flags.CacheFrom, "cache-from", "", "Images to utilise as potential cache sources. The build process does not currently support caching so this is a NOOP.") fs.StringVar(&flags.CertDir, "cert-dir", "", "use certificates at the specified path to access the registry") @@ -246,3 +247,15 @@ func VerifyFlagsArgsOrder(args []string) error { } return nil } + +func GetDefaultAuthFile() string { + authfile := os.Getenv("REGISTRY_AUTH_FILE") + if authfile != "" { + return authfile + } + runtimeDir := os.Getenv("XDG_RUNTIME_DIR") + if runtimeDir != "" { + return filepath.Join(runtimeDir, "containers/auth.json") + } + return "" +}