Search in sources :

Example 66 with PrivilegedAction

use of java.security.PrivilegedAction in project jdk8u_jdk by JetBrains.

the class MetaData method getPrivateFieldValue.

static Object getPrivateFieldValue(Object instance, String name) {
    Field field = fields.get(name);
    if (field == null) {
        int index = name.lastIndexOf('.');
        final String className = name.substring(0, index);
        final String fieldName = name.substring(1 + index);
        field = AccessController.doPrivileged(new PrivilegedAction<Field>() {

            public Field run() {
                try {
                    Field field = Class.forName(className).getDeclaredField(fieldName);
                    field.setAccessible(true);
                    return field;
                } catch (ClassNotFoundException exception) {
                    throw new IllegalStateException("Could not find class", exception);
                } catch (NoSuchFieldException exception) {
                    throw new IllegalStateException("Could not find field", exception);
                }
            }
        });
        fields.put(name, field);
    }
    try {
        return field.get(instance);
    } catch (IllegalAccessException exception) {
        throw new IllegalStateException("Could not get value of the field", exception);
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Point(java.awt.Point)

Example 67 with PrivilegedAction

use of java.security.PrivilegedAction in project jdk8u_jdk by JetBrains.

the class DefaultMBeanServerInterceptor method checkMBeanTrustPermission.

private static void checkMBeanTrustPermission(final Class<?> theClass) throws SecurityException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        Permission perm = new MBeanTrustPermission("register");
        PrivilegedAction<ProtectionDomain> act = new PrivilegedAction<ProtectionDomain>() {

            public ProtectionDomain run() {
                return theClass.getProtectionDomain();
            }
        };
        ProtectionDomain pd = AccessController.doPrivileged(act);
        AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd });
        sm.checkPermission(perm, acc);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) MBeanTrustPermission(javax.management.MBeanTrustPermission) MBeanPermission(javax.management.MBeanPermission) MBeanTrustPermission(javax.management.MBeanTrustPermission) Permission(java.security.Permission)

Example 68 with PrivilegedAction

use of java.security.PrivilegedAction in project jdk8u_jdk by JetBrains.

the class Queue method dispatchEvent.

/**
     * Dispatches an event. The manner in which the event is
     * dispatched depends upon the type of the event and the
     * type of the event's source object:
     *
     * <table border=1 summary="Event types, source types, and dispatch methods">
     * <tr>
     *     <th>Event Type</th>
     *     <th>Source Type</th>
     *     <th>Dispatched To</th>
     * </tr>
     * <tr>
     *     <td>ActiveEvent</td>
     *     <td>Any</td>
     *     <td>event.dispatch()</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>Component</td>
     *     <td>source.dispatchEvent(AWTEvent)</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>MenuComponent</td>
     *     <td>source.dispatchEvent(AWTEvent)</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>Other</td>
     *     <td>No action (ignored)</td>
     * </tr>
     * </table>
     * <p>
     * @param event an instance of <code>java.awt.AWTEvent</code>,
     *          or a subclass of it
     * @throws NullPointerException if <code>event</code> is <code>null</code>
     * @since           1.2
     */
protected void dispatchEvent(final AWTEvent event) {
    final Object src = event.getSource();
    final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {

        public Void run() {
            // dispatch the event straight away.
            if (fwDispatcher == null || isDispatchThreadImpl()) {
                dispatchEventImpl(event, src);
            } else {
                fwDispatcher.scheduleDispatch(new Runnable() {

                    @Override
                    public void run() {
                        dispatchEventImpl(event, src);
                    }
                });
            }
            return null;
        }
    };
    final AccessControlContext stack = AccessController.getContext();
    final AccessControlContext srcAcc = getAccessControlContextFrom(src);
    final AccessControlContext eventAcc = event.getAccessControlContext();
    if (srcAcc == null) {
        javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
    } else {
        javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Void>() {

            public Void run() {
                javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
                return null;
            }
        }, stack, srcAcc);
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction)

Example 69 with PrivilegedAction

use of java.security.PrivilegedAction in project jdk8u_jdk by JetBrains.

the class IIORegistry method registerInstalledProviders.

private void registerInstalledProviders() {
    /*
          We need to load installed providers from the
          system classpath (typically the <code>lib/ext</code>
          directory in in the Java installation directory)
          in the privileged mode in order to
          be able read corresponding jar files even if
          file read capability is restricted (like the
          applet context case).
         */
    PrivilegedAction doRegistration = new PrivilegedAction() {

        public Object run() {
            Iterator categories = getCategories();
            while (categories.hasNext()) {
                Class<IIOServiceProvider> c = (Class) categories.next();
                for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                    registerServiceProvider(p);
                }
            }
            return this;
        }
    };
    AccessController.doPrivileged(doRegistration);
}
Also used : PrivilegedAction(java.security.PrivilegedAction) Iterator(java.util.Iterator)

Example 70 with PrivilegedAction

use of java.security.PrivilegedAction in project admin-console-beta by connexta.

the class GraphQLServlet method query.

private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
    if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) {
        Subject.doAs(context.getSubject().get(), new PrivilegedAction<Void>() {

            @Override
            @SneakyThrows
            public Void run() {
                query(query, operationName, variables, schema, req, resp, context);
                return null;
            }
        });
    } else {
        runListeners(operationListeners, l -> runListener(l, it -> it.beforeGraphQLOperation(context, operationName, query, variables)));
        ExecutionResult executionResult = new GraphQL(schema, getQueryExecutionStrategy(), getMutationExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
        List<GraphQLError> errors = executionResult.getErrors();
        Object data = executionResult.getData();
        String response = mapper.writeValueAsString(createResultFromDataAndErrors(data, errors));
        resp.setContentType(APPLICATION_JSON_UTF8);
        resp.setStatus(STATUS_OK);
        resp.getWriter().write(response);
        if (errorsPresent(errors)) {
            runListeners(operationListeners, l -> l.onFailedGraphQLOperation(context, operationName, query, variables, data, errors));
        } else {
            runListeners(operationListeners, l -> l.onSuccessfulGraphQLOperation(context, operationName, query, variables, data));
        }
    }
}
Also used : InvalidSyntaxError(graphql.InvalidSyntaxError) Setter(lombok.Setter) GraphQL(graphql.GraphQL) ExecutionStrategy(graphql.execution.ExecutionStrategy) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) ServletException(javax.servlet.ServletException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) HttpServletRequest(javax.servlet.http.HttpServletRequest) CharStreams(com.google.common.io.CharStreams) GraphQLError(graphql.GraphQLError) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) PrivilegedAction(java.security.PrivilegedAction) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Subject(javax.security.auth.Subject) Consumer(java.util.function.Consumer) ValidationError(graphql.validation.ValidationError) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Part(javax.servlet.http.Part) Optional(java.util.Optional) AccessController(java.security.AccessController) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) InputStream(java.io.InputStream) GraphQL(graphql.GraphQL) SneakyThrows(lombok.SneakyThrows) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult)

Aggregations

PrivilegedAction (java.security.PrivilegedAction)190 IOException (java.io.IOException)44 Subject (javax.security.auth.Subject)28 File (java.io.File)19 AccessControlContext (java.security.AccessControlContext)18 Method (java.lang.reflect.Method)13 InputStream (java.io.InputStream)12 URL (java.net.URL)11 LoginException (com.sun.enterprise.security.auth.login.common.LoginException)10 Field (java.lang.reflect.Field)10 URLClassLoader (java.net.URLClassLoader)10 Principal (java.security.Principal)10 Set (java.util.Set)9 PrivilegedActionException (java.security.PrivilegedActionException)8 Iterator (java.util.Iterator)8 PasswordCredential (com.sun.enterprise.security.auth.login.common.PasswordCredential)7 InvalidOperationException (com.sun.enterprise.security.auth.realm.InvalidOperationException)7 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)7 NoSuchUserException (com.sun.enterprise.security.auth.realm.NoSuchUserException)7 URISyntaxException (java.net.URISyntaxException)7