Search in sources :

Example 1 with PoolableDataSource

use of com.yahoo.athenz.common.server.db.PoolableDataSource in project athenz by yahoo.

the class JDBCObjectStoreTest method testGetConnectionReadWriteOnly.

@Test
public void testGetConnectionReadWriteOnly() throws SQLException {
    PoolableDataSource mockDataSrc = Mockito.mock(PoolableDataSource.class);
    Connection mockConn = Mockito.mock(Connection.class);
    Mockito.doReturn(mockConn).when(mockDataSrc).getConnection();
    JDBCObjectStore store = new JDBCObjectStore(mockDataSrc, null);
    store.setOperationTimeout(60);
    assertNotNull(store.getConnection(true, true));
    // without read store we should also get a connection for a read
    // only operation
    assertNotNull(store.getConnection(true, true));
    store.clearConnections();
}
Also used : Connection(java.sql.Connection) JDBCObjectStore(com.yahoo.athenz.zms.store.jdbc.JDBCObjectStore) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource) Test(org.testng.annotations.Test)

Example 2 with PoolableDataSource

use of com.yahoo.athenz.common.server.db.PoolableDataSource in project athenz by yahoo.

the class JDBCObjectStore method getConnection.

@Override
public ObjectStoreConnection getConnection(boolean autoCommit, boolean readWrite) {
    final String caller = "getConnection";
    try {
        PoolableDataSource src = readWrite ? rwSrc : roSrc;
        JDBCConnection jdbcConn = new JDBCConnection(src.getConnection(), autoCommit);
        jdbcConn.setOperationTimeout(opTimeout);
        return jdbcConn;
    } catch (Exception ex) {
        if (!readWrite) {
            return getConnection(autoCommit, true);
        }
        throw ZMSUtils.error(ResourceException.SERVICE_UNAVAILABLE, ex.getMessage(), caller);
    }
}
Also used : PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource) ResourceException(com.yahoo.athenz.zms.ResourceException)

Example 3 with PoolableDataSource

use of com.yahoo.athenz.common.server.db.PoolableDataSource in project athenz by yahoo.

the class DataSourceFactoryTest method testCreateDataSourceWithFactoryInvalidTTL.

@Test
public void testCreateDataSourceWithFactoryInvalidTTL() {
    System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL, "abc");
    // we ignore the invalid ttl and everything else should work fine
    MockConnectionFactory connectionFactory = new MockConnectionFactory();
    PoolableDataSource src = DataSourceFactory.create(connectionFactory);
    assertNotNull(src);
    System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL);
}
Also used : PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource) Test(org.testng.annotations.Test)

Example 4 with PoolableDataSource

use of com.yahoo.athenz.common.server.db.PoolableDataSource in project athenz by yahoo.

the class AWSCertRecordStoreFactory method create.

@Override
public CertRecordStore create(PrivateKeyStore keyStore) {
    rdsUser = System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_USER);
    rdsIamRole = System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_IAM_ROLE);
    rdsMaster = System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_MASTER_INSTANCE);
    rdsPort = Integer.parseInt(System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_MASTER_PORT, "3306"));
    final String rdsEngine = System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_ENGINE, "mysql");
    final String rdsDatabase = System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_DATABASE, "zts_store");
    final String jdbcStore = String.format("jdbc:%s://%s:%d/%s", rdsEngine, rdsMaster, rdsPort, rdsDatabase);
    String rdsToken = getAuthToken(rdsMaster, rdsPort, rdsUser, rdsIamRole);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Connecting to {} with auth token {}", jdbcStore, rdsToken);
    }
    mysqlConnectionProperties.setProperty(ZTSConsts.DB_PROP_VERIFY_SERVER_CERT, System.getProperty(ZTSConsts.ZTS_PROP_CERT_JDBC_VERIFY_SERVER_CERT, "true"));
    mysqlConnectionProperties.setProperty(ZTSConsts.DB_PROP_USE_SSL, System.getProperty(ZTSConsts.ZTS_PROP_CERT_JDBC_USE_SSL, "true"));
    mysqlConnectionProperties.setProperty(ZTSConsts.DB_PROP_USER, rdsUser);
    mysqlConnectionProperties.setProperty(ZTSConsts.DB_PROP_PASSWORD, rdsToken);
    PoolableDataSource dataSource = DataSourceFactory.create(jdbcStore, mysqlConnectionProperties);
    long credsRefreshTime = Integer.parseInt(System.getProperty(ZTSConsts.ZTS_PROP_AWS_RDS_CREDS_REFRESH_TIME, "300"));
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
    scheduledThreadPool.scheduleAtFixedRate(new CredentialsUpdater(), credsRefreshTime, credsRefreshTime, TimeUnit.SECONDS);
    return new JDBCCertRecordStore(dataSource);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource)

Example 5 with PoolableDataSource

use of com.yahoo.athenz.common.server.db.PoolableDataSource in project athenz by yahoo.

the class JDBCObjectStoreTest method testGetConnection.

@Test
public void testGetConnection() throws SQLException {
    PoolableDataSource mockDataRwSrc = Mockito.mock(PoolableDataSource.class);
    Connection rwMockConn = Mockito.mock(Connection.class);
    Mockito.doReturn(rwMockConn).when(mockDataRwSrc).getConnection();
    PoolableDataSource mockDataRoSrc = Mockito.mock(PoolableDataSource.class);
    Connection roMockConn = Mockito.mock(Connection.class);
    Mockito.doReturn(roMockConn).when(mockDataRoSrc).getConnection();
    JDBCObjectStore store = new JDBCObjectStore(mockDataRwSrc, mockDataRoSrc);
    JDBCConnection jdbcConn = (JDBCConnection) store.getConnection(true, true);
    assertEquals(jdbcConn.con, rwMockConn);
    jdbcConn = (JDBCConnection) store.getConnection(true, false);
    assertEquals(jdbcConn.con, roMockConn);
}
Also used : Connection(java.sql.Connection) JDBCObjectStore(com.yahoo.athenz.zms.store.impl.jdbc.JDBCObjectStore) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource) Test(org.testng.annotations.Test)

Aggregations

PoolableDataSource (com.yahoo.athenz.common.server.db.PoolableDataSource)30 Test (org.testng.annotations.Test)22 Connection (java.sql.Connection)11 JDBCObjectStore (com.yahoo.athenz.zms.store.impl.jdbc.JDBCObjectStore)7 SQLException (java.sql.SQLException)7 JDBCObjectStore (com.yahoo.athenz.zms.store.jdbc.JDBCObjectStore)5 Properties (java.util.Properties)4 Principal (com.yahoo.athenz.auth.Principal)2 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)2 RolesProvider (com.yahoo.athenz.common.server.db.RolesProvider)2 NotificationManager (com.yahoo.athenz.common.server.notification.NotificationManager)2 ResourceException (com.yahoo.athenz.zms.ResourceException)2 File (java.io.File)1 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1