Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: authorization api for jwt claims #4009

Merged
merged 25 commits into from
Sep 6, 2024

Conversation

zhaohuabing
Copy link
Member

@zhaohuabing zhaohuabing commented Aug 6, 2024

API for #3367

A SecurityPolicy enforcing authz on a Gateway resource based on jwtClaims (sub=jason or sub=alice) and iss=foo.bar.com:

  apiVersion: gateway.envoyproxy.io/v1alpha1
  kind: SecurityPolicy
  metadata:
    namespace: envoy-gateway
    name: policy-for-gateway 
  spec:
    targetRef:
      group: gateway.networking.k8s.io
      kind: Gateway
      name: gateway-1
    authorization:
      defaultAction: Deny
      rules:
      - action: Allow
        principal:
          jwt:
            claims:
            - name: sub
              values: [jason, alice]
            - name: iss
              values: [foo.bar.com]

A SecurityPolicy enforcing authz on a HTTPRoute resource based on scope contains "read::message"

  apiVersion: gateway.envoyproxy.io/v1alpha1
  kind: SecurityPolicy
  metadata:
    namespace: envoy-gateway
    name: policy-for-route 
  spec:
    targetRef:
      group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: route-1
    authorization:
      defaultAction: Deny
      rules:
      - action: Allow
        principal:
          jwt:
            sopes: [read::message]

Scopes and claims can be used together, they're And-ed if both are specified.
A SecurityPolicy enforcing authz on a HTTPRoute resource based on scope contains "read::message" and a custom claim message_type:

  apiVersion: gateway.envoyproxy.io/v1alpha1
  kind: SecurityPolicy
  metadata:
    namespace: envoy-gateway
    name: policy-for-route 
  spec:
    targetRef:
      group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: route-1
    authorization:
      defaultAction: Deny
      rules:
      - action: Allow
        principal:
          jwt:
            sopes: [read::message]            
            claims:
            - name: message_type
              values: [foo, bar]

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@zhaohuabing zhaohuabing added the area/api API-related issues label Aug 6, 2024
@zhaohuabing zhaohuabing requested a review from a team as a code owner August 6, 2024 10:02
Copy link

codecov bot commented Aug 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.96%. Comparing base (5998980) to head (e43f31a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4009      +/-   ##
==========================================
+ Coverage   67.95%   67.96%   +0.01%     
==========================================
  Files         189      189              
  Lines       23103    23103              
==========================================
+ Hits        15699    15703       +4     
+ Misses       6281     6279       -2     
+ Partials     1123     1121       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Copy link
Contributor

@zetaab zetaab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good from my perspective

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
// JWT authentication in the same `SecurityPolicy`.
// +optional
// +notImplementedHide
JWT *JWTPrincipal `json:"jwt,omitempty"`
Copy link
Member Author

@zhaohuabing zhaohuabing Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may have better names for the field JWT and type JWTPrincipal :-) @arkodg @missBerg

// If the claim is a string type, the specified value must match exactly.
// If the claim is a string array type, the specified value must match one of the values in the array.
// If multiple values are specified, one of the values must match for the rule to match.
Values []string `json:"values"`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using string for bool as "true"/"false" and integer if we need these types in the future. Any is too flexible, as it can represent nested structures besides simple types, which we won't use.

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
zhaohuabing and others added 6 commits August 29, 2024 09:46
Co-authored-by: Arko Dasgupta <arkodg@users.noreply.github.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Co-authored-by: Arko Dasgupta <arkodg@users.noreply.github.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
api/v1alpha1/authorization_types.go Show resolved Hide resolved
// +kubebuilder:default=String
// +unionDiscriminator
// +optional
ValueType *JWTClaimValueType `json:"valueType,omitempty"`
Copy link
Contributor

@denniskniep denniskniep Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to specify the Matcher instead of the ClaimValueType? I think that would bring more flexibility.

i.e.

type JWTClaim struct {	
	Name string `json:"name"`
	ValueMatcher JWTClaimValueMatcher `json:"valueMatcher "`
}

type JWTClaimValueMatcher struct {
     equalsAny *JWTClaimValues  `json:"valueType,omitempty"`     
     notEqualsAny *JWTClaimValues  `json:"valueType,omitempty"`
     includesAll *JWTClaimValues  `json:"valueType,omitempty"`
     includesAny *JWTClaimValues  `json:"valueType,omitempty"`
     notIncludesAny *JWTClaimValues  `json:"valueType,omitempty"`
}

type JWTClaimValues struct {
     Values []string `json:"values"`
}
  • equalsAny (assume claim is single string)
  • notEqualsAny (assume claim is single string)
  • includesAll (assume claim is string array)
  • includesAny (assume claim is string array)
  • notIncludesAny (assume claim is string array)
  • ...

If necessary for a certain Matcher we could specify different properties

What do you think?

Copy link
Member Author

@zhaohuabing zhaohuabing Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @denniskniep In theory, we could make this API as flexible as the Envoy RBAC API, but I would like to make this API simpler and easier to understand as it's a user-facing API.

I believe the normal use cases for JWT claim based authorization is that we just compare wether the claims equal with any of the specified values (equalsAny). I can't think of any use cases for a substring matching.

Scope comparison is a bit different, all specified scopes must exist in the JWT. It's handled in a dedicated API field.

For the not use case, it can be accomplished with a Allow default action and a Deny rule:

      rules:
      - default: Allow
      - action: Deny
        principal:
          jwt:
            claims:
            - name: user
              values: [jason, alice]

arkodg
arkodg previously approved these changes Sep 3, 2024
Copy link
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks Huabing !

@arkodg arkodg requested review from a team and removed request for missBerg and zetaab September 3, 2024 20:50
zirain
zirain previously approved these changes Sep 4, 2024
@zirain
Copy link
Contributor

zirain commented Sep 4, 2024

--- FAIL: TestSecurityPolicyTarget (0.13s)
    --- FAIL: TestSecurityPolicyTarget/authorization-missing_principal (0.00s)
        securitypolicy_test.go:1126: Unexpected response while creating SecurityPolicy; got err=
            SecurityPolicy.gateway.envoyproxy.io "sp-1725430984228589885" is invalid: [spec: Invalid value: "object": if authorization.rules.principal.jwt is used, jwt must be defined, spec.authorization.rules[0].principal.jwt: Invalid value: "object": at least one of claims or scopes must be specified]
            ;missing strings within error=["at least one of clientCIDRs or jwt must be specified"]
FAIL
FAIL	github.com/envoyproxy/gateway/test/cel-validation	7.719s

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@zhaohuabing zhaohuabing dismissed stale reviews from zirain and arkodg via 41fc238 September 6, 2024 03:29
@zhaohuabing zhaohuabing merged commit 4ed16f2 into envoyproxy:main Sep 6, 2024
26 of 27 checks passed
@zhaohuabing zhaohuabing deleted the jwt-claim-auth branch September 6, 2024 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api API-related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants