Feature #17862, oAuth2 Service resilient to memcached server restarts

This commit is contained in:
Massimiliano Assante 2019-10-25 11:53:39 +02:00
parent 988cf78d6e
commit 6f32fd961a
2 changed files with 7 additions and 31 deletions

View File

@ -18,6 +18,7 @@ import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.spy.memcached.KetamaConnectionFactory;
import net.spy.memcached.MemcachedClient; import net.spy.memcached.MemcachedClient;
/** /**
@ -33,33 +34,18 @@ public class DistributedCacheClient {
private MemcachedClient mClient; private MemcachedClient mClient;
/**
* Singleton object
*/
private static DistributedCacheClient singleton = new DistributedCacheClient();
/** /**
* Build the singleton instance * Build the singleton instance
*/ */
private DistributedCacheClient(){ public DistributedCacheClient(){
List<InetSocketAddress> addrs = discoverHostOfServiceEndpoint(); List<InetSocketAddress> addrs = discoverHostOfServiceEndpoint();
try { try {
mClient = new MemcachedClient(addrs); mClient = new MemcachedClient(new KetamaConnectionFactory(), addrs);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* Retrieve the singleton instance
*/
public static DistributedCacheClient getInstance(){
if (singleton == null) {
singleton = new DistributedCacheClient();
}
return singleton;
}
public MemcachedClient getMemcachedClient() { public MemcachedClient getMemcachedClient() {
return mClient; return mClient;
} }

View File

@ -40,25 +40,12 @@ public class OauthService {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(OauthService.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(OauthService.class);
/**
* This map contains couples <code, {qualifier-token, insert time, scope, redirect uri, client id}>
*/
private MemcachedClient entries;
/** /**
* Since this is a singleton sub-service, there will be just one call to this constructor and one running thread * Since this is a singleton sub-service, there will be just one call to this constructor and one running thread
* to clean up expired codes. * to clean up expired codes.
*/ */
public OauthService() { public OauthService() {
logger.info("Singleton gcube-oauth service built."); logger.info("Singleton gcube-oauth service built.");
entries = DistributedCacheClient.getInstance().getMemcachedClient();
}
@Override
protected void finalize(){
entries.shutdown();
} }
/** /**
@ -174,6 +161,9 @@ public class OauthService {
return new MemCachedBean("invalid_request"); return new MemCachedBean("invalid_request");
if(!checkIsapplicationTokenType(authorizationService().get(credentials.getClientSecret()).getClientInfo().getType())) // it is not an app token or it is not a token if(!checkIsapplicationTokenType(authorizationService().get(credentials.getClientSecret()).getClientInfo().getType())) // it is not an app token or it is not a token
return new MemCachedBean("invalid_client"); return new MemCachedBean("invalid_client");
MemcachedClient entries = new DistributedCacheClient().getMemcachedClient();
if(entries.get(code) == null) if(entries.get(code) == null)
return new MemCachedBean("invalid_grant"); return new MemCachedBean("invalid_grant");
logger.debug("Got tempCode and looking into memcached for correspondance, "+code); logger.debug("Got tempCode and looking into memcached for correspondance, "+code);