@@ -19,7 +19,7 @@ import (
1919 syncerv2mocks "github.com/smartcontractkit/chainlink/v2/core/services/workflows/syncer/v2/mocks"
2020)
2121
22- func TestRequestAuthorizer_CreateSecrets (t * testing.T ) {
22+ func TestAllowListBasedAuth_CreateSecrets (t * testing.T ) {
2323 params , err := json .Marshal (vaultcommon.CreateSecretsRequest {
2424 EncryptedSecrets : []* vaultcommon.EncryptedSecret {
2525 {
@@ -59,7 +59,7 @@ func TestRequestAuthorizer_CreateSecrets(t *testing.T) {
5959 testAuthForRequests (t , allowListedReq , notAllowListedReq )
6060}
6161
62- func TestRequestAuthorizer_UpdateSecrets (t * testing.T ) {
62+ func TestAllowListBasedAuth_UpdateSecrets (t * testing.T ) {
6363 params , err := json .Marshal (vaultcommon.UpdateSecretsRequest {
6464 EncryptedSecrets : []* vaultcommon.EncryptedSecret {
6565 {
@@ -98,7 +98,7 @@ func TestRequestAuthorizer_UpdateSecrets(t *testing.T) {
9898 testAuthForRequests (t , allowListedReq , notAllowListedReq )
9999}
100100
101- func TestRequestAuthorizer_DeleteSecrets (t * testing.T ) {
101+ func TestAllowListBasedAuth_DeleteSecrets (t * testing.T ) {
102102 params , err := json .Marshal (vaultcommon.DeleteSecretsRequest {
103103 Ids : []* vaultcommon.SecretIdentifier {
104104 {
@@ -131,7 +131,7 @@ func TestRequestAuthorizer_DeleteSecrets(t *testing.T) {
131131 testAuthForRequests (t , allowListedReq , notAllowListedReq )
132132}
133133
134- func TestRequestAuthorizer_ListSecrets (t * testing.T ) {
134+ func TestAllowListBasedAuth_ListSecrets (t * testing.T ) {
135135 params , err := json .Marshal (vaultcommon.ListSecretIdentifiersRequest {
136136 Namespace : "b" ,
137137 })
@@ -159,14 +159,16 @@ func testAuthForRequests(t *testing.T, allowlistedRequest, notAllowlistedRequest
159159 owner := common.Address {1 , 2 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
160160
161161 mockSyncer := syncerv2mocks .NewWorkflowRegistrySyncer (t )
162- auth := NewRequestAuthorizer (lggr , mockSyncer )
162+ auth := NewAllowListBasedAuth (lggr , mockSyncer )
163+ auth .retryCount = 0
164+ auth .retryInterval = time .Millisecond
163165
164166 // Happy path
165167 digest , err := allowlistedRequest .Digest ()
166168 require .NoError (t , err )
167169 digestBytes , err := hex .DecodeString (digest )
168170 require .NoError (t , err )
169- expiry := uint64 ( time .Now ().UTC ().Unix () + 100 ) //nolint:gosec // it is a safe conversion
171+ expiry := time .Now ().UTC ().Unix () + 100
170172 allowlisted := []workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {
171173 {
172174 RequestDigest : [32 ]byte (digestBytes ),
@@ -175,15 +177,16 @@ func testAuthForRequests(t *testing.T, allowlistedRequest, notAllowlistedRequest
175177 },
176178 }
177179 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return (allowlisted )
178- isAuthorized , gotOwner , err := auth .AuthorizeRequest (t .Context (), allowlistedRequest )
179- require .True (t , isAuthorized , err )
180- require .Equal (t , owner .Hex (), gotOwner )
180+ authResult , err := auth .AuthorizeRequest (t .Context (), allowlistedRequest )
181181 require .NoError (t , err )
182+ require .Equal (t , owner .Hex (), authResult .AuthorizedOwner ())
183+ require .Equal (t , expiry , authResult .GetExpiresAt ())
184+ require .NotEmpty (t , authResult .GetDigest ())
182185
183- // Already authorized
184- isAuthorized , _ , err = auth .AuthorizeRequest (t .Context (), allowlistedRequest )
185- require .False (t , isAuthorized )
186- require .ErrorContains (t , err , "already authorized previously" )
186+ // Same request is still authorized here; replay protection lives in the generic Authorizer.
187+ authResult , err = auth .AuthorizeRequest (t .Context (), allowlistedRequest )
188+ require .NoError (t , err )
189+ require .Equal (t , owner . Hex (), authResult . AuthorizedOwner () )
187190
188191 // Expired request
189192 allowlistedReqCopy := allowlistedRequest
@@ -195,16 +198,16 @@ func testAuthForRequests(t *testing.T, allowlistedRequest, notAllowlistedRequest
195198 allowlisted [0 ].RequestDigest = [32 ]byte (allowlistedReqCopyDigestBytes )
196199 allowlisted [0 ].ExpiryTimestamp = uint32 (time .Now ().UTC ().Unix () - 1 ) //nolint:gosec // it is a safe conversion
197200 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return (allowlisted )
198- isAuthorized , _ , err = auth .AuthorizeRequest (t .Context (), allowlistedReqCopy )
199- require .False (t , isAuthorized )
201+ authResult , err = auth .AuthorizeRequest (t .Context (), allowlistedReqCopy )
202+ require .Nil (t , authResult )
200203 require .ErrorContains (t , err , "authorization expired" )
201204
202- isAuthorized , _ , err = auth .AuthorizeRequest (t .Context (), notAllowlistedRequest )
203- require .False (t , isAuthorized )
205+ authResult , err = auth .AuthorizeRequest (t .Context (), notAllowlistedRequest )
206+ require .Nil (t , authResult )
204207 require .ErrorContains (t , err , "not allowlisted" )
205208}
206209
207- func TestRequestAuthorizer_RetriesAllowlistReadsUntilDigestAppears (t * testing.T ) {
210+ func TestAllowListBasedAuth_RetriesUntilRequestIsAllowlisted (t * testing.T ) {
208211 lggr := logger .TestLogger (t )
209212 owner := common.Address {1 , 2 , 3 }
210213 req := makeListSecretsRequest (t , "123" , "b" )
@@ -213,55 +216,47 @@ func TestRequestAuthorizer_RetriesAllowlistReadsUntilDigestAppears(t *testing.T)
213216 require .NoError (t , err )
214217 digestBytes , err := hex .DecodeString (digest )
215218 require .NoError (t , err )
216-
219+ expiry := time . Now (). UTC (). Unix () + 100
217220 allowlisted := []workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {
218221 {
219222 RequestDigest : [32 ]byte (digestBytes ),
220223 Owner : owner ,
221- ExpiryTimestamp : uint32 (time . Now (). UTC (). Unix () + 100 ) , //nolint:gosec // test fixture expiry is bounded and safe here
224+ ExpiryTimestamp : uint32 (expiry ) , //nolint:gosec // it is a safe conversion
222225 },
223226 }
224227
225228 mockSyncer := syncerv2mocks .NewWorkflowRegistrySyncer (t )
229+ auth := NewAllowListBasedAuth (lggr , mockSyncer )
230+ auth .retryCount = 2
231+ auth .retryInterval = time .Millisecond
232+
226233 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return ([]workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {}).Once ()
227234 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return ([]workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {}).Once ()
228235 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return (allowlisted ).Once ()
229236
230- auth := NewRequestAuthorizer (lggr , mockSyncer )
231- sleepCalls := 0
232- auth .sleep = func (d time.Duration ) {
233- require .Equal (t , allowlistReadRetryInterval , d )
234- sleepCalls ++
235- }
236-
237- isAuthorized , gotOwner , err := auth .AuthorizeRequest (t .Context (), req )
238- require .True (t , isAuthorized , err )
237+ authResult , err := auth .AuthorizeRequest (t .Context (), req )
239238 require .NoError (t , err )
240- require .Equal (t , owner .Hex (), gotOwner )
241- require .Equal (t , 2 , sleepCalls )
239+ require .Equal (t , owner .Hex (), authResult . AuthorizedOwner () )
240+ require .Equal (t , expiry , authResult . GetExpiresAt () )
242241}
243242
244- func TestRequestAuthorizer_FailsAfterAllowlistReadRetries (t * testing.T ) {
243+ func TestAllowListBasedAuth_FailsAfterAllowlistReadRetries (t * testing.T ) {
245244 lggr := logger .TestLogger (t )
246245 req := makeListSecretsRequest (t , "123" , "b" )
247246
248247 mockSyncer := syncerv2mocks .NewWorkflowRegistrySyncer (t )
249- mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return ([]workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {}).Times (allowlistReadRetryCount + 1 )
248+ mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return ([]workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {}).Times (3 )
250249
251- auth := NewRequestAuthorizer (lggr , mockSyncer )
252- sleepCalls := 0
253- auth .sleep = func (d time.Duration ) {
254- require .Equal (t , allowlistReadRetryInterval , d )
255- sleepCalls ++
256- }
250+ auth := NewAllowListBasedAuth (lggr , mockSyncer )
251+ auth .retryCount = 2
252+ auth .retryInterval = time .Millisecond
257253
258- isAuthorized , _ , err := auth .AuthorizeRequest (t .Context (), req )
259- require .False (t , isAuthorized )
254+ authResult , err := auth .AuthorizeRequest (t .Context (), req )
255+ require .Nil (t , authResult )
260256 require .ErrorContains (t , err , "not allowlisted" )
261- require .Equal (t , allowlistReadRetryCount , sleepCalls )
262257}
263258
264- func TestRequestAuthorizer_StopsRetriesWhenContextCanceled (t * testing.T ) {
259+ func TestAllowListBasedAuth_StopsRetriesWhenContextCanceled (t * testing.T ) {
265260 lggr := logger .TestLogger (t )
266261 req := makeListSecretsRequest (t , "123" , "b" )
267262
@@ -271,16 +266,13 @@ func TestRequestAuthorizer_StopsRetriesWhenContextCanceled(t *testing.T) {
271266 mockSyncer := syncerv2mocks .NewWorkflowRegistrySyncer (t )
272267 mockSyncer .On ("GetAllowlistedRequests" , mock .Anything ).Return ([]workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest {}).Once ()
273268
274- auth := NewRequestAuthorizer (lggr , mockSyncer )
275- sleepCalls := 0
276- auth .sleep = func (time.Duration ) {
277- sleepCalls ++
278- }
269+ auth := NewAllowListBasedAuth (lggr , mockSyncer )
270+ auth .retryCount = 2
271+ auth .retryInterval = time .Second
279272
280- isAuthorized , _ , err := auth .AuthorizeRequest (ctx , req )
281- require .False (t , isAuthorized )
282- require .ErrorContains (t , err , "not allowlisted" )
283- require .Zero (t , sleepCalls )
273+ authResult , err := auth .AuthorizeRequest (ctx , req )
274+ require .Nil (t , authResult )
275+ require .ErrorIs (t , err , context .Canceled )
284276}
285277
286278func makeListSecretsRequest (t * testing.T , id , namespace string ) jsonrpc.Request [json.RawMessage ] {
0 commit comments