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

Enforce JWT expiration for subscriptions #4166

Merged
merged 16 commits into from
Nov 13, 2023
Merged

Conversation

garypen
Copy link
Contributor

@garypen garypen commented Nov 8, 2023

If a JWT expires whilst a subscription is executing, the subscription should be terminated.

fixes: #3947


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Tests added and passing3
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

It's tricky to automatically test token expiration, so I've tested this feature manually. I used:
https://www.unixtimestamp.com/
https://jwt.io/
to generate timestamps and then JWT tokens using those timestamps as exp claims.

I ran a router configured to support JWT authn and subscription and then subscribed to a feed using curl and ensured my subscription timed out. Sample output snippet:

--graphql
content-type: application/json

{"payload":{"data":{"reviewAdded":{"id":4}}}}
--graphql
content-type: application/json

{}
--graphql
content-type: application/json

{"payload":null,"errors":[{"message":"subscription closed because the JWT has expired","extensions":{"code":"SUBSCRIPTION_JWT_EXPIRED"}}]}
--graphql--

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

If a JWT expires whilst a subscription is executing, the subscription
should be terminated.

fixes: #3947
@garypen garypen self-assigned this Nov 8, 2023

This comment has been minimized.

@router-perf
Copy link

router-perf bot commented Nov 8, 2023

CI performance tests

  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • large-request - Stress test with a 1 MB request payload
  • step - Basic stress test that steps up the number of users over time
  • xlarge-request - Stress test with 10 MB request payload
  • reload - Reload test over a long period of time at a constant rate of users
  • no-graphos - Basic stress test, no GraphOS.
  • xxlarge-request - Stress test with 100 MB request payload
  • step-jemalloc-tuning - Clone of the basic stress test for jemalloc tuning
  • const - Basic stress test that runs with a constant number of users

Try just filtering out the response
Now simply map the response and check for expired JWT tokens.
@garypen garypen requested review from a team, BrynCooke, bnjjj and lrlna November 8, 2023 15:06
@garypen garypen marked this pull request as ready for review November 8, 2023 15:06
We've already verified that our claim:
 - exists
 - is a number

This last change ensures that if we can't create a valid DateTime values
from our timestamp then we log an error and continue as though we didn't
have an expiry date.
@bnjjj
Copy link
Contributor

bnjjj commented Nov 10, 2023

cc @martinbonnin @alessbell @calvincestari in case you would like to do something specific in your client with that error code

apollo-router/Cargo.toml Outdated Show resolved Hide resolved
let stream =
stream
.map(move |mut response: Response| {
// Enforce JWT expiry for deferred and subscription responses
Copy link
Contributor

Choose a reason for hiding this comment

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

could it be enforced on the request side, before executing the subscription?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yes good catch ! I think you can move your logic right here gary to not try to fetch data from subgraphs if your jwt is expired

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've split the logic for enforcement and now enforce subscription expiration in the request as @bnjjj suggested. I've left the defer enforcement in the response. We could remove that I suppose, but it's probably better to have it than not.

@garypen garypen changed the title Enfore JWT expiration for subscriptions Enforce JWT expiration for subscriptions Nov 10, 2023
@garypen garypen requested a review from Geal November 13, 2023 08:18
@garypen garypen merged commit 4d3c594 into dev Nov 13, 2023
2 checks passed
@garypen garypen deleted the garypen/3947-jwt-sub-expire branch November 13, 2023 13:13
@abernix abernix mentioned this pull request Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JWT expiration with subscription
3 participants