Add oci-layout:// source and target support to imagetools create and inspect while keeping merge, filter, and referrer logic shared. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package imagetools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/containerd/containerd/v2/core/remotes/docker"
|
|
"github.com/distribution/reference"
|
|
"github.com/moby/buildkit/session/auth/authprovider"
|
|
)
|
|
|
|
func RegistryAuthForRef(ref string, auth authprovider.AuthConfigProvider) (string, error) {
|
|
if auth == nil {
|
|
return "", nil
|
|
}
|
|
loc, err := ParseLocation(ref)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if loc.IsOCILayout() {
|
|
return "", nil
|
|
}
|
|
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 := auth(context.TODO(), host, nil, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
buf, err := json.Marshal(ac)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.URLEncoding.EncodeToString(buf), nil
|
|
}
|
|
|
|
type withBearerAuthorizer struct {
|
|
docker.Authorizer
|
|
AuthConfig authprovider.AuthConfigProvider
|
|
}
|
|
|
|
func (a *withBearerAuthorizer) Authorize(ctx context.Context, req *http.Request) error {
|
|
ac, err := a.AuthConfig(ctx, req.Host, nil, nil)
|
|
if err == nil && ac.RegistryToken != "" {
|
|
req.Header.Set("Authorization", "Bearer "+ac.RegistryToken)
|
|
return nil
|
|
}
|
|
return a.Authorizer.Authorize(ctx, req)
|
|
}
|