JDBCObjectStoreFactory implements ObjectStoreFactory { @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); String jdbcPassword = keyStore.getApplicationSecret(jdbcAppName, password); Properties readWriteProperties = new Properties(); readWriteProperties.setProperty(ZMSConsts.DB_PROP_USER, jdbcUser); readWriteProperties.setProperty(ZMSConsts.DB_PROP_PASSWORD, jdbcPassword); readWriteProperties.setProperty(ZMSConsts.DB_PROP_VERIFY_SERVER_CERT, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_VERIFY_SERVER_CERT, "false")); readWriteProperties.setProperty(ZMSConsts.DB_PROP_USE_SSL, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_USE_SSL, "false")); readWriteProperties.setProperty(ZMSConsts.DB_PROP_TLS_PROTOCOLS, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_TLS_VERSIONS, "TLSv1.2,TLSv1.3")); PoolableDataSource readWriteSrc = DataSourceFactory.create(jdbcStore, readWriteProperties); PoolableDataSource readOnlySrc = null; String jdbcReadOnlyStore = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RO_STORE); if (jdbcReadOnlyStore != null && jdbcReadOnlyStore.startsWith("jdbc:")) { final String jdbcReadOnlyUser = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RO_USER, jdbcUser); final String readOnlyPassword = System.getProperty(ZMSConsts.ZMS_PROP_JDBC_RO_PASSWORD, password); final String jdbcReadOnlyPassword = keyStore.getApplicationSecret(jdbcAppName, readOnlyPassword); Properties readOnlyProperties = new Properties(); readOnlyProperties.setProperty(ZMSConsts.DB_PROP_USER, jdbcReadOnlyUser); readOnlyProperties.setProperty(ZMSConsts.DB_PROP_PASSWORD, jdbcReadOnlyPassword); readOnlyProperties.setProperty(ZMSConsts.DB_PROP_VERIFY_SERVER_CERT, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_VERIFY_SERVER_CERT, "false")); readOnlyProperties.setProperty(ZMSConsts.DB_PROP_USE_SSL, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_USE_SSL, "false")); readOnlyProperties.setProperty(ZMSConsts.DB_PROP_TLS_PROTOCOLS, System.getProperty(ZMSConsts.ZMS_PROP_JDBC_TLS_VERSIONS, "TLSv1.2,TLSv1.3")); readOnlySrc = DataSourceFactory.create(jdbcReadOnlyStore, readOnlyProperties); } return new JDBCObjectStore(readWriteSrc, readOnlySrc); }  @Override ObjectStore create(PrivateKeyStore keyStore);  }
@Test public void testCreateWriteOnly() { System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_STORE, "jdbc:mysql: System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_USER, "user"); System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_PASSWORD, "password"); System.clearProperty(ZMSConsts.ZMS_PROP_JDBC_RO_STORE); System.clearProperty(ZMSConsts.ZMS_PROP_JDBC_RO_USER); System.clearProperty(ZMSConsts.ZMS_PROP_JDBC_RO_PASSWORD); PrivateKeyStore keyStore = Mockito.mock(PrivateKeyStore.class); Mockito.doReturn("password").when(keyStore).getApplicationSecret("jdbc", "password"); JDBCObjectStoreFactory factory = new JDBCObjectStoreFactory(); ObjectStore store = factory.create(keyStore); assertNotNull(store); }
@Test public void testCreateReadWrite() { System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_STORE, "jdbc:mysql: System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_USER, "user"); System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RW_PASSWORD, "password"); System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RO_STORE, "jdbc:mysql: System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RO_USER, "user"); System.setProperty(ZMSConsts.ZMS_PROP_JDBC_RO_PASSWORD, "password"); PrivateKeyStore keyStore = Mockito.mock(PrivateKeyStore.class); Mockito.doReturn("password").when(keyStore).getApplicationSecret("jdbc", "password"); JDBCObjectStoreFactory factory = new JDBCObjectStoreFactory(); ObjectStore store = factory.create(keyStore); assertNotNull(store); }