Skip to content

Commit 4961bb2

Browse files
committed
oci: Support DOCKERHUB_AUTH
Also update FAQ to thank Docker for supporting the public instance.
1 parent 6ae19b9 commit 4961bb2

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

cmd/oci/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"log"
88
"net/http"
99
"os"
10+
"strings"
1011

1112
"github.com/google/go-containerregistry/pkg/authn"
1213
"github.com/google/go-containerregistry/pkg/gcrane"
1314
"github.com/google/go-containerregistry/pkg/logs"
15+
"github.com/google/go-containerregistry/pkg/name"
1416
"github.com/jonjohnsonjr/dagdotdev/internal/explore"
1517

1618
sha256simd "github.com/minio/sha256-simd"
@@ -57,9 +59,45 @@ func main() {
5759
kcs = append(kcs, gcrane.Keychain)
5860
}
5961

62+
if dh := os.Getenv("DOCKERHUB_AUTH"); dh != "" {
63+
kc, err := newHubKeychain(dh)
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
kcs = append(kcs, kc)
68+
}
69+
6070
if len(kcs) != 0 {
6171
opt = append(opt, explore.WithKeychain(authn.NewMultiKeychain(kcs...)))
6272
}
6373

6474
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), explore.New(opt...)))
6575
}
76+
77+
func newHubKeychain(env string) (*keychain, error) {
78+
user, pass, ok := strings.Cut(env, ":")
79+
if !ok {
80+
return nil, fmt.Errorf("invalid DOCKERHUB_AUTH, expected user:pass")
81+
}
82+
83+
return &keychain{
84+
user: user,
85+
pass: pass,
86+
}, nil
87+
}
88+
89+
type keychain struct {
90+
user string
91+
pass string
92+
}
93+
94+
func (k *keychain) Resolve(r authn.Resource) (authn.Authenticator, error) {
95+
if r.RegistryStr() != name.DefaultRegistry {
96+
return authn.Anonymous, nil
97+
}
98+
99+
return authn.FromConfig(authn.AuthConfig{
100+
Username: k.user,
101+
Password: k.pass,
102+
}), nil
103+
}

internal/explore/templates.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ Enter a <strong>public</strong> repository, e.g. <tt>"ubuntu"</tt>:
121121
<p>
122122
This service lives on <a href="https://cloud.run">Cloud Run</a> and uses <a href="https://github.com/google/go-containerregistry">google/go-containerregistry</a> for registry interactions.
123123
</p>
124+
<h4>Isn't this expensive to run?</h4>
125+
<p>Not really! Ingress is cheap, Cloud Run is cheap, and GCS is cheap.</p>
126+
<p>To avoid paying for egress, I limit the amount of data that I'll serve directly and instead give you a command you can run on your own machine.</p>
127+
<p>The most expensive part of this is actually the domain name.</p>
124128
<h4>Isn't this expensive for the registry?</h4>
125-
<p>
126-
Not really! The first time a layer is accessed, I download and index it. Browsing the filesystem just uses that index, and opening a file uses Range requests to load small chunks of the layer as needed.
127-
</p>
129+
<p>Not really! The first time a layer is accessed, I download and index it. Browsing the filesystem just uses that index, and opening a file uses Range requests to load small chunks of the layer as needed.</p>
130+
<p>Since I only have to download the whole layer once, this actually reduces traffic to the registry in a lot of cases, e.g. if you share a link with someone rather than having them pull the whole image on their machine.</p>
131+
<p>In fact, Docker has graciously sponsored this service by providing me an account with unlimited public Docker Hub access. Thanks, Docker!</p>
128132
<h4>That can't be true, gzip doesn't support random access!</h4>
129133
<p>
130134
That's not a question.

0 commit comments

Comments
 (0)