Search in sources :

Example 6 with JdbcConfig

use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.

the class HikariDataSourceFactoryTest method createWithEncryptedDatabasePassword.

@Test
public void createWithEncryptedDatabasePassword() {
    String username = "junit";
    String encryptedPassword = // unencrypted value = "dbpassword"
    "ENC(rJ70hNidkrpkTwHoVn2sGSp3h3uBWxjb)";
    String url = "jdbc:h2:mem:";
    JdbcConfig jdbcConfig = mock(JdbcConfig.class);
    when(jdbcConfig.getUsername()).thenReturn(username);
    when(jdbcConfig.getPassword()).thenReturn(encryptedPassword);
    when(jdbcConfig.getUrl()).thenReturn(url);
    InputStream sysInReset = System.in;
    ByteArrayInputStream in = new ByteArrayInputStream("quorum".getBytes());
    System.setIn(in);
    DataSource dataSource = dataSourceFactory.create(jdbcConfig);
    System.setIn(sysInReset);
    assertThat(dataSource).isNotNull().isExactlyInstanceOf(HikariDataSource.class);
    HikariDataSource hikariDataSource = HikariDataSource.class.cast(dataSource);
    assertThat(hikariDataSource.getJdbcUrl()).isEqualTo(url);
    assertThat(hikariDataSource.getUsername()).isEqualTo(username);
    assertThat(hikariDataSource.getPassword()).isEqualTo("dbpassword");
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JdbcConfig(com.quorum.tessera.config.JdbcConfig) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 7 with JdbcConfig

use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.

the class EncryptedRawTransactionDAOProviderTest method provides.

@Test
public void provides() {
    try (var mockedConfigFactory = mockStatic(ConfigFactory.class);
        var mockedDataSourceFactory = mockStatic(DataSourceFactory.class);
        var mockedPersistence = mockStatic(Persistence.class)) {
        mockedPersistence.when(() -> Persistence.createEntityManagerFactory(anyString(), anyMap())).thenReturn(mock(EntityManagerFactory.class));
        Config config = mock(Config.class);
        JdbcConfig jdbcConfig = mock(JdbcConfig.class);
        when(jdbcConfig.isAutoCreateTables()).thenReturn(autocreateTables);
        when(config.getJdbcConfig()).thenReturn(jdbcConfig);
        ConfigFactory configFactory = mock(ConfigFactory.class);
        when(configFactory.getConfig()).thenReturn(config);
        mockedConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
        mockedDataSourceFactory.when(DataSourceFactory::create).thenReturn(mock(DataSourceFactory.class));
        EncryptedRawTransactionDAO result = EncryptedRawTransactionDAOProvider.provider();
        assertThat(result).isNotNull().isExactlyInstanceOf(EncryptedRawTransactionDAOImpl.class);
        mockedPersistence.verify(() -> Persistence.createEntityManagerFactory(anyString(), anyMap()));
        mockedPersistence.verifyNoMoreInteractions();
        EncryptedTransactionDAOProvider.provider();
    }
}
Also used : DataSourceFactory(com.quorum.tessera.data.DataSourceFactory) JdbcConfig(com.quorum.tessera.config.JdbcConfig) Config(com.quorum.tessera.config.Config) EncryptedRawTransactionDAO(com.quorum.tessera.data.EncryptedRawTransactionDAO) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JdbcConfig(com.quorum.tessera.config.JdbcConfig) ConfigFactory(com.quorum.tessera.config.ConfigFactory) Test(org.junit.Test)

Example 8 with JdbcConfig

use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.

the class MigrationTest method beforeTest.

@Before
public void beforeTest() throws IOException {
    Config primaryConfig = new Config();
    primaryConfig.setJdbcConfig(new JdbcConfig());
    primaryConfig.getJdbcConfig().setUsername("junit");
    primaryConfig.getJdbcConfig().setPassword("junit");
    String primaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("primary.db").toString();
    primaryConfig.getJdbcConfig().setUrl(primaryJdbcUrl);
    Config secondaryConfig = new Config();
    secondaryConfig.setJdbcConfig(new JdbcConfig());
    secondaryConfig.getJdbcConfig().setUsername("junit");
    secondaryConfig.getJdbcConfig().setPassword("junit");
    String secondaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("secondary.db").toString();
    secondaryConfig.getJdbcConfig().setUrl(secondaryJdbcUrl);
    primaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("primary-confg.json");
    try (OutputStream outputStream = Files.newOutputStream(primaryConfigPath)) {
        JaxbUtil.marshalWithNoValidation(primaryConfig, outputStream);
    }
    secondaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("secondary-confg.json");
    try (OutputStream outputStream = Files.newOutputStream(secondaryConfigPath)) {
        JaxbUtil.marshalWithNoValidation(secondaryConfig, outputStream);
    }
    args = List.of("--primary", primaryConfigPath.toString(), "--secondary", secondaryConfigPath.toString());
    primaryEntityManagerFactory = Optional.of(primaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
        p.put("jakarta.persistence.schema-generation.database.action", "drop-and-create");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
        emf.createEntityManager();
        return emf;
    }).get();
    secondaryEntityManagerFactory = Optional.of(secondaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
        p.put("jakarta.persistence.schema-generation.database.action", "create");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
        return emf;
    }).get();
    EntityManager secondaryEntityManager = secondaryEntityManagerFactory.createEntityManager();
    secondaryEntityManager.getTransaction().begin();
    IntStream.range(0, encryptedTransactionCount).forEach(i -> {
        EncryptedTransaction encryptedTransaction = generateEncryptedTransaction();
        secondaryEntityManager.persist(encryptedTransaction);
    });
    secondaryEntityManager.getTransaction().commit();
    secondaryEntityManager.getTransaction().begin();
    IntStream.range(0, encryptedRawTransactionCount).forEach(i -> {
        EncryptedRawTransaction encryptedRawTransaction = generateEncryptedRawTransaction();
        secondaryEntityManager.persist(encryptedRawTransaction);
    });
    secondaryEntityManager.getTransaction().commit();
}
Also used : IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) JaxbUtil(com.quorum.tessera.config.util.JaxbUtil) After(org.junit.After) MessageHash(com.quorum.tessera.data.MessageHash) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) CommandLine(picocli.CommandLine) Before(org.junit.Before) OutputStream(java.io.OutputStream) EncryptedRawTransaction(com.quorum.tessera.data.EncryptedRawTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Files(java.nio.file.Files) JdbcConfig(com.quorum.tessera.config.JdbcConfig) IOException(java.io.IOException) Test(org.junit.Test) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Rule(org.junit.Rule) Persistence(jakarta.persistence.Persistence) ConfigConverter(com.quorum.tessera.cli.parsers.ConfigConverter) EntityManager(jakarta.persistence.EntityManager) Config(com.quorum.tessera.config.Config) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) CliType(com.quorum.tessera.cli.CliType) TemporaryFolder(org.junit.rules.TemporaryFolder) EntityManager(jakarta.persistence.EntityManager) JdbcConfig(com.quorum.tessera.config.JdbcConfig) Config(com.quorum.tessera.config.Config) OutputStream(java.io.OutputStream) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JdbcConfig(com.quorum.tessera.config.JdbcConfig) EncryptedRawTransaction(com.quorum.tessera.data.EncryptedRawTransaction) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Before(org.junit.Before)

Aggregations

JdbcConfig (com.quorum.tessera.config.JdbcConfig)8 Test (org.junit.Test)8 Config (com.quorum.tessera.config.Config)6 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)5 ConfigFactory (com.quorum.tessera.config.ConfigFactory)4 DataSourceFactory (com.quorum.tessera.data.DataSourceFactory)4 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 DataSource (javax.sql.DataSource)2 CliType (com.quorum.tessera.cli.CliType)1 ConfigConverter (com.quorum.tessera.cli.parsers.ConfigConverter)1 JaxbUtil (com.quorum.tessera.config.util.JaxbUtil)1 EncryptedRawTransaction (com.quorum.tessera.data.EncryptedRawTransaction)1 EncryptedRawTransactionDAO (com.quorum.tessera.data.EncryptedRawTransactionDAO)1 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)1 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)1 MessageHash (com.quorum.tessera.data.MessageHash)1 PrivacyGroupDAO (com.quorum.tessera.data.PrivacyGroupDAO)1 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)1 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)1 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)1