@@ -188,20 +188,20 @@ func TestStatusWaitRequestsReadyProbe(t *testing.T) {
188188 t .Fatal (err )
189189 }
190190 providerScope := leaseOptionsFromConfig (cfg ).ProviderScope
191- claimed , err := statusLeaseHasExactClaim (backend , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope )
191+ claimed , err := statusLeaseHasExactClaim (context . Background (), backend , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope )
192192 if err != nil || ! claimed {
193193 t .Fatalf ("matching exact claim allowed=%t err=%v" , claimed , err )
194194 }
195195 rejecting := & statusTouchClaimRejectingBackend {statusResolveRecordingBackend : backend }
196- if claimed , err := statusLeaseHasExactClaim (rejecting , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope ); err != nil || claimed {
196+ if claimed , err := statusLeaseHasExactClaim (context . Background (), rejecting , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope ); err != nil || claimed {
197197 t .Fatalf ("provider identity-rejected claim allowed=%t err=%v" , claimed , err )
198198 }
199- if claimed , err := statusLeaseHasExactClaim (backend , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope + "-other" ); err != nil || claimed {
199+ if claimed , err := statusLeaseHasExactClaim (context . Background (), backend , LeaseTarget {LeaseID : "cbx_status" , Server : claimServer }, "aws" , providerScope + "-other" ); err != nil || claimed {
200200 t .Fatalf ("scope-mismatched claim allowed=%t err=%v" , claimed , err )
201201 }
202202 wrongResource := claimServer
203203 wrongResource .CloudID = "i-other"
204- if claimed , err := statusLeaseHasExactClaim (backend , LeaseTarget {LeaseID : "cbx_status" , Server : wrongResource }, "aws" , providerScope ); err != nil || claimed {
204+ if claimed , err := statusLeaseHasExactClaim (context . Background (), backend , LeaseTarget {LeaseID : "cbx_status" , Server : wrongResource }, "aws" , providerScope ); err != nil || claimed {
205205 t .Fatalf ("resource-mismatched claim allowed=%t err=%v" , claimed , err )
206206 }
207207 err = app .status (context .Background (), []string {"--provider" , "aws" , "--id" , "cbx_status" , "--wait" , "--wait-timeout" , "1ns" })
@@ -213,6 +213,60 @@ func TestStatusWaitRequestsReadyProbe(t *testing.T) {
213213 }
214214}
215215
216+ func TestStatusLeaseExactClaimAuthorizerOwnsDynamicScopeValidation (t * testing.T ) {
217+ t .Setenv ("XDG_STATE_HOME" , t .TempDir ())
218+ leaseID := "cbx_dynamic_status"
219+ server := Server {Provider : "aws" , CloudID : "dynamic-resource" , Labels : map [string ]string {
220+ "lease" : leaseID , "provider" : "aws" , "runtime_scope" : "runtime:dynamic/context:owned" ,
221+ }}
222+ if err := claimLeaseForRepoProviderScopePondEndpoint (
223+ leaseID ,
224+ "dynamic-status" ,
225+ "aws" ,
226+ "runtime:dynamic/context:owned" ,
227+ "" ,
228+ t .TempDir (),
229+ time .Minute ,
230+ false ,
231+ server ,
232+ SSHTarget {},
233+ ); err != nil {
234+ t .Fatal (err )
235+ }
236+
237+ ctx := context .WithValue (context .Background (), statusAuthorizerContextKey {}, "present" )
238+ backend := & statusTouchClaimAuthorizingBackend {}
239+ claimed , err := statusLeaseHasExactClaim (ctx , backend , LeaseTarget {LeaseID : leaseID , Server : server }, "aws" , "" )
240+ if err != nil || ! claimed {
241+ t .Fatalf ("dynamic claim authorized=%t err=%v" , claimed , err )
242+ }
243+ if backend .calls != 1 || backend .contextValue != "present" || backend .claim .ProviderScope != "runtime:dynamic/context:owned" {
244+ t .Fatalf ("authorizer calls=%d context=%q claim=%#v" , backend .calls , backend .contextValue , backend .claim )
245+ }
246+
247+ backend .err = errors .New ("dynamic ownership mismatch" )
248+ claimed , err = statusLeaseHasExactClaim (ctx , backend , LeaseTarget {LeaseID : leaseID , Server : server }, "aws" , "" )
249+ if claimed || err == nil || ! strings .Contains (err .Error (), "dynamic ownership mismatch" ) {
250+ t .Fatalf ("rejected dynamic claim authorized=%t err=%v" , claimed , err )
251+ }
252+
253+ backend .calls = 0
254+ wrongProvider := server
255+ wrongProvider .Provider = "gcp"
256+ if claimed , err := statusLeaseHasExactClaim (ctx , backend , LeaseTarget {LeaseID : leaseID , Server : wrongProvider }, "aws" , "" ); err != nil || claimed {
257+ t .Fatalf ("wrong-provider claim authorized=%t err=%v" , claimed , err )
258+ }
259+ if backend .calls != 0 {
260+ t .Fatalf ("authorizer called before exact canonical-provider claim check: %d" , backend .calls )
261+ }
262+ if claimed , err := statusLeaseHasExactClaim (ctx , backend , LeaseTarget {LeaseID : "cbx_missing_dynamic" , Server : server }, "aws" , "" ); err != nil || claimed {
263+ t .Fatalf ("missing claim authorized=%t err=%v" , claimed , err )
264+ }
265+ if backend .calls != 0 {
266+ t .Fatalf ("authorizer called for missing claim: %d" , backend .calls )
267+ }
268+ }
269+
216270func TestStatusWaitBoundsResolveByTimeout (t * testing.T ) {
217271 t .Setenv ("CRABBOX_CONFIG" , filepath .Join (t .TempDir (), "missing.yaml" ))
218272 t .Setenv ("CRABBOX_COORDINATOR" , "" )
@@ -269,6 +323,23 @@ func (*statusTouchClaimRejectingBackend) StatusTouchClaimMatches(LeaseTarget, Le
269323 return false
270324}
271325
326+ type statusAuthorizerContextKey struct {}
327+
328+ type statusTouchClaimAuthorizingBackend struct {
329+ testSSHBackend
330+ calls int
331+ contextValue string
332+ claim LeaseClaim
333+ err error
334+ }
335+
336+ func (b * statusTouchClaimAuthorizingBackend ) AuthorizeStatusTouchClaim (ctx context.Context , _ LeaseTarget , claim LeaseClaim ) error {
337+ b .calls ++
338+ b .contextValue , _ = ctx .Value (statusAuthorizerContextKey {}).(string )
339+ b .claim = claim
340+ return b .err
341+ }
342+
272343func (b * statusResolveRecordingBackend ) Resolve (ctx context.Context , req ResolveRequest ) (LeaseTarget , error ) {
273344 b .requests = append (b .requests , req )
274345 if b .block {
0 commit comments