diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6401cb2..5387e45 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,6 +43,8 @@ jobs: if check_port; then echo "Port 8081 is now open!" + # Sleep for 120 seconds, because there is no deterministic way + # the emulator provides to check if it is up and running :( sleep 120 sudo curl -k --retry 10 https://localhost:8081/_explorer/emulator.pem > ~/emulatorcert.crt sudo cp ~/emulatorcert.crt /usr/local/share/ca-certificates/ @@ -70,7 +72,7 @@ jobs: export AZURE_COSMOS_KEY=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw== export AZURE_COSMOS_ENDPOINT=https://127.0.0.1:8081/ export AZURE_COSMOS_DB_NAME=ConnectorTest - ./dist/cli/index.js update + ./dist/cli/index.js update --allow-self-signed-certificate true - name: Start connector run: | diff --git a/src/cli/config.ts b/src/cli/config.ts index 7c33155..3a2d009 100644 --- a/src/cli/config.ts +++ b/src/cli/config.ts @@ -241,11 +241,11 @@ async function getCollectionsSchema(database: Database, nRows: number): Promise< } -export async function generateConnectorConfig(outputConfigDir: string) { +export async function generateConnectorConfig(outputConfigDir: string, allowSelfSignedCertificate: boolean) { const rowsToFetch = process.env["AZURE_COSMOS_NO_OF_ROWS_TO_FETCH"] ?? "100"; try { - const dbClient = constructCosmosDbClient(); + const dbClient = constructCosmosDbClient(allowSelfSignedCertificate); const schema = await getCollectionsSchema(dbClient, parseInt(rowsToFetch)); const response: any = { diff --git a/src/cli/update.ts b/src/cli/update.ts index 88e2869..0a15df0 100755 --- a/src/cli/update.ts +++ b/src/cli/update.ts @@ -13,19 +13,19 @@ export const cmd = new Command("update") ) .addOption( new Option( - "--overwrite ", - "Overwrite files if already present in the output directory." + "--allow-self-signed-certificate ", + "Allow the config script to use a self-signed certificate. *NOT RECOMMENDED* to set this option for Production" ) .default("false") .choices(["true", "false", "0", "1"]) .preset("true") ) .action((args) => { - cliUpdateAction(resolve(args.outputDirectory), args.overwrite === 'true' || args.overwrite === '1'); + cliUpdateAction(resolve(args.outputDirectory), args.allowSelfSignedCertificate === 'true' || args.allowSelfSignedCertificate === '1'); }); -async function cliUpdateAction(outputDirectory: string, shouldOverWrite: boolean) { - generateConnectorConfig(outputDirectory) +async function cliUpdateAction(outputDirectory: string, allowSelfSignedCertificate: boolean) { + generateConnectorConfig(outputDirectory, allowSelfSignedCertificate) } diff --git a/src/cosmosDb.ts b/src/cosmosDb.ts index 4e24b79..f95a31f 100644 --- a/src/cosmosDb.ts +++ b/src/cosmosDb.ts @@ -1,4 +1,5 @@ import { CosmosClient, Database, Container, SqlQuerySpec } from "@azure/cosmos" +import * as https from 'https'; export type RawCosmosDbConfig = { databaseName: string, @@ -7,10 +8,19 @@ export type RawCosmosDbConfig = { } /* Creates a new cosmos DB client with which the specified database can be queried. */ -function getCosmosDbClient(rawDbConfig: RawCosmosDbConfig): Database { +function getCosmosDbClient(rawDbConfig: RawCosmosDbConfig, allowSelfSignedCertificate?: boolean | undefined): Database { + let httpsAgent: https.Agent | undefined; + if (allowSelfSignedCertificate) { + httpsAgent = new https.Agent({ + rejectUnauthorized: false + }) + } else { + httpsAgent = undefined + }; const dbClient = new CosmosClient({ key: rawDbConfig.key, - endpoint: rawDbConfig.endpoint + endpoint: rawDbConfig.endpoint, + agent: httpsAgent }); return dbClient.database(rawDbConfig.databaseName); @@ -25,14 +35,14 @@ function getEnvVariable(envVarName: string): string { return envVariable; } -export function constructCosmosDbClient() { +export function constructCosmosDbClient(allowSelfSignedCertificate?: boolean | undefined) { const key = getEnvVariable("AZURE_COSMOS_KEY"); const endpoint = getEnvVariable("AZURE_COSMOS_ENDPOINT"); const databaseName = getEnvVariable("AZURE_COSMOS_DB_NAME"); const dbClient = getCosmosDbClient({ databaseName, endpoint, key - }); + }, allowSelfSignedCertificate); return dbClient