Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.6 KB
Raw Blame
import {
  AppConnectionType,
  PieceAuth,
  Property,
} from '@activepieces/pieces-framework';
import { mssqlCommon } from './common';

export const mssqlAuth = PieceAuth.CustomAuth({
  description: `**Connection String (Azure SQL, SQL authentication):** in the portal open your database, choose Connection strings, and copy the ADO.NET (SQL authentication) one, not an Active Directory variant. Replace the {your_password} placeholder, and quote a password containing a semicolon: Password="my;pass";.

**Microsoft Entra ID (service principal):** leave Username and Password empty, and fill in Tenant ID, Client ID and Client Secret from an app registration instead. See [Create Microsoft Entra Users Using Service Principals](https://learn.microsoft.com/en-us/azure/azure-sql/database/authentication-aad-service-principal?view=azuresql) for how to register the app and grant it a database user.`,
  props: {
    connection_string: Property.LongText({
      displayName: 'Connection String',
      description:
        'Azure SQL only. When set, the fields below are ignored except CA Certificate and Minimum TLS Version.',
      required: false,
    }),
    host: Property.ShortText({
      displayName: 'Host',
      description:
        'The hostname or address of the SQL Server. Use Host and Port rather than a named instance, which needs UDP port 1434.',
      required: false,
    }),
    port: Property.Number({
      displayName: 'Port',
      description: 'The port the SQL Server listens on.',
      defaultValue: 1433,
      required: false,
    }),
    database: Property.ShortText({
      displayName: 'Database',
      description: 'The name of the database to connect to.',
      required: false,
    }),
    user: Property.ShortText({
      displayName: 'Username',
      description:
        'The SQL Server login to authenticate as. On Azure SQL this is usually user@servername. Leave empty when authenticating with Tenant ID, Client ID and Client Secret below.',
      required: false,
    }),
    password: PieceAuth.SecretText({
      displayName: 'Password',
      description: 'The password for the login above.',
      required: false,
    }),
    tenant_id: Property.ShortText({
      displayName: 'Tenant ID',
      description:
        'Microsoft Entra ID tenant ID, for authenticating as a service principal instead of a SQL login. Requires Client ID and Client Secret below, and leaves Username and Password empty.',
      required: false,
    }),
    client_id: Property.ShortText({
      displayName: 'Client ID',
      description: 'Application (client) ID of the Entra ID app registration.',
      required: false,
    }),
    client_secret: PieceAuth.SecretText({
      displayName: 'Client Secret',
      description: 'Client secret value of the Entra ID app registration.',
      required: false,
    }),
    encrypt: Property.Checkbox({
      displayName: 'Encrypt Connection',
      description:
        'Encrypt the connection with TLS. Required by Azure SQL; turn off only for an on-premise server with no TLS configured.',
      required: true,
      defaultValue: true,
    }),
    trust_server_certificate: Property.Checkbox({
      displayName: 'Trust Server Certificate',
      description:
        'Skip server certificate verification, for an on-premise server with a self-signed certificate. Leave off for Azure SQL or when a CA certificate is supplied below.',
      required: true,
      defaultValue: false,
    }),
    certificate: Property.LongText({
      displayName: 'CA Certificate',
      description: 'Optional CA certificate (PEM) to verify a self-signed server certificate.',
      required: false,
    }),
    min_tls_version: Property.StaticDropdown({
      displayName: 'Minimum TLS Version',
      description:
        'Optional. Lower this only to reach an older server such as SQL Server 2012 that cannot negotiate TLS 1.2.',
      required: false,
      options: {
        options: [
          { label: 'TLS 1.3', value: 'TLSv1.3' },
          { label: 'TLS 1.2', value: 'TLSv1.2' },
          { label: 'TLS 1.1', value: 'TLSv1.1' },
          { label: 'TLS 1.0', value: 'TLSv1' },
        ],
      },
    }),
  },
  required: true,
  validate: async ({ auth }) => {
    try {
      const pool = await mssqlCommon.connect({
        auth: {
          type: AppConnectionType.CUSTOM_AUTH,
          props: auth,
        },
      });
      try {
        await pool.request().query('SELECT 1');
      } finally {
        await pool.close();
      }
    } catch (e) {
      return {
        valid: false,
        error: e instanceof Error ? e.message : JSON.stringify(e),
      };
    }
    return {
      valid: true,
    };
  },
});