Skip to content

Commit

Permalink
test: get token audience from props
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 25, 2023
1 parent 65c10db commit 5c6d31c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/cmap/auth/mongo_credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Document } from '../../bson';
import {
MongoAPIError,
MongoAzureError,
MongoInvalidArgumentError,
MongoMissingCredentialsError
} from '../../error';
Expand Down Expand Up @@ -43,6 +44,10 @@ export const DEFAULT_ALLOWED_HOSTS = [
'::1'
];

/** Error for when the token audience is missing in the environment. */
const TOKEN_AUDIENCE_MISSING_ERROR =
'TOKEN_AUDIENCE must be set in the auth mechanism properties when PROVIDER_NAME is azure.';

/** @public */
export interface AuthMechanismProperties extends Document {
SERVICE_HOST?: string;
Expand All @@ -55,9 +60,11 @@ export interface AuthMechanismProperties extends Document {
/** @experimental */
REFRESH_TOKEN_CALLBACK?: OIDCRefreshFunction;
/** @experimental */
PROVIDER_NAME?: 'aws';
PROVIDER_NAME?: 'aws' | 'azure';
/** @experimental */
ALLOWED_HOSTS?: string[];
/** @experimental */
TOKEN_AUDIENCE?: string;
}

/** @public */
Expand Down Expand Up @@ -177,6 +184,13 @@ export class MongoCredentials {
);
}

if (
this.mechanismProperties.PROVIDER_NAME === 'azure' &&
!this.mechanismProperties.TOKEN_AUDIENCE
) {
throw new MongoAzureError(TOKEN_AUDIENCE_MISSING_ERROR);
}

if (
this.mechanismProperties.PROVIDER_NAME &&
!ALLOWED_PROVIDER_NAMES.includes(this.mechanismProperties.PROVIDER_NAME)
Expand Down
12 changes: 7 additions & 5 deletions src/cmap/auth/mongodb_oidc/azure_service_workflow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { MongoAzureError } from '../../../error';
import { request } from '../../../utils';
import type { MongoCredentials } from '../mongo_credentials';
import { AzureTokenCache } from './azure_token_cache';
import { ServiceWorkflow } from './service_workflow';

/** Error for when the token audience is missing in the environment. */
const TOKEN_AUDIENCE_MISSING_ERROR = 'TOKEN_AUDIENCE must be set in the environment.';

/** Base URL for getting Azure tokens. */
const AZURE_BASE_URL =
'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01';
Expand All @@ -20,6 +18,10 @@ const RESULT_PROPERTIES = ['access_token', 'expires_in'];
const ENDPOINT_RESULT_ERROR =
'Azure endpoint did not return a value with only access_token and expires_in properties';

/** Error for when the token audience is missing in the environment. */
const TOKEN_AUDIENCE_MISSING_ERROR =
'TOKEN_AUDIENCE must be set in the auth mechanism properties when PROVIDER_NAME is azure.';

/**
* The Azure access token format.
* @internal
Expand Down Expand Up @@ -48,8 +50,8 @@ export class AzureServiceWorkflow extends ServiceWorkflow {
/**
* Get the token from the environment.
*/
async getToken(): Promise<string> {
const tokenAudience = process.env.TOKEN_AUDIENCE;
async getToken(credentials?: MongoCredentials): Promise<string> {
const tokenAudience = credentials?.mechanismProperties.TOKEN_AUDIENCE;
if (!tokenAudience) {
throw new MongoAzureError(TOKEN_AUDIENCE_MISSING_ERROR);
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/auth/mongodb_oidc/service_workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export abstract class ServiceWorkflow implements Workflow {
* and then attempts to read the token from that path.
*/
async execute(connection: Connection, credentials: MongoCredentials): Promise<Document> {
const token = await this.getToken();
const token = await this.getToken(credentials);
const command = commandDocument(token);
return connection.commandAsync(ns(credentials.source), command, undefined);
}
Expand All @@ -25,7 +25,7 @@ export abstract class ServiceWorkflow implements Workflow {
* Get the document to add for speculative authentication.
*/
async speculativeAuth(credentials: MongoCredentials): Promise<Document> {
const token = await this.getToken();
const token = await this.getToken(credentials);
const document = commandDocument(token);
document.db = credentials.source;
return { speculativeAuthenticate: document };
Expand All @@ -34,7 +34,7 @@ export abstract class ServiceWorkflow implements Workflow {
/**
* Get the token from the environment or endpoint.
*/
abstract getToken(): Promise<string>;
abstract getToken(credentials: MongoCredentials): Promise<string>;
}

/**
Expand Down

0 comments on commit 5c6d31c

Please sign in to comment.