Search in sources :

Example 6 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.impl.jdbc.JDBCObjectStore) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource) Test(org.testng.annotations.Test)

Example 7 with PoolableDataSource

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

the class JDBCObjectStoreTest method testGetConnectionReadOnly.

@Test
public void testGetConnectionReadOnly() throws SQLException {
    PoolableDataSource mockDataSrc = Mockito.mock(PoolableDataSource.class);
    Connection mockConn = Mockito.mock(Connection.class);
    Mockito.doReturn(mockConn).when(mockDataSrc).getConnection();
    JDBCObjectStore store = new JDBCObjectStore(null, mockDataSrc);
    assertNotNull(store.getConnection(true, false));
}
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)

Example 8 with PoolableDataSource

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

the class AWSObjectStoreFactory method create.

@Override
public ObjectStore create(PrivateKeyStore keyStore) {
    rdsUser = System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_USER);
    rdsMaster = System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_MASTER_INSTANCE);
    rdsReplica = System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_REPLICA_INSTANCE);
    rdsPort = Integer.parseInt(System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_MASTER_PORT, "3306"));
    final String rdsEngine = System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_ENGINE, "mysql");
    final String rdsDatabase = System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_DATABASE, "zms_server");
    final String jdbcMasterStore = String.format("jdbc:%s://%s:%d/%s", rdsEngine, rdsMaster, rdsPort, rdsDatabase);
    final String rdsMasterToken = getAuthToken(rdsMaster, rdsPort, rdsUser);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Connecting to master {} with auth token {}", jdbcMasterStore, rdsMasterToken);
    }
    setConnectionProperties(mysqlMasterConnectionProperties, rdsMasterToken);
    PoolableDataSource dataMasterSource = DataSourceFactory.create(jdbcMasterStore, mysqlMasterConnectionProperties);
    // now check to see if we also have a read-only replica jdbc store configured
    PoolableDataSource dataReplicaSource = null;
    if (rdsReplica != null) {
        final String jdbcReplicaStore = String.format("jdbc:%s://%s:%d/%s", rdsEngine, rdsReplica, rdsPort, rdsDatabase);
        final String rdsReplicaToken = getAuthToken(rdsReplica, rdsPort, rdsUser);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Connecting to replica {} with auth token {}", jdbcReplicaStore, rdsReplicaToken);
        }
        setConnectionProperties(mysqlReplicaConnectionProperties, rdsReplicaToken);
        dataReplicaSource = DataSourceFactory.create(jdbcReplicaStore, mysqlReplicaConnectionProperties);
    }
    // start our credentials refresh task
    long credsRefreshTime = Integer.parseInt(System.getProperty(ZMSConsts.ZMS_PROP_AWS_RDS_CREDS_REFRESH_TIME, "300"));
    scheduledThreadPool = Executors.newScheduledThreadPool(1);
    scheduledThreadPool.scheduleAtFixedRate(new CredentialsUpdater(), credsRefreshTime, credsRefreshTime, TimeUnit.SECONDS);
    return new JDBCObjectStore(dataMasterSource, dataReplicaSource);
}
Also used : JDBCObjectStore(com.yahoo.athenz.zms.store.impl.jdbc.JDBCObjectStore) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource)

Example 9 with PoolableDataSource

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

the class JDBCObjectStoreFactory method create.

@Override
public ObjectStore create(PrivateKeyStore keyStore) {
    final String jdbcStore = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RW_STORE);
    final String jdbcUser = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RW_USER);
    final String password = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RW_PASSWORD, "");
    final String jdbcAppName = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_APP_NAME, JDBC_APP_NAME);
    String jdbcPassword = keyStore.getApplicationSecret(jdbcAppName, password);
    Properties readWriteProperties = getProperties(jdbcUser, jdbcPassword);
    PoolableDataSource readWriteSrc = DataSourceFactory.create(jdbcStore, readWriteProperties);
    // now check to see if we also have a read-only jdbc store configured
    // if no username and password are specified then we'll use the
    // read-write store credentials
    PoolableDataSource readOnlySrc = null;
    String jdbcReadOnlyStore = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RO_STORE);
    if (jdbcReadOnlyStore != null && jdbcReadOnlyStore.startsWith(JDBC_APP_NAME)) {
        final String jdbcReadOnlyUser = getDefaultSetting(ZMSConsts.ZMS_PROP_JDBC_RO_USER, jdbcUser);
        final String readOnlyPassword = getDefaultSetting(ZMSConsts.ZMS_PROP_JDBC_RO_PASSWORD, password);
        final String jdbcReadOnlyPassword = keyStore.getApplicationSecret(jdbcAppName, readOnlyPassword);
        Properties readOnlyProperties = getProperties(jdbcReadOnlyUser, jdbcReadOnlyPassword);
        readOnlySrc = DataSourceFactory.create(jdbcReadOnlyStore, readOnlyProperties);
    }
    return new JDBCObjectStore(readWriteSrc, readOnlySrc);
}
Also used : JDBCObjectStore(com.yahoo.athenz.zms.store.impl.jdbc.JDBCObjectStore) Properties(java.util.Properties) PoolableDataSource(com.yahoo.athenz.common.server.db.PoolableDataSource)

Example 10 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);
        jdbcConn.setTagLimit(domainTagsLimit, roleTagsLimit, groupTagsLimit);
        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)

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