-
Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathauth.go
More file actions
56 lines (51 loc) · 1.29 KB
/
auth.go
File metadata and controls
56 lines (51 loc) · 1.29 KB
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
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) // #nosec G117 -- Ignore "JSON key "password" matches secret pattern"
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)
}