Search in sources :

Example 1 with SystemPermission

use of org.apache.derby.shared.common.security.SystemPermission in project derby by apache.

the class InternalDriver method checkShutdownPrivileges.

/**
 * Checks for shutdown System Privileges.
 *
 * To perform this check the following policy grant is required
 * <ul>
 * <li> to run the encapsulated test:
 *      permission javax.security.auth.AuthPermission "doAsPrivileged";
 * </ul>
 * or a SQLException will be raised detailing the cause.
 * <p>
 * In addition, for the test to succeed
 * <ul>
 * <li> the given user needs to be covered by a grant:
 *      principal org.apache.derby.authentication.SystemPrincipal "..." {}
 * <li> that lists a shutdown permission:
 *      permission org.apache.derby.shared.common.security.SystemPermission "shutdown";
 * </ul>
 * or it will fail with a SQLException detailing the cause.
 *
 * @param user The user to be checked for shutdown privileges
 * @throws SQLException if the privileges check fails
 */
private void checkShutdownPrivileges(String user) throws SQLException {
    // approve action if not running under a security manager
    if (System.getSecurityManager() == null) {
        return;
    }
    // the check
    try {
        final Permission sp = new SystemPermission(SystemPermission.ENGINE, SystemPermission.SHUTDOWN);
        checkSystemPrivileges(user, sp);
    } catch (AccessControlException ace) {
        throw Util.generateCsSQLException(SQLState.AUTH_SHUTDOWN_MISSING_PERMISSION, user, // overloaded method
        (Object) ace);
    } catch (Exception e) {
        throw Util.generateCsSQLException(SQLState.AUTH_SHUTDOWN_MISSING_PERMISSION, user, // overloaded method
        (Object) e);
    }
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) SystemPermission(org.apache.derby.shared.common.security.SystemPermission) Permission(java.security.Permission) AccessControlException(java.security.AccessControlException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) TimeoutException(java.util.concurrent.TimeoutException) AccessControlException(java.security.AccessControlException) SQLException(java.sql.SQLException) StandardException(org.apache.derby.shared.common.error.StandardException) PrivilegedActionException(java.security.PrivilegedActionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with SystemPermission

use of org.apache.derby.shared.common.security.SystemPermission in project derby by apache.

the class SystemPrivilegesPermissionTest method testSystemPermissionCollections.

/**
 * Test that collections of SystemPermissions behave as expected.
 * Before DERBY-6717, adding multiple single-action permissions with
 * the same name didn't work.
 */
public void testSystemPermissionCollections() {
    Permissions allPerms = new Permissions();
    for (String name : VALID_SYSPERM_NAMES) {
        for (String action : VALID_SYSPERM_ACTIONS) {
            allPerms.add(new SystemPermission(name, action));
        }
    }
    assertEquals(VALID_SYSPERM_NAMES.length, Collections.list(allPerms.elements()).size());
    // all system permissions.
    for (String name : VALID_SYSPERM_NAMES) {
        for (String a1 : VALID_SYSPERM_ACTIONS) {
            // allPerms should imply any valid (name, action) pair.
            assertTrue(allPerms.implies(new SystemPermission(name, a1)));
            // system permission.
            for (String a2 : VALID_SYSPERM_ACTIONS) {
                assertTrue(allPerms.implies(new SystemPermission(name, a1 + ',' + a2)));
            }
        }
    }
    Permissions onePerm = new Permissions();
    onePerm.add(new SystemPermission("server", "shutdown"));
    // onePerm implies server shutdown and nothing else
    assertTrue(onePerm.implies(new SystemPermission("server", "shutdown")));
    assertFalse(onePerm.implies(new SystemPermission("engine", "shutdown")));
    assertFalse(onePerm.implies(new SystemPermission("server", "shutdown,monitor")));
    Permissions somePerms = new Permissions();
    somePerms.add(new SystemPermission("server", "shutdown"));
    somePerms.add(new SystemPermission("jmx", "shutdown,monitor"));
    somePerms.add(new SystemPermission("engine", "shutdown,control"));
    somePerms.add(new SystemPermission("engine", "control,monitor"));
    // somePerms implies the shutdown action for server
    assertTrue(somePerms.implies(new SystemPermission("server", "shutdown")));
    assertFalse(somePerms.implies(new SystemPermission("server", "control")));
    assertFalse(somePerms.implies(new SystemPermission("server", "monitor")));
    assertFalse(somePerms.implies(new SystemPermission("server", "shutdown,monitor")));
    // somePerms implies the shutdown and monitor actions for jmx
    assertTrue(somePerms.implies(new SystemPermission("jmx", "shutdown")));
    assertTrue(somePerms.implies(new SystemPermission("jmx", "monitor")));
    assertFalse(somePerms.implies(new SystemPermission("jmx", "control")));
    assertTrue(somePerms.implies(new SystemPermission("jmx", "shutdown,monitor")));
    assertTrue(somePerms.implies(new SystemPermission("jmx", "monitor,shutdown")));
    assertFalse(somePerms.implies(new SystemPermission("jmx", "monitor,shutdown,control")));
    // somePerms implies shutdown, control and monitor for engine
    assertTrue(somePerms.implies(new SystemPermission("engine", "shutdown")));
    assertTrue(somePerms.implies(new SystemPermission("engine", "control")));
    assertTrue(somePerms.implies(new SystemPermission("engine", "monitor")));
    assertTrue(somePerms.implies(new SystemPermission("engine", "shutdown,monitor")));
    assertTrue(somePerms.implies(new SystemPermission("engine", "shutdown,monitor,control")));
    // A SystemPermission collection should not accept other permissions.
    SystemPermission sp = new SystemPermission("engine", "monitor");
    PermissionCollection collection = sp.newPermissionCollection();
    try {
        collection.add(new AllPermission());
        fail();
    } catch (IllegalArgumentException iae) {
    // expected
    }
    // Read-only collections cannot be added to.
    collection.setReadOnly();
    try {
        collection.add(sp);
        fail();
    } catch (SecurityException se) {
    // expected
    }
    // The collection does not imply other permission types.
    assertFalse(collection.implies(new AllPermission()));
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) PermissionCollection(java.security.PermissionCollection) Permissions(java.security.Permissions) AllPermission(java.security.AllPermission)

Example 3 with SystemPermission

use of org.apache.derby.shared.common.security.SystemPermission in project derby by apache.

the class CacheManagerMBeanTest method withoutPermsTest.

/**
 * Test that the CacheManagerMBean cannot be accessed if the code
 * base lacks SystemPermission("engine", "monitor").
 */
public void withoutPermsTest() throws Exception {
    // boot the database
    getConnection();
    Set<ObjectName> names = queryMBeans(createObjectName("StatementCache", null));
    assertEquals("Should have a single statement cache", 1, names.size());
    ObjectName name = names.iterator().next();
    // This is the permission required to access the MBean, but we don't
    // have it.
    SystemPermission monitorPerm = new SystemPermission("engine", "monitor");
    // Reading attributes should cause security exception.
    for (String attrName : ALL_ATTRIBUTES) {
        try {
            getAttribute(name, attrName);
            fail();
        } catch (RuntimeMBeanException e) {
            vetException(e, monitorPerm);
        }
    }
    // Modifying attributes should also cause security exception.
    try {
        setAttribute(name, "CollectAccessCounts", Boolean.FALSE);
        fail();
    } catch (RuntimeMBeanException e) {
        vetException(e, monitorPerm);
    }
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) RuntimeMBeanException(javax.management.RuntimeMBeanException) ObjectName(javax.management.ObjectName)

Example 4 with SystemPermission

use of org.apache.derby.shared.common.security.SystemPermission in project derby by apache.

the class SystemPrivilegesPermissionTest method testSystemPermission.

/**
 * Tests SystemPermission.
 */
public void testSystemPermission() {
    // test SystemPermission with null name argument
    try {
        new SystemPermission(null, null);
        fail("expected NullPointerException");
    } catch (NullPointerException ex) {
    // expected exception
    }
    // test SystemPermission with empty name argument
    try {
        new SystemPermission("", null);
        fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    // expected exception
    }
    // test SystemPermission with illegal name argument
    try {
        new SystemPermission("illegal_name", null);
        fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    // expected exception
    }
    // actions cannot be null
    try {
        new SystemPermission("server", null);
        fail("expected NullPointerException");
    } catch (NullPointerException ex) {
    // expected exception
    }
    // Illegal and duplicate actions are ignored.
    assertEquals("", new SystemPermission("server", "").getActions());
    assertEquals("", new SystemPermission("server", ",,").getActions());
    assertEquals("", new SystemPermission("server", "illegal_action").getActions());
    assertEquals("control", new SystemPermission("server", "control,").getActions());
    assertEquals("control", new SystemPermission("server", "control,illegal_action").getActions());
    assertEquals("control", new SystemPermission("server", "control,control").getActions());
    assertEquals("control,monitor", new SystemPermission("server", "control, monitor, control").getActions());
    assertEquals("control,monitor", new SystemPermission("server", "monitor, control, monitor").getActions());
    assertEquals("control", new SystemPermission("server", "CoNtRoL").getActions());
    assertEquals("control", new SystemPermission("server", "CoNtRoL,control").getActions());
    String[] validNames = { SystemPermission.ENGINE, SystemPermission.JMX, SystemPermission.SERVER };
    // In order of the canonical actions expected
    String[] validActions = { SystemPermission.CONTROL, SystemPermission.MONITOR, SystemPermission.SHUTDOWN };
    // Check all valid combinations (which is all) with
    // a single action
    Permission[] all = new Permission[validNames.length * validActions.length];
    int c = 0;
    for (int tn = 0; tn < validNames.length; tn++) {
        for (int a = 0; a < validActions.length; a++) {
            Permission p = new SystemPermission(validNames[tn], validActions[a]);
            assertEquals(validNames[tn], p.getName());
            assertEquals(validActions[a], p.getActions());
            // test SystemPermission.equals()
            assertFalse(p.equals(null));
            assertFalse(p.equals(new Object()));
            this.assertEquivalentPermissions(p, p);
            all[c++] = p;
        }
    }
    // All the permissions are different.
    checkDistinctPermissions(all);
    // Check two actions
    for (int n = 0; n < validNames.length; n++) {
        for (int a = 0; a < validActions.length; a++) {
            Permission base = new SystemPermission(validNames[n], validActions[a]);
            // Two actions
            for (int oa = 0; oa < validActions.length; oa++) {
                Permission p = new SystemPermission(validNames[n], validActions[a] + "," + validActions[oa]);
                if (oa == a) {
                    // Same action added twice
                    assertEquivalentPermissions(base, p);
                    // Canonical form should collapse into a single action
                    assertEquals(validActions[a], p.getActions());
                } else {
                    // Implies logic, the one with one permission
                    // is implied by the other but not vice-versa.
                    assertTrue(p.implies(base));
                    assertFalse(base.implies(p));
                    // Names in canonical form
                    int f;
                    int s;
                    if (oa < a) {
                        f = oa;
                        s = a;
                    } else {
                        f = a;
                        s = oa;
                    }
                    assertEquals(validActions[f] + "," + validActions[s], p.getActions());
                }
            }
        }
    }
    // DERBY-3476: The SystemPermission class should be final.
    assertTrue(Modifier.isFinal(SystemPermission.class.getModifiers()));
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) SystemPermission(org.apache.derby.shared.common.security.SystemPermission) AllPermission(java.security.AllPermission) DatabasePermission(org.apache.derby.security.DatabasePermission) Permission(java.security.Permission)

Example 5 with SystemPermission

use of org.apache.derby.shared.common.security.SystemPermission in project derby by apache.

the class SystemPrivilegesPermissionTest method policyTestSystemPermissionGrants.

/**
 * Tests SystemPermissions against the Policy.
 */
public void policyTestSystemPermissionGrants() {
    final Permission shutdown = new SystemPermission(SystemPermission.SERVER, SystemPermission.SHUTDOWN);
    // test SystemPermission for authorized user
    final SystemPrincipal authorizedUser = new SystemPrincipal("authorizedSystemUser");
    execute(authorizedUser, new ShutdownAction(shutdown), true);
    // test SystemPermission for unauthorized user
    final SystemPrincipal unAuthorizedUser = new SystemPrincipal("unAuthorizedSystemUser");
    execute(unAuthorizedUser, new ShutdownAction(shutdown), false);
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) SystemPermission(org.apache.derby.shared.common.security.SystemPermission) AllPermission(java.security.AllPermission) DatabasePermission(org.apache.derby.security.DatabasePermission) Permission(java.security.Permission) SystemPrincipal(org.apache.derby.authentication.SystemPrincipal)

Aggregations

SystemPermission (org.apache.derby.shared.common.security.SystemPermission)7 AllPermission (java.security.AllPermission)4 Permission (java.security.Permission)4 DatabasePermission (org.apache.derby.security.DatabasePermission)3 PermissionCollection (java.security.PermissionCollection)2 AccessControlException (java.security.AccessControlException)1 Permissions (java.security.Permissions)1 PrivilegedActionException (java.security.PrivilegedActionException)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ObjectName (javax.management.ObjectName)1 RuntimeMBeanException (javax.management.RuntimeMBeanException)1 SystemPrincipal (org.apache.derby.authentication.SystemPrincipal)1 StandardException (org.apache.derby.shared.common.error.StandardException)1