Skip to content

Commit

Permalink
HCK-8611: connection string parser (#116)
Browse files Browse the repository at this point in the history
* fix: connection string parser

* feat: improved connection string detection of mssql format

* fix: connection string parser

* fix: aligned improvements between plugins

* fix: unified helpers
  • Loading branch information
chulanovskyi-bs authored Oct 31, 2024
1 parent 991c86d commit 374e600
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions reverse_engineering/helpers/parseConnectionString.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { URL } = require('url');
const { ConnectionPool } = require('mssql');
const { URL } = require('url');

const mssqlPrefix = 'mssql://';
const sqlserverPrefix = 'jdbc:sqlserver://';
Expand All @@ -8,21 +8,21 @@ const sqlserverPrefix = 'jdbc:sqlserver://';
const parseMssqlUrl = ({ url = '' }) => {
const parsed = new URL(url);
return {
database: parsed.pathname.slice(1),
host: parsed.hostname,
port: parsed.port ? Number(parsed.port) : null,
databaseName: parsed.pathname.slice(1),
userName: parsed.username,
userPassword: parsed.password,
};
};

// example: jdbc:sqlserver://synapseworkspace.sql.azuresynapse.net:1433;databaseName=SampleDB;[email protected];password=myStrongPassword123;encrypt=true;trustServerCertificate=false;authentication=ActiveDirectoryPassword;loginTimeout=30;
const parseSqlServerUrl = ({ url = '' }) => {
const [_protocol, params] = url.split(sqlserverPrefix);
const [server, ...paramParts] = params.split(';');
const [_protocol, urlParams] = url.split(sqlserverPrefix);
const [server, ...paramParts] = urlParams.split(';');
const [host, port] = server.split(':');

const parsedParams = paramParts.reduce((acc, part) => {
const params = paramParts.reduce((acc, part) => {
const [key, value] = part.split('=');
if (key && value) {
acc[key] = value;
Expand All @@ -31,33 +31,34 @@ const parseSqlServerUrl = ({ url = '' }) => {
}, {});

return {
server: host,
host,
port: port ? Number(port) : null,
database: parsedParams.databaseName,
user: parsedParams.user,
password: parsedParams.password,
databaseName: params.databaseName,
userName: params.user,
userPassword: params.password,
};
};

// example: Server=tcp:synapseworkspace.sql.azuresynapse.net,1433;Database=SampleDB;Authentication=Active Directory Password;User [email protected];Password=password;Encrypt=true;TrustServerCertificate=false;Connection Timeout=30;
const parseBasicString = ({ string = '' }) => {
const parsed = ConnectionPool.parseConnectionString(string);
return {
databaseName: parsed.database,
host: parsed.server,
port: parsed.port,
userName: parsed.user,
userPassword: parsed.password,
};
};

// Default connection string example:
// Server=host,1433;Database=DatabaseName;User Id=username;Password=password;
const parseConnectionString = ({ string = '' }) => {
let params;
if (string.startsWith(sqlserverPrefix)) {
params = parseSqlServerUrl({ url: string });
return parseSqlServerUrl({ url: string });
} else if (string.startsWith(mssqlPrefix)) {
params = parseMssqlUrl({ url: string });
return parseMssqlUrl({ url: string });
} else {
params = ConnectionPool.parseConnectionString(string);
return parseBasicString({ string });
}

return {
databaseName: params.database,
host: params.server,
port: params.port,
userName: params.user,
userPassword: params.password,
};
};

module.exports = {
Expand Down

0 comments on commit 374e600

Please sign in to comment.