From 00fff6ededd17b7262636208427a8b0618052d55 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 11 Jul 2023 12:35:00 +0200 Subject: [PATCH 1/4] Add local AD option --- .../server/src/integrations/microsoftSqlServer.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 17cbf10e90..fe2222a2e0 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -28,7 +28,8 @@ const DEFAULT_SCHEMA = "dbo" import { ConfidentialClientApplication } from "@azure/msal-node" enum MSSQLConfigAuthType { - ACTIVE_DIRECTORY = "Active Directory", + AZURE_ACTIVE_DIRECTORY = "Azure Active Directory", + LOCAL_ACTIVE_DIRECTORY = "Local Active Directory", } interface MSSQLConfig { @@ -93,13 +94,18 @@ const SCHEMA: Integration = { authType: { type: DatasourceFieldType.SELECT, display: "Advanced auth", - config: { options: [MSSQLConfigAuthType.ACTIVE_DIRECTORY] }, + config: { + options: [ + MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY, + MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY, + ], + }, }, adConfig: { type: DatasourceFieldType.FIELD_GROUP, default: true, display: "Configure Active Directory", - hidden: "'{{authType}}' !== 'Active Directory'", + hidden: `'{{authType}}' !== '${MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY}'`, config: { openByDefault: true, nestedFields: true, @@ -199,7 +205,7 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { } delete clientCfg.encrypt - if (this.config.authType === MSSQLConfigAuthType.ACTIVE_DIRECTORY) { + if (this.config.authType === MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY) { const { clientId, tenantId, clientSecret } = this.config.adConfig! const clientApp = new ConfidentialClientApplication({ auth: { From c4fec6973ff15215647d790aa59fd442f7c0c3ae Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 11 Jul 2023 13:08:15 +0200 Subject: [PATCH 2/4] Add ntml config --- .../src/integrations/microsoftSqlServer.ts | 98 +++++++++++++++---- 1 file changed, 78 insertions(+), 20 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index fe2222a2e0..3b7192750b 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -27,12 +27,14 @@ const DEFAULT_SCHEMA = "dbo" import { ConfidentialClientApplication } from "@azure/msal-node" +import { utils } from "@budibase/shared-core" + enum MSSQLConfigAuthType { AZURE_ACTIVE_DIRECTORY = "Azure Active Directory", LOCAL_ACTIVE_DIRECTORY = "Local Active Directory", } -interface MSSQLConfig { +interface BasicMSSQLConfig { user: string password: string server: string @@ -41,13 +43,30 @@ interface MSSQLConfig { schema: string encrypt?: boolean authType?: MSSQLConfigAuthType - adConfig?: { +} + +interface AzureADMSSQLConfig extends BasicMSSQLConfig { + authType: MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY + adConfig: { clientId: string clientSecret: string tenantId: string } } +interface LocalADMSSQLConfig extends BasicMSSQLConfig { + authType: MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY + localADConfig: { + domain: string + trustServerCertificate: boolean + } +} + +type MSSQLConfig = + | (BasicMSSQLConfig & { authType: undefined }) + | AzureADMSSQLConfig + | LocalADMSSQLConfig + const SCHEMA: Integration = { docs: "https://github.com/tediousjs/node-mssql", plus: true, @@ -128,6 +147,28 @@ const SCHEMA: Integration = { }, }, }, + localADConfig: { + type: DatasourceFieldType.FIELD_GROUP, + default: true, + display: "Configure Local Active Directory", + hidden: `'{{authType}}' !== '${MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY}'`, + config: { + openByDefault: true, + nestedFields: true, + }, + fields: { + domain: { + type: DatasourceFieldType.STRING, + required: true, + display: "Domain", + }, + trustServerCertificate: { + type: DatasourceFieldType.BOOLEAN, + required: true, + display: "Trust server certificate", + }, + }, + }, }, query: { create: { @@ -205,26 +246,43 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { } delete clientCfg.encrypt - if (this.config.authType === MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY) { - const { clientId, tenantId, clientSecret } = this.config.adConfig! - const clientApp = new ConfidentialClientApplication({ - auth: { - clientId, - authority: `https://login.microsoftonline.com/${tenantId}`, - clientSecret, - }, - }) + switch (this.config.authType) { + case MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY: + const { clientId, tenantId, clientSecret } = this.config.adConfig + const clientApp = new ConfidentialClientApplication({ + auth: { + clientId, + authority: `https://login.microsoftonline.com/${tenantId}`, + clientSecret, + }, + }) - const response = await clientApp.acquireTokenByClientCredential({ - scopes: ["https://database.windows.net/.default"], - }) + const response = await clientApp.acquireTokenByClientCredential({ + scopes: ["https://database.windows.net/.default"], + }) - clientCfg.authentication = { - type: "azure-active-directory-access-token", - options: { - token: response!.accessToken, - }, - } + clientCfg.authentication = { + type: "azure-active-directory-access-token", + options: { + token: response!.accessToken, + }, + } + break + case MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY: + const { domain, trustServerCertificate } = this.config.localADConfig + clientCfg.authentication = { + type: "ntml", + options: { + domain, + }, + } + clientCfg.options ??= {} + clientCfg.options.trustServerCertificate = trustServerCertificate + break + case undefined: + break + default: + utils.unreachable(this.config) } const pool = new sqlServer.ConnectionPool(clientCfg) From 4280e08f225b1a9e0cec661a78cd864cd39057cc Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 11 Jul 2023 15:31:59 +0200 Subject: [PATCH 3/4] Renames --- .../src/integrations/microsoftSqlServer.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 3b7192750b..d27e069919 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -31,7 +31,7 @@ import { utils } from "@budibase/shared-core" enum MSSQLConfigAuthType { AZURE_ACTIVE_DIRECTORY = "Azure Active Directory", - LOCAL_ACTIVE_DIRECTORY = "Local Active Directory", + NTLM = "NTLM", } interface BasicMSSQLConfig { @@ -54,9 +54,9 @@ interface AzureADMSSQLConfig extends BasicMSSQLConfig { } } -interface LocalADMSSQLConfig extends BasicMSSQLConfig { - authType: MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY - localADConfig: { +interface NTLMMSSQLConfig extends BasicMSSQLConfig { + authType: MSSQLConfigAuthType.NTLM + ntlmConfig: { domain: string trustServerCertificate: boolean } @@ -65,7 +65,7 @@ interface LocalADMSSQLConfig extends BasicMSSQLConfig { type MSSQLConfig = | (BasicMSSQLConfig & { authType: undefined }) | AzureADMSSQLConfig - | LocalADMSSQLConfig + | NTLMMSSQLConfig const SCHEMA: Integration = { docs: "https://github.com/tediousjs/node-mssql", @@ -116,7 +116,7 @@ const SCHEMA: Integration = { config: { options: [ MSSQLConfigAuthType.AZURE_ACTIVE_DIRECTORY, - MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY, + MSSQLConfigAuthType.NTLM, ], }, }, @@ -147,11 +147,11 @@ const SCHEMA: Integration = { }, }, }, - localADConfig: { + ntlmConfig: { type: DatasourceFieldType.FIELD_GROUP, default: true, - display: "Configure Local Active Directory", - hidden: `'{{authType}}' !== '${MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY}'`, + display: "Configure NTLM", + hidden: `'{{authType}}' !== '${MSSQLConfigAuthType.NTLM}'`, config: { openByDefault: true, nestedFields: true, @@ -268,8 +268,8 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { }, } break - case MSSQLConfigAuthType.LOCAL_ACTIVE_DIRECTORY: - const { domain, trustServerCertificate } = this.config.localADConfig + case MSSQLConfigAuthType.NTLM: + const { domain, trustServerCertificate } = this.config.ntlmConfig clientCfg.authentication = { type: "ntml", options: { From 119506fcc70cb2ecb4d7ab0dce1718b5c2accf4f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 11 Jul 2023 15:41:49 +0200 Subject: [PATCH 4/4] Fix typo --- packages/server/src/integrations/microsoftSqlServer.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index d27e069919..c9e3b82a9f 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -57,8 +57,8 @@ interface AzureADMSSQLConfig extends BasicMSSQLConfig { interface NTLMMSSQLConfig extends BasicMSSQLConfig { authType: MSSQLConfigAuthType.NTLM ntlmConfig: { - domain: string - trustServerCertificate: boolean + domain?: string + trustServerCertificate?: boolean } } @@ -159,12 +159,12 @@ const SCHEMA: Integration = { fields: { domain: { type: DatasourceFieldType.STRING, - required: true, + required: false, display: "Domain", }, trustServerCertificate: { type: DatasourceFieldType.BOOLEAN, - required: true, + required: false, display: "Trust server certificate", }, }, @@ -271,7 +271,7 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { case MSSQLConfigAuthType.NTLM: const { domain, trustServerCertificate } = this.config.ntlmConfig clientCfg.authentication = { - type: "ntml", + type: "ntlm", options: { domain, },