Skip to content

Commit

Permalink
connman: remove connection provider
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Nov 15, 2023
1 parent ae24f73 commit 1012c23
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
import org.finos.legend.connection.Connection;
import org.finos.legend.connection.ConnectionFactory;
import org.finos.legend.connection.DatabaseSupport;
import org.finos.legend.connection.DatabaseType;
import org.finos.legend.connection.IdentityFactory;
import org.finos.legend.connection.IdentitySpecification;
import org.finos.legend.connection.LegendEnvironment;
import org.finos.legend.connection.impl.CoreAuthenticationMechanismType;
import org.finos.legend.connection.impl.InstrumentedConnectionProvider;
import org.finos.legend.connection.impl.KerberosCredentialExtractor;
import org.finos.legend.connection.impl.KeyPairCredentialBuilder;
import org.finos.legend.connection.impl.RelationalDatabaseType;
import org.finos.legend.connection.impl.SnowflakeConnectionBuilder;
import org.finos.legend.connection.impl.StaticJDBCConnectionBuilder;
import org.finos.legend.connection.impl.UserPasswordCredentialBuilder;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.AuthenticationConfiguration;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.ConnectionSpecification;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.EncryptedPrivateKeyPairAuthenticationConfiguration;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.UserPasswordAuthenticationConfiguration;
import org.finos.legend.engine.shared.core.identity.Identity;
Expand All @@ -43,11 +44,8 @@

public abstract class AbstractConnectionFactoryTest<T>
{
protected static final String TEST_CONNECTION_IDENTIFIER = "test::connection";

protected LegendEnvironment environment;
protected IdentityFactory identityFactory;
protected InstrumentedConnectionProvider connectionProvider;
protected ConnectionFactory connectionFactory;

@BeforeEach
Expand Down Expand Up @@ -95,10 +93,8 @@ public void initialize()
.environment(this.environment)
.build();

this.connectionProvider = new InstrumentedConnectionProvider();
this.connectionFactory = ConnectionFactory.builder()
.environment(this.environment)
.connectionProvider(this.connectionProvider)
.credentialBuilders(
new KerberosCredentialExtractor(),
new UserPasswordCredentialBuilder(),
Expand Down Expand Up @@ -126,10 +122,12 @@ public CredentialVault getCredentialVault()
return null;
}

public abstract Connection getConnection(AuthenticationConfiguration authenticationConfiguration);

public abstract Identity getIdentity();

public abstract DatabaseType getDatabaseType();

public abstract ConnectionSpecification getConnectionSpecification();

public abstract AuthenticationConfiguration getAuthenticationConfiguration();

public abstract void runTestWithConnection(T connection) throws Exception;
Expand All @@ -138,10 +136,18 @@ public CredentialVault getCredentialVault()
public void runTest() throws Exception
{
Identity identity = this.getIdentity();
DatabaseType databaseType = this.getDatabaseType();
ConnectionSpecification connectionSpecification = this.getConnectionSpecification();
AuthenticationConfiguration authenticationConfiguration = this.getAuthenticationConfiguration();
this.connectionProvider.injectConnection(this.getConnection(authenticationConfiguration));

Authenticator authenticator = this.connectionFactory.getAuthenticator(identity, TEST_CONNECTION_IDENTIFIER);
Connection databaseConnection = Connection.builder()
.databaseSupport(this.environment.getDatabaseSupport(databaseType))
.identifier("test::connection")
.connectionSpecification(connectionSpecification)
.authenticationConfiguration(authenticationConfiguration)
.build();

Authenticator authenticator = this.connectionFactory.getAuthenticator(identity, databaseConnection);
T connection = this.connectionFactory.getConnection(identity, authenticator);

this.runTestWithConnection(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.finos.legend.connection.DatabaseSupport;
import org.finos.legend.connection.IdentityFactory;
import org.finos.legend.connection.IdentitySpecification;
import org.finos.legend.connection.JDBCConnectionBuilder;
import org.finos.legend.connection.LegendEnvironment;
import org.finos.legend.connection.PostgresTestContainerWrapper;
import org.finos.legend.connection.impl.CoreAuthenticationMechanismType;
import org.finos.legend.connection.impl.InstrumentedConnectionProvider;
import org.finos.legend.connection.impl.JDBCConnectionBuilder;
import org.finos.legend.connection.impl.JDBCConnectionManager;
import org.finos.legend.connection.impl.RelationalDatabaseType;
import org.finos.legend.connection.impl.StaticJDBCConnectionBuilder;
Expand All @@ -49,11 +48,9 @@
public class TestJDBCConnectionManager
{
PostgresTestContainerWrapper postgresContainer;
private static final String TEST_CONNECTION_IDENTIFIER = "test::connection";

private LegendEnvironment environment;
private IdentityFactory identityFactory;
private InstrumentedConnectionProvider connectionProvider;
private ConnectionFactory connectionFactory;
private Connection connection;

Expand Down Expand Up @@ -84,15 +81,14 @@ public void setup()
this.identityFactory = IdentityFactory.builder()
.environment(this.environment)
.build();
this.connectionProvider = new InstrumentedConnectionProvider();
ConnectionSpecification connectionSpecification = new StaticJDBCConnectionSpecification(
this.postgresContainer.getHost(),
this.postgresContainer.getPort(),
this.postgresContainer.getDatabaseName()
);
this.connection = Connection.builder()
.databaseSupport(this.environment.getDatabaseSupport(RelationalDatabaseType.POSTGRES))
.identifier(TEST_CONNECTION_IDENTIFIER)
.identifier("test::connection")
.connectionSpecification(connectionSpecification)
.authenticationConfiguration(new UserPasswordAuthenticationConfiguration(
postgresContainer.getUser(),
Expand Down Expand Up @@ -121,15 +117,13 @@ public void testBasicConnectionPooling() throws Exception
);
this.connectionFactory = ConnectionFactory.builder()
.environment(this.environment)
.connectionProvider(this.connectionProvider)
.credentialBuilders(
new UserPasswordCredentialBuilder()
)
.connectionBuilders(
customizedJDBCConnectionBuilder
)
.build();
this.connectionProvider.injectConnection(this.connection);
Identity identity = identityFactory.createIdentity(
IdentitySpecification.builder()
.name("test-user")
Expand All @@ -138,7 +132,7 @@ public void testBasicConnectionPooling() throws Exception
ConnectionSpecification connectionSpecification = this.connection.getConnectionSpecification();
AuthenticationConfiguration authenticationConfiguration = this.connection.getAuthenticationConfiguration();

Authenticator authenticator = this.connectionFactory.getAuthenticator(identity, TEST_CONNECTION_IDENTIFIER);
Authenticator authenticator = this.connectionFactory.getAuthenticator(identity, this.connection);

JDBCConnectionManager connectionManager = JDBCConnectionManager.getInstance();
Assertions.assertEquals(0, connectionManager.getPoolSize());
Expand Down Expand Up @@ -187,15 +181,13 @@ public void testConnectionPoolingForDifferentIdentities() throws Exception
{
this.connectionFactory = ConnectionFactory.builder()
.environment(this.environment)
.connectionProvider(this.connectionProvider)
.credentialBuilders(
new UserPasswordCredentialBuilder()
)
.connectionBuilders(
new StaticJDBCConnectionBuilder.WithPlaintextUsernamePassword()
)
.build();
this.connectionProvider.injectConnection(this.connection);
Identity identity1 = identityFactory.createIdentity(
IdentitySpecification.builder()
.name("testUser1")
Expand All @@ -213,7 +205,7 @@ public void testConnectionPoolingForDifferentIdentities() throws Exception
Assertions.assertEquals(0, connectionManager.getPoolSize());

// 1. Get a new connection for identity1, which should initialize a pool
this.connectionFactory.getConnection(identity1, this.connectionFactory.getAuthenticator(identity1, TEST_CONNECTION_IDENTIFIER));
this.connectionFactory.getConnection(identity1, this.connectionFactory.getAuthenticator(identity1, this.connection));

String poolName1 = JDBCConnectionManager.getPoolName(identity1, connectionSpecification, authenticationConfiguration);
JDBCConnectionManager.ConnectionPool connectionPool1 = connectionManager.getPool(poolName1);
Expand All @@ -224,7 +216,7 @@ public void testConnectionPoolingForDifferentIdentities() throws Exception
Assertions.assertEquals(0, connectionPool1.getIdleConnections());

// 2. Get a new connection for identity2, which should initialize another pool
this.connectionFactory.getConnection(identity2, this.connectionFactory.getAuthenticator(identity2, TEST_CONNECTION_IDENTIFIER));
this.connectionFactory.getConnection(identity2, this.connectionFactory.getAuthenticator(identity2, this.connection));

String poolName2 = JDBCConnectionManager.getPoolName(identity2, connectionSpecification, authenticationConfiguration);
JDBCConnectionManager.ConnectionPool connectionPool2 = connectionManager.getPool(poolName2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

import org.finos.legend.authentication.vault.CredentialVault;
import org.finos.legend.authentication.vault.impl.PropertiesFileCredentialVault;
import org.finos.legend.connection.AuthenticationMechanism;
import org.finos.legend.connection.impl.CoreAuthenticationMechanismType;
import org.finos.legend.connection.DatabaseType;
import org.finos.legend.connection.PostgresTestContainerWrapper;
import org.finos.legend.connection.Connection;
import org.finos.legend.connection.impl.RelationalDatabaseType;
import org.finos.legend.engine.protocol.pure.v1.model.connection.StaticJDBCConnectionSpecification;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.authentication.vault.PropertiesFileSecret;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.AuthenticationConfiguration;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.ConnectionSpecification;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.UserPasswordAuthenticationConfiguration;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.authentication.vault.PropertiesFileSecret;
import org.finos.legend.engine.shared.core.identity.Identity;

import java.sql.Statement;
Expand Down Expand Up @@ -71,29 +69,25 @@ public CredentialVault getCredentialVault()
}

@Override
public Connection getConnection(AuthenticationConfiguration authenticationConfiguration)
public Identity getIdentity()
{
ConnectionSpecification connectionSpecification = new StaticJDBCConnectionSpecification(
this.postgresContainer.getHost(),
this.postgresContainer.getPort(),
this.postgresContainer.getDatabaseName()
);
return Connection.builder()
.databaseSupport(this.environment.getDatabaseSupport(RelationalDatabaseType.POSTGRES))
.identifier(TEST_CONNECTION_IDENTIFIER)
.authenticationMechanisms(
AuthenticationMechanism.builder()
.type(CoreAuthenticationMechanismType.USER_PASSWORD).build()
)
.connectionSpecification(connectionSpecification)
.authenticationConfiguration(authenticationConfiguration)
.build();
return getAnonymousIdentity(this.identityFactory);
}

@Override
public Identity getIdentity()
public DatabaseType getDatabaseType()
{
return getAnonymousIdentity(this.identityFactory);
return RelationalDatabaseType.POSTGRES;
}

@Override
public ConnectionSpecification getConnectionSpecification()
{
return new StaticJDBCConnectionSpecification(
this.postgresContainer.getHost(),
this.postgresContainer.getPort(),
this.postgresContainer.getDatabaseName()
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

import org.finos.legend.authentication.vault.CredentialVault;
import org.finos.legend.authentication.vault.impl.PropertiesFileCredentialVault;
import org.finos.legend.connection.AuthenticationMechanism;
import org.finos.legend.connection.Connection;
import org.finos.legend.connection.impl.CoreAuthenticationMechanismType;
import org.finos.legend.connection.DatabaseType;
import org.finos.legend.connection.impl.RelationalDatabaseType;
import org.finos.legend.engine.protocol.pure.v1.connection.SnowflakeConnectionSpecification;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.authentication.vault.EnvironmentCredentialVaultSecret;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.authentication.vault.PropertiesFileSecret;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.AuthenticationConfiguration;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.ConnectionSpecification;
import org.finos.legend.engine.protocol.pure.v1.packageableElement.connection.EncryptedPrivateKeyPairAuthenticationConfiguration;
import org.finos.legend.engine.shared.core.identity.Identity;

Expand Down Expand Up @@ -71,7 +70,19 @@ public CredentialVault getCredentialVault()
}

@Override
public Connection getConnection(AuthenticationConfiguration authenticationConfiguration)
public Identity getIdentity()
{
return getAnonymousIdentity(this.identityFactory);
}

@Override
public DatabaseType getDatabaseType()
{
return RelationalDatabaseType.SNOWFLAKE;
}

@Override
public ConnectionSpecification getConnectionSpecification()
{
SnowflakeConnectionSpecification connectionSpecification = new SnowflakeConnectionSpecification();
connectionSpecification.databaseName = "SUMMIT_DEV";
Expand All @@ -80,23 +91,7 @@ public Connection getConnection(AuthenticationConfiguration authenticationConfig
connectionSpecification.region = "us-east-2";
connectionSpecification.cloudType = "aws";
connectionSpecification.role = "SUMMIT_DEV";
return Connection.builder()
.databaseSupport(this.environment.getDatabaseSupport(RelationalDatabaseType.SNOWFLAKE))
.identifier(TEST_CONNECTION_IDENTIFIER)
.authenticationMechanisms(
AuthenticationMechanism.builder()
.type(CoreAuthenticationMechanismType.KEY_PAIR)
.build()
)
.connectionSpecification(connectionSpecification)
.authenticationConfiguration(authenticationConfiguration)
.build();
}

@Override
public Identity getIdentity()
{
return getAnonymousIdentity(this.identityFactory);
return connectionSpecification;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@
import org.finos.legend.authentication.vault.impl.SystemPropertiesCredentialVault;
import org.finos.legend.connection.AuthenticationMechanism;
import org.finos.legend.connection.ConnectionFactory;
import org.finos.legend.connection.ConnectionProvider;
import org.finos.legend.connection.DatabaseSupport;
import org.finos.legend.connection.LegendEnvironment;
import org.finos.legend.connection.impl.CoreAuthenticationMechanismType;
import org.finos.legend.connection.impl.DefaultConnectionProvider;
import org.finos.legend.connection.impl.HACKY__SnowflakeConnectionAdapter;
import org.finos.legend.connection.impl.KerberosCredentialExtractor;
import org.finos.legend.connection.impl.KeyPairCredentialBuilder;
Expand Down Expand Up @@ -473,10 +471,8 @@ private ConnectionFactory setupConnectionFactory(List<VaultConfiguration> vaultC
.build()
).build();

ConnectionProvider connectionProvider = DefaultConnectionProvider.builder().build();
return ConnectionFactory.builder()
.environment(environment)
.connectionProvider(connectionProvider)
.credentialBuilders(
new KerberosCredentialExtractor(),
new UserPasswordCredentialBuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ public Builder fromProtocol(org.finos.legend.engine.protocol.pure.v1.packageable
.build())
: Lists.mutable.empty()
)
.connectionSpecification(protocol.connectionSpecification);
.connectionSpecification(protocol.connectionSpecification)
.authenticationConfiguration(protocol.authenticationConfiguration);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@

package org.finos.legend.connection;

import java.util.Collections;
import java.util.List;

public interface ConnectionExtension
{
List<DatabaseType> getExtraDatabaseTypes();
default List<DatabaseType> getExtraDatabaseTypes()
{
return Collections.emptyList();
}

List<AuthenticationMechanismType> getExtraAuthenticationMechanismTypes();
default List<AuthenticationMechanismType> getExtraAuthenticationMechanismTypes()
{
return Collections.emptyList();
}
}
Loading

0 comments on commit 1012c23

Please sign in to comment.