Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): restrict geolocated default region to provider regions (#49141)

Alaister Youngaa3643f39d39
diff --git a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts
index 4f06720b287..fd2b5c74c96 100644
--- a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts
+++ b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts
@@ -1,3 +1,4 @@
+import { AWS_REGIONS } from 'shared-data'
import { describe, expect, it } from 'vitest'
import {
@@ -7,9 +8,82 @@ import {
} from './ProjectCreation.constants'
import {
filterHighAvailabilityRegions,
+ getAvailableRegions,
getHighAvailabilityRegionCode,
+ resolveDefaultDbRegion,
} from './ProjectCreation.utils'
+describe('resolveDefaultDbRegion', () => {
+ const base = {
+ cloudProvider: 'AWS_NIMBUS',
+ isHighAvailabilityRestricted: false,
+ highAvailabilityRegionName: undefined,
+ isSmartRegionEnabled: false,
+ recommendedSmartRegion: undefined,
+ autoDefaultRegion: undefined,
+ fixedDefaultRegion: AWS_REGIONS.EAST_US.displayName,
+ environment: 'prod',
+ } as const
+
+ it('prefers the high availability region when restricted, even while it is still loading', () => {
+ expect(
+ resolveDefaultDbRegion({
+ ...base,
+ isHighAvailabilityRestricted: true,
+ highAvailabilityRegionName: AWS_REGIONS.EAST_US.displayName,
+ })
+ ).toBe(AWS_REGIONS.EAST_US.displayName)
+ expect(resolveDefaultDbRegion({ ...base, isHighAvailabilityRestricted: true })).toBeUndefined()
+ })
+
+ it('uses the recommended smart region when smart regions are enabled', () => {
+ expect(
+ resolveDefaultDbRegion({
+ ...base,
+ cloudProvider: 'AWS',
+ isSmartRegionEnabled: true,
+ recommendedSmartRegion: 'Americas',
+ autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName,
+ })
+ ).toBe('Americas')
+ })
+
+ it('uses the geolocated region when the provider offers it', () => {
+ expect(
+ resolveDefaultDbRegion({
+ ...base,
+ cloudProvider: 'AWS',
+ autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName,
+ })
+ ).toBe(AWS_REGIONS.SOUTHEAST_ASIA.displayName)
+ })
+
+ it('falls back to the fixed default when the provider does not offer the geolocated region', () => {
+ expect(
+ resolveDefaultDbRegion({ ...base, autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName })
+ ).toBe(AWS_REGIONS.EAST_US.displayName)
+ })
+
+ it('falls back to the fixed default when no geolocated region resolved', () => {
+ expect(resolveDefaultDbRegion(base)).toBe(AWS_REGIONS.EAST_US.displayName)
+ })
+})
+
+describe('getAvailableRegions', () => {
+ it.each(['local', 'staging', 'prod'])('returns all AWS regions for AWS on %s', (environment) => {
+ expect(getAvailableRegions('AWS', environment)).toEqual(AWS_REGIONS)
+ expect(getAvailableRegions('AWS_K8S', environment)).toEqual(AWS_REGIONS)
+ })
+
+ it.each([
+ ['local', { SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA }],
+ ['staging', { SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA }],
+ ['prod', { EAST_US: AWS_REGIONS.EAST_US }],
+ ])('returns the single AWS_NIMBUS region on %s', (environment, expectedRegions) => {
+ expect(getAvailableRegions('AWS_NIMBUS', environment)).toEqual(expectedRegions)
+ })
+})
+
describe('High Availability project creation constraints', () => {
it('pins the Alpha Postgres engine, release channel, and compute size', () => {
expect(HIGH_AVAILABILITY_POSTGRES_ENGINE).toBe('17')
diff --git a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.ts b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.ts
index a99780d99c7..f04d8bca8a5 100644
--- a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.ts
+++ b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.ts
@@ -8,13 +8,16 @@ export function smartRegionToExactRegion(smartOrExactRegion: string) {
return SMART_REGION_TO_EXACT_REGION_MAP.get(smartOrExactRegion) ?? smartOrExactRegion
}
-export function getAvailableRegions(cloudProvider: CloudProvider): Region {
+export function getAvailableRegions(
+ cloudProvider: CloudProvider,
+ environment = process.env.NEXT_PUBLIC_ENVIRONMENT
+): Region {
switch (cloudProvider) {
case 'AWS':
case 'AWS_K8S':
return AWS_REGIONS
case 'AWS_NIMBUS':
- if (process.env.NEXT_PUBLIC_ENVIRONMENT !== 'prod') {
+ if (environment !== 'prod') {
// Only allow Southeast Asia for Nimbus (local/staging)
return {
SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA,
@@ -30,6 +33,41 @@ export function getAvailableRegions(cloudProvider: CloudProvider): Region {
}
}
+type ResolveDefaultDbRegionArgs = {
+ cloudProvider: CloudProvider
+ isHighAvailabilityRestricted: boolean
+ highAvailabilityRegionName: string | undefined
+ isSmartRegionEnabled: boolean
+ recommendedSmartRegion: string | undefined
+ autoDefaultRegion: string | undefined
+ fixedDefaultRegion: string
+ environment?: string
+}
+
+export function resolveDefaultDbRegion({
+ cloudProvider,
+ isHighAvailabilityRestricted,
+ highAvailabilityRegionName,
+ isSmartRegionEnabled,
+ recommendedSmartRegion,
+ autoDefaultRegion,
+ fixedDefaultRegion,
+ environment = process.env.NEXT_PUBLIC_ENVIRONMENT,
+}: ResolveDefaultDbRegionArgs): string | undefined {
+ if (isHighAvailabilityRestricted) return highAvailabilityRegionName
+ if (isSmartRegionEnabled) return recommendedSmartRegion
+
+ // The geolocated default is only usable if the provider actually offers that region
+ // (e.g. AWS_NIMBUS is restricted to a single region)
+ const isAutoDefaultRegionAvailable = Object.entries(
+ getAvailableRegions(cloudProvider, environment)
+ ).some(([, region]) => region.displayName === autoDefaultRegion)
+
+ return isAutoDefaultRegionAvailable && autoDefaultRegion !== undefined
+ ? autoDefaultRegion
+ : fixedDefaultRegion
+}
+
/**
* When launching new projects, they only get assigned a compute size once successfully launched,
* this might assume wrong compute size, but only for projects being rapidly launched after one another on non-default compute sizes.
diff --git a/apps/studio/components/interfaces/ProjectCreation/ProjectCreationForm.tsx b/apps/studio/components/interfaces/ProjectCreation/ProjectCreationForm.tsx
index 7b8b4639dc8..5f3af194804 100644
--- a/apps/studio/components/interfaces/ProjectCreation/ProjectCreationForm.tsx
+++ b/apps/studio/components/interfaces/ProjectCreation/ProjectCreationForm.tsx
@@ -33,6 +33,7 @@ import {
getHighAvailabilityRegionCode,
instanceLabel,
monthlyInstancePrice,
+ resolveDefaultDbRegion,
smartRegionToExactRegion,
} from './ProjectCreation.utils'
import { ProjectCreationFooter } from './ProjectCreationFooter'
@@ -284,12 +285,16 @@ export const ProjectCreationForm = ({
const fixedDefaultRegion = PROVIDERS[selectedCloudProvider].default_region.displayName
const regionError = smartRegionEnabled ? availableRegionsError : defaultRegionError
- const defaultRegion =
- highAvailability && highAvailabilityRegionCode !== undefined
- ? highAvailabilityRegion?.name
- : smartRegionEnabled
- ? recommendedSmartRegion
- : (autoDefaultRegion ?? fixedDefaultRegion)
+ const defaultRegion = resolveDefaultDbRegion({
+ cloudProvider: selectedCloudProvider,
+ isHighAvailabilityRestricted:
+ highAvailability === true && highAvailabilityRegionCode !== undefined,
+ highAvailabilityRegionName: highAvailabilityRegion?.name,
+ isSmartRegionEnabled: smartRegionEnabled,
+ recommendedSmartRegion,
+ autoDefaultRegion,
+ fixedDefaultRegion,
+ })
const canCreateProject = isAdmin && !freePlanWithExceedingLimits && !hasOutstandingInvoices
const canConfigureGitHubOnCreate =
diff --git a/apps/studio/data/misc/get-default-region-query.test.ts b/apps/studio/data/misc/get-default-region-query.test.ts
new file mode 100644
index 00000000000..e5ff8d643bd
--- /dev/null
+++ b/apps/studio/data/misc/get-default-region-query.test.ts
@@ -0,0 +1,41 @@
+import { AWS_REGIONS } from 'shared-data'
+import { describe, expect, it } from 'vitest'
+
+import { getDefaultRegionCandidateKeys } from './get-default-region-query'
+
+describe('getDefaultRegionCandidateKeys', () => {
+ it('returns all AWS regions for the AWS provider', () => {
+ expect(getDefaultRegionCandidateKeys('AWS', undefined, 'prod')).toEqual(
+ Object.keys(AWS_REGIONS)
+ )
+ })
+
+ it.each([
+ ['prod', ['EAST_US']],
+ ['staging', ['SOUTHEAST_ASIA']],
+ ['local', ['SOUTHEAST_ASIA']],
+ ])('restricts AWS_NIMBUS to its only available region on %s', (environment, expectedKeys) => {
+ expect(getDefaultRegionCandidateKeys('AWS_NIMBUS', undefined, environment)).toEqual(
+ expectedKeys
+ )
+ })
+
+ it('narrows the provider regions to the restricted pool', () => {
+ expect(getDefaultRegionCandidateKeys('AWS', ['EAST_US', 'SOUTHEAST_ASIA'], 'prod')).toEqual([
+ 'EAST_US',
+ 'SOUTHEAST_ASIA',
+ ])
+ })
+
+ it('never returns a region the provider does not offer, even if the restricted pool contains it', () => {
+ expect(
+ getDefaultRegionCandidateKeys('AWS_NIMBUS', ['EAST_US', 'SOUTHEAST_ASIA'], 'prod')
+ ).toEqual(['EAST_US'])
+ })
+
+ it('ignores a restricted pool that excludes every provider region', () => {
+ expect(getDefaultRegionCandidateKeys('AWS_NIMBUS', ['SOUTHEAST_ASIA'], 'prod')).toEqual([
+ 'EAST_US',
+ ])
+ })
+})
diff --git a/apps/studio/data/misc/get-default-region-query.ts b/apps/studio/data/misc/get-default-region-query.ts
index d2c17c15060..264cf5f8471 100644
--- a/apps/studio/data/misc/get-default-region-query.ts
+++ b/apps/studio/data/misc/get-default-region-query.ts
@@ -5,6 +5,7 @@ import { AWS_REGIONS } from 'shared-data'
import { miscKeys } from './keys'
import { COUNTRY_LAT_LON } from '@/components/interfaces/ProjectCreation/ProjectCreation.constants'
+import { getAvailableRegions } from '@/components/interfaces/ProjectCreation/ProjectCreation.utils'
import { AWS_REGIONS_COORDINATES } from '@/components/interfaces/Settings/Infrastructure/InfrastructureConfiguration/InstanceConfiguration.constants'
import { fetchHandler } from '@/data/fetchers'
import { getDistanceLatLonKM, tryParseJson } from '@/lib/helpers'
@@ -16,6 +17,21 @@ export type DefaultRegionVariables = {
useRestrictedPool?: boolean
}
+// The geolocation-based default may only ever pick a region the selected cloud provider
+// actually offers (e.g. AWS_NIMBUS is restricted to a single region). The flag-based
+// restricted pool narrows within that set, and is ignored if it would leave no candidates.
+export function getDefaultRegionCandidateKeys(
+ cloudProvider: CloudProvider,
+ restrictedPool?: string[],
+ environment = process.env.NEXT_PUBLIC_ENVIRONMENT
+) {
+ const providerRegionKeys = Object.keys(getAvailableRegions(cloudProvider, environment))
+ const pooledRegionKeys = restrictedPool
+ ? providerRegionKeys.filter((key) => restrictedPool.includes(key))
+ : providerRegionKeys
+ return pooledRegionKeys.length > 0 ? pooledRegionKeys : providerRegionKeys
+}
+
export async function getDefaultRegionOption({
cloudProvider,
restrictedPool,
@@ -34,17 +50,18 @@ export async function getDefaultRegionOption({
if (locLatLon === undefined) return undefined
- const locations =
- useRestrictedPool && restrictedPool
- ? Object.entries(AWS_REGIONS_COORDINATES)
- .filter((x) => restrictedPool.includes(x[0]))
- .reduce((o, val) => ({ ...o, [val[0]]: val[1] }), {})
- : AWS_REGIONS_COORDINATES
+ const candidateKeys = getDefaultRegionCandidateKeys(
+ cloudProvider,
+ useRestrictedPool ? restrictedPool : undefined
+ )
+ const locations = Object.fromEntries(
+ Object.entries(AWS_REGIONS_COORDINATES).filter(([key]) => candidateKeys.includes(key))
+ )
const distances = Object.keys(locations).map((reg) => {
const region: { lat: number; lon: number } = {
- lat: locations[reg as keyof typeof locations][1],
- lon: locations[reg as keyof typeof locations][0],
+ lat: locations[reg][1],
+ lon: locations[reg][0],
}
return getDistanceLatLonKM(locLatLon.lat, locLatLon.lon, region.lat, region.lon)
})