@@ -2,7 +2,7 @@ import { lookup } from 'node:dns/promises'
22import { isIP } from 'node:net'
33import { networkInterfaces } from 'node:os'
44import { SANDBOX_AGENT_PORT } from '@sim/sandbox-agent-protocol'
5- import { env } from '@/lib/core/config/env'
5+ import { env , envNumber } from '@/lib/core/config/env'
66import { getSelectedSandboxProviderId } from '@/lib/core/config/env-capabilities.server'
77import { CodeLanguage } from '@/lib/execution/languages'
88import { executeInSandbox } from '@/lib/execution/remote-sandbox'
@@ -18,6 +18,7 @@ import {
1818 type LateralTargetKind ,
1919} from '@/lib/self-check/confinement'
2020import { type CheckOutcome , errorCode } from '@/lib/self-check/outcome'
21+ import { DATABASE_URL_NAMES , dataStoreEndpoints } from '@/lib/self-check/targets'
2122import type { SelfCheckName , SelfCheckResult } from '@/lib/self-check/types'
2223
2324/** Two sandbox starts and one probe, inside the route's `maxDuration`. */
@@ -65,23 +66,32 @@ function appAddresses(): string[] {
6566 . filter ( isProbeTargetAddress )
6667}
6768
68- /** The data store's addresses as the app resolves them, since user code runs in another namespace. */
69- async function dataStoreTargets ( url : string | undefined , defaultPort : number ) : Promise < Target [ ] > {
70- if ( ! url ) return [ ]
71- let parsed : URL
72- try {
73- parsed = new URL ( url )
74- } catch {
75- return [ ]
76- }
77- const host = parsed . hostname . replace ( / ^ \[ | \] $ / g, '' )
78- const port = Number ( parsed . port ) || defaultPort
79- if ( ! host ) return [ ]
80- const addresses = isIP ( host )
81- ? [ host ]
82- : ( await lookup ( host , { all : true } ) . catch ( ( ) => [ ] ) ) . map ( ( { address } ) => address )
83- // A loopback or link-local address is a sidecar proxy in the app pod, not the data store.
84- return addresses . filter ( isProbeTargetAddress ) . map ( ( address ) : Target => [ address , port ] )
69+ /**
70+ * Every database and Redis address the app is configured with, resolved as the app resolves them
71+ * (user code runs in another namespace, where short Service names do not resolve), each once.
72+ */
73+ async function dataStoreTargets ( ) : Promise < Array < { target : Target ; kind : LateralTargetKind } > > {
74+ const endpoints = dataStoreEndpoints ( {
75+ ...Object . fromEntries ( DATABASE_URL_NAMES . map ( ( name ) => [ name , env [ name ] ] ) ) ,
76+ REDIS_URL : env . REDIS_URL ,
77+ } )
78+ const resolved = await Promise . all (
79+ endpoints . map ( async ( { kind, host, port } ) => {
80+ const addresses = isIP ( host )
81+ ? [ host ]
82+ : ( await lookup ( host , { all : true } ) . catch ( ( ) => [ ] ) ) . map ( ( { address } ) => address )
83+ // A loopback or link-local address is a sidecar proxy in the app pod, not the data store.
84+ return addresses
85+ . filter ( isProbeTargetAddress )
86+ . map ( ( address ) => ( { target : [ address , port ] as Target , kind } ) )
87+ } )
88+ )
89+ const seen = new Set < string > ( )
90+ return resolved . flat ( ) . filter ( ( { target } ) => {
91+ if ( seen . has ( key ( target ) ) ) return false
92+ seen . add ( key ( target ) )
93+ return true
94+ } )
8595}
8696
8797/**
@@ -179,17 +189,10 @@ async function probe(
179189 signal : AbortSignal ,
180190 elapsed : ( ) => number
181191) : Promise < SelfCheckResult [ ] > {
182- const appPort = Number ( env . PORT ) || DEFAULT_APP_PORT
192+ const appPort = envNumber ( env . PORT , DEFAULT_APP_PORT , { min : 1 , integer : true } )
183193 const lateral : Array < { target : Target ; kind : LateralTargetKind } > = [
184194 ...appAddresses ( ) . map ( ( host ) => ( { target : [ host , appPort ] as Target , kind : 'app' as const } ) ) ,
185- ...( await dataStoreTargets ( env . DATABASE_URL , 5432 ) ) . map ( ( target ) => ( {
186- target,
187- kind : 'database' as const ,
188- } ) ) ,
189- ...( await dataStoreTargets ( env . REDIS_URL , 6379 ) ) . map ( ( target ) => ( {
190- target,
191- kind : 'redis' as const ,
192- } ) ) ,
195+ ...( await dataStoreTargets ( ) ) ,
193196 ]
194197 const neighborTarget : Target = [ neighborAddress , SANDBOX_AGENT_PORT ]
195198 const apiTargets : Target [ ] = [ [ CLUSTER_API_NAME , 443 ] ]
@@ -216,6 +219,7 @@ async function probe(
216219 const plan : ConfinementPlan = {
217220 lateral : lateral . map ( ( { target, kind } ) => ( { key : key ( target ) , kind } ) ) ,
218221 neighbor : key ( neighborTarget ) ,
222+ apiTargets : apiTargets . map ( key ) ,
219223 resolveNames : RESOLVE_NAMES ,
220224 }
221225 const judged = judgeProbeResult ( execution . result , plan )
0 commit comments