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

SessionToken cache for invalid sessions tokens #4926

Closed
wants to merge 8 commits into from
Closed

SessionToken cache for invalid sessions tokens #4926

wants to merge 8 commits into from

Conversation

flavionegrao
Copy link
Contributor

When a subscription in the live query server has a sessionToken attached and an updated from the parse servers comes in triggered by a afterSave or afterDelete the live query server has to match the permissions against the User related to the subscription/sessionToken.

When the sessionToken for some reason gets invalidated, the subscription in the live query remains quering the backend every time.
This pull request created a entry in the SessionTokenCache as undefined for this situation then avoiding redundant queries.

@flavionegrao flavionegrao changed the title Master SessionToken cache for invalid sessions tokens Aug 3, 2018
Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

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

Thanks for the Pr.

Can you address the different questions as well as revert the changes in the tests?

We like to avoid breaking changes and all the changes in the tests are kinda scary.

Also, I’m not sure those changes about the cache won’t have any bad effect, as the new code interprets an undefined value as an invalid session, which may not be always true.

done();
});
});
fit('can get inexisting userId', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, this blocks the tests from executing

@@ -2,14 +2,16 @@ import Parse from 'parse/node';
import LRU from 'lru-cache';
import logger from '../logger';

function userForSessionToken(sessionToken){
function userIdForSessionToken(sessionToken) {
var q = new Parse.Query("_Session");
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to change this method?

logger.verbose('Fetch userId %s of sessionToken %s from Cache', userId, sessionToken);
return Parse.Promise.as(userId);

if (this.cache.has(sessionToken)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic may be problematic, if we need to invalidate a session we should mark it INVALID_SESSION_TOKEN.

Also I’m not sure that the solution is the right one as every time a session will be missing, we’ll interpret as invalid. I’d rather add a Boolean onlyFromCache = false of we don’t want to poke the server.

@codecov
Copy link

codecov bot commented Aug 7, 2018

Codecov Report

Merging #4926 into master will decrease coverage by 73.37%.
The diff coverage is 28.57%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #4926       +/-   ##
===========================================
- Coverage   92.96%   19.58%   -73.38%     
===========================================
  Files         119      119               
  Lines        8837     8840        +3     
===========================================
- Hits         8215     1731     -6484     
- Misses        622     7109     +6487
Impacted Files Coverage Δ
src/LiveQuery/SessionTokenCache.js 34.61% <28.57%> (-52.35%) ⬇️
src/cloud-code/HTTPResponse.js 0% <0%> (-100%) ⬇️
src/Adapters/Cache/NullCacheAdapter.js 0% <0%> (-100%) ⬇️
src/requiredParameter.js 0% <0%> (-100%) ⬇️
src/Push/utils.js 4.76% <0%> (-95.24%) ⬇️
src/LiveQuery/Subscription.js 4.76% <0%> (-95.24%) ⬇️
src/AccountLockout.js 2.7% <0%> (-94.6%) ⬇️
src/middlewares.js 3.61% <0%> (-94.58%) ⬇️
src/StatusHandler.js 5.14% <0%> (-94.12%) ⬇️
src/Options/parsers.js 6.06% <0%> (-93.94%) ⬇️
... and 92 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a61ef7e...378442a. Read the comment docs.

Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. Can we focus on:

  • fix the issues with the session token invalidation
  • pull out what you added for the roles cache as it is it’s onw PR (which has been started on Live query CLP #4387)

@@ -22,6 +22,7 @@
"no-multiple-empty-lines": 1,
"prefer-const": "error",
"space-infix-ops": "error",
"no-useless-escape": "off"
"no-useless-escape": "off",
"no-console":"off"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert

@@ -1065,9 +1080,9 @@ describe('ParseLiveQueryServer', function() {
//Return a role with the name "liveQueryRead" as that is what was set on the ACL
const liveQueryRole = new Parse.Role();
liveQueryRole.set('name', 'liveQueryRead');
return [
return Parse.Promise.as([
Copy link
Contributor

Choose a reason for hiding this comment

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

Parse.Promise is removed now, please use Promose

@@ -2,49 +2,75 @@ const SessionTokenCache = require('../lib/LiveQuery/SessionTokenCache').SessionT

describe('SessionTokenCache', function() {

beforeEach(function(done) {
const Parse = require('parse/node');
describe('Valid session', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned before, can we please not change all tests this way? But add new tests as we go?

user.id = userId;
const rolesQuery = new Parse.Query(Parse.Role);
rolesQuery.equalTo("users", user);
return rolesQuery.find({useMasterKey:true});
Copy link
Contributor

Choose a reason for hiding this comment

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

This is invalid, as roles can be referenced in roles etc...

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, the roles cache should /could be shared with the existing parse-server one

@flovilmart
Copy link
Contributor

@flavionegrao I've just opened #4940 which refactors heavily Auth.js. With the refactor, you should be able to:

  • Authenticate a session token and get the user
  • Get all the roles for a particular user
  • Reuse a shared cache used by the server or a new in memory one.

@flavionegrao
Copy link
Contributor Author

flavionegrao commented Aug 8, 2018 via email

@flovilmart
Copy link
Contributor

As I mentioned, I refactored the Auth.js module in order to help you achieve your goal. We can rip out the SessionTokenCache and use the Auth.js features with a custom cache controller or a shared one from Parse Server.

@flavionegrao
Copy link
Contributor Author

flavionegrao commented Aug 8, 2018 via email

@flovilmart
Copy link
Contributor

So just to sum up quickly:

  1. I'M 💯 with you on this PR let me know if you need anything:
  2. The user authentication in liveQuery should be refactored using Auth.js and Auth module refactoring in order to be reusable #4940
  3. CLP + Roles support is provided with Live query CLP #4387, but this PR needs all the goodies of the step 2.
  4. We can then provide a way to provide a cache adapter to the liveQuery server
  5. And optionally, if liveQuery lives on the same server as Parse server, pass an config

What do you think?

@flovilmart
Copy link
Contributor

@flavionegrao I went ahead and rebased the CLP for live query features with all the requirements that you may need:

#4387

Can you check it out an let me know if there's anything else we can do to improve it?

@stale
Copy link

stale bot commented Sep 23, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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.

2 participants