open-consul/agent/xds
hc-github-team-consul-core db4b2cb577
Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153)
## Backport

This PR is auto-generated from #18062 to be assessed for backporting due
to the inclusion of the label backport/1.16.



The below text is copied from the body of the original PR.

---

### Description

<!-- Please describe why you're making this change, in plain English.
-->

- Currently the jwt-auth filter doesn't take into account the service
identity when validating jwt-auth, it only takes into account the path
and jwt provider during validation. This causes issues when multiple
source intentions restrict access to an endpoint with different JWT
providers.
- To fix these issues, rather than use the JWT auth filter for
validation, we use it in metadata mode and allow it to forward the
successful validated JWT token payload to the RBAC filter which will
make the decisions.

This PR ensures requests with and without JWT tokens successfully go
through the jwt-authn filter. The filter however only forwards the data
for successful/valid tokens. On the RBAC filter level, we check the
payload for claims and token issuer + existing rbac rules.

### Testing & Reproduction steps

<!--

* In the case of bugs, describe how to replicate
* If any manual tests were done, document the steps and the conditions
to replicate
* Call out any important/ relevant unit tests, e2e tests or integration
tests you have added or are adding

-->

- This test covers a multi level jwt requirements (requirements at top
level and permissions level). It also assumes you have envoy running,
you have a redis and a sidecar proxy service registered, and have a way
to generate jwks with jwt. I mostly use:
https://www.scottbrady91.com/tools/jwt for this.

- first write your proxy defaults
```
Kind = "proxy-defaults"
name = "global"
config {
  protocol = "http"
}
```
- Create two providers 
```
Kind = "jwt-provider"
Name = "auth0"
Issuer = "https://ronald.local"

JSONWebKeySet = {
    Local = {
     JWKS = "eyJrZXlzIjog....."
    }
}
```

```
Kind = "jwt-provider"
Name = "okta"
Issuer = "https://ronald.local"

JSONWebKeySet = {
   Local = {
     JWKS = "eyJrZXlzIjogW3...."
    }
}
```

- add a service intention
```
Kind = "service-intentions"
Name = "redis"

JWT = {
  Providers = [
    {
      Name = "okta"
    },
  ]
}

Sources = [
  {
    Name = "*"
    Permissions = [{
      Action = "allow"
      HTTP = {
        PathPrefix = "/workspace"
      }
      JWT = {
        Providers = [
          {
            Name = "okta"
            VerifyClaims = [
              {
                  Path = ["aud"]
                  Value = "my_client_app"
              },
              {
                Path = ["sub"]
                Value = "5be86359073c434bad2da3932222dabe"
              }
            ]
          },
        ]
      }

    },
    {
      Action = "allow"
      HTTP = {
        PathPrefix = "/"
      }
      JWT = {
        Providers = [
          {
            Name = "auth0"
          },
        ]
      }

    }]
  }
]
```
- generate 3 jwt tokens: 1 from auth0 jwks, 1 from okta jwks with
different claims than `/workspace` expects and 1 with correct claims
- connect to your envoy (change service and address as needed) to view
logs and potential errors. You can add: `-- --log-level debug` to see
what data is being forwarded
```
consul connect envoy -sidecar-for redis1 -grpc-addr 127.0.0.1:8502
```
- Make the following requests: 
```
curl -s -H "Authorization: Bearer $Auth0_TOKEN" --insecure --cert leaf.cert --key leaf.key --cacert connect-ca.pem https://localhost:20000/workspace -v

RBAC filter denied

curl -s -H "Authorization: Bearer $Okta_TOKEN_with_wrong_claims" --insecure --cert leaf.cert --key leaf.key --cacert connect-ca.pem https://localhost:20000/workspace -v

RBAC filter denied

curl -s -H "Authorization: Bearer $Okta_TOKEN_with_correct_claims" --insecure --cert leaf.cert --key leaf.key --cacert connect-ca.pem https://localhost:20000/workspace -v

Successful request
```


### TODO

* [x] Update test coverage
* [ ] update integration tests (follow-up PR)
* [x] appropriate backport labels added


---

<details>
<summary> Overview of commits </summary>

  - 70536f5a38507d7468f62d00dd93a6968a3d9cf3 

</details>

Co-authored-by: Ronald Ekambi <ronekambi@gmail.com>
2023-07-17 15:50:21 +00:00
..
accesslogs copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
extensionruntime Add Upstream Service Targeting to Property Override Extension (#17517) 2023-05-30 14:53:42 -04:00
testcommon copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
testdata Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
validateupstream-test copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
clusters.go Expose JWKS cluster config through JWTProviderConfigEntry (#17978) (#18002) 2023-07-04 09:53:12 -04:00
clusters_test.go Expose JWKS cluster config through JWTProviderConfigEntry (#17978) (#18002) 2023-07-04 09:53:12 -04:00
config.go Support Envoy's MaxEjectionPercent and BaseEjectionTime config entries for passive health checks (#15979) 2023-04-26 15:59:48 -07:00
config_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
delta.go backport of commit 43d48124139eb3808cb9ebe6ebda83c7e66481d7 (#17742) 2023-06-14 17:18:59 +00:00
delta_envoy_extender_oss_test.go backport of commit c0afba3a0c2ae093fee756a9019d49db25367d69 (#17975) 2023-06-30 14:20:50 +00:00
delta_envoy_extender_test.go Backport of ext-authz Envoy extension: support `localhost` as a valid target URI. into release/1.16.x (#17837) 2023-06-21 21:00:02 +00:00
delta_test.go backport of commit 2735bbe60f316a4d4539752a8dd63a3ca360e49b (#17613) 2023-06-08 14:41:44 +00:00
endpoints.go This fixes an issue where TCP services that are exported cannot be configured to failover. (#17469) 2023-05-25 12:50:20 -04:00
endpoints_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
failover_policy.go Add Upstream Service Targeting to Property Override Extension (#17517) 2023-05-30 14:53:42 -04:00
failover_policy_oss.go [COMPLIANCE] Add Copyright and License Headers (#16854) 2023-04-20 12:40:22 +00:00
golden_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
jwt_authn.go Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
jwt_authn_test.go Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
listeners.go Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
listeners_apigateway.go xds: Remove APIGateway ToIngress function (#17453) 2023-05-25 15:16:37 +00:00
listeners_ingress.go xds: generate listeners directly from API gateway snapshot (#17398) 2023-05-22 17:36:29 -04:00
listeners_test.go backport of commit bfbb7f65a64659d87d331a032679bf639c2a3f29 (#17812) 2023-06-20 15:11:25 +00:00
naming.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
net_fallback.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
net_linux.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
protocol_trace.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
rbac.go Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
rbac_test.go Backport of Use JWT-auth filter in metadata mode & Delegate validation to RBAC filter into release/1.16.x (#18153) 2023-07-17 15:50:21 +00:00
resources.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
resources_oss_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
resources_test.go Backport of api-gateway: stop adding all header filters to virtual host when generating xDS into release/1.16.x (#17658) 2023-06-12 16:58:05 +00:00
response.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
routes.go Backport of api-gateway: stop adding all header filters to virtual host when generating xDS into release/1.16.x (#17658) 2023-06-12 16:58:05 +00:00
routes_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
secrets.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
server.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
server_oss.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
testing.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
xds.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00
xds_protocol_helpers_test.go Disable remote proxy patching except AWS Lambda (#17415) 2023-05-23 11:55:06 +00:00
z_xds_packages.go Various bits of cleanup detected when using Go Workspaces (#17462) 2023-06-05 16:08:39 -04:00
z_xds_packages_test.go copyright headers for agent folder (#16704) 2023-03-28 14:39:22 -04:00