Files
archie/internal/releaseupdate/publish.go
T
sam 9d8f364af1
Quality / check (push) Successful in 5m53s
deploy / gate (push) Canceled after 0s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
feat(releaseupdate): signed OCI distribution sources for releases and the catalogue (#1715)
2026-10-31 18:18:43 +11:00

134 lines
4.4 KiB
Go

package releaseupdate
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"time"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/credentials"
"github.com/samcharles93/archie-core/internal/infrastructure/signedoci"
)
// PublishRequest is one release to sign and push.
type PublishRequest struct {
// Repository is the OCI repository the release is pushed to.
Repository string
Signer ed25519.PrivateKey
Version string
Prerelease bool
// ZipPath is the distribution zip, named ZipName(Version).
ZipPath string
AgentImage string
Changelog string
// Channels are the moving tags pointed at this release besides its own
// version tag.
Channels []string
}
// Publish signs a statement for the release and pushes it with the zip as
// one artifact, tagged with its version and each channel. Registry
// credentials come from the Docker config, as `docker login` left them.
func Publish(ctx context.Context, req PublishRequest) (Statement, error) {
archive, err := os.ReadFile(req.ZipPath)
if err != nil {
return Statement{}, err
}
statement := Statement{
Version: req.Version, Prerelease: req.Prerelease, IssuedAt: time.Now().UTC(), Platform: Platform,
Zip: Blob{Name: ZipName(req.Version), Digest: signedoci.Digest(archive), Size: int64(len(archive))},
AgentImage: req.AgentImage, Changelog: req.Changelog,
}
if err := statement.Validate(); err != nil {
return Statement{}, err
}
for _, channel := range req.Channels {
if channel != "stable" && channel != "next" {
return Statement{}, fmt.Errorf("unknown channel %q", channel)
}
}
encoded, err := json.Marshal(statement)
if err != nil {
return Statement{}, err
}
repository, err := pushRepository(req.Repository)
if err != nil {
return Statement{}, err
}
layers := []ocispec.Descriptor{
{MediaType: StatementMediaType, Digest: digest.FromBytes(encoded), Size: int64(len(encoded))},
{
MediaType: "application/zip", Digest: digest.FromBytes(archive), Size: int64(len(archive)),
Annotations: map[string]string{ocispec.AnnotationTitle: statement.Zip.Name},
},
}
for i, content := range [][]byte{ocispec.DescriptorEmptyJSON.Data, encoded, archive} {
desc := ocispec.DescriptorEmptyJSON
if i > 0 {
desc = layers[i-1]
}
if err := pushBlob(ctx, repository, desc, content); err != nil {
return Statement{}, err
}
}
manifest, err := json.Marshal(ocispec.Manifest{
SchemaVersion: 2, MediaType: ocispec.MediaTypeImageManifest,
ArtifactType: ArtifactType, Config: ocispec.DescriptorEmptyJSON, Layers: layers,
Annotations: map[string]string{
Annotation: base64.StdEncoding.EncodeToString(ed25519.Sign(req.Signer, encoded)),
ocispec.AnnotationCreated: statement.IssuedAt.Format(time.RFC3339),
},
})
if err != nil {
return Statement{}, err
}
desc := ocispec.Descriptor{MediaType: ocispec.MediaTypeImageManifest, Digest: digest.FromBytes(manifest), Size: int64(len(manifest))}
if err := repository.PushReference(ctx, desc, bytes.NewReader(manifest), Tag("exact-pin", req.Version)); err != nil {
return Statement{}, fmt.Errorf("push release manifest: %w", err)
}
for _, channel := range req.Channels {
if err := repository.Tag(ctx, desc, channel); err != nil {
return Statement{}, fmt.Errorf("tag %s: %w", channel, err)
}
}
return statement, nil
}
func pushRepository(reference string) (*remote.Repository, error) {
repository, err := remote.NewRepository(reference)
if err != nil {
return nil, err
}
repository.PlainHTTP = signedoci.Loopback(reference)
store, err := credentials.NewStoreFromDocker(credentials.StoreOptions{})
if err != nil {
return nil, fmt.Errorf("read registry credentials: %w", err)
}
repository.Client = &auth.Client{Credential: credentials.Credential(store)}
return repository, nil
}
// pushBlob uploads content unless the registry already holds it.
func pushBlob(ctx context.Context, repository *remote.Repository, desc ocispec.Descriptor, content []byte) error {
exists, err := repository.Exists(ctx, desc)
if err != nil {
return fmt.Errorf("check blob %s: %w", desc.Digest, err)
}
if exists {
return nil
}
if err := repository.Push(ctx, desc, bytes.NewReader(content)); err != nil {
return fmt.Errorf("push blob %s: %w", desc.Digest, err)
}
return nil
}