Search in sources :

Example 1 with IdentityState

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState in project legend-engine by finos.

the class DataSourceSpecification method getConnectionUsingIdentity.

public Connection getConnectionUsingIdentity(Identity identity, Optional<CredentialSupplier> databaseCredentialSupplierHolder) {
    Optional<LegendKerberosCredential> kerberosCredentialHolder = identity.getCredential(LegendKerberosCredential.class);
    Supplier<DataSource> dataSourceBuilder;
    if (kerberosCredentialHolder.isPresent()) {
        dataSourceBuilder = () -> KerberosUtils.doAs(identity, (PrivilegedAction<DataSource>) () -> this.buildDataSource(identity));
    } else {
        dataSourceBuilder = () -> this.buildDataSource(identity);
    }
    return getConnection(new IdentityState(identity, databaseCredentialSupplierHolder), dataSourceBuilder);
}
Also used : LegendKerberosCredential(org.finos.legend.engine.shared.core.identity.credential.LegendKerberosCredential) PrivilegedAction(java.security.PrivilegedAction) IdentityState(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState) DataSource(javax.sql.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource)

Example 2 with IdentityState

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState in project legend-engine by finos.

the class UserNamePasswordAuthenticationStrategy method handleConnection.

@Override
public Pair<String, Properties> handleConnection(String url, Properties properties, DatabaseManager databaseManager) {
    Properties connectionProperties = new Properties();
    connectionProperties.putAll(properties);
    IdentityState identityState = ConnectionStateManager.getInstance().getIdentityStateUsing(properties);
    PlaintextUserPasswordCredential credential = (PlaintextUserPasswordCredential) getDatabaseCredential(identityState);
    connectionProperties.put("user", credential.getUser());
    connectionProperties.put("password", credential.getPassword());
    return Tuples.pair(url, connectionProperties);
}
Also used : Properties(java.util.Properties) IdentityState(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState) PlaintextUserPasswordCredential(org.finos.legend.engine.shared.core.identity.credential.PlaintextUserPasswordCredential)

Example 3 with IdentityState

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState in project legend-engine by finos.

the class TestConnectionState method connectionStateCreation.

@Test
public void connectionStateCreation() throws Exception {
    Identity identity = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser1");
    // User gets connection to db1
    RelationalDatabaseConnection database1 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db1");
    this.connectionManagerSelector.getDatabaseConnection(identity, database1);
    // verify connection state for user1 exists
    String poolName = String.format("DBPool_Static_host:127.0.0.1_port:%d_db:db1_type:TestDB_testuser1_org.finos.legend.engine.shared.core.identity.credential.AnonymousCredential", server.getPort());
    IdentityState identityState = ConnectionStateManager.getInstance().getConnectionStateManagerPOJO(poolName);
    assertEquals("testuser1", identityState.getIdentity().getName());
    assertNotNull(identityState.getCredentialSupplier());
}
Also used : RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) Identity(org.finos.legend.engine.shared.core.identity.Identity) IdentityState(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState) Test(org.junit.Test)

Example 4 with IdentityState

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState in project legend-engine by finos.

the class TestConnectionState method connectionStateUpdate.

@Test
public void connectionStateUpdate() throws Exception {
    Identity identity = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser1");
    // User gets connection to db1
    RelationalDatabaseConnection database1 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db1");
    this.connectionManagerSelector.getDatabaseConnection(identity, database1);
    // verify connection state for user1 exists
    String poolName = String.format("DBPool_Static_host:127.0.0.1_port:%d_db:db1_type:TestDB_testuser1_org.finos.legend.engine.shared.core.identity.credential.AnonymousCredential", server.getPort());
    DataSourceWithStatistics identityState1 = ConnectionStateManager.getInstance().getDataSourceByPoolName(poolName);
    assertEquals("testuser1", identityState1.getIdentity().getName());
    assertNotNull(identityState1.getCredentialSupplier());
    // User gets another connection to db1
    this.connectionManagerSelector.getDatabaseConnection(identity, database1);
    // verify connection state for user1 exists
    IdentityState identityState2 = ConnectionStateManager.getInstance().getConnectionStateManagerPOJO(poolName);
    assertEquals("testuser1", identityState2.getIdentity().getName());
    assertNotNull(identityState2.getCredentialSupplier());
    // Verify connection state has been reset
    assertNotSame("expected distinct state objects but got the same one", identityState1, identityState2);
}
Also used : RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) DataSourceWithStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics) Identity(org.finos.legend.engine.shared.core.identity.Identity) IdentityState(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState) Test(org.junit.Test)

Example 5 with IdentityState

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState in project legend-engine by finos.

the class TestConnectionState method connectionStateReset.

@Test
public void connectionStateReset() throws Exception {
    Identity identity = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser1");
    // User gets connection to db1
    RelationalDatabaseConnection database1 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db1");
    Connection connection = this.connectionManagerSelector.getDatabaseConnection(identity, database1);
    // verify connection state for user1 exists
    String poolName = String.format("DBPool_Static_host:127.0.0.1_port:%d_db:db1_type:TestDB_testuser1_org.finos.legend.engine.shared.core.identity.credential.AnonymousCredential", server.getPort());
    IdentityState identityState1 = ConnectionStateManager.getInstance().getConnectionStateManagerPOJO(poolName);
    assertEquals("testuser1", identityState1.getIdentity().getName());
    assertNotNull(identityState1.getCredentialSupplier());
    connection.close();
    // Reset connection state - This simulates a case where the state manager evicts state objects
    ConnectionStateManager.getInstance().evictUnusedPoolsOlderThan(Duration.ofMillis(1));
    IdentityState identityState = ConnectionStateManager.getInstance().getConnectionStateManagerPOJO(poolName);
    assertNull(identityState);
    // User gets another connection to db1
    this.connectionManagerSelector.getDatabaseConnection(identity, database1);
    // Verify new connection state has been created for user
    IdentityState identityState2 = ConnectionStateManager.getInstance().getConnectionStateManagerPOJO(poolName);
    assertEquals("testuser1", identityState2.getIdentity().getName());
    assertNotNull(identityState2.getCredentialSupplier());
    assertNotSame("expected distinct state objects but got the same one", identityState1, identityState2);
}
Also used : RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) Connection(java.sql.Connection) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) Identity(org.finos.legend.engine.shared.core.identity.Identity) IdentityState(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState) Test(org.junit.Test)

Aggregations

IdentityState (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState)5 RelationalDatabaseConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection)3 Identity (org.finos.legend.engine.shared.core.identity.Identity)3 Test (org.junit.Test)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 PrivilegedAction (java.security.PrivilegedAction)1 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 DataSource (javax.sql.DataSource)1 DataSourceWithStatistics (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics)1 LegendKerberosCredential (org.finos.legend.engine.shared.core.identity.credential.LegendKerberosCredential)1 PlaintextUserPasswordCredential (org.finos.legend.engine.shared.core.identity.credential.PlaintextUserPasswordCredential)1