diff --git a/internal/controlplane/api/creator.go b/internal/controlplane/api/creator.go index 96c3cbc..c0b4c58 100644 --- a/internal/controlplane/api/creator.go +++ b/internal/controlplane/api/creator.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "io" + "math/rand" "net/http" "net/url" "os" @@ -1630,6 +1631,10 @@ func waitForAnonymousCleanup(ctx context.Context, delay time.Duration) error { } } +func randomGateway(gateways []hub.Gateway) hub.Gateway { + return gateways[rand.Intn(len(gateways))] +} + func newAnonymousBrowser(ctx context.Context, store *hub.Store) (anonymousBrowserLease, error) { if store == nil { return anonymousBrowserLease{}, creator.ErrUnavailable @@ -1646,23 +1651,15 @@ func newAnonymousBrowser(ctx context.Context, store *hub.Store) (anonymousBrowse return anonymousBrowserLease{}, fmt.Errorf("%w: anonymous browser gateway is not configured", creator.ErrUnavailable) } - gatewayByName := make(map[string]hub.Gateway, len(gateways)) - for _, gateway := range gateways { - gatewayByName[gateway.Name] = gateway - } - + gateway := randomGateway(gateways) var template hub.Env - var gateway hub.Gateway for _, candidate := range envs { - candidateGateway, ok := gatewayByName[candidate.Gateway] - if !ok { - continue + if candidate.Gateway == gateway.Name { + template = candidate + break } - template, gateway = candidate, candidateGateway - break } - if gateway.Name == "" { - gateway = gateways[0] + if template.Gateway == "" { template = hub.Env{Fingerprint: hub.Fingerprint{Seed: 1}} } diff --git a/internal/controlplane/api/creator_share_test.go b/internal/controlplane/api/creator_share_test.go index 7a08159..21ecc39 100644 --- a/internal/controlplane/api/creator_share_test.go +++ b/internal/controlplane/api/creator_share_test.go @@ -42,6 +42,24 @@ func TestCompetitorSharePlatform(t *testing.T) { } } +func TestRandomGatewaySelectsFromRegisteredGateways(t *testing.T) { + gateways := []hub.Gateway{ + {Name: "gateway-a"}, + {Name: "gateway-b"}, + {Name: "gateway-c"}, + } + allowed := map[string]bool{ + "gateway-a": true, + "gateway-b": true, + "gateway-c": true, + } + for range 32 { + if gateway := randomGateway(gateways); !allowed[gateway.Name] { + t.Fatalf("randomGateway selected unregistered gateway %q", gateway.Name) + } + } +} + func TestAnonymousBrowserLeasePurgesRuntimeAndProfile(t *testing.T) { var requestBody map[string]any server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {