use of java.security.Permissions in project jdk8u_jdk by JetBrains.
the class PathPermissions method init.
private synchronized void init() {
if (perms != null)
return;
perms = new Permissions();
// this is needed to be able to create the classloader itself!
perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
// add permission to read any "java.*" property
perms.add(new java.util.PropertyPermission("java.*", SecurityConstants.PROPERTY_READ_ACTION));
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
for (int i = 0; i < path.length; i++) {
File f = path[i];
String path;
try {
path = f.getCanonicalPath();
} catch (IOException ioe) {
path = f.getAbsolutePath();
}
if (i == 0) {
codeBase = Launcher.getFileURL(new File(path));
}
if (f.isDirectory()) {
if (path.endsWith(File.separator)) {
perms.add(new FilePermission(path + "-", SecurityConstants.FILE_READ_ACTION));
} else {
perms.add(new FilePermission(path + File.separator + "-", SecurityConstants.FILE_READ_ACTION));
}
} else {
int endIndex = path.lastIndexOf(File.separatorChar);
if (endIndex != -1) {
path = path.substring(0, endIndex + 1) + "-";
perms.add(new FilePermission(path, SecurityConstants.FILE_READ_ACTION));
} else {
// XXX?
}
}
}
return null;
}
});
}
use of java.security.Permissions in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method setupSecurityManager.
@BeforeMethod
public void setupSecurityManager() throws Exception {
// All permissions, a specific ACC will be used to when testing
// with a reduced permission set.
Policy.setPolicy(new Policy() {
final PermissionCollection perms = new Permissions();
{
perms.add(new java.security.AllPermission());
}
public PermissionCollection getPermissions(ProtectionDomain domain) {
return perms;
}
public PermissionCollection getPermissions(CodeSource codesource) {
return perms;
}
public boolean implies(ProtectionDomain domain, Permission perm) {
return perms.implies(perm);
}
});
System.setSecurityManager(new SecurityManager());
}
use of java.security.Permissions in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method getAccessControlContext.
private static AccessControlContext getAccessControlContext(Permission... ps) {
Permissions perms = new Permissions();
for (Permission p : ps) {
perms.add(p);
}
/*
*Create an AccessControlContext that consist a single protection domain
* with only the permissions calculated above
*/
ProtectionDomain pd = new ProtectionDomain(null, perms);
return new AccessControlContext(new ProtectionDomain[] { pd });
}
use of java.security.Permissions in project flink by apache.
the class SerializedThrowableTest method testSerialization.
@Test
public void testSerialization() {
try {
// We need an exception whose class is not in the core class loader
// we solve that by defining an exception class dynamically
// an exception class, as bytes
final byte[] classData = { -54, -2, -70, -66, 0, 0, 0, 51, 0, 21, 10, 0, 3, 0, 18, 7, 0, 19, 7, 0, 20, 1, 0, 16, 115, 101, 114, 105, 97, 108, 86, 101, 114, 115, 105, 111, 110, 85, 73, 68, 1, 0, 1, 74, 1, 0, 13, 67, 111, 110, 115, 116, 97, 110, 116, 86, 97, 108, 117, 101, 5, -103, -52, 22, -41, -23, -36, -25, 47, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 18, 76, 111, 99, 97, 108, 86, 97, 114, 105, 97, 98, 108, 101, 84, 97, 98, 108, 101, 1, 0, 4, 116, 104, 105, 115, 1, 0, 61, 76, 111, 114, 103, 47, 97, 112, 97, 99, 104, 101, 47, 102, 108, 105, 110, 107, 47, 114, 117, 110, 116, 105, 109, 101, 47, 117, 116, 105, 108, 47, 84, 101, 115, 116, 69, 120, 99, 101, 112, 116, 105, 111, 110, 70, 111, 114, 83, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110, 59, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 34, 84, 101, 115, 116, 69, 120, 99, 101, 112, 116, 105, 111, 110, 70, 111, 114, 83, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110, 46, 106, 97, 118, 97, 12, 0, 9, 0, 10, 1, 0, 59, 111, 114, 103, 47, 97, 112, 97, 99, 104, 101, 47, 102, 108, 105, 110, 107, 47, 114, 117, 110, 116, 105, 109, 101, 47, 117, 116, 105, 108, 47, 84, 101, 115, 116, 69, 120, 99, 101, 112, 116, 105, 111, 110, 70, 111, 114, 83, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110, 1, 0, 19, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 0, 33, 0, 2, 0, 3, 0, 0, 0, 1, 0, 26, 0, 4, 0, 5, 0, 1, 0, 6, 0, 0, 0, 2, 0, 7, 0, 1, 0, 1, 0, 9, 0, 10, 0, 1, 0, 11, 0, 0, 0, 47, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 2, 0, 12, 0, 0, 0, 6, 0, 1, 0, 0, 0, 21, 0, 13, 0, 0, 0, 12, 0, 1, 0, 0, 0, 5, 0, 14, 0, 15, 0, 0, 0, 1, 0, 16, 0, 0, 0, 2, 0, 17 };
// dummy class loader that has no access to any classes
ClassLoader loader = new URLClassLoader(new URL[0]);
// define a class into the classloader
Class<?> clazz = MemoryUtils.UNSAFE.defineClass("org.apache.flink.runtime.util.TestExceptionForSerialization", classData, 0, classData.length, loader, new ProtectionDomain(new CodeSource(null, (Certificate[]) null), new Permissions()));
// create an instance of the exception (no message, no cause)
Exception userException = clazz.asSubclass(Exception.class).newInstance();
// check that we cannot simply copy the exception
try {
byte[] serialized = InstantiationUtil.serializeObject(userException);
InstantiationUtil.deserializeObject(serialized, getClass().getClassLoader());
fail("should fail with a class not found exception");
} catch (ClassNotFoundException e) {
// as we want it
}
// validate that the SerializedThrowable mimics the original exception
SerializedThrowable serialized = new SerializedThrowable(userException);
assertEquals(userException.getMessage(), serialized.getMessage());
assertEquals(userException.toString(), serialized.toString());
assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(serialized));
assertArrayEquals(userException.getStackTrace(), serialized.getStackTrace());
// copy the serialized throwable and make sure everything still works
SerializedThrowable copy = CommonTestUtils.createCopySerializable(serialized);
assertEquals(userException.getMessage(), copy.getMessage());
assertEquals(userException.toString(), copy.toString());
assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(copy));
assertArrayEquals(userException.getStackTrace(), copy.getStackTrace());
// deserialize the proper exception
Throwable deserialized = copy.deserializeError(loader);
assertEquals(clazz, deserialized.getClass());
// deserialization with the wrong classloader does not lead to a failure
Throwable wronglyDeserialized = copy.deserializeError(getClass().getClassLoader());
assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(wronglyDeserialized));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.security.Permissions in project stanbol by apache.
the class UserAwarePolicy method getUserPermissionsFromSystemGraph.
/**
* Returns the permissions of the specified user according to the entries in
* the sytemGraph.
*
* @param user
* @return
* @throws java.lang.IllegalArgumentException
* @throws java.lang.SecurityException
*/
private PermissionCollection getUserPermissionsFromSystemGraph(final Principal user) throws IllegalArgumentException, SecurityException, UserUnregisteredException {
final PermissionCollection result = new Permissions();
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
logger.debug("Get permissions for user " + user.getName());
List<String> permissions = getAllPermissionsOfAUserByName(user.getName());
for (String permissionStr : permissions) {
logger.debug("Add permission {}", permissionStr);
Permission perm = permissionMap.get(permissionStr);
// <code>Permission</code> object is not in the map.
if (perm == null) {
try {
perm = PermissionParser.getPermission(permissionStr, getClass().getClassLoader());
} catch (IllegalArgumentException e) {
logger.error("parsing " + permissionStr, e);
continue;
} catch (RuntimeException e) {
logger.error("instantiating " + permissionStr, e);
continue;
}
}
result.add(perm);
}
return null;
}
});
return result;
}
Aggregations