Skip to content

Commit

Permalink
Add tests to validate PolicyContextHandlers are set correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhanders34 committed Dec 20, 2024
1 parent 70d1bd7 commit 13b26a8
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -177,6 +179,7 @@ protected void verifyResponseWithoutDeprecated(String response, String getCaller
mustContain(response, getCallerPrincipal);
mustContain(response, isCallerInRoleManager);
mustContain(response, isCallerInRoleEmployee);
verifyPolicyContextHandlers(response);
}

protected void verifyResponse(String response, String getCallerPrincipal, String getCallerIdentity, String isCallerInRoleManager, String isCallerInRoleEmployee) {
Expand All @@ -192,13 +195,93 @@ protected void verifyResponse(String response, String getCallerPrincipal, String
mustContain(response, getCallerIdentity);
mustContain(response, isCallerInRoleManager);
mustContain(response, isCallerInRoleEmployee);
verifyPolicyContextHandlers(response);
}
}

private static final Map<String, Set<String>> expectedHandlers = new HashMap<>();
private static final Map<String, Set<String>> notExpectedHandlers = new HashMap<>();
private static final String EE7_8 = "EE7_8";
private static final String EE9_10 = "EE9_10";
private static final String EE11 = "EE11";
static {
Set<String> ee7_8expectedHandlers = new HashSet<>();
Set<String> ee7_8notExpectedHandlers = new HashSet<>();
Set<String> ee9_10expectedHandlers = new HashSet<>();
Set<String> ee9_10notExpectedHandlers = new HashSet<>();
Set<String> ee11expectedHandlers = new HashSet<>();
Set<String> ee11notExpectedHandlers = new HashSet<>();

String commonPolicyContextHandler = "javax.security.auth.Subject.container";
String principalMapperContextHandler = "jakarta.security.jacc.PrincipalMapper";
String[] soapMessagePolicyContextHandlers = new String[] { "javax.xml.soap.SOAPMessage", "jakarta.xml.soap.SOAPMessage" };
String[] httpServletRequestPolicyContextHandlers = new String[] { "javax.servlet.http.HttpServletRequest", "jakarta.servlet.http.HttpServletRequest" };
String[] ejbPolicyContextHandlers = new String[] { "javax.ejb.EnterpriseBean", "jakarta.ejb.EnterpriseBean" };
String[] ejbArgumentsPolicyContextHandlers = new String[] { "javax.ejb.arguments", "jakarta.ejb.arguments" };
int JAVAX_INDEX = 0;
int JAKARTA_INDEX = 1;

// javax.security.auth.Subject.container is expected in all of versions
ee7_8expectedHandlers.add(commonPolicyContextHandler);
ee9_10expectedHandlers.add(commonPolicyContextHandler);
ee11expectedHandlers.add(commonPolicyContextHandler);

// jakarta.security.jacc.PrincipalMapper is only expected with EE 11
ee7_8notExpectedHandlers.add(principalMapperContextHandler);
ee9_10notExpectedHandlers.add(principalMapperContextHandler);
ee11expectedHandlers.add(principalMapperContextHandler);

// For ejb.arguments handlers, the jakarta is expected for all versions, but the javax is expected for everything except EE 11
ee7_8expectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAVAX_INDEX]);
ee9_10expectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAVAX_INDEX]);
ee11notExpectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAVAX_INDEX]);

ee7_8expectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAKARTA_INDEX]);
ee9_10expectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAKARTA_INDEX]);
ee11expectedHandlers.add(ejbArgumentsPolicyContextHandlers[JAKARTA_INDEX]);

// For all other handlers, the jakarta is expected for all versions and the javax one is only expected for EE 7_8
String[][] remainingContextHandlers = { soapMessagePolicyContextHandlers, httpServletRequestPolicyContextHandlers, ejbPolicyContextHandlers };
for (String[] handlers : remainingContextHandlers) {
ee7_8expectedHandlers.add(handlers[JAVAX_INDEX]);
ee9_10notExpectedHandlers.add(handlers[JAVAX_INDEX]);
ee11notExpectedHandlers.add(handlers[JAVAX_INDEX]);

ee7_8expectedHandlers.add(handlers[JAKARTA_INDEX]);
ee9_10expectedHandlers.add(handlers[JAKARTA_INDEX]);
ee11expectedHandlers.add(handlers[JAKARTA_INDEX]);
}

expectedHandlers.put(EE7_8, ee7_8expectedHandlers);
expectedHandlers.put(EE9_10, ee9_10expectedHandlers);
expectedHandlers.put(EE11, ee11expectedHandlers);

notExpectedHandlers.put(EE7_8, ee7_8notExpectedHandlers);
notExpectedHandlers.put(EE9_10, ee9_10notExpectedHandlers);
notExpectedHandlers.put(EE11, ee11notExpectedHandlers);
}

private void verifyPolicyContextHandlers(String response) {
String key = JakartaEEAction.isEE11OrLaterActive() ? EE11 : JakartaEEAction.isEE9OrLaterActive() ? EE9_10 : EE7_8;
Set<String> expected = expectedHandlers.get(key);
Set<String> notExpected = notExpectedHandlers.get(key);

for (String exp : expected) {
mustContain(response, "handlerKey(" + exp + ")=true");
}
for (String notExp : notExpected) {
mustNotContain(response, "handlerKey(" + notExp + ")=true");
}
}

private void mustContain(String response, String target) {
assertTrue(target + " not found in response", response.contains(target));
}

private void mustNotContain(String response, String target) {
assertTrue(target + " found in response", !response.contains(target));
}

protected void verifyResponse(String response, String getCallerPrincipal, String getCallerIdentity, String isCallerInRoleManager, String isCallerInRoleEmployee,
String isDeclaredRole) {
verifyResponse(response, getCallerPrincipal, getCallerIdentity, isCallerInRoleManager, isCallerInRoleEmployee);
Expand All @@ -223,4 +306,4 @@ protected void verifyExceptionWithUserAndRole(String response, String exMsg, Str
assertTrue("Failed to find user name " + user + " in authorization failed message", response.contains(user));
assertTrue("Failed to find method " + method + " in authorization failed message for user not granted access ", response.contains(method));
}
}
}
3 changes: 2 additions & 1 deletion dev/com.ibm.ws.ejbcontainer.security_test.servlets/bnd.bnd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#*******************************************************************************
# Copyright (c) 2020, 2022 IBM Corporation and others.
# Copyright (c) 2020, 2024 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
Expand All @@ -24,6 +24,7 @@ test.project: true
com.ibm.websphere.javaee.ejb.3.1;version=latest,\
com.ibm.websphere.javaee.annotation.1.1;version=latest,\
com.ibm.websphere.javaee.servlet.3.1;version=latest,\
com.ibm.websphere.javaee.jacc.1.5;version=latest,\
com.ibm.websphere.security;version=latest,\
com.ibm.ws.security.jaas.common;version=latest

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2012, 2020 IBM Corporation and others.
* Copyright (c) 2012, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand All @@ -14,9 +14,11 @@
package com.ibm.ws.ejbcontainer.security.test;

import java.security.Principal;
import java.util.Set;
import java.util.logging.Logger;

import javax.ejb.SessionContext;
import javax.security.jacc.PolicyContext;

/**
*
Expand Down Expand Up @@ -65,6 +67,12 @@ protected String authenticate(String method, SessionContext context, Logger logg
result.append(" isCallerInRole(**)=");
result.append(context.isCallerInRole("**"));
result.append("\n");
Set<String> handlerKeys = PolicyContext.getHandlerKeys();
for (String key : handlerKeys) {
result.append("handlerKey(");
result.append(key);
result.append(")=true\n");
}
logger.info("result: " + result);
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2020 IBM Corporation and others.
* Copyright (c) 2020, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand All @@ -14,6 +14,7 @@
package com.ibm.ws.ejbcontainer.security.test;

import java.security.Principal;
import java.util.Set;
import java.util.logging.Logger;

import javax.annotation.Resource;
Expand All @@ -23,6 +24,7 @@
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.ejb.StatefulTimeout;
import javax.security.jacc.PolicyContext;

/**
* Bean implementation class for Stateful Enterprise Bean to be used in
Expand Down Expand Up @@ -193,6 +195,12 @@ protected String authenticate(String method) {
result.append(" isCallerInRole(Emp)=");
result.append(isEmp);
result.append("\n");
Set<String> handlerKeys = PolicyContext.getHandlerKeys();
for (String key : handlerKeys) {
result.append("handlerKey(");
result.append(key);
result.append(")=true\n");
}
logger.info("result: " + result);
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -90,6 +90,8 @@ public static void setUp() throws Exception {

client = new BasicAuthClient(server);
mySSLClient = new SSLBasicAuthClient(server);
client.setJaccValidation(true);
mySSLClient.setJaccValidation(true);

assertNotNull("The application did not report is was started",
server.waitForStringInLog("CWWKZ0001I"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -59,6 +59,8 @@ public static void setUp() throws Exception {

myClient = new BasicAuthClient(myServer, BasicAuthClient.DEFAULT_REALM, BasicAuthClient.DEFAULT_JSP_NAME, BasicAuthClient.DEFAULT_JSP_CONTEXT_ROOT);
mySSLClient = new SSLBasicAuthClient(myServer, BasicAuthClient.DEFAULT_REALM, BasicAuthClient.DEFAULT_JSP_NAME, BasicAuthClient.DEFAULT_JSP_CONTEXT_ROOT);
myClient.setJaccValidation(true);
mySSLClient.setJaccValidation(true);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -68,6 +68,8 @@ public static LibertyServer serverSetUp(ServerMode mode) throws Exception {
JACCFatUtils.transformApps(myServer, "basicauth.war", "basicauthXMI.ear", "basicauthXMInoAuthz.ear", "basicauthXML.ear", "basicauthXMLnoAuthz.ear");
myClient = new BasicAuthClient(myServer);
mySSLClient = new SSLBasicAuthClient(myServer);
myClient.setJaccValidation(true);
mySSLClient.setJaccValidation(true);
return myServer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -60,6 +60,8 @@ public static void setUp() throws Exception {

myClient = new BasicAuthClient(myServer);
mySSLClient = new SSLBasicAuthClient(myServer);
myClient.setJaccValidation(true);
mySSLClient.setJaccValidation(true);
}

public BasicAuthWithCustomRegistryTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -838,6 +838,7 @@ private static ClientCertAuthClient setupClient(String certFile, boolean secure)
} else {
client = new ClientCertAuthClient(myServer.getHostname(), myServer.getHttpDefaultSecurePort(), false, myServer, CLIENT_CERT_SERVLET, "/clientcert", null, null);
}
client.setJaccValidation(true);
return client;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -187,6 +187,8 @@ public static void setUp() throws Exception {

client = new FormLoginClient(server);
sslClient = new SSLFormLoginClient(server);
client.setJaccValidation(true);
sslClient.setJaccValidation(true);

String methodName = "setUp";
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2011, 2022 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -121,6 +121,9 @@ public static void setUp() throws Exception {
basicAuthClient = new BasicAuthClient(server, BasicAuthClient.DEFAULT_REALM, DYNAMIC_ANNOTATIONS_PURE_SERVLET, DYNAMIC_ANNOTATIONS_PURE_CONTEXT_ROOT);
secureBasicAuthClient = new SSLBasicAuthClient(server, BasicAuthClient.DEFAULT_REALM, DYNAMIC_ANNOTATIONS_PURE_SERVLET, DYNAMIC_ANNOTATIONS_PURE_CONTEXT_ROOT);
conflictBasicAuthClient = new BasicAuthClient(server, BasicAuthClient.DEFAULT_REALM, DYNAMIC_ANNOTATIONS_CONFLICT_SERVLET, DYNAMIC_ANNOTATIONS_CONFLICT_CONTEXT_ROOT);
basicAuthClient.setJaccValidation(true);
secureBasicAuthClient.setJaccValidation(true);
conflictBasicAuthClient.setJaccValidation(true);
}

protected static void verifyServerStartedWithJaccFeature(LibertyServer server) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2014, 2020 IBM Corporation and others.
* Copyright (c) 2014, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -74,6 +74,8 @@ public static void setUp() throws Exception {

myClient = new FormLoginJSPClient(myServer, FormLoginClient.DEFAULT_JSP_NAME, FormLoginClient.DEFAULT_JSP_CONTEXT_ROOT);
mySSLClient = new SSLFormLoginClient(myServer, SSLFormLoginClient.DEFAULT_JSP_NAME, SSLFormLoginClient.DEFAULT_JSP_CONTEXT_ROOT);
myClient.setJaccValidation(true);
mySSLClient.setJaccValidation(true);
}

public FormLoginJSPTest() {
Expand Down
Loading

0 comments on commit 13b26a8

Please sign in to comment.