// Package distribution names where an instance takes signed artifacts from: // its releases and its extension catalogue. package distribution import ( "crypto/ed25519" "encoding/base64" "errors" "fmt" "strings" ) // Source is an OCI repository and the public key that must have signed what // it serves. The key, not the registry, is what makes an artifact trusted, so // a source is never valid without one. type Source struct { Reference string Key ed25519.PublicKey } // Parse builds a source from its stored form. A blank reference and key mean // fallback; setting only one of them is refused, since a reference without // its key, or a key for the default reference, is a misconfiguration. func Parse(reference, key string, fallback Source) (Source, error) { reference, key = strings.TrimSpace(reference), strings.TrimSpace(key) if reference == "" && key == "" { return fallback, nil } if reference == "" || key == "" { return Source{}, errors.New("a distribution source needs both a reference and a key") } if !repository(reference) { return Source{}, fmt.Errorf("distribution source %q must be an OCI repository such as registry.example/owner/name", reference) } decoded, err := ParseKey(key) if err != nil { return Source{}, err } return Source{Reference: reference, Key: decoded}, nil } // ParseKey decodes a base64 ed25519 public key. func ParseKey(key string) (ed25519.PublicKey, error) { decoded, err := base64.StdEncoding.DecodeString(strings.TrimSpace(key)) if err != nil || len(decoded) != ed25519.PublicKeySize { return nil, errors.New("distribution key must be a base64 ed25519 public key") } return decoded, nil } // EncodeKey is the stored form of a public key. func EncodeKey(key ed25519.PublicKey) string { return base64.StdEncoding.EncodeToString(key) } // repository accepts registry/path with an optional registry port and no tag // or digest: a source names a repository, and its tags are the channels. func repository(reference string) bool { host, path, ok := strings.Cut(reference, "/") return ok && host != "" && path != "" && strings.Count(host, ":") <= 1 && !strings.ContainsAny(path, ":@") && !strings.ContainsAny(reference, " \t\n") && !strings.Contains(reference, "//") } // mustKey decodes a built-in key; a malformed one is a build defect. func mustKey(encoded string) ed25519.PublicKey { key, err := ParseKey(encoded) if err != nil { panic("distribution: invalid built-in key") } return key } // DefaultCatalogue is the archipelago marketplace an instance trusts until an // operator names another. func DefaultCatalogue() Source { return Source{ Reference: "git.catlow.cloud/sam/archipelago/catalogue", Key: mustKey("PLouShOzvRqy1QbYaJplaM41/ulgKFN8ZQ7g1rI02Fc="), } } // DefaultReleases is where Archie's own releases are published. func DefaultReleases() Source { return Source{ Reference: "git.catlow.cloud/sam/archie", Key: mustKey("gIBqoQMwy+8WXmGfiuHtnUa50W0IqfzkTR6iF5q3dSM="), } }