-
Notifications
You must be signed in to change notification settings - Fork 241
/
keychain.go
77 lines (65 loc) · 1.77 KB
/
keychain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package bundle
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/types"
"github.com/docker/docker/pkg/homedir"
"github.com/google/go-containerregistry/pkg/authn"
)
type podmanKeychain struct {
mu sync.Mutex
}
var PodmanKeyChain authn.Keychain = &podmanKeychain{}
func (pk *podmanKeychain) Resolve(target authn.Resource) (authn.Authenticator, error) {
pk.mu.Lock()
defer pk.mu.Unlock()
authFile, err := os.Open(getPathToPodmanAuth())
// Return error only when the auth file is there but somehow unable to read.
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return authn.Anonymous, nil
}
return nil, err
}
defer authFile.Close()
cf, err := config.LoadFromReader(authFile)
if err != nil {
return nil, err
}
key := target.RegistryStr()
cfg, err := cf.GetAuthConfig(key)
if err != nil {
return nil, err
}
empty := types.AuthConfig{}
if cfg == empty {
return authn.Anonymous, nil
}
return authn.FromConfig(authn.AuthConfig{
Username: cfg.Username,
Password: cfg.Password,
Auth: cfg.Auth,
IdentityToken: cfg.IdentityToken,
RegistryToken: cfg.RegistryToken,
}), nil
}
func getPathToPodmanAuth() string {
var (
defaultPerUIDPathFormat = filepath.FromSlash("/run/containers/%d/auth.json")
xdgRuntimeDirPath = filepath.FromSlash("containers/auth.json")
nonLinuxAuthFilePath = filepath.FromSlash(".config/containers/auth.json")
)
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
return filepath.Join(homedir.Get(), nonLinuxAuthFilePath)
}
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir != "" {
return filepath.Join(runtimeDir, xdgRuntimeDirPath)
}
return fmt.Sprintf(defaultPerUIDPathFormat, os.Getuid())
}