@@ -18,7 +18,9 @@ use sandbox_clients_support::{self as clients, http, scripts};
1818use serde_json:: json;
1919use testkit:: policy:: { self , Access , Endpoint as _} ;
2020use testkit:: sdk:: raw:: proto;
21- use testkit:: { Context , Error , GrpcCode , PYTHON_BINARIES , Result , ResultExt as _, Sandbox } ;
21+ use testkit:: {
22+ Context , Credential , Error , GrpcCode , PYTHON_BINARIES , Profile , Result , ResultExt as _, Sandbox ,
23+ } ;
2224
2325const BASH : & str = "/usr/bin/bash" ;
2426
@@ -194,15 +196,12 @@ async fn policy_binding_updates_provider_environment_live(cx: &mut Context) -> t
194196#[ testkit:: test]
195197async fn profile_rest_endpoint_rewrites_header_and_body ( cx : & mut Context ) -> testkit:: Result {
196198 let server = HttpRecorder :: start ( "ok" ) . await ?;
197- let sandbox = profile_endpoint_sandbox (
198- cx,
199- "rwrt" ,
200- & format ! (
201- "{{host: {TEST_HOST}, port: {}, path: /probe, protocol: rest, access: full, \
202- enforcement: enforce, request_body_credential_rewrite: true}}",
203- server. port
204- ) ,
205- )
199+ let sandbox = profile_endpoint_sandbox ( cx, "rwrt" , server. port , |ep| {
200+ ep. rest ( Access :: Full )
201+ . path ( "/probe" )
202+ . enforce ( )
203+ . request_body_credential_rewrite ( true ) ;
204+ } )
206205 . await ?;
207206 let token = provider_token ( & sandbox) . await ?;
208207 let body = json ! ( { "credential" : token } ) . to_string ( ) ;
@@ -245,15 +244,10 @@ async fn profile_rest_endpoint_rewrites_header_and_body(cx: &mut Context) -> tes
245244#[ testkit:: test]
246245async fn profile_websocket_endpoint_rewrites_text_frames ( cx : & mut Context ) -> testkit:: Result {
247246 let server = WebSocketRecorder :: start ( ) . await ?;
248- let sandbox = profile_endpoint_sandbox (
249- cx,
250- "wstxt" ,
251- & format ! (
252- "{{host: {TEST_HOST}, port: {}, path: /ws, protocol: websocket, \
253- enforcement: enforce, access: read-write, websocket_credential_rewrite: true}}",
254- server. port
255- ) ,
256- )
247+ let sandbox = profile_endpoint_sandbox ( cx, "wstxt" , server. port , |ep| {
248+ ep. websocket ( Access :: ReadWrite ) . path ( "/ws" ) . enforce ( ) ;
249+ ep. websocket_credential_rewrite = true ;
250+ } )
257251 . await ?;
258252 sandbox
259253 . run_python (
@@ -380,31 +374,30 @@ async fn send_binary_frame(
380374/// `endpoints`, its profile declares the REST and WebSocket ports as its
381375/// endpoints; without, a policy must bind the provider explicitly.
382376async fn install_gating_provider ( cx : & Context , endpoints : Option < ( u16 , u16 ) > ) -> Result < String > {
383- let endpoints = endpoints. map_or_else ( Vec :: new, |( rest, websocket) | {
384- vec ! [
385- format!( "{{host: {TEST_HOST}, port: {rest}, protocol: rest, access: full}}" ) ,
386- format!(
387- "{{host: {TEST_HOST}, port: {websocket}, protocol: websocket, access: read-write}}"
388- ) ,
389- ]
390- } ) ;
391- install_provider_with_endpoints ( cx, & endpoints) . await
377+ let mut profile = gating_profile ( & cx. name ( "cg" ) ) ;
378+ if let Some ( ( rest, websocket) ) = endpoints {
379+ profile = profile
380+ . endpoint ( TEST_HOST , rest, |ep| {
381+ ep. rest ( Access :: Full ) ;
382+ } )
383+ . endpoint ( TEST_HOST , websocket, |ep| {
384+ ep. websocket ( Access :: ReadWrite ) ;
385+ } ) ;
386+ }
387+ install_gating_profile ( cx, & profile) . await
392388}
393389
394- /// A long-running sandbox with a provider from
395- /// [`install_provider_with_endpoints`] whose profile declares `endpoint`, a
396- /// YAML flow mapping, and a policy that allows the same endpoint.
397- async fn profile_endpoint_sandbox ( cx : & Context , suffix : & str , endpoint : & str ) -> Result < Sandbox > {
398- let provider = install_provider_with_endpoints ( cx, & [ endpoint. to_string ( ) ] ) . await ?;
399- let policy = policy:: base_with ( & format ! (
400- "network_policies:
401- credential_gating:
402- name: credential_gating
403- endpoints: [{endpoint}]
404- binaries: {binaries}
405- " ,
406- binaries = policy:: binaries_yaml( PYTHON_BINARIES ) ,
407- ) ) ?;
390+ /// A long-running sandbox with the same endpoint in its provider profile and policy.
391+ async fn profile_endpoint_sandbox (
392+ cx : & Context ,
393+ suffix : & str ,
394+ port : u16 ,
395+ configure : impl Fn ( & mut proto:: NetworkEndpoint ) ,
396+ ) -> Result < Sandbox > {
397+ let profile = gating_profile ( & cx. name ( "cg" ) ) . endpoint ( TEST_HOST , port, & configure) ;
398+ let provider = install_gating_profile ( cx, & profile) . await ?;
399+ let mut policy = policy:: base ( ) ;
400+ configure ( allow_host ( & mut policy, "credential_gating" , port) ) ;
408401 create_sandbox ( cx, suffix, policy, & [ & provider] ) . await
409402}
410403
@@ -419,55 +412,42 @@ async fn install_bound_provider(
419412 port : u16 ,
420413) -> Result < String > {
421414 let name = cx. name ( suffix) ;
422- let profile = format ! (
423- "id: {name}
424- display_name: E2E static endpoint binding
425- category: other
426- credentials:
427- - name: bound_token
428- env_vars: [{env_var}]
429- required: true
430- auth_style: bearer
431- header_name: authorization
432- endpoints:
433- - {{host: {TEST_HOST}, port: {port}, path: /allowed/**, protocol: rest, access: full, enforcement: enforce}}
434- binaries: [{BASH}]
435- "
436- ) ;
415+ let profile = Profile :: new ( & name)
416+ . display_name ( "E2E static endpoint binding" )
417+ . credential ( bearer_credential ( "bound_token" , env_var) )
418+ . endpoint ( TEST_HOST , port, |ep| {
419+ ep. rest ( Access :: Full ) . path ( "/allowed/**" ) . enforce ( ) ;
420+ } )
421+ . binaries ( [ BASH ] )
422+ . to_yaml ( ) ?;
437423 cx. install_provider ( & name, & profile, & [ ( env_var, secret) ] )
438424 . await ?;
439425 Ok ( name)
440426}
441427
442- /// Install a run-unique provider holding `TEST_SECRET` in `TOKEN_ENV`, whose
443- /// profile declares `endpoints`, each a YAML flow mapping.
444- async fn install_provider_with_endpoints ( cx : & Context , endpoints : & [ String ] ) -> Result < String > {
445- let name = cx. name ( "cg" ) ;
446- let endpoints = if endpoints. is_empty ( ) {
447- String :: new ( )
448- } else {
449- format ! ( "endpoints: [{}]\n " , endpoints. join( ", " ) )
450- } ;
451- let binaries: String = PYTHON_BINARIES
452- . iter ( )
453- . map ( |path| format ! ( " - path: {path}\n " ) )
454- . collect ( ) ;
455- let profile = format ! (
456- "id: {name}
457- display_name: E2E Credential Gating
458- category: other
459- credentials:
460- - name: token
461- env_vars: [{TOKEN_ENV}]
462- required: true
463- auth_style: bearer
464- header_name: authorization
465- {endpoints}binaries:
466- {binaries}"
467- ) ;
468- cx. install_provider ( & name, & profile, & [ ( TOKEN_ENV , TEST_SECRET ) ] )
469- . await ?;
470- Ok ( name)
428+ fn bearer_credential ( name : & str , env_var : & str ) -> Credential {
429+ Credential :: new ( name)
430+ . env_var ( env_var)
431+ . required ( )
432+ . auth_style ( "bearer" )
433+ . header_name ( "authorization" )
434+ }
435+
436+ fn gating_profile ( name : & str ) -> Profile {
437+ Profile :: new ( name)
438+ . display_name ( "E2E Credential Gating" )
439+ . credential ( bearer_credential ( "token" , TOKEN_ENV ) )
440+ . binaries ( PYTHON_BINARIES . iter ( ) . copied ( ) )
441+ }
442+
443+ async fn install_gating_profile ( cx : & Context , profile : & Profile ) -> Result < String > {
444+ cx. install_provider (
445+ profile. id ( ) ,
446+ & profile. to_yaml ( ) ?,
447+ & [ ( TOKEN_ENV , TEST_SECRET ) ] ,
448+ )
449+ . await ?;
450+ Ok ( profile. id ( ) . to_string ( ) )
471451}
472452
473453fn sandbox_with (
0 commit comments