Skip to content

Commit

Permalink
Ticket registry based on infinispan cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Dec 21, 2015
1 parent 8fa64b6 commit b7b7e49
Show file tree
Hide file tree
Showing 55 changed files with 325 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cas-server-core-audit/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-events/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-logging/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-logout/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-monitor/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-services/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-tickets/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-util/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core-validation/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-core/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
2 changes: 1 addition & 1 deletion cas-server-extension-clearpass/src/test/java/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Configuration shutdownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
Expand Down
26 changes: 26 additions & 0 deletions cas-server-integration-infinispan/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
description = 'Apereo CAS Infinispan Integration'
dependencies {
compile group: 'org.springframework', name: 'spring-context-support', version:springVersion
compile group: 'org.springframework', name: 'spring-context', version:springVersion
compile(group: 'org.infinispan', name: 'infinispan-core', version:infinispanVersion) {
exclude(module: 'guava')
}
compile(group: 'org.infinispan', name: 'infinispan-commons', version:infinispanVersion) {
exclude(module: 'guava')
}

compile project(':cas-server-core-api')
compile project(':cas-server-core-tickets')

testCompile(group: 'org.infinispan', name: 'infinispan-embedded', version:infinispanVersion) {
exclude(module: 'guava')
exclude(module: 'leveldb-api')
exclude(module: 'leveldbjni')
}
testCompile project(path: ":cas-server-core-authentication", configuration: "tests")
testCompile project(':cas-server-core-services')
testCompile project(path: ":cas-server-core-services", configuration: "tests")
testCompile project(':cas-server-core-util')
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.jasig.cas.ticket.registry;

import org.infinispan.Cache;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.registry.encrypt.AbstractCrypticTicketRegistry;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

/**
* This is {@link InfinispanTicketRegistry}. Infinispan is a distributed in-memory key/value data store with optional schema.
* It offers advanced functionality such as transactions, events, querying and distributed processing.
* See <a href="http://infinispan.org/features/">http://infinispan.org/features/</a> for more info.
*
* @author Misagh Moayyed
* @since 4.2.0
*/
@Component("infinispanTicketRegistry")
public final class InfinispanTicketRegistry extends AbstractCrypticTicketRegistry {

@Resource(name="infinispanTicketsCache")
private Cache<String, Ticket> cache;

/**
* Instantiates a new Infinispan ticket registry.
*/
public InfinispanTicketRegistry() {
}

@Override
protected void updateTicket(final Ticket ticket) {
this.cache.put(ticket.getId(), ticket);
}

@Override
protected boolean needsCallback() {
return true;
}

/**
* Add a ticket to the registry. Ticket storage is based on the ticket id.
*
* @param ticket The ticket we wish to add to the cache.
*/
@Override
public void addTicket(final Ticket ticket) {
this.cache.put(ticket.getId(), ticket);
}

/**
* Retrieve a ticket from the registry.
*
* @param ticketId the id of the ticket we wish to retrieve
* @return the requested ticket.
*/
@Override
public Ticket getTicket(final String ticketId) {
final Ticket ticket = this.cache.get(ticketId);
return getProxiedTicketInstance(ticket);
}

/**
* Remove a specific ticket from the registry.
*
* @param ticketId The id of the ticket to delete.
* @return true if the ticket was removed and false if the ticket did not
* exist.
*/
@Override
public boolean deleteTicket(final String ticketId) {
if (getTicket(ticketId) == null) {
return false;
}
this.cache.evict(ticketId);
return true;

}

/**
*
* Retrieve all tickets from the registry.
*
* Note! Usage of this method can be computational and I/O intensive and should not be used for other than
* debugging.
*
* @return collection of tickets currently stored in the registry. Tickets
* might or might not be valid i.e. expired.
*/
@Override
public Collection<Ticket> getTickets() {
return this.cache.values();
}

public void setCache(final Cache<String, Ticket> cache) {
this.cache = cache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.jasig.cas.ticket.registry;

import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.TestUtils;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicket;
import org.jasig.cas.ticket.TicketGrantingTicketImpl;
import org.jasig.cas.ticket.support.NeverExpiresExpirationPolicy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

/**
* This is {@link InfinispanTicketRegistryTests}.
*
* @since 4.2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/infinispan-springcache-tests.xml")
public class InfinispanTicketRegistryTests {

@Autowired
@Qualifier("infinispanTicketRegistry")
private TicketRegistry infinispanTicketRegistry;

@Test
public void updateTicketShouldOverwriteTicketInStorage() {
final Ticket ticket = getTicket();
infinispanTicketRegistry.addTicket(ticket);
assertFalse(infinispanTicketRegistry.getTicket(ticket.getId()).isExpired());
final TicketGrantingTicket ticket2 = (TicketGrantingTicket) ticket;
ticket2.markTicketExpired();
infinispanTicketRegistry.addTicket(ticket);
assertTrue(infinispanTicketRegistry.getTicket(ticket.getId()).isExpired());
}

@Test
public void addTicketExistsInCache() {
final Ticket ticket = getTicket();
infinispanTicketRegistry.addTicket(ticket);
assertEquals(infinispanTicketRegistry.getTicket(ticket.getId()), ticket);
}

@Test
public void deleteTicketRemovesFromCacheReturnsTrue() {
final Ticket ticket = getTicket();
infinispanTicketRegistry.addTicket(ticket);
assertTrue(infinispanTicketRegistry.deleteTicket(ticket.getId()));
assertNull(infinispanTicketRegistry.getTicket(ticket.getId()));
}

@Test
public void deleteTicketOnNonExistingTicketReturnsFalse() {
final String ticketId = "does_not_exist";
assertFalse(infinispanTicketRegistry.deleteTicket(ticketId));
}

@Test
public void getTicketReturnsTicketFromCacheOrNull() {
final Ticket ticket = getTicket();
infinispanTicketRegistry.addTicket(ticket);
assertEquals(infinispanTicketRegistry.getTicket(ticket.getId()), ticket);
assertNull(infinispanTicketRegistry.getTicket(""));
}

private Ticket getTicket() {
final Authentication authentication = TestUtils.getAuthentication();
return new TicketGrantingTicketImpl("123", authentication, new NeverExpiresExpirationPolicy());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


<bean id="infinispanTicketRegistry" class="org.jasig.cas.ticket.registry.InfinispanTicketRegistry" />
<bean id="infinispanTicketsCache" factory-bean="cacheManager" factory-method="getCache" />
<bean id="cacheManager" class="org.infinispan.manager.DefaultCacheManager"
c:configurationFile-ref="configFilePath"/>

<bean id="configFilePath" factory-bean="configFile" factory-method="getFilename" />
<bean id="configFile" class="org.springframework.core.io.ClassPathResource"
c:path="infinispan.xml" />
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<infinispan xsi:schemaLocation="urn:infinispan:config:8.1 http://www.infinispan.org/schemas/infinispan-config-8.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:infinispan:config:8.1">

<cache-container default-cache="cas">
<jmx duplicate-domains="true" />
<local-cache name="cas" />
</cache-container>
</infinispan>
Loading

0 comments on commit b7b7e49

Please sign in to comment.