Skip to content

Session storage refactoring#37992

Merged
mshustov merged 11 commits intoelastic:masterfrom
mshustov:issue-33775-session-storage-refactoring
Jun 7, 2019
Merged

Session storage refactoring#37992
mshustov merged 11 commits intoelastic:masterfrom
mshustov:issue-33775-session-storage-refactoring

Conversation

@mshustov
Copy link
Contributor

@mshustov mshustov commented Jun 4, 2019

Summary

Current implementation passes bound to an incoming request session storage to AuthHandler. This API doesn't allow registerAuth caller to use session storage outside of AuthHandler function.

I changed api in the way when registerAuth returns sessionStorageFactory, therefore it can be used in any place of registerAuth. Security team needs this api to perform logout in route handler

// before
registerAuth(
  (req: KibanaRequest, sessionStorage: SessionStorage, t: authToolkit) => AuthResult, 
  SessionStorageCookieOptions
) => void

// after
registerAuth(
  (req: KibanaRequest, t: authToolkit) => AuthResult, 
  SessionStorageCookieOptions
) => { sessionStorageFactory: SessionStorageFactory }

// in handler
const sessionStorage = sessionStorageFactory.asScoped(request);
sessionStorage.clear();

there are tons of changes in docs after I merged master and run yarn core:acceptApiChanges. looks like the reason for this is@microsoft/api-documenter update #37759

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers


class ScopedCookieSessionStorage<T extends Record<string, any>> implements SessionStorage<T> {
constructor(private readonly server: Server, private readonly request: Request) {}
constructor(private readonly server: Server, private readonly request: Readonly<Request>) {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

issue #33775 has point to restrict access to hapi Request object in registerAuth handler. thus we will operate KibanaRequest-ish object here

}
}

export const toRawRequest = (request: KibanaRequest) => request[requestSymbol];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this api shouldn't be exposed outside of http service

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Because this is kind of sensitive, perhaps we should add a comment and mark it with @internal, it's easy to do a refactor like export * from './request' and accidentally expose this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return {
asScoped(request: Request) {
return new ScopedCookieSessionStorage<T>(server, request);
asScoped(request: Readonly<Request> | KibanaRequest) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As soon as #36509 is implemented we pass bound session storage to a consumer. There is an open question on how to restrict access to this API outside of the plugin boundaries.
#36509 (comment)

@mshustov mshustov added Feature:New Platform Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// release_note:skip Skip the PR/issue when compiling release notes v7.3.0 labels Jun 4, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform

@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security

@mshustov mshustov marked this pull request as ready for review June 4, 2019 13:19
@mshustov mshustov requested a review from a team as a code owner June 4, 2019 13:19
@kobelb
Copy link
Contributor

kobelb commented Jun 4, 2019

From the consumption standpoint, this looks great!

To be honest, I didn't realize that you could access the sessionStorageFactory within the registerAuth "callback" before the promise returned from registerAuth returns:

  const { sessionStorageFactory } = await registerAuth<StorageData>((req, t) => {
      const user = { id: '42' };
      const sessionStorage = sessionStorageFactory.asScoped(req);
      sessionStorage.set({ value: user, expires: Date.now() + 1000 });
      return t.authenticated(user);
    }, cookieOptions);

@elasticmachine
Copy link
Contributor

💔 Build Failed

@mshustov mshustov added the review label Jun 5, 2019
}
}

export const toRawRequest = (request: KibanaRequest) => request[requestSymbol];
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Because this is kind of sensitive, perhaps we should add a comment and mark it with @internal, it's easy to do a refactor like export * from './request' and accidentally expose this


export interface SessionStorageFactory<T> {
asScoped: (request: Request) => SessionStorage<T>;
asScoped: (request: Readonly<Request> | KibanaRequest) => SessionStorage<T>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see we use Readonly<Request> in tests, but would a consumer of the API ever get access to a Request to pass it in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, one of the next actions in #33775 is restrict access to hapi Request object, which is exposed via registerAuth #34631 (comment)

@mshustov mshustov requested a review from rudolf June 6, 2019 08:22
@elasticmachine
Copy link
Contributor

💔 Build Failed

@mshustov mshustov force-pushed the issue-33775-session-storage-refactoring branch from 793cd31 to c6fe0e1 Compare June 6, 2019 15:00
Copy link
Contributor

@joshdover joshdover left a comment

Choose a reason for hiding this comment

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

The main change here looks good. Not worried about any session leakage since SessionStorageFactory only exposes request-scoped session storage

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@mshustov mshustov merged commit f753474 into elastic:master Jun 7, 2019
@mshustov mshustov deleted the issue-33775-session-storage-refactoring branch June 7, 2019 08:18
mshustov added a commit to mshustov/kibana that referenced this pull request Jun 7, 2019
* Kibana request keep a reference to raw request. used to bind hapi-cookie

* CookieSessionStorage should work with KibanaRequest

as soon as registerAuth refactored to restrict access to hapi Request, CookieSessionStorage won't work with hapi request directly

* change registerAuth public api

* adopt auth lifecycle tests

* move lifecycle auth tests from integration to unit and adopt to new api.

* mark toRawRequest as internal to prevent exposure

* generate docs

* reword test cases

* mark Request internals in tsdoc
mshustov added a commit that referenced this pull request Jun 7, 2019
* Kibana request keep a reference to raw request. used to bind hapi-cookie

* CookieSessionStorage should work with KibanaRequest

as soon as registerAuth refactored to restrict access to hapi Request, CookieSessionStorage won't work with hapi request directly

* change registerAuth public api

* adopt auth lifecycle tests

* move lifecycle auth tests from integration to unit and adopt to new api.

* mark toRawRequest as internal to prevent exposure

* generate docs

* reword test cases

* mark Request internals in tsdoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes review Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// v7.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants