Skip to content

Commit

Permalink
shell completion: respect CONTAINERS_REGISTRIES_CONF
Browse files Browse the repository at this point in the history
Found in debian testing where by default there are no unqualified search
registries installed. As such the test failed as the FIXME said. Now
there is no need for the test to assume anything.

Instead set our own config via CONTAINERS_REGISTRIES_CONF then we can
do exact matches, except that env was not read in the shell completion
code so move some code around to make it read the var in the same way as
podman login/logout.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Dec 17, 2024
1 parent 3e385eb commit 153a975
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
5 changes: 4 additions & 1 deletion cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/ssh"
"github.com/containers/image/v5/pkg/sysregistriesv2"
imageTypes "github.com/containers/image/v5/types"
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
Expand Down Expand Up @@ -276,7 +277,9 @@ func getSecrets(cmd *cobra.Command, toComplete string, cType completeType) ([]st
}

func getRegistries() ([]string, cobra.ShellCompDirective) {
regs, err := sysregistriesv2.UnqualifiedSearchRegistries(nil)
sysCtx := &imageTypes.SystemContext{}
SetRegistriesConfPath(sysCtx)
regs, err := sysregistriesv2.UnqualifiedSearchRegistries(sysCtx)
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
24 changes: 24 additions & 0 deletions cmd/podman/common/registries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"os"

"github.com/containers/image/v5/types"
)

// SetRegistriesConfPath sets the registries.conf path for the specified context.
// NOTE: this is a verbatim copy from c/common/libimage which we're not using
// to prevent leaking c/storage into this file. Maybe this should go into c/image?
func SetRegistriesConfPath(systemContext *types.SystemContext) {
if systemContext.SystemRegistriesConfPath != "" {
return
}
if envOverride, ok := os.LookupEnv("CONTAINERS_REGISTRIES_CONF"); ok {
systemContext.SystemRegistriesConfPath = envOverride
return
}
if envOverride, ok := os.LookupEnv("REGISTRIES_CONFIG_PATH"); ok {
systemContext.SystemRegistriesConfPath = envOverride
return
}
}
19 changes: 1 addition & 18 deletions cmd/podman/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,7 @@ func login(cmd *cobra.Command, args []string) error {
sysCtx := &types.SystemContext{
DockerInsecureSkipTLSVerify: skipTLS,
}
setRegistriesConfPath(sysCtx)
common.SetRegistriesConfPath(sysCtx)
loginOptions.GetLoginSet = cmd.Flag("get-login").Changed
return auth.Login(context.Background(), sysCtx, &loginOptions.LoginOptions, args)
}

// setRegistriesConfPath sets the registries.conf path for the specified context.
// NOTE: this is a verbatim copy from c/common/libimage which we're not using
// to prevent leaking c/storage into this file. Maybe this should go into c/image?
func setRegistriesConfPath(systemContext *types.SystemContext) {
if systemContext.SystemRegistriesConfPath != "" {
return
}
if envOverride, ok := os.LookupEnv("CONTAINERS_REGISTRIES_CONF"); ok {
systemContext.SystemRegistriesConfPath = envOverride
return
}
if envOverride, ok := os.LookupEnv("REGISTRIES_CONFIG_PATH"); ok {
systemContext.SystemRegistriesConfPath = envOverride
return
}
}
2 changes: 1 addition & 1 deletion cmd/podman/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func init() {
// Implementation of podman-logout.
func logout(cmd *cobra.Command, args []string) error {
sysCtx := &types.SystemContext{}
setRegistriesConfPath(sysCtx)
common.SetRegistriesConfPath(sysCtx)
return auth.Logout(sysCtx, &logoutOptions, args)
}
9 changes: 7 additions & 2 deletions test/system/600-completion.bats
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ function check_shell_completion() {

*REGISTRY*)
run_completion "$@" $cmd "${extra_args[@]}" ""
### FIXME how can we get the configured registries?
_check_completion_end NoFileComp
### FIXME this fails if no registries are configured
assert "${#lines[@]}" -gt 2 "$* $cmd: No REGISTRIES found in suggestions"
# We can assume quay.io as we force our own CONTAINERS_REGISTRIES_CONF below.
assert "${lines[0]}" == "quay.io" "unqualified-search-registries from registries.conf listed"

match=true
# resume
Expand Down Expand Up @@ -311,6 +311,11 @@ function _check_no_suggestions() {
# create secret
run_podman secret create $random_secret_name $secret_file

# create our own registries.conf so we know what registry is set
local CONTAINERS_REGISTRIES_CONF="$PODMAN_TMPDIR/registries.conf"
echo 'unqualified-search-registries = ["quay.io"]' > "$CONTAINERS_REGISTRIES_CONF"
export CONTAINERS_REGISTRIES_CONF

# Called with no args -- start with 'podman --help'. check_shell_completion() will
# recurse for any subcommands.
check_shell_completion
Expand Down

0 comments on commit 153a975

Please sign in to comment.