Search in sources :

Example 1 with HandleImplIIOP

use of org.jboss.ejb.iiop.HandleImplIIOP in project wildfly by wildfly.

the class EjbCorbaServant method _invoke.

/**
 * Receives IIOP requests to this servant's <code>EJBObject</code>s
 * and forwards them to the bean container, through the JBoss
 * <code>MBean</code> server.
 */
public OutputStream _invoke(final String opName, final InputStream in, final ResponseHandler handler) {
    EjbLogger.ROOT_LOGGER.tracef("EJBObject invocation: %s", opName);
    SkeletonStrategy op = methodInvokerMap.get(opName);
    if (op == null) {
        EjbLogger.ROOT_LOGGER.debugf("Unable to find opname '%s' valid operations:%s", opName, methodInvokerMap.keySet());
        throw new BAD_OPERATION(opName);
    }
    final NamespaceContextSelector selector = componentView.getComponent().getNamespaceContextSelector();
    final ClassLoader oldCl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    NamespaceContextSelector.pushCurrentSelector(selector);
    try {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
        org.omg.CORBA_2_3.portable.OutputStream out;
        try {
            Object retVal;
            if (!home && opName.equals("_get_handle")) {
                retVal = new HandleImplIIOP(orb.object_to_string(_this_object()));
            } else if (home && opName.equals("_get_homeHandle")) {
                retVal = homeHandle;
            } else if (home && opName.equals("_get_EJBMetaData")) {
                retVal = ejbMetaData;
            } else {
                Principal identityPrincipal = null;
                Principal principal = null;
                Object credential = null;
                if (this.sasCurrent != null) {
                    final byte[] incomingIdentity = this.sasCurrent.get_incoming_principal_name();
                    // we have an identity token, which is a trust based mechanism
                    if (incomingIdentity != null && incomingIdentity.length > 0) {
                        String name = new String(incomingIdentity, StandardCharsets.UTF_8);
                        int domainIndex = name.indexOf('@');
                        if (domainIndex > 0)
                            name = name.substring(0, domainIndex);
                        identityPrincipal = new NamePrincipal(name);
                    }
                    final byte[] incomingUsername = this.sasCurrent.get_incoming_username();
                    if (incomingUsername != null && incomingUsername.length > 0) {
                        final byte[] incomingPassword = this.sasCurrent.get_incoming_password();
                        String name = new String(incomingUsername, StandardCharsets.UTF_8);
                        int domainIndex = name.indexOf('@');
                        if (domainIndex > 0) {
                            name = name.substring(0, domainIndex);
                        }
                        principal = new NamePrincipal(name);
                        credential = new String(incomingPassword, StandardCharsets.UTF_8).toCharArray();
                    }
                }
                final Object[] params = op.readParams((org.omg.CORBA_2_3.portable.InputStream) in);
                if (!this.home && opName.equals("isIdentical") && params.length == 1) {
                    // handle isIdentical specially
                    Object val = params[0];
                    retVal = val instanceof org.omg.CORBA.Object && handleIsIdentical((org.omg.CORBA.Object) val);
                } else {
                    if (this.securityDomain != null) {
                        // an elytron security domain is available: authenticate and authorize the client before invoking the component.
                        SecurityIdentity identity = this.securityDomain.getAnonymousSecurityIdentity();
                        AuthenticationConfiguration authenticationConfiguration = AuthenticationConfiguration.EMPTY;
                        if (identityPrincipal != null) {
                            // permission to run as the identity token principal.
                            if (principal != null) {
                                char[] password = (char[]) credential;
                                authenticationConfiguration = authenticationConfiguration.useName(principal.getName()).usePassword(password);
                                SecurityIdentity authenticatedIdentity = this.authenticate(principal, password);
                                identity = authenticatedIdentity.createRunAsIdentity(identityPrincipal.getName(), true);
                            } else {
                                // no TLS nor initial context token found - check if the anonymous identity has
                                // permission to run as the identity principal.
                                identity = this.securityDomain.getAnonymousSecurityIdentity().createRunAsIdentity(identityPrincipal.getName(), true);
                            }
                        } else if (principal != null) {
                            char[] password = (char[]) credential;
                            // we have an initial context token containing a username/password pair.
                            authenticationConfiguration = authenticationConfiguration.useName(principal.getName()).usePassword(password);
                            identity = this.authenticate(principal, password);
                        }
                        final InterceptorContext interceptorContext = new InterceptorContext();
                        this.prepareInterceptorContext(op, params, interceptorContext);
                        try {
                            final AuthenticationContext context = AuthenticationContext.captureCurrent().with(MatchRule.ALL.matchProtocol("iiop"), authenticationConfiguration);
                            retVal = identity.runAs((PrivilegedExceptionAction<Object>) () -> context.run((PrivilegedExceptionAction<Object>) () -> this.componentView.invoke(interceptorContext)));
                        } catch (PrivilegedActionException e) {
                            throw e.getCause();
                        }
                    } else {
                        // legacy security behavior: setup the security context if a SASCurrent is available and invoke the component.
                        // One of the EJB security interceptors will authenticate and authorize the client.
                        final InterceptorContext interceptorContext = new InterceptorContext();
                        prepareInterceptorContext(op, params, interceptorContext);
                        retVal = this.componentView.invoke(interceptorContext);
                    }
                }
            }
            out = (org.omg.CORBA_2_3.portable.OutputStream) handler.createReply();
            if (op.isNonVoid()) {
                op.writeRetval(out, retVal);
            }
        } catch (Throwable e) {
            EjbLogger.ROOT_LOGGER.trace("Exception in EJBObject invocation", e);
            if (e instanceof MBeanException) {
                e = ((MBeanException) e).getTargetException();
            }
            RmiIdlUtil.rethrowIfCorbaSystemException(e);
            out = (org.omg.CORBA_2_3.portable.OutputStream) handler.createExceptionReply();
            op.writeException(out, e);
        }
        return out;
    } finally {
        NamespaceContextSelector.popCurrentSelector();
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldCl);
    }
}
Also used : ServerAuthenticationContext(org.wildfly.security.auth.server.ServerAuthenticationContext) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) OutputStream(org.omg.CORBA.portable.OutputStream) NamespaceContextSelector(org.jboss.as.naming.context.NamespaceContextSelector) HandleImplIIOP(org.jboss.ejb.iiop.HandleImplIIOP) InterceptorContext(org.jboss.invocation.InterceptorContext) AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) PrivilegedActionException(java.security.PrivilegedActionException) SkeletonStrategy(org.wildfly.iiop.openjdk.rmi.marshal.strategy.SkeletonStrategy) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) MBeanException(javax.management.MBeanException) BAD_OPERATION(org.omg.CORBA.BAD_OPERATION) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) Principal(java.security.Principal)

Example 2 with HandleImplIIOP

use of org.jboss.ejb.iiop.HandleImplIIOP 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)

Aggregations

HandleImplIIOP (org.jboss.ejb.iiop.HandleImplIIOP)2 Principal (java.security.Principal)1 PrivilegedAction (java.security.PrivilegedAction)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 MBeanException (javax.management.MBeanException)1 NamespaceContextSelector (org.jboss.as.naming.context.NamespaceContextSelector)1 HomeHandleImplIIOP (org.jboss.ejb.iiop.HomeHandleImplIIOP)1 InterceptorContext (org.jboss.invocation.InterceptorContext)1 BAD_OPERATION (org.omg.CORBA.BAD_OPERATION)1 SystemException (org.omg.CORBA.SystemException)1 ApplicationException (org.omg.CORBA.portable.ApplicationException)1 OutputStream (org.omg.CORBA.portable.OutputStream)1 RemarshalException (org.omg.CORBA.portable.RemarshalException)1 InputStream (org.omg.CORBA_2_3.portable.InputStream)1 OutputStream (org.omg.CORBA_2_3.portable.OutputStream)1 SkeletonStrategy (org.wildfly.iiop.openjdk.rmi.marshal.strategy.SkeletonStrategy)1 AuthenticationConfiguration (org.wildfly.security.auth.client.AuthenticationConfiguration)1 AuthenticationContext (org.wildfly.security.auth.client.AuthenticationContext)1 NamePrincipal (org.wildfly.security.auth.principal.NamePrincipal)1