fix: passed original host and added context comment

This commit is contained in:
Sheen Capadngan
2025-09-10 20:47:14 +08:00
parent a0595fa9b4
commit 7ea0cc6cb7

View File

@@ -151,15 +151,26 @@ export const SqlDatabaseProvider = ({ gatewayService }: TSqlDatabaseProviderDTO)
return { ...providerInputs, hostIp }; return { ...providerInputs, hostIp };
}; };
const $getClient = async (providerInputs: z.infer<typeof DynamicSecretSqlDBSchema> & { hostIp: string }) => { const $getClient = async (
providerInputs: z.infer<typeof DynamicSecretSqlDBSchema> & { hostIp: string; originalHost: string }
) => {
const ssl = providerInputs.ca const ssl = providerInputs.ca
? { rejectUnauthorized: false, ca: providerInputs.ca, servername: providerInputs.host } ? { rejectUnauthorized: false, ca: providerInputs.ca, servername: providerInputs.host }
: undefined; : undefined;
const isMsSQLClient = providerInputs.client === SqlProviders.MsSQL; const isMsSQLClient = providerInputs.client === SqlProviders.MsSQL;
const isAzureSql = isMsSQLClient && new RE2(/\.database\.windows\.net$/i).test(providerInputs.host);
const azureServerLabel = isAzureSql ? providerInputs.host.split(".")[0] : undefined;
/*
We route through the gateway by setting connection.host = "localhost".
Azure SQL identifies the logical server from the TDS login name when the host
isn’t the Azure FQDN. Therefore, when using the gateway, ensure username is
"user@<azure-server-name>" so Azure opens the correct logical server.
Direct connections to the Azure FQDN usually don’t require this suffix.
*/
const isGatewayForwardedTraffic = providerInputs.host === "localhost";
const isAzureSql = isMsSQLClient && new RE2(/\.database\.windows\.net$/i).test(providerInputs.originalHost);
const azureServerLabel =
isAzureSql && isGatewayForwardedTraffic ? providerInputs.originalHost?.split(".")[0] : undefined;
const effectiveUser = const effectiveUser =
isAzureSql && !providerInputs.username.includes("@") isAzureSql && !providerInputs.username.includes("@")
? `${providerInputs.username}@${azureServerLabel}` ? `${providerInputs.username}@${azureServerLabel}`
@@ -226,7 +237,8 @@ export const SqlDatabaseProvider = ({ gatewayService }: TSqlDatabaseProviderDTO)
...providerInputs, ...providerInputs,
port, port,
host, host,
hostIp: providerInputs.hostIp hostIp: providerInputs.hostIp,
originalHost: providerInputs.host
}); });
// oracle needs from keyword // oracle needs from keyword
const testStatement = providerInputs.client === SqlProviders.Oracle ? "SELECT 1 FROM DUAL" : "SELECT 1"; const testStatement = providerInputs.client === SqlProviders.Oracle ? "SELECT 1 FROM DUAL" : "SELECT 1";
@@ -271,7 +283,8 @@ export const SqlDatabaseProvider = ({ gatewayService }: TSqlDatabaseProviderDTO)
const db = await $getClient({ const db = await $getClient({
...providerInputs, ...providerInputs,
port, port,
host host,
originalHost: providerInputs.host
}); });
try { try {
const expiration = new Date(expireAt).toISOString(); const expiration = new Date(expireAt).toISOString();
@@ -318,7 +331,8 @@ export const SqlDatabaseProvider = ({ gatewayService }: TSqlDatabaseProviderDTO)
const db = await $getClient({ const db = await $getClient({
...providerInputs, ...providerInputs,
port, port,
host host,
originalHost: providerInputs.host
}); });
try { try {
const revokeStatement = handlebars.compile(providerInputs.revocationStatement)({ username, database }); const revokeStatement = handlebars.compile(providerInputs.revocationStatement)({ username, database });
@@ -357,7 +371,8 @@ export const SqlDatabaseProvider = ({ gatewayService }: TSqlDatabaseProviderDTO)
const db = await $getClient({ const db = await $getClient({
...providerInputs, ...providerInputs,
port, port,
host host,
originalHost: providerInputs.host
}); });
const expiration = new Date(expireAt).toISOString(); const expiration = new Date(expireAt).toISOString();
const { database } = providerInputs; const { database } = providerInputs;