feat(storepkg): fetch packages from any registry, plain HTTP on loopback only

This commit is contained in:
sam committed 2026-10-04 01:53:00 +10:00
1 parent 1e435e9224
commit 5b42e4cba9
3 files changed
+38 -11

No files matched your search

+1 -1
View File
@@ -266,7 +266,7 @@ func (b *server) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants}
if b.pg != nil {
packages := storepkg.Service{
Registry: registry.LocalRegistry{},
Registry: registry.OCIRegistry{},
Store: postgres.NewInstalledPackages(b.pg),
}
// A projection failure degrades the org resources a package would have
+10 -10
View File
@@ -27,21 +27,20 @@ const (
maxPackageBytes = 64 << 20
)
// LocalRegistry accepts a local OCI registry over HTTP and verifies the
// manifest and sole layer before returning declarative package content.
type LocalRegistry struct{}
// OCIRegistry fetches a package from an OCI registry and verifies the pinned
// manifest and sole layer before returning package content. A loopback
// registry is reached over plain HTTP; every other host over HTTPS. The digest
// pin, not the transport, is what makes the content trustworthy.
type OCIRegistry struct{}
var _ domain.Registry = LocalRegistry{}
var _ domain.Registry = OCIRegistry{}
func (LocalRegistry) Fetch(ctx context.Context, reference, pin string) (domain.Descriptor, []byte, error) {
if !localReference(reference) {
return domain.Descriptor{}, nil, errors.New("package registry must be local")
}
func (OCIRegistry) Fetch(ctx context.Context, reference, pin string) (domain.Descriptor, []byte, error) {
repository, err := remote.NewRepository(reference)
if err != nil {
return domain.Descriptor{}, nil, err
}
repository.PlainHTTP = true
repository.PlainHTTP = loopbackReference(reference)
_, stream, err := repository.FetchReference(ctx, pin)
if err != nil {
return domain.Descriptor{}, nil, err
@@ -68,7 +67,8 @@ func (LocalRegistry) Fetch(ctx context.Context, reference, pin string) (domain.D
return decodeOCI(manifest, layer, pin)
}
func localReference(reference string) bool {
// loopbackReference reports whether the reference names a registry on this host.
func loopbackReference(reference string) bool {
host, _, ok := strings.Cut(reference, "/")
if !ok {
return false
@@ -0,0 +1,27 @@
package storepkg
import "testing"
// Plain HTTP is for this host only: a reference that merely looks local must
// still go over HTTPS.
func TestLoopbackReference(t *testing.T) {
tests := []struct {
reference string
want bool
}{
{"localhost:5000/bws:1.0.0", true},
{"127.0.0.1:5001/bws", true},
{"[::1]:5000/bws", true},
{"ghcr.io/samcharles93/archipelago/bws:1.0.0", false},
{"localhost.evil.example/bws", false},
{"127.0.0.1.evil.example/bws", false},
{"bws", false},
}
for _, tt := range tests {
t.Run(tt.reference, func(t *testing.T) {
if got := loopbackReference(tt.reference); got != tt.want {
t.Fatalf("loopbackReference(%q) = %v, want %v", tt.reference, got, tt.want)
}
})
}
}