Transcript
Page 1: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Infinite  Session  Clustering  with    Apache  Shiro  &  Cassandra  

Les  Hazlewood  @lhazlewood  Apache  Shiro  Project  Chair  

CTO,  Stormpath  stormpath.com  

Silicon  Valley  C*  Day  2014  

Page 2: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

 .com  •  User  Management  and  AuthenAcaAon  API  

•  Security  for  your  applicaAons  •  User  security  workflows  •  Security  best  pracAces  •  Developer  tools,  SDKs,  libraries  

Page 3: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

•  ApplicaAon  security  framework  

•  ASF  TLP  hMp://shiro.apache.org  

•  Quick  and  Easy  •  Simplifies  Security  

What  is  Apache  Shiro?  

Page 4: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Web  Session  Management  

Auxiliary  Features  

AuthorizaAon  AuthenAcaAon  

Cryptography  Session  

Management  

Web  Support  

Page 5: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Quick  Concepts  

Subject currentUser = SecurityUtils.getSubject();

currentUser.login(...) currentUser.isPermitted(...)

Page 6: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Defined  

Managing  the  lifecycle  of  Subject-­‐specific  temporal  data  context  

Page 7: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Features  

•  Heterogeneous  client  access  •  POJO/J2SE  based  (IoC  friendly)  •  Event  listeners  •  Host  address  retenAon  •  InacAvity/expiraAon  support  (touch())  •  Transparent  web  use  -­‐  HMpSession  •  Container-­‐Independent  Clustering!  

Page 8: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Acquiring  and  CreaNng  Sessions  Subject subject = SecurityUtils.getSubject() //guarantee a session Session session = subject.getSession(); //get a session if it exists subject.getSession(false);

Page 9: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  API  getStartTimestamp()

getLastAccessTime()

getAttribute(key)

setAttribute(key, value)

get/setTimeout(long)

touch()

...

Page 10: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject   .getSession()  à   Session  

Page 11: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

.getSession()  à   Session  

Page 12: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

.getSession()  à  

Session  Factory  

Session  

Page 13: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  Factory  

Session  

Page 14: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Factory  

Session  

Page 15: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Session  

Page 16: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

Session  

Data  store  

Page 17: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

ValidaAon  Scheduler  

Session  

Data  store  

Page 18: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Management  Architecture  Subject  

SessionManager  

SessionDAO  

.getSession()  à  

Session  ID  Generator  

Session  Cache  

Session  Factory  

ValidaAon  Scheduler   Session  

Listeners  

Session  

Data  store  

Page 19: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Clustering:  Clustered  Data  Store  of  Choice  

SessionDAO  

Session  ID  Generator  

Session  Cache  

ValidaAon  Scheduler  

Data  store  

Page 20: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Web  ConfiguraNon  

•  web.xml  elements  

•  Protects  all  URLs  

•  InnovaAve  Filtering  (URL-­‐specific  chains)  

•  JSP  Tag  support  

•  Transparent  HMpSession  support  

Page 21: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

web.xml  <listener> <listener-class> org.apache.shiro.web.env.EnvironmentLoaderListener </listener-class> </listener> <filter> <filter-name>ShiroFilter</filter-name> <filter-class> org.apache.shiro.web.servlet.ShiroFilter </filter-class> </filter>

Page 22: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

web.xml  cont’d  <filter-mapping> <filter-name>ShiroFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>

Page 23: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

shiro.ini  overview  [main] # bean config here [users] # optional static user accounts (and their roles) here [roles] # optional static roles (and their permissions) here [urls] # filter chains here

Page 24: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Clustering  

Page 25: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Two  Approaches  •  Write  a  SessionDAO  

 •  Use  EnterpriseCacheSessionDAO  and  

write  a  CacheManager  

Page 26: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Cassandra  SessionDAO  

Page 27: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

SessionDAO  Concerns  

SessionManager  

SessionDAO  

Session  ID  Generator  

Session  Cache  

Data  store  

Page 28: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Custom  SessionDAO  public class MySessionDAO extends AbstractSessionDAO { protected void doCreate(Session s){...} protected void doReadSession(Serializable id){...} protected void delete(Session s){...} protected void update(Session s){...}

Collection<Session> getActiveSessions(){...} } Or public class MySessionDAO extends CachingSessionDAO { ... //enables write-through caching }

Page 29: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

NaNve  Web  Session  Manager  [main] sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager securityManager.sessionManager = $sessionManager

Page 30: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Cassandra  SessionDAO  [main] ... cassandraCluster = com.leshazlewood.samples.shiro.cassandra.ClusterFactory

sessionDAO = com.leshazlewood.samples.shiro.cassandra.CassandraSessionDAO sessionDAO.cluster = $cassandraCluster sessionDAO.keyspaceName = shirosessions sessionDAO.tableName = sessions ...

Page 31: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Plug  in  the  SessionDAO  [main] ... sessionManager.sessionDAO = $sessionDAO

Page 32: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Sessions  Table  (CQL  3)  CREATE TABLE sessions ( id timeuuid PRIMARY KEY, start_ts timestamp, stop_ts timestamp, last_access_ts timestamp, timeout bigint, expired boolean, host varchar, serialized_value blob )

Page 33: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

No  ValidaNon  Scheduler?  

Page 34: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

No  ValidaNon  Scheduler?  

Use  Cassandra’s  TTL  

Page 35: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

TTL  for  session  Nmeout  [main] # Cassandra can enforce a TTL. # No need for Shiro to invalidate! sessionManager.sessionValidationSchedulerEnabled = false

Page 36: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Session  Upsert  (CQL  3)  UPDATE sessions USING TTL $timeout SET start_ts = ?, stop_ts = ?, last_access_ts = ?, timeout = ?,

expired = ?, host = ?, serialized_value = ? WHERE id = ?

Page 37: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

But  what  about  tombstones!?!?  

Page 38: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Sessions  Table  (revised)  CREATE TABLE sessions ( id timeuuid PRIMARY KEY, start_ts timestamp, stop_ts timestamp, last_access_ts timestamp, timeout bigint, expired boolean, host varchar, serialized_value blob ) WITH gc_grace_seconds = 86400 AND compacation = {‘class’:’LeveledCompactionStrategy’}

Page 39: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

But  what  about  row  caching?  

Page 40: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Row  Cache?  Don’t  need  it!    •  SSTable  likely  in  OperaAng  System  page  cache  (off  heap)  

•  DO  use  Key  Cache  (very  important,  enabled  by  default  in  1.2+)  

Page 41: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Demo  Time!  

Page 42: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Code  

$ git clone https://github.com/lhazlewood/shiro-cassandra-sample.git $ cd shiro-cassandra-sample $ $CASSANDRA_HOME/bin/cassandra $ mvn jetty:run Open a browser to http://localhost:8080

Page 43: Cassandra Day SV 2014: Infinite Session Clustering with Apache Cassandra

@lhazlewood  |  @goStormpath  

Thank  You!  

•  [email protected]  •  TwiMer:  @lhazlewood  •  hMp://www.stormpath.com  


Recommended