1111use std:: sync:: { Arc , Mutex , PoisonError } ;
1212use std:: time:: Duration ;
1313
14+ use base64:: Engine as _;
15+ use base64:: engine:: general_purpose:: STANDARD as BASE64 ;
1416use host_server_support:: { HttpRecorder , Request , Response , TEST_HOST } ;
1517use serde_json:: json;
16- use testkit:: { Context , PYTHON_BINARIES , Poll , Result , Sandbox , policy, poll_until} ;
18+ use testkit:: policy:: { self , Access , Endpoint as _} ;
19+ use testkit:: sdk:: raw:: proto;
20+ use testkit:: { Context , Credential , PYTHON_BINARIES , Poll , Profile , Result , Sandbox , poll_until} ;
1721
1822fn main ( ) -> std:: process:: ExitCode {
1923 testkit:: main ( )
@@ -49,11 +53,12 @@ done"#,
4953 )
5054}
5155
52- /// A shell command that requests `resource_url` with the credential from the
53- /// environment as a bearer token, and fails unless the resource accepts it.
56+ /// A shell command that requests `resource_url` twice with the credential
57+ /// from the environment, as a bearer token and as the password of HTTP Basic
58+ /// `x-access-token`, and fails unless the resource accepts both.
5459fn probe ( resource_url : & str ) -> String {
5560 format ! (
56- r#"python3 -c 'import os, urllib.request; urllib.request.urlopen(urllib.request.Request("{resource_url}", headers={{"Authorization": "Bearer " + os.environ["{TOKEN_ENV}"]}} ), timeout=5).read()'"#
61+ r#"python3 -c 'import os, base64, urllib.request; token = os.environ["{TOKEN_ENV}"]; auths = ["Bearer " + token, "Basic " + base64.b64encode(("x-access-token:" + token).encode()).decode()]; [ urllib.request.urlopen(urllib.request.Request("{resource_url}", headers={{"Authorization": auth}} ), timeout=5).read() for auth in auths] '"#
5762 )
5863}
5964
@@ -68,9 +73,8 @@ async fn long_running_process_survives_rotations_and_reconfigure_revokes(
6873) -> testkit:: Result {
6974 let issuer = Issuer :: start ( ) . await ?;
7075 let provider = cx. name ( "refresh" ) ;
71- let profile = profile ( & provider, issuer. port ( ) ) ;
72- let provider_type = cx. import_profile ( & profile) . await ?;
73- cx. create_provider ( & provider, & provider_type, & [ ( TOKEN_ENV , BOOTSTRAP_TOKEN ) ] )
76+ let profile = profile ( & provider, issuer. port ( ) ) . to_yaml ( ) ?;
77+ cx. install_provider ( & provider, & profile, & [ ( TOKEN_ENV , BOOTSTRAP_TOKEN ) ] )
7478 . await ?;
7579 let refresh = cx. provider_refresh ( & provider, TOKEN_ENV ) ;
7680 let configure = async || {
@@ -90,7 +94,7 @@ async fn long_running_process_survives_rotations_and_reconfigure_revokes(
9094 . sandbox ( "rfh" )
9195 . image ( testkit:: python_image ( ) )
9296 . providers ( [ provider. as_str ( ) ] )
93- . policy ( probe_policy ( issuer. port ( ) ) ? )
97+ . policy ( probe_policy ( issuer. port ( ) ) )
9498 . command ( [ "sh" , "-c" , & main_process ( & resource_url) ] )
9599 . create_ready ( )
96100 . await ?;
@@ -104,14 +108,14 @@ async fn long_running_process_survives_rotations_and_reconfigure_revokes(
104108
105109 for _ in 0 ..ROTATIONS {
106110 refresh. rotate ( ) . await ?. success ( ) ?;
111+ wait_for_probe (
112+ & sandbox,
113+ "ok" ,
114+ "the long-running process to use the latest rotated token" ,
115+ )
116+ . await
117+ . map_err ( |error| error. context ( format ! ( "issuer saw {}" , issuer. summary( ) ) ) ) ?;
107118 }
108- wait_for_probe (
109- & sandbox,
110- "ok" ,
111- "the long-running process to use the latest rotated token" ,
112- )
113- . await
114- . map_err ( |error| error. context ( format ! ( "issuer saw {}" , issuer. summary( ) ) ) ) ?;
115119
116120 configure ( ) . await ?;
117121 wait_for_probe (
@@ -121,10 +125,31 @@ async fn long_running_process_survives_rotations_and_reconfigure_revokes(
121125 )
122126 . await ?;
123127
124- sandbox
125- . exec ( [ "sh" , "-c" , & probe ( & resource_url) ] )
126- . await ?
127- . success ( ) ?;
128+ refresh. rotate ( ) . await ?. success ( ) ?;
129+ poll_until (
130+ "a new process to reach the resource after reconfiguring" ,
131+ PROBE_TIMEOUT ,
132+ Duration :: from_millis ( 500 ) ,
133+ async || {
134+ let fresh = sandbox. exec ( [ "sh" , "-c" , & probe ( & resource_url) ] ) . await ?;
135+ Ok ( if fresh. exit_code == 0 {
136+ Poll :: Ready ( ( ) )
137+ } else {
138+ Poll :: Pending ( format ! (
139+ "the probe exited {}: {}" ,
140+ fresh. exit_code,
141+ fresh. stderr. trim( )
142+ ) )
143+ } )
144+ } ,
145+ )
146+ . await
147+ . map_err ( |error| error. context ( format ! ( "issuer saw {}" , issuer. summary( ) ) ) ) ?;
148+ let old = trigger_probe ( & sandbox) . await ?;
149+ testkit:: ensure!(
150+ old == "failed" ,
151+ "the long-running process's revoked handle worked again after a new token was issued: {old:?}"
152+ ) ;
128153 Ok ( ( ) )
129154}
130155
@@ -164,7 +189,8 @@ async fn wait_for_probe(sandbox: &Sandbox, expected: &str, what: &str) -> Result
164189
165190/// An OAuth issuer and the resource it guards. Each `POST /token` issues a
166191/// new token and invalidates the last. `GET /probe` answers `204` to the
167- /// current token and `401` to anything else.
192+ /// current token, as a bearer token or as the password of HTTP Basic
193+ /// `x-access-token`, and `401` to anything else.
168194struct Issuer {
169195 server : HttpRecorder ,
170196}
@@ -185,8 +211,14 @@ impl Issuer {
185211 } ) )
186212 }
187213 ( "GET" , "/probe" )
188- if request. header ( "authorization" ) . as_deref ( )
189- == Some ( & format ! ( "Bearer {}" , state. 1 ) ) =>
214+ if request. header ( "authorization" ) . is_some_and ( |auth| {
215+ auth == format ! ( "Bearer {}" , state. 1 )
216+ || auth
217+ == format ! (
218+ "Basic {}" ,
219+ BASE64 . encode( format!( "x-access-token:{}" , state. 1 ) )
220+ )
221+ } ) =>
190222 {
191223 Response :: new ( 204 )
192224 }
@@ -215,49 +247,52 @@ impl Issuer {
215247
216248/// A profile whose credential refreshes with OAuth client credentials from
217249/// the issuer on `port`, and is bound to the resource on the same port.
218- fn profile ( id : & str , port : u16 ) -> String {
219- let binaries = PYTHON_BINARIES
220- . iter ( )
221- . map ( |binary| format ! ( "\" {binary}\" " ) )
222- . collect :: < Vec < _ > > ( )
223- . join ( ", " ) ;
224- format ! (
225- "id: {id}
226- display_name: Provider refresh handle test
227- category: other
228- credentials:
229- - name: access_token
230- env_vars: [{TOKEN_ENV}]
231- required: true
232- auth_style: bearer
233- header_name: authorization
234- refresh:
235- strategy: oauth2_client_credentials
236- token_url: http://127.0.0.1:{port}/token
237- refresh_before_seconds: 30
238- max_lifetime_seconds: 300
239- material:
240- - name: client_id
241- required: true
242- - name: client_secret
243- required: true
244- secret: true
245- endpoints:
246- - {{host: {TEST_HOST}, port: {port}, path: /probe, protocol: rest, access: full, enforcement: enforce}}
247- binaries: [{binaries}]
248- "
249- )
250+ fn profile ( id : & str , port : u16 ) -> Profile {
251+ let material = |name : & str , secret : bool | proto:: ProviderCredentialRefreshMaterial {
252+ name : name. to_string ( ) ,
253+ required : true ,
254+ secret,
255+ ..proto:: ProviderCredentialRefreshMaterial :: default ( )
256+ } ;
257+ let seconds = |seconds| Some ( prost_types:: Duration { seconds, nanos : 0 } ) ;
258+ Profile :: new ( id)
259+ . display_name ( "Provider refresh handle test" )
260+ . credential (
261+ Credential :: new ( "access_token" )
262+ . env_var ( TOKEN_ENV )
263+ . required ( )
264+ . auth_style ( "bearer" )
265+ . header_name ( "authorization" )
266+ . refresh ( proto:: ProviderCredentialRefresh {
267+ strategy : proto:: ProviderCredentialRefreshStrategy :: Oauth2ClientCredentials
268+ as i32 ,
269+ token_url : format ! ( "http://127.0.0.1:{port}/token" ) ,
270+ refresh_before : seconds ( 30 ) ,
271+ max_lifetime : seconds ( 300 ) ,
272+ material : vec ! [
273+ material( "client_id" , false ) ,
274+ material( "client_secret" , true ) ,
275+ ] ,
276+ ..proto:: ProviderCredentialRefresh :: default ( )
277+ } ) ,
278+ )
279+ . endpoint ( TEST_HOST , port, |ep| {
280+ ep. rest ( Access :: Full ) . enforce ( ) . path ( "/probe" ) ;
281+ } )
282+ . binaries ( PYTHON_BINARIES . iter ( ) . copied ( ) )
250283}
251284
252- fn probe_policy ( port : u16 ) -> Result < testkit:: sdk:: raw:: proto:: SandboxPolicy > {
253- policy:: base_with ( & format ! (
254- "network_policies:
255- refresh_probe:
256- name: refresh_probe
257- endpoints:
258- - {{host: {TEST_HOST}, port: {port}, path: /probe, protocol: rest, access: full, enforcement: enforce}}
259- binaries: {binaries}
260- " ,
261- binaries = policy:: binaries_yaml( PYTHON_BINARIES ) ,
262- ) )
285+ fn probe_policy ( port : u16 ) -> proto:: SandboxPolicy {
286+ let mut policy = policy:: base ( ) ;
287+ policy:: allow (
288+ & mut policy,
289+ "refresh_probe" ,
290+ TEST_HOST ,
291+ port,
292+ PYTHON_BINARIES ,
293+ )
294+ . rest ( Access :: Full )
295+ . enforce ( )
296+ . path ( "/probe" ) ;
297+ policy
263298}
0 commit comments