Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL extensions support for Windows auth in the case of MSSQL DB #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.6.2.jre11</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/SQLDatabaseConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.sql.*;
import java.util.Properties;

public class SQLDatabaseConnection {

// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionUrl =
args.length == 3 ? args[2] : "jdbc:sqlserver://localhost:1434;servername=localhost\\MSSQLSERVER01;database=master;integratedSecurity=true;encrypt=false;trustServerCertificate=false";

ResultSet resultSet = null;
try{
//System.loadLibrary(args[2]);
System.setProperty("java.library.path", args[0]);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(connectionUrl);
DatabaseMetaData metadata = connection.getMetaData();
Statement statement = connection.createStatement();

// Create and execute a SELECT SQL statement.
String selectSql = "SELECT TOP (1000) [lastrun]" +
" ,[cpu_busy]" +
" ,[io_busy]" +
" ,[idle]" +
" ,[pack_received]" +
" ,[pack_sent]" +
" ,[connections]" +
" ,[pack_errors]" +
" ,[total_read]" +
" ,[total_write]" +
" ,[total_errors]" +
" FROM [master].[dbo].[spt_monitor]";
resultSet = statement.executeQuery(selectSql);

// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(Integer.parseInt(args[1])));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
import com.google.common.base.Strings;
import org.slf4j.Logger;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;


public class JDBCConnectionAdapter {
Expand All @@ -23,6 +28,10 @@ public class JDBCConnectionAdapter {
private final String connUrl;
private final Map<String, String> connectionProperties;

private String winLibPath;

private boolean enableWindowsAuthentication;


private JDBCConnectionAdapter(String connStr, Map<String, String> connectionProperties) {
this.connUrl = connStr;
Expand All @@ -36,20 +45,26 @@ static JDBCConnectionAdapter create(String connUrl, Map<String, String> connecti

Connection open(String driver) throws SQLException, ClassNotFoundException {
Connection connection;
java.util.logging.Logger log = java.util.logging.Logger.getLogger("com.microsoft.sqlserver.jdbc");
log.setLevel(Level.FINE);
Class.forName(driver);

Properties properties = new Properties();

if (connectionProperties != null) {
for (String key : connectionProperties.keySet()) {
if (!Strings.isNullOrEmpty(connectionProperties.get(key)))
properties.put(key, connectionProperties.get(key));
logger.info("driver====>"+driver);
logger.info("Passed all checks for properties and attempting to connect to =====>"+ connUrl);
long timestamp1 = System.currentTimeMillis();
if(enableWindowsAuthentication){
System.setProperty("java.library.path", winLibPath);
logger.info("setting the libreary path :"+ winLibPath);
connection = DriverManager.getConnection(connUrl);
} else {
Properties properties = new Properties();
if (connectionProperties != null) {
for (String key : connectionProperties.keySet()) {
if (!Strings.isNullOrEmpty(connectionProperties.get(key)))
properties.put(key, connectionProperties.get(key));
}
}
connection = DriverManager.getConnection(connUrl, properties);
}

logger.debug("Passed all checks for properties and attempting to connect to: "+ connUrl);
long timestamp1 = System.currentTimeMillis();
connection = DriverManager.getConnection(connUrl, properties);
long timestamp2 = System.currentTimeMillis();
logger.debug("Connection received in JDBC ConnectionAdapter in :"+ (timestamp2-timestamp1)+ " ms");

Expand All @@ -67,4 +82,12 @@ void closeStatement(Statement statement) throws SQLException {
void closeConnection(Connection connection) throws SQLException {
connection.close();
}

public void setWinLibPath(String winLibPath) {
this.winLibPath = winLibPath;
}

public void setEnableWindowsAuthentication(boolean enableWindowsAuthentication) {
this.enableWindowsAuthentication = enableWindowsAuthentication;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/appdynamics/extensions/sql/SQLMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ private SQLMonitorTask createTask(Map<String, ?> server, TasksExecutionServicePr

Map<String, String> connectionProperties = getConnectionProperties(server);
JDBCConnectionAdapter jdbcAdapter = JDBCConnectionAdapter.create(connUrl, connectionProperties);
boolean windowsAuthentication = (System.getProperty("os.name").toLowerCase().contains("win") && connUrl.contains("integratedSecurity"));
logger.info("setting the connUrl==============================>"+ connUrl);
if(windowsAuthentication) {
jdbcAdapter.setEnableWindowsAuthentication(windowsAuthentication);
jdbcAdapter.setWinLibPath(getWinLibPath(server));
}


logger.debug("Task Created for "+server.get("displayName"));

Expand Down Expand Up @@ -108,6 +115,11 @@ private String createConnectionUrl(Map<String, ?> server) {
return url;
}

private String getWinLibPath(Map<String, ?> server) {
String url = Util.convertToString(server.get("driverDllFolderPath"), "");
return url;
}

private Map<String, String> getConnectionProperties(Map<String, ?> server) {
Map<String, String> connectionProperties = (Map<String, String>) server.get("connectionProperties");
String password = connectionProperties.get("password");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/appdynamics/extensions/sql/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static String convertToString(final Object field,final String defaultStr){
if(field == null){
return defaultStr;
}
return field.toString();
return field+"";
}


Expand Down