Skip to content

Commit

Permalink
Merge pull request #6 from Bit-Quill/alinaliBQ/AD-521/expose_local_port
Browse files Browse the repository at this point in the history
[AD-521] get SSH port from JDBC
  • Loading branch information
alinaliBQ authored Feb 9, 2022
2 parents 40f639b + 701e790 commit 1e44ab6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
76 changes: 76 additions & 0 deletions src/odbc-test/src/java_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,82 @@ BOOST_AUTO_TEST_CASE(TestDriverManagerGetConnection)
connection = SharedPointer< GlobalJObject >(nullptr);
}

BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetSshTunnelPort) {
PrepareContext();
BOOST_REQUIRE(_ctx.Get() != nullptr);

// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
bool success = _ctx.Get()->DriverManagerGetConnection(_jdbcConnectionString.c_str(), connection, errInfo);
if (!success || errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
BOOST_REQUIRE(connection.Get());
AutoCloseConnection autoCloseConnection(_ctx, connection);

// see if SSH tunnel is active
bool isActive;
success = _ctx.Get()->DocumentDbConnectionIsSshTunnelActive(connection, isActive, errInfo);
// if tunnel is not shown as active, or operation not successful, BOOST FAIL
if (!success || errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
BOOST_CHECK(isActive);

// SSH tunnel confirmed to be active, get SSH tunnel local port
int32_t port;
success = _ctx.Get()->DocumentDbConnectionGetSshLocalPort(connection, port, errInfo);
if (!success || errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
std::string errMsg = errInfo.errMsg;
BOOST_FAIL(errMsg);
}

// if connection successful, port should be a positive number
BOOST_CHECK(port > 0);
}

// TODO Enable when we can get external SSH tunnel working
BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetSshTunnelPortSshTunnelNotActive, * disabled()) {
//BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetSshTunnelPortSshTunnelNotActive) {
// test when SSH tunnel is not active, the SSH tunnel port should be 0
// TODO do things so SSH tunnel is not active, but connection is open
PrepareContext();
BOOST_REQUIRE(_ctx.Get() != nullptr);

// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
bool success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
if (!success || errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
BOOST_REQUIRE(connection.Get());
AutoCloseConnection autoCloseConnection(_ctx, connection);

// check if SSH tunnel is not active
bool isActive;
success = _ctx.Get()->DocumentDbConnectionIsSshTunnelActive(connection, isActive, errInfo);
// if SSH tunnel is active, or operation not successful, BOOST FAIL
if (isActive || !success || errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}

BOOST_CHECK(!isActive);

// SSH tunnel confirmed to be not active, get SSH tunnel local port
int32_t port;
success = _ctx.Get()->DocumentDbConnectionGetSshLocalPort(connection, port, errInfo);
if (errInfo.code != odbc::java::IGNITE_JNI_ERR_SUCCESS) {
std::string errMsg = errInfo.errMsg;
BOOST_FAIL(errMsg);
}

// if SSH tunnel not active, SSH local port number should be 0
BOOST_CHECK_EQUAL(port, 0);
}

BOOST_AUTO_TEST_CASE(TestConnectionGetMetaData) {
PrepareContext();
BOOST_REQUIRE(_ctx.Get() != nullptr);
Expand Down
7 changes: 6 additions & 1 deletion src/odbc/include/ignite/odbc/jni/java.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ namespace ignite {
jmethodID m_DocumentDbConnectionPropertiesGetPropertiesFromConnectionString;

jclass c_DocumentDbConnection;
jmethodID m_DocumentDbConnectionGetSshLocalPort;
jmethodID m_DocumentDbConnectionIsSshTunnelActive;
jmethodID m_DocumentDbConnectionInit;
jmethodID m_DocumentDbClose;

Expand Down Expand Up @@ -448,6 +450,9 @@ namespace ignite {
void ConnectionClose(const SharedPointer< GlobalJObject >& connection, JniErrorInfo& errInfo);
bool ConnectionGetMetaData(const SharedPointer< GlobalJObject >& connection, SharedPointer< GlobalJObject>& databaseMetaData, JniErrorInfo& errInfo);

bool DocumentDbConnectionIsSshTunnelActive(const SharedPointer< GlobalJObject >& connection, bool& isActive, JniErrorInfo& errInfo);
bool DocumentDbConnectionGetSshLocalPort(const SharedPointer< GlobalJObject >& connection, int32_t& result, JniErrorInfo& errInfo);

bool DatabaseMetaDataGetTables(
const SharedPointer< GlobalJObject >& databaseMetaData,
const std::string& catalog,
Expand Down Expand Up @@ -699,5 +704,5 @@ namespace ignite {
} // namespace jni
} // namespace odbc
} // namespace ignite

#endif //_IGNITE_ODBC_JNI_JAVA

45 changes: 44 additions & 1 deletion src/odbc/src/jni/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ namespace ignite
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)Ljava/sql/ResultSet;",
false);

const char* const C_DOCUMENTDB_CONNECTION = "software/amazon/documentdb/jdbc/DocumentDbConnectionProperties";
const char* const C_DOCUMENTDB_CONNECTION = "software/amazon/documentdb/jdbc/DocumentDbConnection";
JniMethod const M_DOCUMENTDB_CONNECTION_GET_SSH_LOCAL_PORT =
JniMethod("getSshLocalPort", "()I", false);
JniMethod const M_DOCUMENTDB_CONNECTION_IS_SSH_TUNNEL_ACTIVE =
JniMethod("isSshTunnelActive", "()Z", false);

const char* const C_DRIVERMANAGER = "java/sql/DriverManager";
JniMethod const M_DRIVERMANAGER_GET_CONNECTION =
Expand Down Expand Up @@ -487,6 +491,8 @@ namespace ignite

c_DocumentDbConnection = FindClass(env, C_DOCUMENTDB_CONNECTION);
//m_DocumentDbConnectionInit = FindMethod(env, c_DocumentDbConnection, M_DOCUMENTDB_CONNECTION_PROPERTIES_INIT);
m_DocumentDbConnectionGetSshLocalPort = FindMethod(env, c_DocumentDbConnection, M_DOCUMENTDB_CONNECTION_GET_SSH_LOCAL_PORT);
m_DocumentDbConnectionIsSshTunnelActive = FindMethod(env, c_DocumentDbConnection, M_DOCUMENTDB_CONNECTION_IS_SSH_TUNNEL_ACTIVE);

c_DriverManager = FindClass(env, C_DRIVERMANAGER);
m_DriverManagerGetConnection = FindMethod(env, c_DriverManager, M_DRIVERMANAGER_GET_CONNECTION);
Expand Down Expand Up @@ -822,6 +828,43 @@ namespace ignite
ExceptionCheck(env, &errInfo);
}

bool JniContext::DocumentDbConnectionIsSshTunnelActive(
const SharedPointer< GlobalJObject >& connection,
bool& isActive,
JniErrorInfo& errInfo) {
if (!connection.Get()) {
errInfo.code = IGNITE_JNI_ERR_GENERIC;
errInfo.errMsg = "Connection object must be set.";
return false;
}
JNIEnv* env = Attach();
jboolean res = env->CallBooleanMethod(
connection.Get()->GetRef(),
jvm->GetMembers().m_DocumentDbConnectionIsSshTunnelActive);
ExceptionCheck(env, &errInfo);
if (errInfo.code == IGNITE_JNI_ERR_SUCCESS) {
isActive = res != JNI_FALSE;
}
return errInfo.code == IGNITE_JNI_ERR_SUCCESS;
}

bool JniContext::DocumentDbConnectionGetSshLocalPort(
const SharedPointer< GlobalJObject >& connection,
int32_t& result,
JniErrorInfo& errInfo) {
if (!connection.Get()) {
errInfo.code = IGNITE_JNI_ERR_GENERIC;
errInfo.errMsg = "Connection object must be set.";
return false;
}
JNIEnv* env = Attach();
result = env->CallIntMethod(
connection.Get()->GetRef(),
jvm->GetMembers().m_DocumentDbConnectionGetSshLocalPort);
ExceptionCheck(env, &errInfo);
return errInfo.code == IGNITE_JNI_ERR_SUCCESS;
}

bool JniContext::ConnectionGetMetaData(
const SharedPointer< GlobalJObject >& connection,
SharedPointer< GlobalJObject >& databaseMetaData,
Expand Down

0 comments on commit 1e44ab6

Please sign in to comment.