Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions v2/internal/pkg/registriesd/registriesd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -49,12 +48,16 @@ func PrepareRegistrydCustomDir(workingDir, registriesDirPath string, registryHos
}

func GetDefaultRegistrydConfigPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("unable to determine the current user : %w", err)
// Use os.UserHomeDir() instead of user.Current() to support containerized
// environments with arbitrary UIDs that don't exist in /etc/passwd.
// os.UserHomeDir() reads $HOME env var first, avoiding passwd lookups.
homeDir, err := os.UserHomeDir()
if err == nil {
return registriesDirPathWithHomeDir(homeDir), nil
}

return registriesDirPathWithHomeDir(usr.HomeDir), nil
// Fall back to system default if home directory cannot be determined.
return systemRegistriesDirPath, nil
}

func registriesDirPathWithHomeDir(homeDir string) string {
Expand Down
20 changes: 20 additions & 0 deletions v2/internal/pkg/registriesd/registriesd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package registriesd

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -44,3 +45,22 @@ func TestGetCustomRegistrydConfigPath(t *testing.T) {
expectedRegistriesD := filepath.Join(testFolder, "containers/registries.d")
assert.Equal(t, expectedRegistriesD, registriesDirPath)
}

func TestGetDefaultRegistrydConfigPath(t *testing.T) {
testHome := t.TempDir()
t.Setenv("HOME", testHome)

// When user registries.d doesn't exist, should return system path
path, err := GetDefaultRegistrydConfigPath()
assert.NoError(t, err)
assert.Equal(t, systemRegistriesDirPath, path)

// When user registries.d exists, should return user path
expectedUserPath := filepath.Join(testHome, ".config/containers/registries.d")
err = os.MkdirAll(expectedUserPath, 0755)
assert.NoError(t, err)

path, err = GetDefaultRegistrydConfigPath()
assert.NoError(t, err)
assert.Equal(t, expectedUserPath, path)
}