use of java.security.Permission in project wildfly by wildfly.
the class SecurityHelper method runWithSecurityManager.
public static <T> T runWithSecurityManager(final Callable<T> action, final AccessControlContext securityContext) throws Exception {
Policy previousPolicy = Policy.getPolicy();
SecurityManager previousSM = System.getSecurityManager();
// let's be a bit brutal here and just allow any code do anything by default for the time this method executes.
Policy.setPolicy(new Policy() {
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return true;
}
});
// with our new totally unsecure policy, let's install a new security manager
System.setSecurityManager(new SecurityManager());
try {
// run the code to test with limited privs defined by the securityContext
return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
return action.call();
}
}, securityContext);
} catch (PrivilegedActionException e) {
throw e.getException();
} finally {
// and reset back the previous security settings
System.setSecurityManager(previousSM);
Policy.setPolicy(previousPolicy);
}
}
use of java.security.Permission in project wildfly by wildfly.
the class JpaDsRestartTestCase method deploy.
@Deployment
public static Archive<?> deploy() {
WebArchive war = ShrinkWrap.create(WebArchive.class, "dsrestartjpa.war");
war.addPackage(JpaInjectedSfsb.class.getPackage());
// WEB-INF/classes is implied
war.addAsResource(JpaDsRestartTestCase.class.getPackage(), "persistence.xml", "META-INF/persistence.xml");
war.addAsManifestResource(JpaDsRestartTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF");
final Permission[] permissions = new Permission[] { ControllerPermission.CAN_ACCESS_MODEL_CONTROLLER, ControllerPermission.CAN_ACCESS_IMMUTABLE_MANAGEMENT_RESOURCE_REGISTRATION };
war.addAsManifestResource(PermissionUtils.createPermissionsXmlAsset(permissions), "permissions.xml");
return war;
}
use of java.security.Permission in project wildfly by wildfly.
the class JndiPermissionTestCase method testCollection.
@Test
public void testCollection() {
final PermissionCollection permissionCollection = new JndiPermission("", "").newPermissionCollection();
Enumeration<Permission> e;
permissionCollection.add(new JndiPermission("foo/bar", "lookup,bind"));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind")));
assertFalse(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind,unbind")));
assertFalse(permissionCollection.implies(new JndiPermission("foo/bar", "unbind")));
assertNotNull(e = permissionCollection.elements());
assertTrue(e.hasMoreElements());
assertEquals(new JndiPermission("foo/bar", "lookup,bind"), e.nextElement());
assertFalse(e.hasMoreElements());
permissionCollection.add(new JndiPermission("foo/bar", "unbind"));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind,unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "unbind")));
assertNotNull(e = permissionCollection.elements());
assertTrue(e.hasMoreElements());
assertEquals(new JndiPermission("foo/bar", "lookup,bind,unbind"), e.nextElement());
assertFalse(e.hasMoreElements());
permissionCollection.add(new JndiPermission("-", "lookup"));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind,unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("baz/zap", "lookup")));
assertTrue(permissionCollection.implies(new JndiPermission("", "lookup")));
assertFalse(permissionCollection.implies(new JndiPermission("baz/zap", "lookup,bind,unbind")));
assertFalse(permissionCollection.implies(new JndiPermission("baz/zap", "unbind")));
assertNotNull(e = permissionCollection.elements());
assertTrue(e.hasMoreElements());
assertEquals(new JndiPermission("foo/bar", "lookup,bind,unbind"), e.nextElement());
assertTrue(e.hasMoreElements());
assertEquals(new JndiPermission("-", "lookup"), e.nextElement());
assertFalse(e.hasMoreElements());
permissionCollection.add(new JndiPermission("-", "bind,unbind"));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "lookup,bind,unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("foo/bar", "unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("baz/zap", "lookup")));
assertTrue(permissionCollection.implies(new JndiPermission("", "lookup")));
assertTrue(permissionCollection.implies(new JndiPermission("baz/zap", "lookup,bind,unbind")));
assertTrue(permissionCollection.implies(new JndiPermission("baz/zap", "unbind")));
assertNotNull(e = permissionCollection.elements());
assertTrue(e.hasMoreElements());
assertEquals(new JndiPermission("-", "lookup,bind,unbind"), e.nextElement());
assertFalse(e.hasMoreElements());
}
use of java.security.Permission in project wildfly by wildfly.
the class LimitedModulePermissionsTestCase method testReadJavaHome.
/**
* Test which reads system property without Permission.
*/
@Test
public void testReadJavaHome() {
try {
CheckJSMUtils.getSystemProperty("java.home");
fail("Access should be denied");
} catch (AccessControlException e) {
Permission expectedPerm = new PropertyPermission("java.home", "read");
assertEquals("Permission type doesn't match", expectedPerm, e.getPermission());
}
}
use of java.security.Permission in project wildfly by wildfly.
the class CheckIdentityPermissionServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
SecurityIdentity si = null;
final String user = req.getParameter(PARAM_USER);
if (user != null) {
final String password = req.getParameter(PARAM_PASSWORD);
try {
si = SecurityDomain.getCurrent().authenticate(user, new PasswordGuessEvidence(password.toCharArray()));
} catch (Exception e) {
e.printStackTrace();
resp.sendError(SC_FORBIDDEN, e.getMessage());
return;
}
} else {
si = SecurityDomain.getCurrent().getCurrentSecurityIdentity();
}
String className = req.getParameter(PARAM_CLASS);
if (className == null) {
resp.sendError(SC_BAD_REQUEST, "Parameter class has to be provided");
return;
}
String target = req.getParameter(PARAM_TARGET);
String action = req.getParameter(PARAM_ACTION);
Permission perm = null;
try {
if (target == null) {
perm = (Permission) Class.forName(className).newInstance();
} else if (action == null) {
perm = (Permission) Class.forName(className).getConstructor(String.class).newInstance(target);
} else {
perm = (Permission) Class.forName(className).getConstructor(String.class, String.class).newInstance(target, action);
}
} catch (Exception e) {
throw new ServletException("Unable to create permission instance", e);
}
final PrintWriter writer = resp.getWriter();
writer.print(si.implies(perm));
writer.close();
}
Aggregations