use of java.security.Permissions in project joda-time by JodaOrg.
the class TestDateTimeZone method testZoneInfoProviderResourceLoading.
public void testZoneInfoProviderResourceLoading() {
final Set<String> ids = new HashSet<String>(DateTimeZone.getAvailableIDs());
ids.remove(DateTimeZone.getDefault().getID());
final String id = ids.toArray(new String[ids.size()])[new Random().nextInt(ids.size())];
try {
Policy.setPolicy(new Policy() {
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
Permissions p = new Permissions();
// enable everything
p.add(new AllPermission());
return p;
}
@Override
public void refresh() {
}
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return !(permission instanceof FilePermission) && !permission.getName().contains(id);
}
});
System.setSecurityManager(new SecurityManager());
// will throw IllegalArgumentException if the resource can
// not be loaded
final DateTimeZone zone = DateTimeZone.forID(id);
assertNotNull(zone);
} finally {
System.setSecurityManager(null);
Policy.setPolicy(ALLOW);
}
}
use of java.security.Permissions in project lucene-solr by apache.
the class LuceneTestCase method runWithRestrictedPermissions.
/**
* Runs a code part with restricted permissions (be sure to add all required permissions,
* because it would start with empty permissions). You cannot grant more permissions than
* our policy file allows, but you may restrict writing to several dirs...
* <p><em>Note:</em> This assumes a {@link SecurityManager} enabled, otherwise it
* stops test execution. If enabled, it needs the following {@link SecurityPermission}:
* {@code "createAccessControlContext"}
*/
public static <T> T runWithRestrictedPermissions(PrivilegedExceptionAction<T> action, Permission... permissions) throws Exception {
assumeTrue("runWithRestrictedPermissions requires a SecurityManager enabled", System.getSecurityManager() != null);
// be sure to have required permission, otherwise doPrivileged runs with *no* permissions:
AccessController.checkPermission(new SecurityPermission("createAccessControlContext"));
final PermissionCollection perms = new Permissions();
Arrays.stream(permissions).forEach(perms::add);
final AccessControlContext ctx = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
try {
return AccessController.doPrivileged(action, ctx);
} catch (PrivilegedActionException e) {
throw e.getException();
}
}
use of java.security.Permissions in project wildfly by wildfly.
the class SecurityHelper method getSecurityContextForJNDILookup.
private static AccessControlContext getSecurityContextForJNDILookup(Collection<JndiPermission> jndiPermissions) {
CodeSource src = new CodeSource(null, (Certificate[]) null);
Permissions perms = new Permissions();
for (JndiPermission p : jndiPermissions) {
perms.add(p);
}
ProtectionDomain domain = new ProtectionDomain(src, perms);
AccessControlContext ctx = new AccessControlContext(new ProtectionDomain[] { domain });
return ctx;
}
use of java.security.Permissions in project jdk8u_jdk by JetBrains.
the class MBeanInstantiator method getClassLoader.
private ClassLoader getClassLoader(final ObjectName name) {
if (clr == null) {
return null;
}
// Restrict to getClassLoader permission only
Permissions permissions = new Permissions();
permissions.add(new MBeanPermission("*", null, name, "getClassLoader"));
ProtectionDomain protectionDomain = new ProtectionDomain(null, permissions);
ProtectionDomain[] domains = { protectionDomain };
AccessControlContext ctx = new AccessControlContext(domains);
ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return clr.getClassLoader(name);
}
}, ctx);
return loader;
}
use of java.security.Permissions in project jdk8u_jdk by JetBrains.
the class RMIConnectionImpl method withPermissions.
private static AccessControlContext withPermissions(Permission... perms) {
Permissions col = new Permissions();
for (Permission thePerm : perms) {
col.add(thePerm);
}
final ProtectionDomain pd = new ProtectionDomain(null, col);
return new AccessControlContext(new ProtectionDomain[] { pd });
}
Aggregations