Search in sources :

Example 11 with PasswordCredential

use of javax.resource.spi.security.PasswordCredential in project Payara by payara.

the class DSManagedConnectionFactory method createManagedConnection.

/**
 * Creates a new physical connection to the underlying EIS resource
 * manager.
 *
 * @param subject       <code>Subject</code> instance passed by the application server
 * @param cxRequestInfo <code>ConnectionRequestInfo</code> which may be created
 *                      as a result of the invocation <code>getConnection(user, password)</code>
 *                      on the <code>DataSource</code> object
 * @return <code>ManagedConnection</code> object created
 * @throws ResourceException           if there is an error in instantiating the
 *                                     <code>DataSource</code> object used for the
 *                                     creation of the <code>ManagedConnection</code> object
 * @throws SecurityException           if there ino <code>PasswordCredential</code> object
 *                                     satisfying this request
 * @throws ResourceAllocationException if there is an error in allocating the
 *                                     physical connection
 */
@Override
public javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
    logFine("In createManagedConnection");
    PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
    javax.sql.DataSource dataSource = getDataSource();
    java.sql.Connection dsConn = null;
    ManagedConnectionImpl mc = null;
    ClassLoader appClassLoader = Utility.getClassLoader();
    // do not propagate application class loader to the database driver
    // may cause memory leaks in embedded databases
    Utility.setContextClassLoader(null);
    try {
        /* For the case where the user/passwd of the connection pool is
            * equal to the PasswordCredential for the connection request
            * get a connection from this pool directly.
            * for all other conditions go create a new connection
            */
        String user = getUser();
        if (user == null || isEqual(pc, user, getPassword())) {
            dsConn = AccessController.doPrivileged((PrivilegedExceptionAction<java.sql.Connection>) dataSource::getConnection);
        } else {
            dsConn = AccessController.doPrivileged((PrivilegedExceptionAction<java.sql.Connection>) () -> dataSource.getConnection(pc.getUserName(), new String(pc.getPassword())));
        }
    } catch (PrivilegedActionException ex) {
        java.sql.SQLException sqle = (SQLException) ex.getCause();
        // _logger.log(Level.WARNING, "jdbc.exc_create_conn", sqle.getMessage());
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "jdbc.exc_create_conn", sqle.getMessage());
        }
        StringManager localStrings = StringManager.getManager(DataSourceObjectBuilder.class);
        String msg = localStrings.getString("jdbc.cannot_allocate_connection", sqle.getMessage());
        ResourceAllocationException rae = new ResourceAllocationException(msg);
        rae.initCause(sqle);
        throw rae;
    } finally {
        Utility.setContextClassLoader(appClassLoader);
    }
    try {
        mc = constructManagedConnection(null, dsConn, pc, this);
        // GJCINT
        validateAndSetIsolation(mc);
    } finally {
        if (mc == null) {
            if (dsConn != null) {
                try {
                    dsConn.close();
                } catch (SQLException e) {
                    _logger.log(Level.FINEST, "Exception while closing connection : " + "createManagedConnection" + dsConn);
                }
            }
        }
    }
    return mc;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) SQLException(java.sql.SQLException) PasswordCredential(javax.resource.spi.security.PasswordCredential) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) StringManager(com.sun.enterprise.util.i18n.StringManager) DataSourceObjectBuilder(com.sun.gjc.common.DataSourceObjectBuilder) ResourceAllocationException(javax.resource.spi.ResourceAllocationException)

Example 12 with PasswordCredential

use of javax.resource.spi.security.PasswordCredential in project Payara by payara.

the class SecurityUtils method getPasswordCredential.

/**
 * This method returns the <code>PasswordCredential</code> object, given
 * the <code>ManagedConnectionFactory</code>, subject and the
 * <code>ConnectionRequestInfo</code>. It first checks if the
 * <code>ConnectionRequestInfo</code> is null or not. If it is not null,
 * it constructs a <code>PasswordCredential</code> object with
 * the user and password fields from the <code>ConnectionRequestInfo</code> and returns this
 * <code>PasswordCredential</code> object. If the <code>ConnectionRequestInfo</code>
 * is null, it retrieves the <code>PasswordCredential</code> objects from
 * the <code>Subject</code> parameter and returns the first
 * <code>PasswordCredential</code> object which contains a
 * <code>ManagedConnectionFactory</code>, instance equivalent
 * to the <code>ManagedConnectionFactory</code>, parameter.
 *
 * @param mcf     <code>ManagedConnectionFactory</code>
 * @param subject <code>Subject</code>
 * @param info    <code>ConnectionRequestInfo</code>
 * @return <code>PasswordCredential</code>
 * @throws <code>ResourceException</code> generic exception if operation fails
 * @throws <code>SecurityException</code> if access to the <code>Subject</code> instance is denied
 */
public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf, final Subject subject, javax.resource.spi.ConnectionRequestInfo info) throws ResourceException {
    if (info == null) {
        if (subject == null) {
            return null;
        } else {
            PasswordCredential pc = (PasswordCredential) AccessController.doPrivileged(new PrivilegedAction() {

                public Object run() {
                    Set passwdCredentialSet = subject.getPrivateCredentials(PasswordCredential.class);
                    Iterator iter = passwdCredentialSet.iterator();
                    while (iter.hasNext()) {
                        PasswordCredential temp = (PasswordCredential) iter.next();
                        if (temp.getManagedConnectionFactory().equals(mcf)) {
                            return temp;
                        }
                    }
                    return null;
                }
            });
            if (pc == null) {
                String msg = sm.getString("su.no_passwd_cred");
                throw new javax.resource.spi.SecurityException(msg);
            } else {
                return pc;
            }
        }
    } else {
        ConnectionRequestInfoImpl cxReqInfo = (ConnectionRequestInfoImpl) info;
        PasswordCredential pc = new PasswordCredential(cxReqInfo.getUser(), cxReqInfo.getPassword());
        pc.setManagedConnectionFactory(mcf);
        return pc;
    }
}
Also used : ConnectionRequestInfoImpl(com.sun.gjc.spi.ConnectionRequestInfoImpl) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) PasswordCredential(javax.resource.spi.security.PasswordCredential) Iterator(java.util.Iterator)

Example 13 with PasswordCredential

use of javax.resource.spi.security.PasswordCredential in project Payara by payara.

the class ConnectionPoolObjectsUtils method createSubject.

public static Subject createSubject(ManagedConnectionFactory mcf, final ResourcePrincipal prin) {
    final Subject tempSubject = new Subject();
    if (prin != null) {
        String password = prin.getPassword();
        if (password != null) {
            final PasswordCredential pc = new PasswordCredential(prin.getName(), password.toCharArray());
            pc.setManagedConnectionFactory(mcf);
            PrivilegedAction<Void> action = () -> {
                tempSubject.getPrincipals().add(prin);
                tempSubject.getPrivateCredentials().add(pc);
                return null;
            };
            AccessController.doPrivileged(action);
        }
    }
    return tempSubject;
}
Also used : PasswordCredential(javax.resource.spi.security.PasswordCredential) Subject(javax.security.auth.Subject)

Example 14 with PasswordCredential

use of javax.resource.spi.security.PasswordCredential in project cxf by apache.

the class ManagedConnectionImplTest method testGetConnectionWithDudSubjectB.

@Test
public void testGetConnectionWithDudSubjectB() throws ResourceException {
    String user = new String("user");
    char[] password = { 'a', 'b', 'c' };
    PasswordCredential creds = new PasswordCredential(user, password);
    subj.getPrivateCredentials().add(creds);
    Object o = mci.getConnection(subj, cri);
    verifyProxyInterceptors(o);
}
Also used : PasswordCredential(javax.resource.spi.security.PasswordCredential) Test(org.junit.Test)

Example 15 with PasswordCredential

use of javax.resource.spi.security.PasswordCredential in project jaybird by FirebirdSQL.

the class FBManagedConnection method getCombinedConnectionRequestInfo.

private FBConnectionRequestInfo getCombinedConnectionRequestInfo(Subject subject, ConnectionRequestInfo cri) throws ResourceException {
    if (cri == null) {
        cri = mcf.getDefaultConnectionRequestInfo();
    }
    try {
        FBConnectionRequestInfo fbcri = (FBConnectionRequestInfo) cri;
        if (subject != null) {
            // ManagedConnectionFactory, option A.
            for (Object cred : subject.getPrivateCredentials()) {
                if (cred instanceof PasswordCredential && mcf.equals(((PasswordCredential) cred).getManagedConnectionFactory())) {
                    PasswordCredential pcred = (PasswordCredential) cred;
                    String user = pcred.getUserName();
                    String password = new String(pcred.getPassword());
                    fbcri.setPassword(password);
                    fbcri.setUserName(user);
                    break;
                }
            }
        }
        return fbcri;
    } catch (ClassCastException cce) {
        throw new FBResourceException("Incorrect ConnectionRequestInfo class supplied");
    }
}
Also used : PasswordCredential(javax.resource.spi.security.PasswordCredential)

Aggregations

PasswordCredential (javax.resource.spi.security.PasswordCredential)16 DataSourceObjectBuilder (com.sun.gjc.common.DataSourceObjectBuilder)4 SQLException (java.sql.SQLException)4 ResourceException (javax.resource.ResourceException)4 Subject (javax.security.auth.Subject)4 StringManager (com.sun.enterprise.util.i18n.StringManager)3 Principal (java.security.Principal)3 ResourceAllocationException (javax.resource.spi.ResourceAllocationException)3 ResourcePrincipal (com.sun.enterprise.deployment.ResourcePrincipal)2 Properties (java.util.Properties)2 NamingException (javax.naming.NamingException)2 ManagedConnection (javax.resource.spi.ManagedConnection)2 ManagedConnectionFactory (javax.resource.spi.ManagedConnectionFactory)2 XAResource (javax.transaction.xa.XAResource)2 PoolInfo (org.glassfish.resourcebase.resources.api.PoolInfo)2 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)2 NamePrincipal (org.wildfly.security.auth.principal.NamePrincipal)2 ConnectorRuntime (com.sun.appserv.connectors.internal.api.ConnectorRuntime)1 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)1 Config (com.sun.enterprise.config.serverbeans.Config)1