docker-container: ensure credentials are passed when pulling buildkit image

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2020-11-04 13:04:04 -08:00
parent 778fbb4669
commit 3b69482a2f
7 changed files with 53 additions and 19 deletions

View File

@ -3,6 +3,8 @@ package imagetools
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
@ -107,3 +109,23 @@ func toCredentialsFunc(a Auth) func(string) (string, string, error) {
return ac.Username, ac.Password, nil
}
}
func RegistryAuthForRef(ref string, a Auth) (string, error) {
r, err := parseRef(ref)
if err != nil {
return "", err
}
host := reference.Domain(r)
if host == "docker.io" {
host = "https://index.docker.io/v1/"
}
ac, err := a.GetAuthConfig(host)
if err != nil {
return "", err
}
buf, err := json.Marshal(ac)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(buf), nil
}