| 1 |
package ociauth |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"encoding/json" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"slices" |
| 14 |
"strings" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type Config interface { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
EntryForRegistry(host string) (ConfigEntry, error) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type ConfigEntry struct { |
| 32 |
|
| 33 |
RefreshToken string |
| 34 |
|
| 35 |
AccessToken string |
| 36 |
|
| 37 |
Username string |
| 38 |
|
| 39 |
Password string |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type ConfigFile struct { |
| 45 |
data configData |
| 46 |
runner HelperRunner |
| 47 |
} |
| 48 |
|
| 49 |
var ErrHelperNotFound = errors.New("helper not found") |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
type HelperRunner = func(helperName string, serverURL string) (ConfigEntry, error) |
| 60 |
|
| 61 |
|
| 62 |
type configData struct { |
| 63 |
Auths map[string]authConfig `json:"auths"` |
| 64 |
CredsStore string `json:"credsStore,omitempty"` |
| 65 |
CredHelpers map[string]string `json:"credHelpers,omitempty"` |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
type authConfig struct { |
| 70 |
|
| 71 |
|
| 72 |
derivedFrom []string |
| 73 |
|
| 74 |
Username string `json:"username,omitempty"` |
| 75 |
Password string `json:"password,omitempty"` |
| 76 |
|
| 77 |
|
| 78 |
Auth string `json:"auth,omitempty"` |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
IdentityToken string `json:"identitytoken,omitempty"` |
| 83 |
|
| 84 |
|
| 85 |
RegistryToken string `json:"registrytoken,omitempty"` |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func LoadWithEnv(runner HelperRunner, env []string) (*ConfigFile, error) { |
| 92 |
if runner == nil { |
| 93 |
runner = ExecHelperWithEnv(env) |
| 94 |
} |
| 95 |
getenv := os.Getenv |
| 96 |
if env != nil { |
| 97 |
getenv = getenvFunc(env) |
| 98 |
} |
| 99 |
for _, f := range configFileLocations { |
| 100 |
filename := f(getenv) |
| 101 |
if filename == "" { |
| 102 |
continue |
| 103 |
} |
| 104 |
data, err := os.ReadFile(filename) |
| 105 |
if err != nil { |
| 106 |
if os.IsNotExist(err) { |
| 107 |
continue |
| 108 |
} |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
f, err := decodeConfigFile(data) |
| 112 |
if err != nil { |
| 113 |
return nil, fmt.Errorf("invalid config file %q: %v", filename, err) |
| 114 |
} |
| 115 |
return &ConfigFile{ |
| 116 |
data: f, |
| 117 |
runner: runner, |
| 118 |
}, nil |
| 119 |
} |
| 120 |
return &ConfigFile{ |
| 121 |
runner: runner, |
| 122 |
}, nil |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func Load(runner HelperRunner) (*ConfigFile, error) { |
| 134 |
return LoadWithEnv(runner, nil) |
| 135 |
} |
| 136 |
|
| 137 |
func getenvFunc(env []string) func(string) string { |
| 138 |
return func(key string) string { |
| 139 |
for i := len(env) - 1; i >= 0; i-- { |
| 140 |
if e := env[i]; len(e) >= len(key)+1 && e[len(key)] == '=' && e[:len(key)] == key { |
| 141 |
return e[len(key)+1:] |
| 142 |
} |
| 143 |
} |
| 144 |
return "" |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
var configFileLocations = []func(func(string) string) string{ |
| 149 |
func(getenv func(string) string) string { |
| 150 |
if d := getenv("DOCKER_CONFIG"); d != "" { |
| 151 |
return filepath.Join(d, "config.json") |
| 152 |
} |
| 153 |
return "" |
| 154 |
}, |
| 155 |
func(getenv func(string) string) string { |
| 156 |
if home := userHomeDir(getenv); home != "" { |
| 157 |
return filepath.Join(home, ".docker", "config.json") |
| 158 |
} |
| 159 |
return "" |
| 160 |
}, |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
func(getenv func(string) string) string { |
| 165 |
if d := getenv("XDG_RUNTIME_DIR"); d != "" { |
| 166 |
return filepath.Join(d, "containers", "auth.json") |
| 167 |
} |
| 168 |
return "" |
| 169 |
}, |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
var userHomeDir = func(getenv func(string) string) string { |
| 178 |
env := "HOME" |
| 179 |
switch runtime.GOOS { |
| 180 |
case "windows": |
| 181 |
env = "USERPROFILE" |
| 182 |
case "plan9": |
| 183 |
env = "home" |
| 184 |
} |
| 185 |
if v := getenv(env); v != "" { |
| 186 |
return v |
| 187 |
} |
| 188 |
|
| 189 |
switch runtime.GOOS { |
| 190 |
case "android": |
| 191 |
return "/sdcard" |
| 192 |
case "ios": |
| 193 |
return "/" |
| 194 |
} |
| 195 |
return "" |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func (c *ConfigFile) EntryForRegistry(registryHostname string) (ConfigEntry, error) { |
| 201 |
helper, ok := c.data.CredHelpers[registryHostname] |
| 202 |
explicit := true |
| 203 |
if !ok { |
| 204 |
helper = c.data.CredsStore |
| 205 |
explicit = false |
| 206 |
} |
| 207 |
if helper != "" { |
| 208 |
entry, err := c.runner(helper, registryHostname) |
| 209 |
if err == nil || explicit || !errors.Is(err, ErrHelperNotFound) { |
| 210 |
return entry, err |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
} |
| 217 |
auth := c.data.Auths[registryHostname] |
| 218 |
if auth.IdentityToken != "" && auth.Username != "" { |
| 219 |
return ConfigEntry{}, fmt.Errorf("ambiguous auth credentials") |
| 220 |
} |
| 221 |
if len(auth.derivedFrom) > 1 { |
| 222 |
return ConfigEntry{}, fmt.Errorf("more than one auths entry for %q (%s)", registryHostname, strings.Join(auth.derivedFrom, ", ")) |
| 223 |
} |
| 224 |
|
| 225 |
return ConfigEntry{ |
| 226 |
RefreshToken: auth.IdentityToken, |
| 227 |
AccessToken: auth.RegistryToken, |
| 228 |
Username: auth.Username, |
| 229 |
Password: auth.Password, |
| 230 |
}, nil |
| 231 |
} |
| 232 |
|
| 233 |
func decodeConfigFile(data []byte) (configData, error) { |
| 234 |
var f configData |
| 235 |
if err := json.Unmarshal(data, &f); err != nil { |
| 236 |
return configData{}, fmt.Errorf("decode failed: %v", err) |
| 237 |
} |
| 238 |
for addr, ac := range f.Auths { |
| 239 |
if ac.Auth != "" { |
| 240 |
var err error |
| 241 |
ac.Username, ac.Password, err = decodeAuth(ac.Auth) |
| 242 |
if err != nil { |
| 243 |
return configData{}, fmt.Errorf("cannot decode auth field for %q: %v", addr, err) |
| 244 |
} |
| 245 |
} |
| 246 |
f.Auths[addr] = ac |
| 247 |
if !strings.Contains(addr, "//") { |
| 248 |
continue |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
addr1 := urlHost(addr) |
| 257 |
if addr1 == addr { |
| 258 |
continue |
| 259 |
} |
| 260 |
if ac1, ok := f.Auths[addr1]; ok { |
| 261 |
if len(ac1.derivedFrom) == 0 { |
| 262 |
|
| 263 |
continue |
| 264 |
} |
| 265 |
ac = ac1 |
| 266 |
} |
| 267 |
ac.derivedFrom = append(ac.derivedFrom, addr) |
| 268 |
slices.Sort(ac.derivedFrom) |
| 269 |
f.Auths[addr1] = ac |
| 270 |
} |
| 271 |
return f, nil |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
func urlHost(url string) string { |
| 278 |
stripped := url |
| 279 |
if strings.HasPrefix(url, "http://") { |
| 280 |
stripped = strings.TrimPrefix(url, "http://") |
| 281 |
} else if strings.HasPrefix(url, "https://") { |
| 282 |
stripped = strings.TrimPrefix(url, "https://") |
| 283 |
} |
| 284 |
|
| 285 |
hostName, _, _ := strings.Cut(stripped, "/") |
| 286 |
return hostName |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
func decodeAuth(authStr string) (string, string, error) { |
| 291 |
s, err := base64.StdEncoding.DecodeString(authStr) |
| 292 |
if err != nil { |
| 293 |
return "", "", fmt.Errorf("invalid base64-encoded string") |
| 294 |
} |
| 295 |
username, password, ok := strings.Cut(string(s), ":") |
| 296 |
if !ok || username == "" { |
| 297 |
return "", "", errors.New("no username found") |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
return username, strings.Trim(password, "\x00"), nil |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
func ExecHelper(helperName string, serverURL string) (ConfigEntry, error) { |
| 307 |
return ExecHelperWithEnv(nil)(helperName, serverURL) |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
func ExecHelperWithEnv(env []string) HelperRunner { |
| 315 |
return func(helperName string, serverURL string) (ConfigEntry, error) { |
| 316 |
var out bytes.Buffer |
| 317 |
cmd := exec.Command("docker-credential-"+helperName, "get") |
| 318 |
|
| 319 |
|
| 320 |
cmd.Stdin = strings.NewReader(serverURL) |
| 321 |
cmd.Stdout = &out |
| 322 |
cmd.Stderr = &out |
| 323 |
cmd.Env = env |
| 324 |
if err := cmd.Run(); err != nil { |
| 325 |
if !errors.As(err, new(*exec.ExitError)) { |
| 326 |
if errors.Is(err, exec.ErrNotFound) { |
| 327 |
return ConfigEntry{}, fmt.Errorf("%w: %v", ErrHelperNotFound, err) |
| 328 |
} |
| 329 |
return ConfigEntry{}, fmt.Errorf("cannot run auth helper: %v", err) |
| 330 |
} |
| 331 |
t := strings.TrimSpace(out.String()) |
| 332 |
if t == "credentials not found in native keychain" { |
| 333 |
return ConfigEntry{}, nil |
| 334 |
} |
| 335 |
return ConfigEntry{}, fmt.Errorf("error getting credentials: %s", t) |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
type helperCredentials struct { |
| 341 |
Username string |
| 342 |
Secret string |
| 343 |
} |
| 344 |
var creds helperCredentials |
| 345 |
if err := json.Unmarshal(out.Bytes(), &creds); err != nil { |
| 346 |
return ConfigEntry{}, err |
| 347 |
} |
| 348 |
if creds.Username == "<token>" { |
| 349 |
return ConfigEntry{ |
| 350 |
RefreshToken: creds.Secret, |
| 351 |
}, nil |
| 352 |
} |
| 353 |
return ConfigEntry{ |
| 354 |
Password: creds.Secret, |
| 355 |
Username: creds.Username, |
| 356 |
}, nil |
| 357 |
} |
| 358 |
} |