Search in sources :

Example 81 with PrivilegedActionException

use of java.security.PrivilegedActionException in project hadoop by apache.

the class KerberosTestUtils method doAs.

public static <T> T doAs(String principal, final Callable<T> callable) throws Exception {
    LoginContext loginContext = null;
    try {
        Set<Principal> principals = new HashSet<Principal>();
        principals.add(new KerberosPrincipal(KerberosTestUtils.getClientPrincipal()));
        Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
        loginContext = new LoginContext("", subject, null, new KerberosConfiguration(principal));
        loginContext.login();
        subject = loginContext.getSubject();
        return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {

            @Override
            public T run() throws Exception {
                return callable.call();
            }
        });
    } catch (PrivilegedActionException ex) {
        throw ex.getException();
    } finally {
        if (loginContext != null) {
            loginContext.logout();
        }
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) PrivilegedActionException(java.security.PrivilegedActionException) Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException) LoginContext(javax.security.auth.login.LoginContext) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 82 with PrivilegedActionException

use of java.security.PrivilegedActionException in project groovy by apache.

the class MetaClassImpl method addProperties.

private void addProperties() {
    BeanInfo info;
    //     introspect
    try {
        if (isBeanDerivative(theClass)) {
            info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws IntrospectionException {
                    return Introspector.getBeanInfo(theClass, Introspector.IGNORE_ALL_BEANINFO);
                }
            });
        } else {
            info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws IntrospectionException {
                    return Introspector.getBeanInfo(theClass);
                }
            });
        }
    } catch (PrivilegedActionException pae) {
        throw new GroovyRuntimeException("exception during bean introspection", pae.getException());
    }
    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    // build up the metaproperties based on the public fields, property descriptors,
    // and the getters and setters
    setupProperties(descriptors);
    EventSetDescriptor[] eventDescriptors = info.getEventSetDescriptors();
    for (EventSetDescriptor descriptor : eventDescriptors) {
        Method[] listenerMethods = descriptor.getListenerMethods();
        for (Method listenerMethod : listenerMethods) {
            final MetaMethod metaMethod = CachedMethod.find(descriptor.getAddListenerMethod());
            // we skip that here
            if (metaMethod == null)
                continue;
            addToAllMethodsIfPublic(metaMethod);
            String name = listenerMethod.getName();
            if (listeners.containsKey(name)) {
                listeners.put(name, AMBIGUOUS_LISTENER_METHOD);
            } else {
                listeners.put(name, metaMethod);
            }
        }
    }
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) PropertyDescriptor(java.beans.PropertyDescriptor) PrivilegedActionException(java.security.PrivilegedActionException) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) EventSetDescriptor(java.beans.EventSetDescriptor)

Example 83 with PrivilegedActionException

use of java.security.PrivilegedActionException in project robovm by robovm.

the class myPrivilegedExceptionAction method test_doAsPrivileged_02.

/**
     * javax.security.auth.Subject#doAsPrivileged(Subject subject,
     *                                                   PrivilegedExceptionAction action,
     *                                                   AccessControlContext acc)
     */
public void test_doAsPrivileged_02() {
    Subject subj = new Subject();
    PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction();
    PrivilegedExceptionAction<Object> peaNull = null;
    AccessControlContext acc = AccessController.getContext();
    try {
        Object obj = Subject.doAsPrivileged(null, pea, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, pea, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, peaNull, acc);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of NullPointerException");
    }
    try {
        Subject.doAsPrivileged(subj, new PrivilegedExceptionAction<Object>() {

            public Object run() throws PrivilegedActionException {
                throw new PrivilegedActionException(null);
            }
        }, acc);
        fail("PrivilegedActionException wasn't thrown");
    } catch (PrivilegedActionException e) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedActionException(java.security.PrivilegedActionException) Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException)

Example 84 with PrivilegedActionException

use of java.security.PrivilegedActionException in project AsmackService by rtreffer.

the class LoginContext method logoutImpl.

/**
     * The real implementation of logout() method whose calls are wrapped into
     * appropriate doPrivileged calls in logout().
     */
private void logoutImpl() throws LoginException {
    if (subject == null) {
        //$NON-NLS-1$
        throw new LoginException("auth.38");
    }
    loggedIn = false;
    Throwable firstProblem = null;
    int total = 0;
    for (Module module : modules) {
        try {
            module.module.logout();
            ++total;
        } catch (Throwable ex) {
            if (firstProblem == null) {
                firstProblem = ex;
            }
        }
    }
    if (firstProblem != null || total == 0) {
        if (firstProblem instanceof PrivilegedActionException && firstProblem.getCause() != null) {
            firstProblem = firstProblem.getCause();
        }
        if (firstProblem instanceof LoginException) {
            throw (LoginException) firstProblem;
        }
        //$NON-NLS-1$
        throw (LoginException) new LoginException("auth.37").initCause(firstProblem);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) LoginModule(org.apache.harmony.javax.security.auth.spi.LoginModule)

Example 85 with PrivilegedActionException

use of java.security.PrivilegedActionException in project AsmackService by rtreffer.

the class LoginContext method loginImpl.

/**
     * The real implementation of login() method whose calls are wrapped into
     * appropriate doPrivileged calls in login().
     */
private void loginImpl() throws LoginException {
    if (subject == null) {
        subject = new Subject();
    }
    if (sharedState == null) {
        sharedState = new HashMap<String, Object>();
    }
    // PHASE 1: Calling login()-s
    Throwable firstProblem = null;
    int[] logged = new int[4];
    int[] total = new int[4];
    for (Module module : modules) {
        try {
            // if a module fails during Class.forName(), then it breaks overall 
            // attempt - see catch() below
            module.create(subject, callbackHandler, sharedState);
            if (module.module.login()) {
                ++total[module.getFlag()];
                ++logged[module.getFlag()];
                if (module.getFlag() == SUFFICIENT) {
                    break;
                }
            }
        } catch (Throwable ex) {
            if (firstProblem == null) {
                firstProblem = ex;
            }
            if (module.klass == null) {
                /*
                     * an exception occurred during class lookup - overall
                     * attempt must fail a little trick: increase the REQUIRED's
                     * number - this will look like a failed REQUIRED module
                     * later, so overall attempt will fail
                     */
                ++total[REQUIRED];
                break;
            }
            ++total[module.getFlag()];
            // something happened after the class was loaded
            if (module.getFlag() == REQUISITE) {
                // ... and no need to walk down anymore
                break;
            }
        }
    }
    // end of PHASE1, 
    // Let's decide whether we have either overall success or a total failure
    boolean fail = true;
    // if any REQ* module failed - then it's failure
    if (logged[REQUIRED] != total[REQUIRED] || logged[REQUISITE] != total[REQUISITE]) {
    // fail = true;
    } else {
        if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
            // must have at least one SUFFICIENT or OPTIONAL
            if (logged[OPTIONAL] != 0 || logged[SUFFICIENT] != 0) {
                fail = false;
            }
        //else { fail = true; }
        } else {
            fail = false;
        }
    }
    int[] commited = new int[4];
    // clear it
    total[0] = total[1] = total[2] = total[3] = 0;
    if (!fail) {
        for (Module module : modules) {
            if (module.klass != null) {
                ++total[module.getFlag()];
                try {
                    module.module.commit();
                    ++commited[module.getFlag()];
                } catch (Throwable ex) {
                    if (firstProblem == null) {
                        firstProblem = ex;
                    }
                }
            }
        }
    }
    // need to decide once again
    fail = true;
    if (commited[REQUIRED] != total[REQUIRED] || commited[REQUISITE] != total[REQUISITE]) {
    //fail = true;
    } else {
        if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
            /*
                 * neither REQUIRED nor REQUISITE was configured. must have at
                 * least one SUFFICIENT or OPTIONAL
                 */
            if (commited[OPTIONAL] != 0 || commited[SUFFICIENT] != 0) {
                fail = false;
            } else {
            //fail = true;
            }
        } else {
            fail = false;
        }
    }
    if (fail) {
        for (Module module : modules) {
            try {
                module.module.abort();
            } catch (/*LoginException*/
            Throwable ex) {
                if (firstProblem == null) {
                    firstProblem = ex;
                }
            }
        }
        if (firstProblem instanceof PrivilegedActionException && firstProblem.getCause() != null) {
            firstProblem = firstProblem.getCause();
        }
        if (firstProblem instanceof LoginException) {
            throw (LoginException) firstProblem;
        }
        //$NON-NLS-1$
        throw (LoginException) new LoginException("auth.37").initCause(firstProblem);
    }
    loggedIn = true;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) LoginModule(org.apache.harmony.javax.security.auth.spi.LoginModule) Subject(org.apache.harmony.javax.security.auth.Subject)

Aggregations

PrivilegedActionException (java.security.PrivilegedActionException)135 IOException (java.io.IOException)58 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)56 Subject (javax.security.auth.Subject)23 LoginContext (javax.security.auth.login.LoginContext)14 LoginException (javax.security.auth.login.LoginException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 Method (java.lang.reflect.Method)11 URISyntaxException (java.net.URISyntaxException)11 HashSet (java.util.HashSet)11 ServletException (javax.servlet.ServletException)11 AccessControlContext (java.security.AccessControlContext)10 Principal (java.security.Principal)9 GSSException (org.ietf.jgss.GSSException)9 Field (java.lang.reflect.Field)8 SolrServerException (org.apache.solr.client.solrj.SolrServerException)7 GSSManager (org.ietf.jgss.GSSManager)7 MalformedURLException (java.net.MalformedURLException)6 ArrayList (java.util.ArrayList)6 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)6