Search in sources :

Example 1 with NamespaceContextSelector

use of org.jboss.as.naming.context.NamespaceContextSelector 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.
                        SecurityContext legacyContext = null;
                        if (this.legacySecurityDomain != null && (identityPrincipal != null || principal != null)) {
                            // we don't have any real way to establish trust in identity based auth so we just use
                            // the SASCurrent as a credential, and a custom legacy login module can make a decision for us.
                            final Object finalCredential = identityPrincipal != null ? this.sasCurrent : credential;
                            final Principal finalPrincipal = identityPrincipal != null ? identityPrincipal : principal;
                            if (WildFlySecurityManager.isChecking()) {
                                legacyContext = AccessController.doPrivileged((PrivilegedExceptionAction<SecurityContext>) () -> {
                                    SecurityContext sc = SecurityContextFactory.createSecurityContext(this.legacySecurityDomain);
                                    sc.getUtil().createSubjectInfo(finalPrincipal, finalCredential, null);
                                    return sc;
                                });
                            } else {
                                legacyContext = SecurityContextFactory.createSecurityContext(this.legacySecurityDomain);
                                legacyContext.getUtil().createSubjectInfo(finalPrincipal, finalCredential, null);
                            }
                        }
                        if (legacyContext != null) {
                            setSecurityContextOnAssociation(legacyContext);
                        }
                        try {
                            final InterceptorContext interceptorContext = new InterceptorContext();
                            if (legacyContext != null) {
                                interceptorContext.putPrivateData(SecurityContext.class, legacyContext);
                            }
                            prepareInterceptorContext(op, params, interceptorContext);
                            retVal = this.componentView.invoke(interceptorContext);
                        } finally {
                            if (legacyContext != null) {
                                clearSecurityContextOnAssociation();
                            }
                        }
                    }
                }
            }
            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) SecurityContext(org.jboss.security.SecurityContext) 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 NamespaceContextSelector

use of org.jboss.as.naming.context.NamespaceContextSelector in project wildfly by wildfly.

the class EJBComponent method lookup.

public Object lookup(String name) throws IllegalArgumentException {
    if (name == null) {
        throw EjbLogger.ROOT_LOGGER.jndiNameCannotBeNull();
    }
    final NamespaceContextSelector namespaceContextSelector = NamespaceContextSelector.getCurrentSelector();
    if (namespaceContextSelector == null) {
        throw EjbLogger.ROOT_LOGGER.noNamespaceContextSelectorAvailable(name);
    }
    Context jndiContext = null;
    String namespaceStrippedJndiName = name;
    // get the appropriate JNDI context and strip the lookup jndi name of the component namespace prefix
    if (name.startsWith("java:app/")) {
        jndiContext = namespaceContextSelector.getContext("app");
        namespaceStrippedJndiName = name.substring("java:app/".length());
    } else if (name.startsWith("java:module/")) {
        jndiContext = namespaceContextSelector.getContext("module");
        namespaceStrippedJndiName = name.substring("java:module/".length());
    } else if (name.startsWith("java:comp/")) {
        jndiContext = namespaceContextSelector.getContext("comp");
        namespaceStrippedJndiName = name.substring("java:comp/".length());
    } else if (!name.startsWith("java:")) {
        // if it *doesn't* start with java: prefix, then default it to java:comp
        jndiContext = namespaceContextSelector.getContext("comp");
        // no need to strip the name since it doesn't start with java: prefix.
        // Also prefix the "env/" to the jndi name, since a lookup without a java: namespace prefix is effectively
        // a lookup under java:comp/env/<jndi-name>
        namespaceStrippedJndiName = "env/" + name;
    } else if (name.startsWith("java:global/")) {
        // with the absolute name (including the java:global prefix)
        try {
            jndiContext = new InitialContext();
        } catch (NamingException ne) {
            throw EjbLogger.ROOT_LOGGER.failToLookupJNDI(name, ne);
        }
    } else {
        throw EjbLogger.ROOT_LOGGER.failToLookupJNDINameSpace(name);
    }
    EjbLogger.ROOT_LOGGER.debugf("Looking up %s in jndi context: %s", namespaceStrippedJndiName, jndiContext);
    try {
        return jndiContext.lookup(namespaceStrippedJndiName);
    } catch (NamingException ne) {
        throw EjbLogger.ROOT_LOGGER.failToLookupStrippedJNDI(namespaceContextSelector, jndiContext, ne);
    }
}
Also used : InitialContext(javax.naming.InitialContext) InterceptorContext(org.jboss.invocation.InterceptorContext) Context(javax.naming.Context) CurrentInvocationContext(org.jboss.as.ejb3.context.CurrentInvocationContext) NamingException(javax.naming.NamingException) NamespaceContextSelector(org.jboss.as.naming.context.NamespaceContextSelector) InitialContext(javax.naming.InitialContext)

Aggregations

NamespaceContextSelector (org.jboss.as.naming.context.NamespaceContextSelector)2 InterceptorContext (org.jboss.invocation.InterceptorContext)2 Principal (java.security.Principal)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 MBeanException (javax.management.MBeanException)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 CurrentInvocationContext (org.jboss.as.ejb3.context.CurrentInvocationContext)1 HandleImplIIOP (org.jboss.ejb.iiop.HandleImplIIOP)1 SecurityContext (org.jboss.security.SecurityContext)1 BAD_OPERATION (org.omg.CORBA.BAD_OPERATION)1 OutputStream (org.omg.CORBA.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 SecurityIdentity (org.wildfly.security.auth.server.SecurityIdentity)1 ServerAuthenticationContext (org.wildfly.security.auth.server.ServerAuthenticationContext)1