forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomAuthenticationWebFailoverTestCase.java
216 lines (181 loc) · 11.3 KB
/
CustomAuthenticationWebFailoverTestCase.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* JBoss, Home of Professional Open Source.
* Copyright 2022, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.clustering.cluster.web.authentication;
import static org.jboss.as.test.shared.PermissionUtils.createPermissionsXmlAsset;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.test.clustering.cluster.AbstractClusteringTestCase;
import org.jboss.as.test.clustering.cluster.web.authentication.custom.CustomAuthenticationMechanism;
import org.jboss.as.test.clustering.cluster.web.authentication.custom.LogoutServlet;
import org.jboss.as.test.clustering.cluster.web.authentication.custom.SecuredServlet;
import org.jboss.as.test.clustering.single.web.SimpleServlet;
import org.jboss.as.test.http.util.TestHttpClientUtils;
import org.jboss.as.test.shared.ManagementServerSetupTask;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.security.permission.ElytronPermission;
import org.wildfly.test.security.common.elytron.ServletElytronDomainSetup;
/**
* Validates that a user remains authenticated following failover when using FORM authentication.
* @author Paul Ferraro
*/
@RunWith(Arquillian.class)
@ServerSetup({ CustomAuthenticationWebFailoverTestCase.ElytronDomainSetupOverride.class, CustomAuthenticationWebFailoverTestCase.ServletElytronDomainSetupOverride.class, CustomAuthenticationWebFailoverTestCase.ServerSetup.class })
public class CustomAuthenticationWebFailoverTestCase extends AbstractClusteringTestCase {
private static final String MODULE_NAME = CustomAuthenticationWebFailoverTestCase.class.getSimpleName();
private static final String SECURITY_DOMAIN_NAME = "authentication";
@Deployment(name = DEPLOYMENT_1, managed = false, testable = false)
@TargetsContainer(NODE_1)
public static Archive<?> deployment0() {
return getDeployment();
}
@Deployment(name = DEPLOYMENT_2, managed = false, testable = false)
@TargetsContainer(NODE_2)
public static Archive<?> deployment1() {
return getDeployment();
}
private static Archive<?> getDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class, MODULE_NAME + ".war");
war.addPackage(SecuredServlet.class.getPackage());
war.setWebXML(SimpleServlet.class.getPackage(), "web.xml");
war.addAsWebInfResource("beans.xml", "beans.xml");
war.addAsWebInfResource(CustomAuthenticationWebFailoverTestCase.class.getPackage(), "jboss-web.xml", "jboss-web.xml");
war.addAsWebInfResource(CustomAuthenticationWebFailoverTestCase.class.getPackage(), "distributable-web.xml", "distributable-web.xml");
war.addAsManifestResource(createPermissionsXmlAsset(new ElytronPermission("getSecurityDomain"), new ElytronPermission("authenticate")), "permissions.xml");
return war;
}
@Test
public void test(
@ArquillianResource(SecuredServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1,
@ArquillianResource(SecuredServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2)
throws IOException, URISyntaxException {
URI uri1 = SecuredServlet.createURI(baseURL1);
URI uri2 = SecuredServlet.createURI(baseURL2);
try (CloseableHttpClient client = TestHttpClientUtils.promiscuousCookieHttpClient()) {
try (CloseableHttpResponse response = client.execute(new HttpGet(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertEquals(CustomAuthenticationMechanism.CREDENTIAL_REQUIRED_MESSAGE, response.getFirstHeader(CustomAuthenticationMechanism.MESSAGE_HEADER).getValue());
}
try (CloseableHttpResponse response = client.execute(new HttpGet(uri2))) {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertEquals(CustomAuthenticationMechanism.CREDENTIAL_REQUIRED_MESSAGE, response.getFirstHeader(CustomAuthenticationMechanism.MESSAGE_HEADER).getValue());
}
HttpUriRequest request = new HttpGet(uri1);
// Validate login with incorrect credentials
request.setHeader(CustomAuthenticationMechanism.USERNAME_HEADER, "allowed");
request.setHeader(CustomAuthenticationMechanism.PASSWORD_HEADER, "wrong");
try (CloseableHttpResponse response = client.execute(request)) {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertEquals(CustomAuthenticationMechanism.INVALID_CREDENTIAL_MESSAGE, response.getFirstHeader(CustomAuthenticationMechanism.MESSAGE_HEADER).getValue());
}
// Login as unauthorized user
request.setHeader(CustomAuthenticationMechanism.USERNAME_HEADER, "forbidden");
request.setHeader(CustomAuthenticationMechanism.PASSWORD_HEADER, "password");
try (CloseableHttpResponse response = client.execute(request)) {
Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
}
// Logout so we can login again as an authorized user
try (CloseableHttpResponse response = client.execute(new HttpGet(LogoutServlet.createURI(baseURL1)))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
}
// Validate login with correct credentials
request.setHeader(CustomAuthenticationMechanism.USERNAME_HEADER, "allowed");
request.setHeader(CustomAuthenticationMechanism.PASSWORD_HEADER, "password");
try (CloseableHttpResponse response = client.execute(request)) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER));
Assert.assertEquals("allowed", response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER).getValue());
}
// Validate that subsequent request is already authenticated
try (CloseableHttpResponse response = client.execute(new HttpGet(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER));
Assert.assertEquals("allowed", response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER).getValue());
}
undeploy(DEPLOYMENT_1);
// Validate that failover request is auto-authenticated
try (CloseableHttpResponse response = client.execute(new HttpGet(uri2))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER));
Assert.assertEquals("allowed", response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER).getValue());
}
deploy(DEPLOYMENT_1);
// Validate that failback request is auto-authenticated
try (CloseableHttpResponse response = client.execute(new HttpGet(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER));
Assert.assertEquals("allowed", response.getFirstHeader(SecuredServlet.PRINCIPAL_HEADER).getValue());
}
// Logout
try (CloseableHttpResponse response = client.execute(new HttpGet(LogoutServlet.createURI(baseURL1)))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
}
// Verify that we are no longer authenticated on either server
try (CloseableHttpResponse response = client.execute(new HttpGet(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertEquals(CustomAuthenticationMechanism.CREDENTIAL_REQUIRED_MESSAGE, response.getFirstHeader(CustomAuthenticationMechanism.MESSAGE_HEADER).getValue());
}
try (CloseableHttpResponse response = client.execute(new HttpGet(uri2))) {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertEquals(CustomAuthenticationMechanism.CREDENTIAL_REQUIRED_MESSAGE, response.getFirstHeader(CustomAuthenticationMechanism.MESSAGE_HEADER).getValue());
}
}
}
static class ElytronDomainSetupOverride extends ElytronDomainServerSetupTask {
public ElytronDomainSetupOverride() {
super(SECURITY_DOMAIN_NAME);
}
}
static class ServletElytronDomainSetupOverride extends ServletElytronDomainSetup {
protected ServletElytronDomainSetupOverride() {
super(SECURITY_DOMAIN_NAME, false);
}
}
static class ServerSetup extends ManagementServerSetupTask {
ServerSetup() {
super(NODE_1_2, createContainerConfigurationBuilder()
.setupScript(createScriptBuilder()
.startBatch()
.add("/subsystem=undertow/application-security-domain=%s:write-attribute(name=integrated-jaspi, value=false)", SECURITY_DOMAIN_NAME)
.endBatch()
.build())
.build());
}
}
}