Skip to content

Commit

Permalink
Update to accept allow-self-signed-certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
codingkarthik committed Apr 4, 2024
1 parent 2c0c7ff commit a903f10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
10 changes: 5 additions & 5 deletions src/cli/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export const cmd = new Command("update")
)
.addOption(
new Option(
"--overwrite <boolean>",
"Overwrite files if already present in the output directory."
"--allow-self-signed-certificate <boolean>",
"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)
}
18 changes: 14 additions & 4 deletions src/cosmosDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CosmosClient, Database, Container, SqlQuerySpec } from "@azure/cosmos"
import * as https from 'https';

export type RawCosmosDbConfig = {
databaseName: string,
Expand All @@ -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);
Expand All @@ -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

Expand Down

0 comments on commit a903f10

Please sign in to comment.