forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EESecurityInjectionEnabledJakartaTestCase.java
143 lines (125 loc) · 6.2 KB
/
EESecurityInjectionEnabledJakartaTestCase.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
/*
* Copyright 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.test.integration.elytron.securityapi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.jboss.arquillian.container.test.api.Deployer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.test.integration.security.common.Utils;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.security.common.AbstractElytronSetupTask;
import org.wildfly.test.security.common.elytron.ConfigurableElement;
import org.wildfly.test.security.common.elytron.SimpleSecurityDomain;
import org.wildfly.test.undertow.common.UndertowApplicationSecurityDomain;
/**
* Validates that the {@code ee-security} subsystem is enabled when a deployment implements Jakarta Security (ex.
* implementing an {@link jakarta.security.enterprise.identitystore.IdentityStore IdentityStore}). Returns a custom
* principal to client via {@link jakarta.security.enterprise.SecurityContext}.
*
* @author <a href="mailto:carodrig@redhat.com">Cameron Rodriguez</a>
*/
@RunWith(Arquillian.class)
@RunAsClient
@ServerSetup({ EESecurityInjectionEnabledJakartaTestCase.ServerSetup.class })
public class EESecurityInjectionEnabledJakartaTestCase extends EESecurityInjectionEnabledAbstractTestCase {
@ArquillianResource
private Deployer deployer;
private static final String WEB_APP_NAME = "WFLY-17541-Jakarta";
@Deployment(name = WEB_APP_NAME, managed = false, testable = false)
public static WebArchive warDeployment() {
Class<EESecurityInjectionEnabledJakartaTestCase> testClass = EESecurityInjectionEnabledJakartaTestCase.class;
return ShrinkWrap.create(WebArchive.class,testClass.getSimpleName() + ".war")
.addClasses(testClass, EESecurityInjectionEnabledAbstractTestCase.class)
.addClasses(ServerSetup.class, EESecurityInjectionEnabledAbstractTestCase.ServerSetup.class,
AbstractElytronSetupTask.class)
.addClasses(TestInjectServlet.class)
.addClasses(TestAuthenticationMechanism.class, TestIdentityStoreCustomWrapper.class, TestIdentityStore.class)
.addAsWebInfResource(Utils.getJBossWebXmlAsset(TEST_APP_DOMAIN), "jboss-web.xml")
.addAsWebInfResource(testClass.getPackage(), "WFLY-17541-jakarta-web.xml", "web.xml")
.addAsWebInfResource(testClass.getPackage(), "WFLY-17541-index.xml", "index.xml")
.addAsManifestResource(new StringAsset("Dependencies: " + MODULE_NAME + "\n"), "MANIFEST.MF");
}
@Override
Header[] setRequestAuthHeader() {
return new BasicHeader[] {
new BasicHeader("X-USERNAME", "user1"),
new BasicHeader("X-PASSWORD", "password1")
};
}
@Test @InSequence(1)
public void deployApplication() {
deployer.deploy(WEB_APP_NAME);
}
@Test @InSequence(2)
public void testUnsuccessfulAuthentication(@ArquillianResource URL webAppURL) throws IOException, URISyntaxException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpGet request = new HttpGet(webAppURL.toURI() + "/inject");
HttpResponse response = httpClient.execute(request);
assertEquals(401, response.getStatusLine().getStatusCode());
// Check that the challenge message was sent
Header authHeader = response.getFirstHeader(TestAuthenticationMechanism.MESSAGE_HEADER);
assertNotNull(authHeader);
assertEquals(TestAuthenticationMechanism.MESSAGE, authHeader.getValue());
}
}
@Override
@Test @InSequence(3)
public void testCustomPrincipalWithInject(@ArquillianResource URL webAppURL) throws IOException, URISyntaxException {
super.testCustomPrincipalWithInject(webAppURL);
}
@Test @InSequence(4)
public void undeployApplication() {
deployer.undeploy(WEB_APP_NAME);
}
static class ServerSetup extends EESecurityInjectionEnabledAbstractTestCase.ServerSetup {
@Override
protected ConfigurableElement[] getConfigurableElements() {
ConfigurableElement[] elements = new ConfigurableElement[3];
// Add module with custom principal and principal transformer
elements[0] = module;
// Create security domain with default permission mapper
elements[1] = SimpleSecurityDomain.builder()
.withName(TEST_SECURITY_DOMAIN)
.withPermissionMapper(DEFAULT_PERMISSION_MAPPER)
.build();
// Add security domain to Undertow configuration
elements[2] = UndertowApplicationSecurityDomain.builder()
.withName(TEST_APP_DOMAIN)
.withSecurityDomain(TEST_SECURITY_DOMAIN)
.withIntegratedJaspi(false)
.build();
return elements;
}
}
}