Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec // indirect
Expand Down Expand Up @@ -225,7 +226,7 @@ require (
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -584,6 +586,8 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
4 changes: 1 addition & 3 deletions internal/api/attestationconfigapi/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package attestationconfigapi

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -18,7 +17,6 @@ import (

"github.com/flashbots/cvm-reverse-proxy/internal/attestation/variant"
"github.com/flashbots/cvm-reverse-proxy/internal/constants"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -104,7 +102,7 @@ func TestFetchLatestSEVSNPVersion(t *testing.T) {
},
}
fetcher := newFetcherWithClientAndVerifier(client, stubVerifier{}, constants.CDNRepositoryURL)
res, err := fetcher.FetchLatestVersion(context.Background(), tc.attestation)
res, err := fetcher.FetchLatestVersion(t.Context(), tc.attestation)
assert := assert.New(t)
if tc.wantErr {
assert.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/attestationconfigapi/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (i *List) SortReverse() {
}

// AddVersion adds new to i's list and sorts the element in descending order.
func (i *List) AddVersion(new string) {
i.List = append(i.List, new)
func (i *List) AddVersion(ver string) {
i.List = append(i.List, ver)
i.List = variant.RemoveDuplicate(i.List)

i.SortReverse()
Expand Down
96 changes: 25 additions & 71 deletions internal/api/versionsapi/cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
apiclient "github.com/flashbots/cvm-reverse-proxy/internal/api/client"
"github.com/flashbots/cvm-reverse-proxy/internal/api/versionsapi"
"github.com/flashbots/cvm-reverse-proxy/internal/logger"

"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

func newAddCmd() *cobra.Command {
Expand Down Expand Up @@ -54,19 +52,8 @@ func runAdd(cmd *cobra.Command, _ []string) (retErr error) {
return err
}
log := logger.NewTextLogger(flags.logLevel)
log.Debug("Using flags", "dryRun", flags.dryRun, "kind", flags.kind, "latest", flags.latest, "ref", flags.ref,
"release", flags.release, "stream", flags.stream, "version", flags.version)

log.Debug("Validating flags")
if err := flags.validate(log); err != nil {
return err
}

log.Debug("Creating version struct")
ver, err := versionsapi.NewVersion(flags.ref, flags.stream, flags.version, flags.kind)
if err != nil {
return fmt.Errorf("creating version: %w", err)
}
log.Debug("Using flags", "dryRun", flags.dryRun, "kind", flags.version.Kind(), "latest", flags.latest, "ref", flags.version.Ref(),
"stream", flags.version.Stream(), "version", flags.version.Version())

log.Debug("Creating versions API client")
client, clientClose, err := versionsapi.NewClient(cmd.Context(), flags.region, flags.bucket, flags.distributionID, flags.dryRun, log)
Expand All @@ -81,35 +68,35 @@ func runAdd(cmd *cobra.Command, _ []string) (retErr error) {
}()

log.Info("Adding version")
if err := ensureVersion(cmd.Context(), client, flags.kind, ver, versionsapi.GranularityMajor, log); err != nil {
if err := ensureVersion(cmd.Context(), client, flags.version, versionsapi.GranularityMajor, log); err != nil {
return err
}

if err := ensureVersion(cmd.Context(), client, flags.kind, ver, versionsapi.GranularityMinor, log); err != nil {
if err := ensureVersion(cmd.Context(), client, flags.version, versionsapi.GranularityMinor, log); err != nil {
return err
}

if flags.latest {
if err := updateLatest(cmd.Context(), client, flags.kind, ver, log); err != nil {
if err := updateLatest(cmd.Context(), client, flags.version, log); err != nil {
return fmt.Errorf("setting latest version: %w", err)
}
}

log.Info(fmt.Sprintf("List major->minor URL: %s", ver.ListURL(versionsapi.GranularityMajor)))
log.Info(fmt.Sprintf("List minor->patch URL: %s", ver.ListURL(versionsapi.GranularityMinor)))
log.Info(fmt.Sprintf("List major->minor URL: %s", flags.version.ListURL(versionsapi.GranularityMajor)))
log.Info(fmt.Sprintf("List minor->patch URL: %s", flags.version.ListURL(versionsapi.GranularityMinor)))

return nil
}

func ensureVersion(ctx context.Context, client *versionsapi.Client, kind versionsapi.VersionKind, ver versionsapi.Version, gran versionsapi.Granularity,
func ensureVersion(ctx context.Context, client *versionsapi.Client, ver versionsapi.Version, gran versionsapi.Granularity,
log *slog.Logger,
) error {
verListReq := versionsapi.List{
Ref: ver.Ref(),
Stream: ver.Stream(),
Granularity: gran,
Base: ver.WithGranularity(gran),
Kind: kind,
Kind: ver.Kind(),
}
verList, err := client.FetchVersionList(ctx, verListReq)
var notFoundErr *apiclient.NotFoundError
Expand Down Expand Up @@ -141,11 +128,11 @@ func ensureVersion(ctx context.Context, client *versionsapi.Client, kind version
return nil
}

func updateLatest(ctx context.Context, client *versionsapi.Client, kind versionsapi.VersionKind, ver versionsapi.Version, log *slog.Logger) error {
func updateLatest(ctx context.Context, client *versionsapi.Client, ver versionsapi.Version, log *slog.Logger) error {
latest := versionsapi.Latest{
Ref: ver.Ref(),
Stream: ver.Stream(),
Kind: kind,
Kind: ver.Kind(),
}
latest, err := client.FetchVersionLatest(ctx, latest)
var notFoundErr *apiclient.NotFoundError
Expand All @@ -165,7 +152,7 @@ func updateLatest(ctx context.Context, client *versionsapi.Client, kind versions
Ref: ver.Ref(),
Stream: ver.Stream(),
Version: ver.Version(),
Kind: kind,
Kind: ver.Kind(),
}
if err := client.UpdateVersionLatest(ctx, latest); err != nil {
return fmt.Errorf("updating latest version: %w", err)
Expand All @@ -175,60 +162,20 @@ func updateLatest(ctx context.Context, client *versionsapi.Client, kind versions
}

type addFlags struct {
version string
stream string
ref string
release bool
version versionsapi.Version
latest bool
dryRun bool
region string
bucket string
distributionID string
kind versionsapi.VersionKind
logLevel slog.Level
}

func (f *addFlags) validate(log *slog.Logger) error {
if !semver.IsValid(f.version) {
return fmt.Errorf("version %q is not a valid semantic version", f.version)
}
if semver.Canonical(f.version) != f.version {
return fmt.Errorf("version %q is not a canonical semantic version", f.version)
}

if f.ref == "" && !f.release {
return fmt.Errorf("either --ref or --release must be set")
}

if f.kind == versionsapi.VersionKindUnknown {
return fmt.Errorf("unknown version kind %q", f.kind)
}

if f.release {
log.Debug(fmt.Sprintf("Setting ref to %q, as release flag is set", versionsapi.ReleaseRef))
f.ref = versionsapi.ReleaseRef
} else {
log.Debug("Setting latest to true, as release flag is not set")
f.latest = true // always set latest for non-release versions
}

if err := versionsapi.ValidateRef(f.ref); err != nil {
return fmt.Errorf("invalid ref %w", err)
}

if err := versionsapi.ValidateStream(f.ref, f.stream); err != nil {
return fmt.Errorf("invalid stream %w", err)
}

return nil
}

func parseAddFlags(cmd *cobra.Command) (addFlags, error) {
ref, err := cmd.Flags().GetString("ref")
if err != nil {
return addFlags{}, err
}
ref = versionsapi.CanonicalizeRef(ref)
stream, err := cmd.Flags().GetString("stream")
if err != nil {
return addFlags{}, err
Expand Down Expand Up @@ -275,17 +222,24 @@ func parseAddFlags(cmd *cobra.Command) (addFlags, error) {
return addFlags{}, err
}

if release {
ref = versionsapi.ReleaseRef
} else {
latest = true // always set latest for non-release versions
}

ver, err := versionsapi.NewVersion(ref, stream, version, kind)
if err != nil {
return addFlags{}, fmt.Errorf("creating version: %w", err)
}

return addFlags{
version: version,
stream: stream,
ref: versionsapi.CanonicalizeRef(ref),
release: release,
version: ver,
latest: latest,
dryRun: dryRun,
region: region,
bucket: bucket,
distributionID: distributionID,
logLevel: logLevel,
kind: kind,
}, nil
}
4 changes: 1 addition & 3 deletions internal/api/versionsapi/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ package versionsapi

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"

"github.com/flashbots/cvm-reverse-proxy/internal/constants"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
Expand Down Expand Up @@ -193,7 +191,7 @@ func TestFetchVersionList(t *testing.T) {

fetcher := Fetcher{client, constants.CDNRepositoryURL}

list, err := fetcher.FetchVersionList(context.Background(), tc.list)
list, err := fetcher.FetchVersionList(t.Context(), tc.list)

if tc.wantErr {
assert.Error(err)
Expand Down
9 changes: 5 additions & 4 deletions internal/api/versionsapi/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Version struct {
// NewVersion creates a new Version object and validates it.
func NewVersion(ref, stream, version string, kind VersionKind) (Version, error) {
ver := Version{
ref: ref,
ref: CanonicalizeRef(ref),
stream: stream,
version: version,
kind: kind,
Expand All @@ -62,7 +62,7 @@ func NewVersionFromShortPath(shortPath string, kind VersionKind) (Version, error
}

ver := Version{
ref: ref,
ref: ref, // Canonicalized by parseShortPath.
stream: stream,
version: version,
kind: kind,
Expand Down Expand Up @@ -331,7 +331,7 @@ func CanonicalizeRef(ref string) string {
canRef := notAZ09Regexp.ReplaceAllString(ref, "-")

if canRef == ReleaseRef {
return "" // No ref should be cannonicalized to the release ref.
return "" // No ref should be canonicalized to the release ref.
}

return canRef
Expand Down Expand Up @@ -401,7 +401,7 @@ func MeasurementURL(version Version) (measurementURL, signatureURL *url.URL, err
}

var (
shortPathRegex = regexp.MustCompile(`^ref/([a-zA-Z0-9-]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
shortPathRegex = regexp.MustCompile(`^ref/([^/]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
shortPathReleaseRegex = regexp.MustCompile(`^stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
)

Expand All @@ -422,6 +422,7 @@ func parseShortPath(shortPath string) (ref, stream, version string, err error) {
if shortPathRegex.MatchString(shortPath) {
matches := shortPathRegex.FindStringSubmatch(shortPath)
ref := matches[1]
ref = CanonicalizeRef(ref)
if err := ValidateRef(ref); err != nil {
return "", "", "", err
}
Expand Down
Loading
Loading