Search in sources :

Example 6 with ResourcePrincipal

use of com.sun.enterprise.deployment.ResourcePrincipal in project Payara by payara.

the class ConnectorAdminServiceUtils method getDefaultResourcePrincipal.

/*
     * Returns a ResourcePrincipal object populated with a pool's
     * default USERNAME and PASSWORD
     *
     * @throws NamingException if poolname lookup fails
     */
public static ResourcePrincipal getDefaultResourcePrincipal(PoolInfo poolInfo) throws NamingException {
    // All this to get the default user name and principal
    ConnectorConnectionPool connectorConnectionPool = null;
    try {
        String jndiNameForPool = getReservePrefixedJNDINameForPool(poolInfo);
        Context ic = ConnectorRuntime.getRuntime().getNamingManager().getInitialContext();
        connectorConnectionPool = (ConnectorConnectionPool) ic.lookup(jndiNameForPool);
    } catch (NamingException ne) {
        throw ne;
    }
    ConnectorDescriptorInfo cdi = connectorConnectionPool.getConnectorDescriptorInfo();
    Set mcfConfigProperties = cdi.getMCFConfigProperties();
    Iterator mcfConfPropsIter = mcfConfigProperties.iterator();
    String userName = "";
    String password = "";
    while (mcfConfPropsIter.hasNext()) {
        ConnectorConfigProperty prop = (ConnectorConfigProperty) mcfConfPropsIter.next();
        if (prop.getName().toUpperCase(Locale.getDefault()).equals("USERNAME") || prop.getName().toUpperCase(Locale.getDefault()).equals("USER")) {
            userName = prop.getValue();
        } else if (prop.getName().toUpperCase(Locale.getDefault()).equals("PASSWORD")) {
            password = prop.getValue();
        }
    }
    // Now return the ResourcePrincipal
    return new ResourcePrincipal(userName, password);
}
Also used : Context(javax.naming.Context) ConnectorConnectionPool(com.sun.enterprise.connectors.ConnectorConnectionPool) Set(java.util.Set) Iterator(java.util.Iterator) ConnectorDescriptorInfo(com.sun.enterprise.connectors.ConnectorDescriptorInfo) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) NamingException(javax.naming.NamingException) ConnectorConfigProperty(com.sun.enterprise.deployment.ConnectorConfigProperty)

Example 7 with ResourcePrincipal

use of com.sun.enterprise.deployment.ResourcePrincipal in project Payara by payara.

the class ConnectorConnectionPoolAdminServiceImpl method getDefaultSubject.

/**
 * Utility method that is used to get the default subject for the
 * specified mcf and resource principal.
 * @param poolInfo
 * @param mcf
 * @param prin
 * @return
 * @throws javax.resource.ResourceException
 */
protected Subject getDefaultSubject(PoolInfo poolInfo, ManagedConnectionFactory mcf, ResourcePrincipal prin) throws ResourceException {
    ResourcePrincipal resourcePrincipal = null;
    if (prin == null) {
        try {
            resourcePrincipal = getDefaultResourcePrincipal(poolInfo, mcf);
        } catch (NamingException ne) {
            _logger.log(Level.WARNING, "jdbc.pool_not_reachable", ne.getMessage());
            String l10nMsg = localStrings.getString("pingpool.name_not_bound", poolInfo);
            ResourceException e = new ResourceException(l10nMsg + poolInfo);
            e.initCause(ne);
            throw e;
        }
    } else {
        resourcePrincipal = prin;
    }
    final Subject defaultSubject = ConnectionPoolObjectsUtils.createSubject(mcf, resourcePrincipal);
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("using subject: " + defaultSubject);
    }
    return defaultSubject;
}
Also used : ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) NamingException(javax.naming.NamingException) ResourceException(javax.resource.ResourceException) Subject(javax.security.auth.Subject)

Example 8 with ResourcePrincipal

use of com.sun.enterprise.deployment.ResourcePrincipal in project Payara by payara.

the class ConnectionManagerImpl method allocateConnection.

public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo, String jndiNameToUse, Object conn) throws ResourceException {
    validateResourceAndPool();
    PoolManager poolmgr = ConnectorRuntime.getRuntime().getPoolManager();
    boolean resourceShareable = true;
    ResourceReferenceDescriptor ref = poolmgr.getResourceReference(jndiNameToUse, logicalName);
    if (ref != null) {
        String shareableStr = ref.getSharingScope();
        if (shareableStr.equals(ref.RESOURCE_UNSHAREABLE)) {
            resourceShareable = false;
        }
    }
    // TODO V3 refactor all the 3 cases viz, no res-ref, app-auth, cont-auth.
    if (ref == null) {
        if (getLogger().isLoggable(Level.FINE)) {
            getLogger().log(Level.FINE, "poolmgr.no_resource_reference", jndiNameToUse);
        }
        return internalGetConnection(mcf, defaultPrin, cxRequestInfo, resourceShareable, jndiNameToUse, conn, true);
    }
    String auth = ref.getAuthorization();
    if (auth.equals(ResourceReferenceDescriptor.APPLICATION_AUTHORIZATION)) {
        if (cxRequestInfo == null) {
            String msg = getLocalStrings().getString("con_mgr.null_userpass");
            throw new ResourceException(msg);
        }
        ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolInfo);
        return internalGetConnection(mcf, null, cxRequestInfo, resourceShareable, jndiNameToUse, conn, false);
    } else {
        ResourcePrincipal prin = null;
        Set principalSet = null;
        Principal callerPrincipal = null;
        SecurityContext securityContext = null;
        ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
        // TODO V3 is SecurityContext.getCurrent() the right way ? Does it need to be injected ?
        if (connectorRuntime.isServer() && (securityContext = SecurityContext.getCurrent()) != null && (callerPrincipal = securityContext.getCallerPrincipal()) != null && (principalSet = securityContext.getPrincipalSet()) != null) {
            AuthenticationService authService = connectorRuntime.getAuthenticationService(rarName, poolInfo);
            if (authService != null) {
                prin = (ResourcePrincipal) authService.mapPrincipal(callerPrincipal, principalSet);
            }
        }
        if (prin == null) {
            prin = ref.getResourcePrincipal();
            if (prin == null) {
                if (getLogger().isLoggable(Level.FINE)) {
                    getLogger().log(Level.FINE, "default-resource-principal not" + "specified for " + jndiNameToUse + ". Defaulting to" + " user/password specified in the pool");
                }
                prin = defaultPrin;
            } else if (!prin.equals(defaultPrin)) {
                ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolInfo);
            }
        }
        return internalGetConnection(mcf, prin, cxRequestInfo, resourceShareable, jndiNameToUse, conn, false);
    }
}
Also used : Set(java.util.Set) SecurityContext(com.sun.enterprise.security.SecurityContext) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) ResourceException(javax.resource.ResourceException) ResourceReferenceDescriptor(com.sun.enterprise.deployment.ResourceReferenceDescriptor) PoolManager(com.sun.enterprise.resource.pool.PoolManager) AuthenticationService(com.sun.enterprise.connectors.authentication.AuthenticationService) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal)

Example 9 with ResourcePrincipal

use of com.sun.enterprise.deployment.ResourcePrincipal in project Payara by payara.

the class ASEjbMDBConnFactory method check.

public Result check(EjbDescriptor descriptor) {
    Result result = getInitializedResult();
    ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
    boolean oneFailed = false;
    boolean oneWarn = false;
    try {
        IASEjbExtraDescriptors iasEjbExtraDesc = descriptor.getIASEjbExtraDescriptors();
        MdbConnectionFactoryDescriptor mdbConnFacDesc = iasEjbExtraDesc.getMdbConnectionFactory();
        if (mdbConnFacDesc != null) {
            String jndiName = mdbConnFacDesc.getJndiName();
            if (jndiName == null || jndiName.length() == 0) {
                oneFailed = true;
                addErrorDetails(result, compName);
                result.failed(smh.getLocalString(getClass().getName() + ".failed", "FAILED [AS-EJB mdb-connection-factory] : jndi-name cannot be an empty string"));
            } else {
                if (jndiName.startsWith("jms/")) {
                    addGoodDetails(result, compName);
                    result.passed(smh.getLocalString(getClass().getName() + ".passed", "PASSED [AS-EJB mdb-connection-factory] : jndi-name is {0}", new Object[] { jndiName }));
                } else {
                    oneWarn = true;
                    addWarningDetails(result, compName);
                    result.warning(smh.getLocalString(getClass().getName() + ".warning", "WARNING [AS-EJB mdb-connection-factory] : jndi-name {0} should start with jms/", new Object[] { jndiName }));
                }
            }
            ResourcePrincipal defPrinci = mdbConnFacDesc.getDefaultResourcePrincipal();
            if (defPrinci != null) {
                String name = defPrinci.getName();
                if (name == null || name.length() == 0) {
                    oneFailed = true;
                    addErrorDetails(result, compName);
                    result.failed(smh.getLocalString(getClass().getName() + ".failed1", "FAILED [AS-EJB default-resource-principal] : name cannot be an empty string"));
                } else {
                    addGoodDetails(result, compName);
                    result.passed(smh.getLocalString(getClass().getName() + ".passed1", "PASSED [AS-EJB default-resource-principal] : name is {0}", new Object[] { name }));
                }
                String password = defPrinci.getPassword();
                if (password == null || password.length() == 0) {
                    addWarningDetails(result, compName);
                    result.warning(smh.getLocalString(getClass().getName() + ".warning2", "WARNING [AS-EJB default-resource-principal] : password is an empty string"));
                } else {
                    addGoodDetails(result, compName);
                    result.passed(smh.getLocalString(getClass().getName() + ".passed2", "PASSED [AS-EJB default-resource-principal] : password is  {0}", new Object[] { password }));
                }
            } else {
                addNaDetails(result, compName);
                result.notApplicable(smh.getLocalString(getClass().getName() + ".notApplicable", "NOT APPLICABLE [AS-EJB mdb-connection-factory] : default-resource-prncipal element is not defined"));
            }
        } else {
            if (descriptor instanceof EjbMessageBeanDescriptor) {
                boolean failed = false;
                int count = getCountNodeSet("sun-ejb-jar/enterprise-beans/ejb[ejb-name=\"" + descriptor.getName() + "\"]/mdb-resource-adapter");
                if (count > 0) {
                    String value = getXPathValue("sun-ejb-jar/enterprise-beans/ejb[ejb-name=\"" + descriptor.getName() + "\"]/mdb-resource-adapter/resource-adapter-mid");
                    if (value == null || value.length() == 0) {
                        failed = true;
                    }
                } else {
                    failed = true;
                }
                if (failed) {
                    EjbMessageBeanDescriptor mdbDesc = (EjbMessageBeanDescriptor) descriptor;
                    if (mdbDesc.hasTopicDest() && mdbDesc.hasDurableSubscription()) {
                        oneFailed = true;
                        addErrorDetails(result, compName);
                        result.failed(smh.getLocalString(getClass().getName() + ".failed2", "FAILED [AS-EJB ejb] : mdb-connection-factory has to be defined for an MDB with destination-type " + "as Topic and subscription-durability as Durable"));
                    }
                /**
                 *                        else{
                 *                            oneWarn = true;
                 *                            result.warning(smh.getLocalString(getClass().getName()+".warning1",
                 *                                 "WARNING [AS-EJB ejb] : mdb-connection-factory should be defined for a Message Driven Bean"));
                 *                        }
                 */
                }
            } else {
                addNaDetails(result, compName);
                result.notApplicable(smh.getLocalString(getClass().getName() + ".notApplicable1", "NOT APPLICABLE [AS-EJB ejb] : mdb-connection-factory element is not defined"));
            }
        }
        if (oneFailed)
            result.setStatus(Result.FAILED);
        else if (oneWarn)
            result.setStatus(Result.WARNING);
    } catch (Exception ex) {
        addErrorDetails(result, compName);
        result.failed(smh.getLocalString(getClass().getName() + ".notRun", "NOT RUN [AS-EJB cmp] Could not create descriptor Object."));
    }
    return result;
}
Also used : EjbMessageBeanDescriptor(org.glassfish.ejb.deployment.descriptor.EjbMessageBeanDescriptor) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) IASEjbExtraDescriptors(org.glassfish.ejb.deployment.descriptor.runtime.IASEjbExtraDescriptors) MdbConnectionFactoryDescriptor(org.glassfish.ejb.deployment.descriptor.runtime.MdbConnectionFactoryDescriptor) ComponentNameConstructor(com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor) Result(com.sun.enterprise.tools.verifier.Result)

Example 10 with ResourcePrincipal

use of com.sun.enterprise.deployment.ResourcePrincipal in project Payara by payara.

the class ConnectorConnectionPoolAdminServiceImpl method getConnection.

/**
 * Get a sql connection from the DataSource specified by the jdbcJndiName.
 * This API is intended to be used in the DAS. The motivation for having this
 * API is to provide the CMP backend a means of acquiring a connection during
 * the codegen phase. If a user is trying to deploy an app on a remote server,
 * without this API, a resource reference has to be present both in the DAS
 * and the server instance. This makes the deployment more complex for the
 * user since a resource needs to be forcibly created in the DAS Too.
 * This API will mitigate this need.
 *
 * @param resourceInfo the jndi name of the resource being used to get Connection from
 *                 This resource can either be a pmf resource or a jdbc resource
 * @param user     the user used to authenticate this request
 * @param password the password used to authenticate this request
 * @return a java.sql.Connection
 * @throws java.sql.SQLException in case of errors
 */
public Connection getConnection(ResourceInfo resourceInfo, String user, String password) throws SQLException {
    java.sql.Connection con = null;
    try {
        // DASResourcesUtil.setAdminConfigContext();
        PoolInfo poolInfo = getPoolNameFromResourceJndiName(resourceInfo);
        if (poolInfo == null) {
            throw new SQLException("No pool by name exists ");
        }
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine("ConnectorRuntime.getConnection :: poolName : " + poolInfo);
        }
        // Maintain consitency with the ConnectionManagerImpl change to be checked in later
        String passwd = (password == null) ? "" : password;
        // From what we have seen so far, the user cannot be null
        // but password can be
        // if user is null we will use default authentication
        // TODO: Discuss if this is the right thing to do
        ResourcePrincipal prin = (user == null) ? null : new ResourcePrincipal(user, passwd);
        con = (java.sql.Connection) getUnpooledConnection(poolInfo, prin, true);
        if (con == null) {
            String i18nMsg = localStrings.getString("ccp_adm.null_unpooled_connection");
            throw new SQLException(i18nMsg);
        }
    } catch (ResourceException re) {
        SQLException sqle = new SQLException(re.getMessage());
        sqle.initCause(re);
        _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine(" getConnection in ConnectorRuntime failed : " + re);
        }
        throw sqle;
    } catch (Exception ex) {
        SQLException sqle = new SQLException(ex.getMessage());
        sqle.initCause(ex);
        _logger.log(Level.WARNING, "jdbc.exc_get_conn", ex.getMessage());
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine(" getConnection in ConnectorRuntime failed : " + ex);
        }
        throw sqle;
    }
    return con;
}
Also used : Connection(java.sql.Connection) SQLException(java.sql.SQLException) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ResourceException(javax.resource.ResourceException) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)

Aggregations

ResourcePrincipal (com.sun.enterprise.deployment.ResourcePrincipal)13 NamingException (javax.naming.NamingException)7 ResourceException (javax.resource.ResourceException)6 Subject (javax.security.auth.Subject)5 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)4 ManagedConnectionFactory (javax.resource.spi.ManagedConnectionFactory)4 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)3 Result (com.sun.enterprise.tools.verifier.Result)3 ComponentNameConstructor (com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor)3 Principal (java.security.Principal)3 Set (java.util.Set)3 ManagedConnection (javax.resource.spi.ManagedConnection)3 PoolInfo (org.glassfish.resourcebase.resources.api.PoolInfo)3 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)2 ConnectorSecurityMap (com.sun.enterprise.connectors.authentication.ConnectorSecurityMap)2 RuntimeSecurityMap (com.sun.enterprise.connectors.authentication.RuntimeSecurityMap)2 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 PasswordCredential (javax.resource.spi.security.PasswordCredential)2 XAResource (javax.transaction.xa.XAResource)2