@@ -11,6 +11,7 @@ import {
1111 DeviceOperationError ,
1212 type DevicePlatform ,
1313 type DevicePlatformAvailability ,
14+ type SettingsSecretValue ,
1415 type SimlockDeviceHostConfig ,
1516} from "@t3tools/contracts" ;
1617import * as Cause from "effect/Cause" ;
@@ -28,8 +29,10 @@ import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } fr
2829import * as HostProcess from "@t3tools/shared/HostProcess" ;
2930import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime" ;
3031
32+ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver" ;
3133import * as ServerConfig from "../config.ts" ;
3234import * as ProcessRunner from "../processRunner.ts" ;
35+ import { requireSettingsSecret } from "../secrets/settingsSecretValue.ts" ;
3336import { writeAgentDeviceConfig } from "./AgentDeviceTarget.ts" ;
3437import * as DeviceHost from "./DeviceHost.ts" ;
3538
@@ -75,6 +78,8 @@ interface Lease {
7578 readonly dir : string ;
7679 readonly configPath : string ;
7780 readonly installDirectory : string ;
81+ /** The token the lease was opened with; ending it must not depend on reading the secret again. */
82+ readonly token : string ;
7883}
7984
8085const catalogId = ( platform : DevicePlatform , model : string ) => `simlock:${ platform } :${ model } ` ;
@@ -108,20 +113,25 @@ export const machineSimlockToken = (environment: NodeJS.ProcessEnv) => {
108113
109114/**
110115 * The saved token, else this machine's agent-device token; each only for the gateway it belongs to,
111- * so a URL being edited never receives either.
116+ * so a URL being edited never receives either. A saved secret reference that cannot be read fails
117+ * instead of falling back to this machine's token.
112118 */
113- export const resolveSimlockToken = (
119+ export const resolveSimlockToken = Effect . fn ( "SimlockDeviceHost.resolveToken" ) ( function * (
120+ secrets : SecretResolver . SecretResolver [ "Service" ] ,
114121 gatewayUrl : string ,
115- saved : { readonly gatewayUrl : string ; readonly agentToken : string } | undefined ,
122+ saved : { readonly gatewayUrl : string ; readonly agentToken : SettingsSecretValue } | undefined ,
116123 environment : NodeJS . ProcessEnv ,
117- ) => {
124+ ) {
118125 if ( saved ?. agentToken && sameOrigin ( saved . gatewayUrl , gatewayUrl ) )
119- return { agentToken : saved . agentToken , source : "settings" as const } ;
126+ return {
127+ agentToken : yield * requireSettingsSecret ( secrets , saved . agentToken ) ,
128+ source : "settings" as const ,
129+ } ;
120130 const machine = machineSimlockToken ( environment ) ;
121131 return machine && sameOrigin ( machine . gatewayUrl , gatewayUrl )
122132 ? { agentToken : machine . agentToken , source : "env" as const }
123133 : null ;
124- } ;
134+ } ) ;
125135
126136/** What went wrong talking to a gateway, without the gateway's own text. */
127137export const describeGatewayFailure = ( cause : unknown , gatewayUrl : string , tokenOwner : string ) => {
@@ -181,9 +191,9 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
181191 const server = yield * ServerConfig . ServerConfig ;
182192 const services = yield * Effect . context < FileSystem . FileSystem | Path . Path > ( ) ;
183193 const environment = yield * HostProcess . Environment ;
194+ const secrets = yield * SecretResolver . SecretResolver ;
184195 const hostId = config . id ;
185196 const gateway = config . gatewayUrl . replace ( / \/ $ / , "" ) ;
186- const agentToken = resolveSimlockToken ( config . gatewayUrl , config , environment ) ?. agentToken ?? "" ;
187197 const agentDeviceUrl = `${ gateway } /v1/agent-device` ;
188198 const hostDir = path . join ( server . stateDir , "device" , "simlock" , hostId ) ;
189199 const leases = new Map < string , Lease > ( ) ;
@@ -209,7 +219,18 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
209219 ...( cause === undefined ? { } : { cause } ) ,
210220 } ) ;
211221
222+ // Read on each use, so a rotated or unlocked secret applies without rebuilding the host.
223+ const currentToken = resolveSimlockToken ( secrets , config . gatewayUrl , config , environment ) . pipe (
224+ Effect . mapError ( ( error ) => unavailable ( error . message , error ) ) ,
225+ Effect . flatMap ( ( token ) =>
226+ token
227+ ? Effect . succeed ( token . agentToken )
228+ : Effect . fail ( unavailable ( `Add an agent token for ${ config . label } in Settings.` ) ) ,
229+ ) ,
230+ ) ;
231+
212232 const request = (
233+ agentToken : string ,
213234 method : "GET" | "POST" | "DELETE" ,
214235 route : string ,
215236 body ?: Readonly < Record < string , unknown > > ,
@@ -284,10 +305,8 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
284305 const fetchCatalog = Effect . gen ( function * ( ) {
285306 const now = yield * Clock . currentTimeMillis ;
286307 if ( catalog && now - catalog . at < CATALOG_TTL_MS ) return catalog . devices ;
287- if ( ! agentToken ) {
288- return yield * unavailable ( `Add an agent token for ${ config . label } in Settings.` ) ;
289- }
290- const decoded = yield * request ( "GET" , "/v1/catalog" ) . pipe (
308+ const agentToken = yield * currentToken ;
309+ const decoded = yield * request ( agentToken , "GET" , "/v1/catalog" ) . pipe (
291310 Effect . flatMap ( decodeCatalog ) ,
292311 Effect . mapError ( ( cause ) =>
293312 unavailable ( describeGatewayFailure ( cause , gateway , config . label ) , cause ) ,
@@ -357,7 +376,9 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
357376 /** Ends the lease through its session, or over HTTP when the session is gone. */
358377 const release = Effect . fn ( "SimlockDeviceHost.release" ) ( function * ( lease : Lease ) {
359378 yield * agentDevice ( lease , [ "close" ] ) . pipe (
360- Effect . catch ( ( ) => request ( "DELETE" , `/v1/leases/${ encodeURIComponent ( lease . leaseId ) } ` ) ) ,
379+ Effect . catch ( ( ) =>
380+ request ( lease . token , "DELETE" , `/v1/leases/${ encodeURIComponent ( lease . leaseId ) } ` ) ,
381+ ) ,
361382 Effect . tapError ( ( cause ) =>
362383 Effect . logWarning ( "Could not release Simlock lease" , { leaseId : lease . leaseId , cause } ) ,
363384 ) ,
@@ -376,6 +397,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
376397 yield * Effect . sleep ( Duration . millis ( Math . min ( left , Duration . toMillis ( RENEW_INTERVAL ) ) ) ) ;
377398 if ( ( yield * Clock . currentTimeMillis ) >= deadline ) break ;
378399 const renewed = yield * request (
400+ lease . token ,
379401 "POST" ,
380402 `/v1/leases/${ encodeURIComponent ( lease . leaseId ) } /renew` ,
381403 { ttlMs : LEASE_TTL_MS } ,
@@ -429,6 +451,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
429451 const session = `t3-${ suffix } ` ;
430452 const dir = path . join ( hostDir , suffix ) ;
431453 const configPath = path . join ( dir , "agent-device.json" ) ;
454+ const agentToken = yield * currentToken ;
432455 yield * writeAgentDeviceConfig ( configPath , {
433456 baseUrl : agentDeviceUrl ,
434457 token : agentToken ,
@@ -465,6 +488,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
465488 dir,
466489 configPath,
467490 installDirectory,
491+ token : agentToken ,
468492 } ;
469493 if ( stops !== stopsAtOpen )
470494 return yield * unavailable ( "Device support stopped while the device was opening." ) ;
@@ -557,7 +581,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
557581 onSome : ( lease ) =>
558582 Effect . succeed ( {
559583 baseUrl : agentDeviceUrl ,
560- token : agentToken ,
584+ token : lease . token ,
561585 agentDeviceId : null ,
562586 session : lease . session ,
563587 stateDir : lease . dir ,
@@ -588,7 +612,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
588612 if ( ! lease ) return null ;
589613 return {
590614 url : `${ gateway } /v1/leases/${ encodeURIComponent ( lease . leaseId ) } /${ stream ? "stream" : "input" } ` ,
591- headers : { authorization : `Bearer ${ agentToken } ` } ,
615+ headers : { authorization : `Bearer ${ lease . token } ` } ,
592616 protocols : [ ] ,
593617 } ;
594618 } ) ;
0 commit comments