Skip to content

Commit

Permalink
Added generalt caching configuration and initial cache on the session…
Browse files Browse the repository at this point in the history
… key server requests with a ttl of 5 minutes.
  • Loading branch information
Andrew Dunn committed Mar 28, 2017
1 parent 24c73ea commit fe162da
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 11 deletions.
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2017 AT&T Intellectual Property, [http://www.att.com]
*
* SPDX-License-Identifier: MIT
*
*******************************************************************************/
package com.mangosolutions.rcloud.rawgist;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import com.mangosolutions.rcloud.rawgist.CacheConfigurationProperties.GistCacheConfiguration;

@Configuration
@EnableConfigurationProperties(CacheConfigurationProperties.class)
public class CacheConfiguration {

private final Logger logger = LoggerFactory.getLogger(CacheConfiguration.class);

@Autowired
private CacheConfigurationProperties cacheConfigurationProperties;

@Autowired
private HazelcastInstance hazelcastInstance;

@Bean
public HazelcastCacheManager cacheManager() {
HazelcastCacheManager cacheManager = new HazelcastCacheManager(hazelcastInstance);
configureCaches(cacheManager);
return cacheManager;
}

public void configureCaches(HazelcastCacheManager hazelcastCacheManager) {
HazelcastInstance hazelcastInstance = hazelcastCacheManager.getHazelcastInstance();
for(GistCacheConfiguration cacheConfig: cacheConfigurationProperties.getCaches()) {
String name = cacheConfig.getName();
if(!StringUtils.isEmpty(name)) {
createAndApplyCacheConfiguration(hazelcastInstance, cacheConfig);
}
}
}

private void createAndApplyCacheConfiguration(HazelcastInstance hazelcastInstance, GistCacheConfiguration cacheConfig) {
Config config = hazelcastInstance.getConfig();
Map<String, MapConfig> mapConfigs = config.getMapConfigs();
String cacheName = cacheConfig.getName();
if(mapConfigs.containsKey(cacheName)) {
logger.warn("Altering existing configuration of cache config {}", mapConfigs.get(cacheName));
}
MapConfig mapConfig = new MapConfig(cacheName);//config.getMapConfig(cacheConfig.getName());
mapConfig.setEvictionPercentage(cacheConfig.getEvictionPercentage());
mapConfig.setEvictionPolicy(cacheConfig.getEvictionPolicy());
mapConfig.setTimeToLiveSeconds(cacheConfig.getTtl());
mapConfig.setMinEvictionCheckMillis(cacheConfig.getEvictionCheck());
config.addMapConfig(mapConfig);
logger.info("Configured cache {} with with settings: {}", cacheName, mapConfig);
}

}
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2017 AT&T Intellectual Property, [http://www.att.com]
*
* SPDX-License-Identifier: MIT
*
*******************************************************************************/
package com.mangosolutions.rcloud.rawgist;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.hazelcast.config.EvictionPolicy;

@ConfigurationProperties()
public class CacheConfigurationProperties {

private List<GistCacheConfiguration> caches;

public List<GistCacheConfiguration> getCaches() {
return caches;
}

public void setCaches(List<GistCacheConfiguration> caches) {
this.caches = caches;
}

public static class GistCacheConfiguration {

private String name;
private int evictionPercentage = 25;
private EvictionPolicy evictionPolicy = EvictionPolicy.LFU;
private int ttl = 300;
private int evictionCheck = 500;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEvictionPercentage() {
return evictionPercentage;
}
public void setEvictionPercentage(int evictionPercentage) {
this.evictionPercentage = evictionPercentage;
}
public EvictionPolicy getEvictionPolicy() {
return evictionPolicy;
}
public void setEvictionPolicy(EvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
}
public int getTtl() {
return ttl;
}
public void setTtl(int ttl) {
this.ttl = ttl;
}
public int getEvictionCheck() {
return evictionCheck;
}
public void setEvictionCheck(int evictionCheck) {
this.evictionCheck = evictionCheck;
}

}


}
Expand Up @@ -40,6 +40,8 @@ public class SessionKeyServerSecurityConfiguration extends WebSecurityConfigurer
@Autowired
private SessionKeyServerProperties keyserverProperties;



@Override
protected void configure(HttpSecurity http) throws Exception {
http
Expand All @@ -56,12 +58,9 @@ protected void configure(HttpSecurity http) throws Exception {
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(preauthAuthProvider());
}

@Bean
public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() {
UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper =
new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>();


@Bean
public SessionKeyServerUserDetailsService getSessionKeyServerUserDetailsService() {
SessionKeyServerUserDetailsService service = new SessionKeyServerUserDetailsService();
String serverUrl = keyserverProperties.getUrl();
if(!StringUtils.isEmpty(serverUrl)) {
Expand All @@ -73,8 +72,15 @@ public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> user
logger.info("Setting the session key URL to {}", serverUrl);
service.setRealm(realm.trim());
}
return service;
}

@Bean
public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() {
UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper =
new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>();

wrapper.setUserDetailsService(service);
wrapper.setUserDetailsService(this.getSessionKeyServerUserDetailsService());
return wrapper;
}

Expand Down
10 changes: 10 additions & 0 deletions rcloud-gist-service/src/main/resources/application.yml
Expand Up @@ -41,3 +41,13 @@ gists:
port: 4301
realm: rcloud
url: http://${gists.keyserver.host}:${gists.keyserver.port}/valid?token={token}&realm={realm}

caches:
-
name: 'sessionkeys'
evictionPercentage: 25
evictionPolicy: LRU
ttl: 300
evictionCheck: 500


0 comments on commit fe162da

Please sign in to comment.