Search in sources :

Example 26 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class EJBSecurityTestCase method testDenyAllAnnotation.

@Test
public void testDenyAllAnnotation() throws Exception {
    final Context ctx = new InitialContext();
    final Restriction restrictedBean = (Restriction) ctx.lookup("java:module/" + AnnotatedSLSB.class.getSimpleName() + "!" + Restriction.class.getName());
    try {
        restrictedBean.restrictedMethod();
        Assert.fail("Call to restrictedMethod() method was expected to fail");
    } catch (EJBAccessException ejbae) {
    // expected
    }
    final FullAccess fullAccessBean = (FullAccess) ctx.lookup("java:module/" + AnnotatedSLSB.class.getSimpleName() + "!" + FullAccess.class.getName());
    fullAccessBean.doAnything();
    final AnnotatedSLSB annotatedBean = (AnnotatedSLSB) ctx.lookup("java:module/" + AnnotatedSLSB.class.getSimpleName() + "!" + AnnotatedSLSB.class.getName());
    try {
        annotatedBean.restrictedMethod();
        Assert.fail("Call to restrictedMethod() method was expected to fail");
    } catch (EJBAccessException ejbae) {
    //expected
    }
    // full access, should work
    annotatedBean.doAnything();
    try {
        annotatedBean.restrictedBaseClassMethod();
        Assert.fail("Call to restrictedBaseClassMethod() method was expected to fail");
    } catch (EJBAccessException ejbae) {
    //expected
    }
    // should be accessible, since the overridden method isn't annotated with @DenyAll
    annotatedBean.overriddenMethod();
    final FullyRestrictedBean fullyRestrictedBean = (FullyRestrictedBean) ctx.lookup("java:module/" + FullyRestrictedBean.class.getSimpleName() + "!" + FullyRestrictedBean.class.getName());
    try {
        fullyRestrictedBean.overriddenMethod();
        Assert.fail("Call to overriddenMethod() method was expected to fail");
    } catch (EJBAccessException ejae) {
    // expected
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Example 27 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class SecurityDDOverrideTestCase method testDDOverride.

/**
     * Tests that the overriden roles allowed, via ejb-jar.xml, on an EJB method are taken into account for EJB method
     * invocations
     *
     * @throws Exception
     */
@Test
public void testDDOverride() throws Exception {
    final Context ctx = new InitialContext();
    final PartialDDBean partialDDBean = (PartialDDBean) ctx.lookup("java:module/" + PartialDDBean.class.getSimpleName() + "!" + PartialDDBean.class.getName());
    try {
        partialDDBean.denyAllMethod();
        Assert.fail("Call to denyAllMethod() was expected to fail");
    } catch (EJBAccessException ejbae) {
    // expected
    }
    // expected to pass
    partialDDBean.permitAllMethod();
    // login as user1 and test
    LoginContext lc = Util.getCLMLoginContext("user1", "password1");
    lc.login();
    try {
        // expected to pass since user1 belongs to Role1
        partialDDBean.toBeInvokedOnlyByRole1();
        // expected to fail since user1 *doesn't* belong to Role2
        try {
            partialDDBean.toBeInvokedByRole2();
            Assert.fail("Call to toBeInvokedByRole2() was expected to fail");
        } catch (EJBAccessException ejbae) {
        // expected
        }
    } finally {
        lc.logout();
    }
    // login as user2 and test
    lc = Util.getCLMLoginContext("user2", "password2");
    lc.login();
    try {
        // expected to pass since user2 belongs to Role2
        partialDDBean.toBeInvokedByRole2();
        // expected to fail since user2 *doesn't* belong to Role1
        try {
            partialDDBean.toBeInvokedOnlyByRole1();
            Assert.fail("Call to toBeInvokedOnlyByRole1() was expected to fail");
        } catch (EJBAccessException ejbae) {
        // expected
        }
    } finally {
        lc.logout();
    }
}
Also used : InitialContext(javax.naming.InitialContext) LoginContext(javax.security.auth.login.LoginContext) Context(javax.naming.Context) LoginContext(javax.security.auth.login.LoginContext) PartialDDBean(org.jboss.as.test.integration.ejb.security.dd.override.PartialDDBean) InitialContext(javax.naming.InitialContext) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Example 28 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AsynchronousSecurityTestCase method testAsyncSecurityPermition.

@Test
public void testAsyncSecurityPermition() throws Exception {
    SecuredStatelessBean.reset();
    SecuredStatelessRemote securedBean = lookupInterface(SecuredStatelessBean.class, SecuredStatelessRemote.class);
    LoginContext lc = Util.getCLMLoginContext("rolefail", "password");
    lc.login();
    // Test 1
    try {
        Future<Boolean> future = securedBean.uncheckedMethod();
        SecuredStatelessBean.startLatch.countDown();
        boolean result = future.get();
        Assert.assertTrue(result);
        // Test 2
        future = null;
        result = false;
        SecuredStatelessBean.reset();
        try {
            future = securedBean.excludedMethod();
            SecuredStatelessBean.startLatch.countDown();
            result = future.get();
        } catch (ExecutionException ee) {
            if (!(ee.getCause() instanceof EJBAccessException)) {
                Assert.fail("Exception cause was not EJBAccessException and was " + ee);
            }
        } catch (EJBAccessException ejbe) {
        // it's ok too
        }
        Assert.assertFalse(result);
    } finally {
        lc.logout();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) ExecutionException(java.util.concurrent.ExecutionException) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Example 29 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AsynchronousSecurityTestCase method testAsynchSecurityMethod.

@Test
public void testAsynchSecurityMethod() throws Exception {
    SecuredStatelessRemote securedBean = lookupInterface(SecuredStatelessBean.class, SecuredStatelessRemote.class);
    boolean result = false;
    Future<Boolean> future;
    // Test 1
    SecuredStatelessBean.reset();
    LoginContext lc = Util.getCLMLoginContext("somebody", "password");
    lc.login();
    try {
        future = securedBean.method();
        SecuredStatelessBean.startLatch.countDown();
        result = future.get();
    } finally {
        lc.logout();
    }
    Assert.assertTrue(result);
    // Test 2
    SecuredStatelessBean.reset();
    future = null;
    result = false;
    lc = Util.getCLMLoginContext("rolefail", "password");
    lc.login();
    try {
        future = securedBean.method();
        SecuredStatelessBean.startLatch.countDown();
        result = future.get();
    } catch (ExecutionException ee) {
        if (!(ee.getCause() instanceof EJBAccessException)) {
            Assert.fail("Exception cause was not EJBAccessException and was " + ee);
        }
    } catch (EJBAccessException ejbe) {
    // it's ok too
    } finally {
        lc.logout();
    }
    Assert.assertFalse(result);
    // Test 3
    SecuredStatelessBean.reset();
    future = null;
    result = false;
    lc = Util.getCLMLoginContext("nosuchuser", "password");
    lc.login();
    try {
        future = securedBean.method();
        SecuredStatelessBean.startLatch.countDown();
        result = future.get();
    } catch (ExecutionException ee) {
        if (!(ee.getCause() instanceof EJBAccessException)) {
            Assert.fail("Exception cause was not EJBAccessException and was " + ee);
        }
    } catch (EJBAccessException ejbe) {
    // it's ok too
    } finally {
        lc.logout();
    }
    Assert.assertFalse(result);
}
Also used : LoginContext(javax.security.auth.login.LoginContext) ExecutionException(java.util.concurrent.ExecutionException) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Example 30 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnotationAuthorizationTestCase method testRolesAllowedOverridenInBaseClass_HR.

@Test
public void testRolesAllowedOverridenInBaseClass_HR() throws Exception {
    LoginContext lc = Util.getCLMLoginContext("hr", "hr");
    lc.login();
    try {
        String response = rolesAllowedOverridenBean.aMethod("aMethod");
        assertEquals("aMethod", response);
        try {
            rolesAllowedOverridenBean.bMethod("bMethod");
            fail("Expected EJBAccessException not thrown");
        } catch (EJBAccessException ignored) {
        }
    } finally {
        lc.logout();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Aggregations

EJBAccessException (javax.ejb.EJBAccessException)42 Test (org.junit.Test)26 LoginContext (javax.security.auth.login.LoginContext)16 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)11 OpenEJBException (org.apache.openejb.OpenEJBException)5 Principal (java.security.Principal)4 NamingException (javax.naming.NamingException)4 ApplicationException (org.apache.openejb.ApplicationException)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 EJBHome (javax.ejb.EJBHome)3 EJBLocalHome (javax.ejb.EJBLocalHome)3 EJBLocalObject (javax.ejb.EJBLocalObject)3 EJBObject (javax.ejb.EJBObject)3 LoginException (javax.security.auth.login.LoginException)3 ServletException (javax.servlet.ServletException)3 BeanContext (org.apache.openejb.BeanContext)3 ThreadContext (org.apache.openejb.core.ThreadContext)3