@@ -500,3 +500,77 @@ func assertRunningRejectedPolicyRequirement(t *testing.T, allowed bool, err erro
500500 assertRunningRejected (t , allowed , err )
501501 assert .IsType (t , PolicyRequirementError ("" ), err )
502502}
503+
504+ func TestPolicyContextRequireSignatureVerification (t * testing.T ) {
505+ pc , err := NewPolicyContext (& Policy {Default : PolicyRequirements {NewPRReject ()}})
506+ require .NoError (t , err )
507+ defer func () {
508+ err := pc .Destroy ()
509+ require .NoError (t , err )
510+ }()
511+
512+ // Test default value is false
513+ assert .False (t , pc .requireSigned )
514+
515+ // Test setting to true
516+ pc .RequireSignatureVerification (true )
517+ assert .True (t , pc .requireSigned )
518+
519+ // Test setting back to false
520+ pc .RequireSignatureVerification (false )
521+ assert .False (t , pc .requireSigned )
522+ }
523+
524+ func TestPolicyContextIsRunningImageAllowedWithRequireSigned (t * testing.T ) {
525+ pc , err := NewPolicyContext (& Policy {
526+ Default : PolicyRequirements {NewPRReject ()},
527+ Transports : map [string ]PolicyTransportScopes {
528+ "docker" : {
529+ "docker.io/testing/manifest:insecureOnly" : {
530+ NewPRInsecureAcceptAnything (),
531+ },
532+ "docker.io/testing/manifest:insecureWithOther" : {
533+ NewPRInsecureAcceptAnything (),
534+ xNewPRSignedByKeyPath (SBKeyTypeGPGKeys , "fixtures/public-key.gpg" , NewPRMMatchRepository ()),
535+ },
536+ "docker.io/testing/manifest:signedOnly" : {
537+ xNewPRSignedByKeyPath (SBKeyTypeGPGKeys , "fixtures/public-key.gpg" , NewPRMMatchRepository ()),
538+ },
539+ },
540+ },
541+ })
542+ require .NoError (t , err )
543+ defer func () {
544+ err := pc .Destroy ()
545+ require .NoError (t , err )
546+ }()
547+
548+ // Test with requireSigned=false (default behavior)
549+ // insecureAcceptAnything should be accepted
550+ img := pcImageMock (t , "fixtures/dir-img-valid" , "testing/manifest:insecureOnly" )
551+ res , err := pc .IsRunningImageAllowed (context .Background (), img )
552+ assertRunningAllowed (t , res , err )
553+
554+ // Test with rejectInsecure=true
555+ pc .RequireSignatureVerification (true )
556+
557+ // insecureAcceptAnything only: should be rejected
558+ img = pcImageMock (t , "fixtures/dir-img-valid" , "testing/manifest:insecureOnly" )
559+ res , err = pc .IsRunningImageAllowed (context .Background (), img )
560+ assertRunningRejectedPolicyRequirement (t , res , err )
561+
562+ // insecureAcceptAnything + signed requirement: first requirement has no effect, second is secure and valid
563+ img = pcImageMock (t , "fixtures/dir-img-valid" , "testing/manifest:insecureWithOther" )
564+ res , err = pc .IsRunningImageAllowed (context .Background (), img )
565+ assertRunningAllowed (t , res , err )
566+
567+ // signed requirement only: should work normally
568+ img = pcImageMock (t , "fixtures/dir-img-valid" , "testing/manifest:signedOnly" )
569+ res , err = pc .IsRunningImageAllowed (context .Background (), img )
570+ assertRunningAllowed (t , res , err )
571+
572+ // Test with unsigned image and insecureAcceptAnything + signed requirement: first requirement has no effect, second is secure but rejects
573+ img = pcImageMock (t , "fixtures/dir-img-unsigned" , "testing/manifest:insecureWithOther" )
574+ res , err = pc .IsRunningImageAllowed (context .Background (), img )
575+ assertRunningRejectedPolicyRequirement (t , res , err )
576+ }
0 commit comments