-
Notifications
You must be signed in to change notification settings - Fork 615
/
SslAuthenticationTest.java
164 lines (143 loc) · 6.49 KB
/
SslAuthenticationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.infinispan.client.hotrod;
import static org.infinispan.server.hotrod.test.HotRodTestingUtil.hotRodCacheConfiguration;
import static org.testng.AssertJUnit.assertEquals;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import javax.security.auth.Subject;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.exceptions.HotRodClientException;
import org.infinispan.client.hotrod.test.HotRodClientTestingUtil;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.security.AuthorizationPermission;
import org.infinispan.security.Security;
import org.infinispan.security.mappers.CommonNameRoleMapper;
import org.infinispan.server.core.security.simple.SimpleServerAuthenticationProvider;
import org.infinispan.server.hotrod.HotRodServer;
import org.infinispan.server.hotrod.configuration.HotRodServerConfigurationBuilder;
import org.infinispan.server.hotrod.test.HotRodTestingUtil;
import org.infinispan.test.SingleCacheManagerTest;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.testng.annotations.Test;
/**
* @author Adrian Brock
* @author Tristan Tarrant
* @since 5.3
*/
@Test(testName = "client.hotrod.SslAuthenticationTest", groups = "functional")
public class SslAuthenticationTest extends SingleCacheManagerTest {
private static final Log log = LogFactory.getLog(SslAuthenticationTest.class);
static final Subject ADMIN = TestingUtil.makeSubject("CN=admin");
public static final String UNAUTHORIZED = "unauthorized";
private RemoteCacheManager remoteCacheManager;
protected HotRodServer hotrodServer;
@Override
protected EmbeddedCacheManager createCacheManager() throws Exception {
GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
global
.security()
.authorization()
.enable()
.principalRoleMapper(new CommonNameRoleMapper())
.role("admin")
.permission(AuthorizationPermission.ALL)
.role("HotRodClient1")
.permission(AuthorizationPermission.READ)
.permission(AuthorizationPermission.WRITE)
.role("RodHot")
.permission(AuthorizationPermission.READ)
.permission(AuthorizationPermission.WRITE);
org.infinispan.configuration.cache.ConfigurationBuilder builder = hotRodCacheConfiguration();
builder.security().authorization().enable().role("HotRodClient1").role("admin");
cacheManager = TestCacheManagerFactory.createCacheManager(global, builder);
cacheManager.getCache();
org.infinispan.configuration.cache.ConfigurationBuilder unauthorizedBuilder = hotRodCacheConfiguration();
unauthorizedBuilder.security().authorization().enable().role("RodHot").role("admin");
cacheManager.defineConfiguration(UNAUTHORIZED, unauthorizedBuilder.build());
cacheManager.getCache(UNAUTHORIZED);
return cacheManager;
}
@Override
protected void setup() throws Exception {
Security.doAs(ADMIN, (PrivilegedExceptionAction<Object>) () -> {
cacheManager = createCacheManager();
if (cache == null) cache = cacheManager.getCache();
return null;
});
hotrodServer = new HotRodServer();
HotRodServerConfigurationBuilder serverBuilder = HotRodTestingUtil.getDefaultHotRodConfiguration();
ClassLoader cl = SslAuthenticationTest.class.getClassLoader();
SimpleServerAuthenticationProvider sap = new SimpleServerAuthenticationProvider();
serverBuilder
.ssl()
.enable()
.requireClientAuth(true)
.keyStoreFileName(cl.getResource("keystore_server.p12").getPath())
.keyStorePassword("secret".toCharArray())
.keyStoreType("pkcs12")
.keyAlias("hotrod")
.trustStoreFileName(cl.getResource("ca.p12").getPath())
.trustStorePassword("secret".toCharArray())
.trustStoreType("pkcs12");
serverBuilder
.authentication()
.enable()
.serverName("localhost")
.addAllowedMech("EXTERNAL")
.serverAuthenticationProvider(sap);
Security.doAs(ADMIN, (PrivilegedExceptionAction<Object>) () -> {
hotrodServer.start(serverBuilder.build(), cacheManager);
return null;
});
log.info("Started server on port: " + hotrodServer.getPort());
ConfigurationBuilder clientBuilder = HotRodClientTestingUtil.newRemoteConfigurationBuilder();
clientBuilder
.addServer()
.host("127.0.0.1")
.port(hotrodServer.getPort())
.socketTimeout(3000)
.connectionPool()
.maxActive(1)
.security()
.authentication()
.enable()
.saslMechanism("EXTERNAL")
.ssl()
.enable()
.keyStoreFileName(cl.getResource("keystore_client.p12").getPath())
.keyStorePassword("secret".toCharArray())
.keyStoreType("pkcs12")
.keyAlias("client1")
.trustStoreFileName(cl.getResource("ca.p12").getPath())
.trustStorePassword("secret".toCharArray())
.trustStoreType("pkcs12");
remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
}
@Override
protected void teardown() {
HotRodClientTestingUtil.killRemoteCacheManager(remoteCacheManager);
HotRodClientTestingUtil.killServers(hotrodServer);
super.teardown();
}
@Override
protected void clearContent() {
Security.doAs(ADMIN, (PrivilegedAction<Object>) () -> {
cacheManager.getCache().clear();
return null;
});
}
public void testSSLAuthentication() {
RemoteCache<String, String> cache = remoteCacheManager.getCache();
cache.put("k","v");
assertEquals("v", cache.get("k"));
}
@Test(expectedExceptions = HotRodClientException.class, expectedExceptionsMessageRegExp = ".*ISPN000287.*")
public void testSSLUnauthorized() {
RemoteCache<String, String> cache = remoteCacheManager.getCache(UNAUTHORIZED);
cache.put("k1","v1");
assertEquals("v1", cache.get("k1"));
}
}