-
Notifications
You must be signed in to change notification settings - Fork 3
/
google.go
82 lines (73 loc) · 2.14 KB
/
google.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
78
79
80
81
82
package explore
import (
"log"
"net/http"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"golang.org/x/oauth2"
)
func isGoogle(host string) bool {
if host != "gcr.io" && !strings.HasSuffix(host, ".gcr.io") && !strings.HasSuffix(host, ".pkg.dev") && !strings.HasSuffix(host, ".google.com") {
return false
}
return true
}
// TODO: ugh
func (h *handler) googleOptions(w http.ResponseWriter, r *http.Request, repo string) []google.Option {
ctx := r.Context()
opts := []google.Option{}
opts = append(opts, google.WithContext(ctx))
if repo == "mirror.gcr.io" {
t := remote.DefaultTransport
t = transport.NewRetry(t)
t = transport.NewUserAgent(t, h.userAgent)
if r.URL.Query().Get("trace") != "" {
t = transport.NewTracer(t)
}
t = transport.Wrap(t)
opts = append(opts, google.WithTransport(t))
return opts
}
auth := authn.Anonymous
if h.keychain != nil {
ref, err := name.NewRepository(repo)
if err == nil {
maybeAuth, err := h.keychain.Resolve(ref)
if err == nil {
auth = maybeAuth
} else {
logs.Debug.Printf("Resolve(%q) = %v", repo, err)
}
} else {
logs.Debug.Printf("NewRepository(%q) = %v", repo, err)
}
}
parsed, err := name.NewRepository(repo)
if err == nil && isGoogle(parsed.Registry.String()) {
if at, err := r.Cookie("access_token"); err == nil {
tok := &oauth2.Token{
AccessToken: at.Value,
Expiry: at.Expires,
}
if rt, err := r.Cookie("refresh_token"); err == nil {
tok.RefreshToken = rt.Value
}
if h.oauth != nil {
ts := h.oauth.TokenSource(r.Context(), tok)
auth = google.NewTokenSourceAuthenticator(ts)
}
}
}
opts = append(opts, google.WithAuth(auth))
if t, err := h.transportFromCookie(w, r, repo, auth); err != nil {
log.Printf("failed to get transport from cookie: %v", err)
} else {
opts = append(opts, google.WithTransport(t))
}
return opts
}