Skip to content

Commit af291a4

Browse files
committed
Add unit test to validate email_verified in claim validation rules
Signed-off-by: Anish Ramasekar <[email protected]>
1 parent 916c786 commit af291a4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

staging/src/k8s.io/apiserver/pkg/apis/apiserver/validation/validation_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,61 @@ func TestValidateAuthenticationConfiguration(t *testing.T) {
585585
},
586586
want: "",
587587
},
588+
{
589+
name: "valid authentication configuration that uses verified email via claim validation rule",
590+
in: &api.AuthenticationConfiguration{
591+
JWT: []api.JWTAuthenticator{
592+
{
593+
Issuer: api.Issuer{
594+
URL: "https://issuer-url",
595+
Audiences: []string{"audience"},
596+
},
597+
ClaimValidationRules: []api.ClaimValidationRule{
598+
{
599+
// By explicitly comparing the value to true, we let type-checking see the result will be
600+
// a boolean, and to make sure a non-boolean email_verified claim will be caught at runtime.
601+
Expression: `claims.?email_verified.orValue(true) == true`,
602+
},
603+
},
604+
// allow email claim only when email_verified is present and true
605+
ClaimMappings: api.ClaimMappings{
606+
Username: api.PrefixedClaimOrExpression{
607+
Expression: `{claims.?email: "panda"}`,
608+
},
609+
},
610+
},
611+
},
612+
},
613+
want: "",
614+
},
615+
{
616+
name: "valid authentication configuration that uses verified email via claim validation rule incorrectly",
617+
in: &api.AuthenticationConfiguration{
618+
JWT: []api.JWTAuthenticator{
619+
{
620+
Issuer: api.Issuer{
621+
URL: "https://issuer-url",
622+
Audiences: []string{"audience"},
623+
},
624+
ClaimValidationRules: []api.ClaimValidationRule{
625+
{
626+
// This expression was previously documented in the godoc for the JWT authenticator
627+
// and was incorrect. It was changed to the above expression in the previous test case.
628+
// Testing the old expression here to confirm it fails validation.
629+
Expression: `claims.?email_verified.orValue(true)`,
630+
},
631+
},
632+
// allow email claim only when email_verified is present and true
633+
ClaimMappings: api.ClaimMappings{
634+
Username: api.PrefixedClaimOrExpression{
635+
Expression: `{claims.?email: "panda"}`,
636+
},
637+
},
638+
},
639+
},
640+
},
641+
want: `[jwt[0].claimValidationRules[0].expression: Invalid value: "claims.?email_verified.orValue(true)": must evaluate to bool, jwt[0].claimMappings.username.expression: Invalid value: "{claims.?email: \"panda\"}": claims.email_verified must be used in claimMappings.username.expression or claimMappings.extra[*].valueExpression or claimValidationRules[*].expression when claims.email is used in claimMappings.username.expression]`,
642+
},
588643
{
589644
name: "valid authentication configuration",
590645
in: &api.AuthenticationConfiguration{

0 commit comments

Comments
 (0)