Skip to content

Commit

Permalink
minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
codingkarthik committed Sep 18, 2024
1 parent 20dd884 commit 1c38b3e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ type UpdateConfig = {
connection: {
endpoint: string;
databaseName: string;
authenticationConfig: AzureCosmosAuthenticationConfig;
authentication: AzureCosmosAuthenticationConfig;
};
schema: CollectionsSchema;
};
Expand Down Expand Up @@ -321,7 +321,7 @@ export async function generateConnectorConfig(outputConfigDir: string) {
version: 2,
connection: {
endpoint: cosmosEndpoint,
authenticationConfig: connectionConfig,
authentication: connectionConfig,
databaseName: cosmosDbName,
},
schema,
Expand Down
25 changes: 21 additions & 4 deletions src/connector/connector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as sdk from "@hasura/ndc-sdk-typescript";
import { CollectionsSchema, getNdcSchemaResponse } from "./schema";
import { constructCosmosDbClient } from "./db/cosmosDb";
import {
AzureCosmosAuthenticationConfig,
getCosmosDbClient,
} from "./db/cosmosDb";
import { Database } from "@azure/cosmos";
import { executeQuery } from "./execution";
import { readFileSync } from "fs";
Expand All @@ -10,7 +13,7 @@ export type Configuration = ConnectorConfig;
export type ConnectorConfig = {
connection: {
endpoint: string;
key: string;
authentication: AzureCosmosAuthenticationConfig;
databaseName: string;
};
schema: CollectionsSchema;
Expand Down Expand Up @@ -40,11 +43,25 @@ export function createConnector(): sdk.Connector<Configuration, State> {
},

tryInitState: async function (
_: Configuration,
config: Configuration,
__: unknown,
): Promise<State> {
try {
const databaseClient = constructCosmosDbClient().dbClient;
const {
databaseName,
authentication: authenticationConfig,
endpoint,
} = config.connection;
console.log(
"Initializing the state of the connector",
authenticationConfig,
);
const databaseClient = getCosmosDbClient(
endpoint,
databaseName,
authenticationConfig,
);

return Promise.resolve({
databaseClient,
});
Expand Down
27 changes: 20 additions & 7 deletions src/connector/db/cosmosDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { throwError } from "../../utils";

export type ManagedIdentityConfig = {
type: "ManagedIdentity";
clientId: string;
// Name of the ENV var where the key can be found
fromEnvVar: string;
};

export type CosmosKeyConfig = {
type: "Key";
key: string;
// Name of the ENV var where the key can be found
fromEnvVar: string;
};

export type AzureCosmosAuthenticationConfig =
Expand All @@ -23,22 +25,33 @@ export type RawCosmosDbConfig = {
};

/* Creates a new cosmos DB client with which the specified database can be queried. */
function getCosmosDbClient(
export function getCosmosDbClient(
endpoint: string,
databaseName: string,
connectionConfig: AzureCosmosAuthenticationConfig,
): Database {
let dbClient: CosmosClient;
switch (connectionConfig.type) {
case "Key":
const key =
getEnvVariable(connectionConfig.fromEnvVar, true) ??
throwError(
`Azure Cosmos Key not found in the env var "${connectionConfig.fromEnvVar}"`,
);
dbClient = new CosmosClient({
key: connectionConfig.key,
key,
endpoint,
});
break;
case "ManagedIdentity":
const managedIdentityClientId =
getEnvVariable(connectionConfig.fromEnvVar, true) ??
throwError(
`Azure Cosmos Key not found in the env var "${connectionConfig.fromEnvVar}"`,
);

let credentials = new DefaultAzureCredential({
managedIdentityClientId: connectionConfig.clientId,
managedIdentityClientId,
});
dbClient = new CosmosClient({
endpoint,
Expand Down Expand Up @@ -83,12 +96,12 @@ function getConnectionConfig(): AzureCosmosAuthenticationConfig | null {
if (key) {
return {
type: "Key",
key,
fromEnvVar: "AZURE_COSMOS_KEY",
};
} else if (managed_identity_client_id) {
return {
type: "ManagedIdentity",
clientId: managed_identity_client_id,
fromEnvVar: "AZURE_COSMOS_MANAGED_CLIENT_ID",
};
}
}
Expand Down

0 comments on commit 1c38b3e

Please sign in to comment.