Search in sources :

Example 96 with PrivilegedAction

use of java.security.PrivilegedAction in project undertow by undertow-io.

the class LocalNameResolvingHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final InetSocketAddress address = exchange.getDestinationAddress();
    if (address != null) {
        if ((resolveType == ResolveType.FORWARD || resolveType == ResolveType.FORWARD_AND_REVERSE) && address.isUnresolved()) {
            try {
                if (System.getSecurityManager() == null) {
                    final InetSocketAddress resolvedAddress = new InetSocketAddress(InetAddress.getByName(address.getHostName()), address.getPort());
                    exchange.setDestinationAddress(resolvedAddress);
                } else {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        @Override
                        public Object run() throws UnknownHostException {
                            final InetSocketAddress resolvedAddress = new InetSocketAddress(InetAddress.getByName(address.getHostName()), address.getPort());
                            exchange.setDestinationAddress(resolvedAddress);
                            return null;
                        }
                    });
                }
            } catch (UnknownHostException e) {
                UndertowLogger.REQUEST_LOGGER.debugf(e, "Could not resolve hostname %s", address.getHostString());
            }
        } else if (resolveType == ResolveType.REVERSE || resolveType == ResolveType.FORWARD_AND_REVERSE) {
            if (System.getSecurityManager() == null) {
                address.getHostName();
            } else {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {

                    @Override
                    public Object run() {
                        address.getHostName();
                        return null;
                    }
                });
            }
            //we call set source address because otherwise the underlying channel could just return a new address
            exchange.setDestinationAddress(address);
        }
    }
    next.handleRequest(exchange);
}
Also used : UnknownHostException(java.net.UnknownHostException) PrivilegedAction(java.security.PrivilegedAction) InetSocketAddress(java.net.InetSocketAddress)

Example 97 with PrivilegedAction

use of java.security.PrivilegedAction in project undertow by undertow-io.

the class PeerNameResolvingHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final InetSocketAddress address = exchange.getSourceAddress();
    if (address != null) {
        if ((resolveType == ResolveType.FORWARD || resolveType == ResolveType.FORWARD_AND_REVERSE) && address.isUnresolved()) {
            try {
                if (System.getSecurityManager() == null) {
                    final InetSocketAddress resolvedAddress = new InetSocketAddress(InetAddress.getByName(address.getHostName()), address.getPort());
                    exchange.setSourceAddress(resolvedAddress);
                } else {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        @Override
                        public Object run() throws UnknownHostException {
                            final InetSocketAddress resolvedAddress = new InetSocketAddress(InetAddress.getByName(address.getHostName()), address.getPort());
                            exchange.setSourceAddress(resolvedAddress);
                            return null;
                        }
                    });
                }
            } catch (UnknownHostException e) {
                UndertowLogger.REQUEST_LOGGER.debugf(e, "Could not resolve hostname %s", address.getHostString());
            }
        } else if (resolveType == ResolveType.REVERSE || resolveType == ResolveType.FORWARD_AND_REVERSE) {
            if (System.getSecurityManager() == null) {
                address.getHostName();
            } else {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {

                    @Override
                    public Object run() {
                        address.getHostName();
                        return null;
                    }
                });
            }
            //we call set source address because otherwise the underlying channel could just return a new address
            exchange.setSourceAddress(address);
        }
    }
    next.handleRequest(exchange);
}
Also used : UnknownHostException(java.net.UnknownHostException) PrivilegedAction(java.security.PrivilegedAction) InetSocketAddress(java.net.InetSocketAddress)

Example 98 with PrivilegedAction

use of java.security.PrivilegedAction in project OpenAM by OpenRock.

the class UpgradeResourceTypeStepTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    privilegedAction = mock(PrivilegedAction.class);
    resourceTypeService = mock(ResourceTypeService.class);
    connectionFactory = mock(ConnectionFactory.class);
    configManager = mock(ServiceConfigManager.class);
    upgradeResourceTypeStep = new UpgradeResourceTypeStep(configManager, resourceTypeService, privilegedAction, connectionFactory, Collections.<String>emptySet()) {

        @Override
        protected Document getEntitlementXML() throws UpgradeException {
            return document;
        }

        @Override
        protected Set<String> getRealmNamesFromParent() throws UpgradeException {
            return realms;
        }

        @Override
        protected Set<String> policiesEligibleForUpgrade(String appName, String realm) throws UpgradeException {
            return policies;
        }
    };
    when(document.getElementsByTagName(anyString())).thenReturn(new NodeList() {

        @Override
        public Node item(int i) {
            return null;
        }

        @Override
        public int getLength() {
            return 0;
        }
    });
    // Mock global and application type service configuration
    ServiceConfig globalConfig = mock(ServiceConfig.class);
    when(configManager.getGlobalConfig(anyString())).thenReturn(globalConfig);
    ServiceConfig appTypesConfig = mock(ServiceConfig.class);
    when(globalConfig.getSubConfig(anyString())).thenReturn(appTypesConfig);
    // Mock organisation and application service configuration
    ServiceConfig orgConfig = mock(ServiceConfig.class);
    when(configManager.getOrganizationConfig(anyString(), anyString())).thenReturn(orgConfig);
    ServiceConfig appsConfig = mock(ServiceConfig.class);
    when(orgConfig.getSubConfig(anyString())).thenReturn(appsConfig);
    // Mock application names
    when(appsConfig.getSubConfigNames()).thenReturn(Collections.singleton("MyApplication"));
    // Mock application data
    ServiceConfig appConfig = mock(ServiceConfig.class);
    when(appsConfig.getSubConfig("MyApplication")).thenReturn(appConfig);
    when(appConfig.getAttributes()).thenReturn(appData);
    // Mock application type on application and application type data
    ServiceConfig appTypeConfig = mock(ServiceConfig.class);
    when(appTypesConfig.getSubConfig("MyApplicationType")).thenReturn(appTypeConfig);
    when(appTypeConfig.getAttributes()).thenReturn(appTypeData);
    setupDataStructures();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ResourceTypeService(org.forgerock.openam.entitlement.service.ResourceTypeService) Document(org.w3c.dom.Document) UpgradeException(org.forgerock.openam.upgrade.UpgradeException) ConnectionFactory(org.forgerock.openam.sm.datalayer.api.ConnectionFactory) PrivilegedAction(java.security.PrivilegedAction) ServiceConfig(com.sun.identity.sm.ServiceConfig) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 99 with PrivilegedAction

use of java.security.PrivilegedAction in project wildfly by wildfly.

the class DynamicIIOPStub method invoke.

/**
     * Sends a request message to the server, receives the reply from the
     * server, and returns an <code>Object</code> result to the caller.
     */
public Object invoke(String operationName, final StubStrategy stubStrategy, Object[] params) throws Throwable {
    if (operationName.equals("_get_handle") && this instanceof javax.ejb.EJBObject) {
        if (handle == null) {
            handle = new HandleImplIIOP(this);
        }
        return handle;
    } else if (operationName.equals("_get_homeHandle") && this instanceof javax.ejb.EJBHome) {
        if (handle == null) {
            handle = new HomeHandleImplIIOP(this);
        }
        return handle;
    } else {
        //FIXME
        // all invocations are now made using remote invocation
        // local invocations between two different applications cause
        // ClassCastException between Stub and Interface
        // (two different modules are loading the classes)
        // problem was unnoticeable with JacORB because it uses
        // remote invocations to all stubs to which interceptors are
        // registered and a result all that JacORB always used
        // remote invocations
        // remote call path
        // To check whether this is a local stub or not we must call
        // org.omg.CORBA.portable.ObjectImpl._is_local(), and _not_
        // javax.rmi.CORBA.Util.isLocal(Stub s), which in Sun's JDK
        // always return false.
        InputStream in = null;
        try {
            try {
                OutputStream out = (OutputStream) _request(operationName, true);
                stubStrategy.writeParams(out, params);
                tracef("sent request: %s", operationName);
                in = (InputStream) _invoke(out);
                if (stubStrategy.isNonVoid()) {
                    trace("received reply");
                    final InputStream finalIn = in;
                    return doPrivileged(new PrivilegedAction<Object>() {

                        public Object run() {
                            return stubStrategy.readRetval(finalIn);
                        }
                    });
                } else {
                    return null;
                }
            } catch (final ApplicationException ex) {
                trace("got application exception");
                in = (InputStream) ex.getInputStream();
                final InputStream finalIn1 = in;
                throw doPrivileged(new PrivilegedAction<Exception>() {

                    public Exception run() {
                        return stubStrategy.readException(ex.getId(), finalIn1);
                    }
                });
            } catch (RemarshalException ex) {
                trace("got remarshal exception");
                return invoke(operationName, stubStrategy, params);
            }
        } catch (SystemException ex) {
            if (EjbLogger.EJB3_INVOCATION_LOGGER.isTraceEnabled()) {
                EjbLogger.EJB3_INVOCATION_LOGGER.trace("CORBA system exception in IIOP stub", ex);
            }
            throw Util.mapSystemException(ex);
        } finally {
            _releaseReply(in);
        }
    }
}
Also used : InputStream(org.omg.CORBA_2_3.portable.InputStream) OutputStream(org.omg.CORBA_2_3.portable.OutputStream) RemarshalException(org.omg.CORBA.portable.RemarshalException) ApplicationException(org.omg.CORBA.portable.ApplicationException) SystemException(org.omg.CORBA.SystemException) RemarshalException(org.omg.CORBA.portable.RemarshalException) ApplicationException(org.omg.CORBA.portable.ApplicationException) SystemException(org.omg.CORBA.SystemException) HandleImplIIOP(org.jboss.ejb.iiop.HandleImplIIOP) HomeHandleImplIIOP(org.jboss.ejb.iiop.HomeHandleImplIIOP) PrivilegedAction(java.security.PrivilegedAction) HomeHandleImplIIOP(org.jboss.ejb.iiop.HomeHandleImplIIOP)

Example 100 with PrivilegedAction

use of java.security.PrivilegedAction in project wildfly by wildfly.

the class PersistenceUnitServiceImpl method stop.

@Override
public void stop(final StopContext context) {
    final ExecutorService executor = executorInjector.getValue();
    final AccessControlContext accessControlContext = AccessController.doPrivileged(GetAccessControlContextAction.getInstance());
    final Runnable task = new Runnable() {

        // run async in a background thread
        @Override
        public void run() {
            PrivilegedAction<Void> privilegedAction = new PrivilegedAction<Void>() {

                // run as security privileged action
                @Override
                public Void run() {
                    if (phaseOnePersistenceUnitServiceInjectedValue.getOptionalValue() != null) {
                        ROOT_LOGGER.stoppingPersistenceUnitService(2, pu.getScopedPersistenceUnitName());
                    } else {
                        ROOT_LOGGER.stoppingService("Persistence Unit", pu.getScopedPersistenceUnitName());
                    }
                    ClassLoader old = Thread.currentThread().getContextClassLoader();
                    Thread.currentThread().setContextClassLoader(classLoader);
                    if (javaNamespaceSetup != null) {
                        javaNamespaceSetup.setup(Collections.<String, Object>emptyMap());
                    }
                    try {
                        if (entityManagerFactory != null) {
                            WritableServiceBasedNamingStore.pushOwner(deploymentUnitServiceName);
                            try {
                                if (entityManagerFactory.isOpen()) {
                                    entityManagerFactory.close();
                                }
                            } catch (Throwable t) {
                                ROOT_LOGGER.failedToStopPUService(t, pu.getScopedPersistenceUnitName());
                            } finally {
                                entityManagerFactory = null;
                                pu.setTempClassLoaderFactory(null);
                                WritableServiceBasedNamingStore.popOwner();
                                persistenceUnitRegistry.remove(getScopedPersistenceUnitName());
                            }
                        }
                    } finally {
                        Thread.currentThread().setContextClassLoader(old);
                        if (javaNamespaceSetup != null) {
                            javaNamespaceSetup.teardown(Collections.<String, Object>emptyMap());
                        }
                    }
                    if (proxyBeanManager != null) {
                        proxyBeanManager.setDelegate(null);
                        proxyBeanManager = null;
                    }
                    context.complete();
                    return null;
                }
            };
            WildFlySecurityManager.doChecked(privilegedAction, accessControlContext);
        }
    };
    try {
        executor.execute(task);
    } catch (RejectedExecutionException e) {
        task.run();
    } finally {
        context.asynchronous();
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

PrivilegedAction (java.security.PrivilegedAction)129 IOException (java.io.IOException)33 AccessControlContext (java.security.AccessControlContext)18 File (java.io.File)13 InputStream (java.io.InputStream)11 Method (java.lang.reflect.Method)11 ExecutorService (java.util.concurrent.ExecutorService)8 Field (java.lang.reflect.Field)7 Subject (javax.security.auth.Subject)7 PrivilegedActionException (java.security.PrivilegedActionException)6 ArrayList (java.util.ArrayList)6 Properties (java.util.Properties)6 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)6 FileInputStream (java.io.FileInputStream)5 ProtectionDomain (java.security.ProtectionDomain)5 Enumeration (java.util.Enumeration)5 FileNotFoundException (java.io.FileNotFoundException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 MalformedURLException (java.net.MalformedURLException)4