Skip to content

Commit

Permalink
Fix authentication on IP change
Browse files Browse the repository at this point in the history
  • Loading branch information
saschat committed Apr 16, 2018
1 parent b958682 commit 6f157fd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/rubyeye/xmemcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,13 @@ public KeyIterator getKeyIterator(InetSocketAddress address)
*/
public Map<InetSocketAddress, AuthInfo> getAuthInfoMap();

/**
* Retruns the AuthInfo for all server strings (hostname:port)
*
* @return A map of AuthInfos for server strings (hostname:port)
*/
public Map<String, AuthInfo> getAuthInfoStringMap();

/**
* "incr" are used to change data for some item in-place, incrementing it. The data for the item
* is treated as decimal representation of a 64-bit unsigned integer. If the current data value
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/rubyeye/xmemcached/XMemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class XMemcachedClient implements XMemcachedClientMBean, MemcachedClient
protected final AtomicInteger serverOrderCount = new AtomicInteger();

private Map<InetSocketAddress, AuthInfo> authInfoMap = new HashMap<InetSocketAddress, AuthInfo>();
private Map<String, AuthInfo> authInfoStringMap = new HashMap<String, AuthInfo>();

private String name; // cache name

Expand Down Expand Up @@ -263,6 +264,15 @@ public Map<InetSocketAddress, AuthInfo> getAuthInfoMap() {

public void setAuthInfoMap(Map<InetSocketAddress, AuthInfo> map) {
this.authInfoMap = map;
this.authInfoStringMap = new HashMap<String, AuthInfo>();
for (Map.Entry<InetSocketAddress, AuthInfo> entry : map.entrySet()) {
String server = AddrUtil.getServerString(entry.getKey());
this.authInfoStringMap.put(server, entry.getValue());
}
}

public Map<String, AuthInfo> getAuthInfoStringMap() {
return this.authInfoStringMap;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.rubyeye.xmemcached.impl.MemcachedTCPSession;
import net.rubyeye.xmemcached.networking.MemcachedSession;
import net.rubyeye.xmemcached.networking.MemcachedSessionConnectListener;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
* Client state listener for auth
Expand All @@ -18,9 +19,10 @@ public class AuthMemcachedConnectListener implements MemcachedSessionConnectList

public void onConnect(MemcachedSession session, MemcachedClient client) {
MemcachedTCPSession tcpSession = (MemcachedTCPSession) session;
Map<InetSocketAddress, AuthInfo> authInfoMap = client.getAuthInfoMap();
Map<String, AuthInfo> authInfoMap = client.getAuthInfoStringMap();
if (authInfoMap != null) {
AuthInfo authInfo = authInfoMap.get(tcpSession.getRemoteSocketAddress());
AuthInfo authInfo =
authInfoMap.get(AddrUtil.getServerString(tcpSession.getRemoteSocketAddress()));
if (authInfo != null) {
XMemcachedClient xMemcachedClient = (XMemcachedClient) client;
AuthTask task = new AuthTask(authInfo, xMemcachedClient.getCommandFactory(), tcpSession);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/rubyeye/xmemcached/utils/AddrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ public static InetSocketAddress getOneAddress(String server) {
public static boolean isEnableShutDownHook() {
return Boolean.valueOf(System.getProperty("xmemcached.shutdown.hook.enable", "false"));
}

/**
* Create an unresolved server string (hostname:port) from an InetSocketAddress.
*/
public static final String getServerString(InetSocketAddress addr) {
return addr.getHostString() + ":" + Integer.toString(addr.getPort());
}
}

0 comments on commit 6f157fd

Please sign in to comment.