@@ -19,11 +19,11 @@ import * as RpcHttp from "../rpc/http.ts";
1919import * as ClientCapabilities from "../platform/capabilities.ts" ;
2020import * as RemoteEnvironmentAuthorization from "./service.ts" ;
2121import * as TokenStore from "./tokenStore.ts" ;
22- import * as ConnectionResolver from "../connection/resolver.ts" ;
23- import * as ConnectionProfileStore from "../connection/profileStore.ts" ;
24- import * as ConnectionCredentialStore from "../connection/credentialStore.ts" ;
2522import { BearerConnectionCredential , BearerConnectionProfile } from "../connection/catalog.ts" ;
23+ import * as ConnectionCredentialStore from "../connection/credentialStore.ts" ;
2624import { BearerConnectionTarget } from "../connection/model.ts" ;
25+ import * as ConnectionProfileStore from "../connection/profileStore.ts" ;
26+ import * as ConnectionResolver from "../connection/resolver.ts" ;
2727
2828const ENVIRONMENT_ID = EnvironmentId . make ( "environment-1" ) ;
2929const ENDPOINT = {
@@ -52,17 +52,12 @@ const BOOTSTRAP: RelayEnvironmentConnectResponse = {
5252
5353type RecordedResponse = Response | ( ( init : RequestInit ) => Response ) ;
5454
55- function recordedFetch (
56- responses :
57- | ReadonlyArray < RecordedResponse >
58- | ( ( input : RequestInfo | URL , init : RequestInit ) => Response ) ,
59- ) {
55+ function recordedFetch ( responses : ReadonlyArray < RecordedResponse > ) {
6056 const calls : Array < readonly [ RequestInfo | URL , RequestInit ] > = [ ] ;
6157 let responseIndex = 0 ;
6258 const fetchFn = ( ( input , init ) => {
6359 calls . push ( [ input , init ?? { } ] ) ;
64- const response =
65- typeof responses === "function" ? responses ( input , init ?? { } ) : responses [ responseIndex ++ ] ;
60+ const response = responses [ responseIndex ++ ] ;
6661 return response === undefined
6762 ? Promise . reject ( new Error ( `Unexpected fetch call to ${ String ( input ) } ` ) )
6863 : Promise . resolve ( typeof response === "function" ? response ( init ?? { } ) : response ) ;
@@ -116,7 +111,7 @@ const persistedToken = (
116111
117112const makeHarness = Effect . fn ( "TestRemoteAuthorization.makeHarness" ) ( function * ( input : {
118113 readonly initialToken ?: TokenStore . RemoteDpopAccessToken ;
119- readonly responses : Parameters < typeof recordedFetch > [ 0 ] ;
114+ readonly responses : ReadonlyArray < RecordedResponse > ;
120115 readonly bootstrap ?: RelayEnvironmentConnectResponse ;
121116 readonly beforeBootstrap ?: Effect . Effect < void , ManagedRelay . ManagedRelayClientError > ;
122117 readonly beforePut ?: Effect . Effect < void > ;
@@ -239,29 +234,29 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
239234 } ;
240235} ) ;
241236
242- const makeResolverHarness = Effect . fn ( "TestRemoteAuthorization.makeResolverHarness" ) ( function * (
243- responses : Parameters < typeof recordedFetch > [ 0 ] ,
244- options ?: {
245- readonly authorization ?: "t3-connect" ;
246- readonly initialToken ?: TokenStore . RemoteDpopAccessToken ;
247- readonly bootstrap ?: RelayEnvironmentConnectResponse ;
248- } ,
237+ const prepareDirectBearer = Effect . fn ( "TestRemoteAuthorization.prepareDirectBearer" ) ( function * (
238+ harness : Effect . Success < ReturnType < typeof makeHarness > > ,
239+ times : number ,
249240) {
250- const harness = yield * makeHarness ( { ...options , responses } ) ;
251241 const target = new BearerConnectionTarget ( {
252242 environmentId : ENVIRONMENT_ID ,
253243 label : DESCRIPTOR . label ,
254244 connectionId : "direct-1" ,
255245 } ) ;
256- const profile = new BearerConnectionProfile ( {
257- connectionId : target . connectionId ,
258- environmentId : target . environmentId ,
259- label : target . label ,
260- httpBaseUrl : ENDPOINT . httpBaseUrl ,
261- wsBaseUrl : ENDPOINT . wsBaseUrl ,
262- ...( options ?. authorization === undefined ? { } : { authorization : options . authorization } ) ,
263- } ) ;
264- const unused = ( ) => Effect . die ( "Unexpected connection store or SSH operation" ) ;
246+ const entry = {
247+ target,
248+ profile : Option . some (
249+ new BearerConnectionProfile ( {
250+ connectionId : target . connectionId ,
251+ environmentId : ENVIRONMENT_ID ,
252+ label : target . label ,
253+ httpBaseUrl : ENDPOINT . httpBaseUrl ,
254+ wsBaseUrl : ENDPOINT . wsBaseUrl ,
255+ } ) ,
256+ ) ,
257+ enabled : true ,
258+ } ;
259+ const unused = ( ) => Effect . die ( "unused" ) ;
265260 const layer = ConnectionResolver . layer . pipe (
266261 Layer . provide (
267262 Layer . mergeAll (
@@ -287,75 +282,74 @@ const makeResolverHarness = Effect.fn("TestRemoteAuthorization.makeResolverHarne
287282 ) ,
288283 ) ,
289284 ) ;
290- return { ...harness , layer, entry : { target, profile : Option . some ( profile ) , enabled : true } } ;
285+ const resolver = yield * ConnectionResolver . ConnectionResolver . pipe ( Effect . provide ( layer ) ) ;
286+ return yield * Effect . forEach ( Array . from ( { length : times } ) , ( ) =>
287+ resolver . prepare ( entry ) . pipe ( Effect . result ) ,
288+ ) ;
291289} ) ;
292290
293291describe ( "RemoteEnvironmentAuthorization" , ( ) => {
294- it . effect ( "uses one fresh descriptor GET for a cold direct bearer prepare " , ( ) =>
292+ it . effect ( "prepares a cold direct bearer connection with one descriptor request " , ( ) =>
295293 Effect . gen ( function * ( ) {
296- const descriptor = {
297- ...DESCRIPTOR ,
298- orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
299- } ;
300- const harness = yield * makeResolverHarness ( ( url ) =>
301- String ( url ) . endsWith ( "/.well-known/t3/environment" )
302- ? Response . json ( descriptor )
303- : websocketTicket ( "cold-ticket" ) ,
304- ) ;
305- const prepared = yield * Effect . gen ( function * ( ) {
306- const resolver = yield * ConnectionResolver . ConnectionResolver ;
307- return yield * resolver . prepare ( harness . entry ) ;
308- } ) . pipe ( Effect . provide ( harness . layer ) ) ;
294+ const harness = yield * makeHarness ( {
295+ responses : [
296+ Response . json ( {
297+ ...DESCRIPTOR ,
298+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
299+ } ) ,
300+ websocketTicket ( "cold-ticket" ) ,
301+ ] ,
302+ } ) ;
309303
310- expect ( prepared . socketUrl ) . toContain ( "wsTicket=cold-ticket" ) ;
311- expect ( prepared ) . not . toHaveProperty ( "descriptor" ) ;
312- expect (
313- harness . fetch . calls
314- . filter ( ( [ url ] ) => String ( url ) . endsWith ( "/.well-known/t3/environment" ) )
315- . map ( ( [ , init ] ) => init . method ?? "GET" ) ,
316- ) . toEqual ( [ "GET" ] ) ;
304+ const [ prepared ] = yield * prepareDirectBearer ( harness , 1 ) ;
305+
306+ expect ( prepared ) . toMatchObject ( {
307+ _tag : "Success" ,
308+ success : { socketUrl : expect . stringContaining ( "wsTicket=cold-ticket" ) } ,
309+ } ) ;
310+ expect ( harness . fetch . calls . map ( ( [ url ] ) => new URL ( String ( url ) ) . pathname ) ) . toEqual ( [
311+ "/.well-known/t3/environment" ,
312+ "/api/auth/websocket-ticket/rpc" ,
313+ ] ) ;
317314 } ) ,
318315 ) ;
319316
320- it . effect . each ( [
321- {
322- name : "identity" ,
323- environmentId : EnvironmentId . make ( "another-environment" ) ,
324- reason : "configuration" ,
325- } ,
326- { name : "protocol" , orchestrationProtocolVersion : 0 , reason : "unsupported" } ,
327- ] ) ( "checks fresh $name data on a warm direct bearer prepare" , ( change ) =>
317+ it . effect ( "blocks a warm direct bearer prepare when the environment changes identity" , ( ) =>
328318 Effect . gen ( function * ( ) {
329- let descriptor = {
330- ...DESCRIPTOR ,
331- orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
332- } ;
333- const harness = yield * makeResolverHarness ( ( url ) =>
334- String ( url ) . endsWith ( "/.well-known/t3/environment" )
335- ? Response . json ( descriptor )
336- : websocketTicket ( "warm-ticket" ) ,
337- ) ;
338- yield * Effect . gen ( function * ( ) {
339- const resolver = yield * ConnectionResolver . ConnectionResolver ;
340- yield * resolver . prepare ( harness . entry ) ;
341- descriptor = { ...descriptor , ...change } ;
342- const failure = yield * resolver . prepare ( harness . entry ) . pipe ( Effect . flip ) ;
343- expect ( failure ) . toMatchObject ( {
344- _tag : "ConnectionBlockedError" ,
345- reason : change . reason ,
346- } ) ;
347- } ) . pipe ( Effect . provide ( harness . layer ) ) ;
319+ const harness = yield * makeHarness ( {
320+ responses : [
321+ Response . json ( {
322+ ...DESCRIPTOR ,
323+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
324+ } ) ,
325+ websocketTicket ( "first-ticket" ) ,
326+ Response . json ( {
327+ ...DESCRIPTOR ,
328+ environmentId : "another-environment" ,
329+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
330+ } ) ,
331+ websocketTicket ( "second-ticket" ) ,
332+ ] ,
333+ } ) ;
334+
335+ const [ first , second ] = yield * prepareDirectBearer ( harness , 2 ) ;
336+
337+ expect ( first ?. _tag ) . toBe ( "Success" ) ;
338+ expect ( second ) . toMatchObject ( {
339+ _tag : "Failure" ,
340+ failure : { _tag : "ConnectionBlockedError" , reason : "configuration" } ,
341+ } ) ;
348342 } ) ,
349343 ) ;
350-
351- it . effect ( "fetches a fresh bearer descriptor while issuing fresh websocket tickets" , ( ) =>
344+ it . effect ( "validates a fresh bearer descriptor while issuing fresh websocket tickets" , ( ) =>
352345 Effect . gen ( function * ( ) {
353- let ticketCount = 0 ;
354346 const harness = yield * makeHarness ( {
355- responses : ( url ) =>
356- String ( url ) . endsWith ( "/.well-known/t3/environment" )
357- ? Response . json ( DESCRIPTOR )
358- : websocketTicket ( ++ ticketCount === 1 ? "first-ticket" : "second-ticket" ) ,
347+ responses : [
348+ Response . json ( DESCRIPTOR ) ,
349+ websocketTicket ( "first-ticket" ) ,
350+ Response . json ( DESCRIPTOR ) ,
351+ websocketTicket ( "second-ticket" ) ,
352+ ] ,
359353 } ) ;
360354
361355 const [ first , second ] = yield * Effect . gen ( function * ( ) {
@@ -384,60 +378,6 @@ describe("RemoteEnvironmentAuthorization", () => {
384378 } ) ,
385379 ) ;
386380
387- it . effect . each ( [ "cached" , "renewed" ] as const ) (
388- "uses one fresh direct descriptor with a %s DPoP token and permits old-host updates" ,
389- ( tokenState ) =>
390- Effect . gen ( function * ( ) {
391- const directDescriptor = { ...DESCRIPTOR , orchestrationProtocolVersion : 0 } ;
392- const relayHttpBaseUrl = "https://relay-route.example.test" ;
393- const harness = yield * makeResolverHarness (
394- ( url ) => {
395- const endpoint = String ( url ) ;
396- if ( endpoint . endsWith ( "/.well-known/t3/environment" ) ) {
397- return Response . json (
398- endpoint . startsWith ( ENDPOINT . httpBaseUrl )
399- ? directDescriptor
400- : { ...DESCRIPTOR , orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION } ,
401- ) ;
402- }
403- return endpoint . endsWith ( "/oauth/token" )
404- ? accessToken ( "renewed-token" )
405- : websocketTicket ( "direct-ticket" ) ;
406- } ,
407- {
408- authorization : "t3-connect" ,
409- ...( tokenState === "cached" ? { initialToken : persistedToken ( ) } : { } ) ,
410- bootstrap : {
411- ...BOOTSTRAP ,
412- endpoint : {
413- ...ENDPOINT ,
414- httpBaseUrl : relayHttpBaseUrl ,
415- wsBaseUrl : "wss://relay-route.example.test" ,
416- } ,
417- } ,
418- } ,
419- ) ;
420- yield * Effect . gen ( function * ( ) {
421- const resolver = yield * ConnectionResolver . ConnectionResolver ;
422- const { prepared, descriptor } = yield * resolver . prepareForUpdate ( harness . entry ) ;
423- expect ( descriptor ) . toMatchObject ( directDescriptor ) ;
424- expect ( prepared . socketUrl ) . toContain ( "wsTicket=direct-ticket" ) ;
425- expect ( prepared . httpBaseUrl ) . toBe ( ENDPOINT . httpBaseUrl ) ;
426- expect ( prepared ) . not . toHaveProperty ( "descriptor" ) ;
427- expect (
428- harness . fetch . calls . filter (
429- ( [ url ] ) => String ( url ) === `${ ENDPOINT . httpBaseUrl } /.well-known/t3/environment` ,
430- ) ,
431- ) . toHaveLength ( 1 ) ;
432- expect ( yield * resolver . prepare ( harness . entry ) . pipe ( Effect . flip ) ) . toMatchObject ( {
433- _tag : "ConnectionBlockedError" ,
434- reason : "unsupported" ,
435- } ) ;
436- } ) . pipe ( Effect . provide ( harness . layer ) ) ;
437- expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( tokenState === "cached" ? 0 : 1 ) ;
438- } ) ,
439- ) ;
440-
441381 it . effect ( "reuses a same-account persisted environment token without contacting the relay" , ( ) =>
442382 Effect . gen ( function * ( ) {
443383 const cached = new TokenStore . RemoteDpopAccessToken ( {
0 commit comments