Skip to content

Commit

Permalink
Added support to import / publish flows for PVA GCC High env (microso…
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyanziano authored Aug 16, 2021
1 parent 5fde1b8 commit 2a99641
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Composer/packages/electron-server/src/auth/oneAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class OneAuthInstance extends OneAuthBase {
// use the signed in account to acquire a token
const reqParams = new this.oneAuth.AuthParameters(
DEFAULT_AUTH_SCHEME,
DEFAULT_AUTH_AUTHORITY,
params.authority || DEFAULT_AUTH_AUTHORITY,
params.targetResource,
this.signedInAccount.realm,
''
Expand All @@ -168,7 +168,7 @@ export class OneAuthInstance extends OneAuthBase {
// use the signed in account to acquire a token
const reqParams = new this.oneAuth.AuthParameters(
DEFAULT_AUTH_SCHEME,
DEFAULT_AUTH_AUTHORITY,
params.authority || DEFAULT_AUTH_AUTHORITY,
params.targetResource,
this.signedInAccount.realm,
''
Expand Down Expand Up @@ -341,7 +341,7 @@ export class OneAuthInstance extends OneAuthBase {
// sign-in every time with auth parameters to get a token
const reqParams = new this.oneAuth.AuthParameters(
DEFAULT_AUTH_SCHEME,
DEFAULT_AUTH_AUTHORITY,
params.authority || DEFAULT_AUTH_AUTHORITY,
params.targetResource,
'',
''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join } from 'path';

import {
PowerVirtualAgentsProvider,
PVA_GCC_HIGH_APP_ID,
PVA_GOV_APP_ID,
PVA_PROD_APP_ID,
PVA_TEST_APP_ID,
Expand Down Expand Up @@ -208,4 +209,17 @@ describe('Power Virtual Agents provider', () => {
expect(credentials.targetResource).toBe(PVA_GOV_APP_ID);
expect(accessToken).toBe('accessToken');
});

it('should authenticate with credentials for the GCC High / gov environment', async () => {
provider = new PowerVirtualAgentsProvider({
...metadata,
baseUrl: 'https://high.api.powerva.microsoft.us/api/botmanagement/v1',
});
const accessToken = await provider.authenticate();

const args = mockGetAccessToken.mock.calls[0];
const credentials = args[0];
expect(credentials.targetResource).toBe(PVA_GCC_HIGH_APP_ID);
expect(accessToken).toBe('accessToken');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const COMPOSER_1P_APP_ID = 'ce48853e-0605-4f77-8746-d70ac63cc6bc';
export const PVA_TEST_APP_ID = 'a522f059-bb65-47c0-8934-7db6e5286414';
export const PVA_PROD_APP_ID = '96ff4394-9197-43aa-b393-6a41652e21f8';
export const PVA_GOV_APP_ID = '9315aedd-209b-43b3-b149-2abff6a95d59';
export const PVA_GCC_HIGH_APP_ID = '69c6e40c-465f-4154-987d-da5cba10734e';

export type PowerVirtualAgentsMetadata = IContentProviderMetadata & {
baseUrl: string;
Expand All @@ -30,7 +31,7 @@ export type PowerVirtualAgentsMetadata = IContentProviderMetadata & {
triggerId?: string;
};

const getAuthCredentials = (baseUrl: string) => {
const getAuthCredentials = (baseUrl: string, metadata: PowerVirtualAgentsMetadata) => {
const url = new URL(baseUrl);
if (url.hostname.includes('.int.') || url.hostname.includes('.ppe.')) {
log('Using INT / PPE auth credentials.');
Expand All @@ -46,6 +47,14 @@ const getAuthCredentials = (baseUrl: string) => {
scopes: [`${PVA_GOV_APP_ID}/.default`],
targetResource: PVA_GOV_APP_ID,
};
} else if (url.hostname.includes('high.api.powerva.microsoft.us')) {
log('Using GCC High auth credentials.');
return {
authority: `https://login.microsoftonline.us/${metadata.tenantId}`,
clientId: COMPOSER_1P_APP_ID,
scopes: [`${PVA_GCC_HIGH_APP_ID}/.default`],
targetResource: PVA_GCC_HIGH_APP_ID,
};
}
log('Using PROD auth credentials.');
return {
Expand Down Expand Up @@ -82,6 +91,12 @@ const getBaseUrl = () => {
return url;
}

case 'gcc-high': {
const url = 'https://high.api.powerva.microsoft.us/api/botmanagement/v1';
log('GCC High env detected, grabbing PVA content from %s', url);
return url;
}

default: {
const url = 'https://bots.int.customercareintelligence.net/api/botmanagement/v1';
log('No env flag detected, grabbing PVA content from %s', url);
Expand Down Expand Up @@ -162,7 +177,7 @@ export class PowerVirtualAgentsProvider extends ExternalContentProvider<PowerVir
try {
// login to the 1P app and get an access token
const { baseUrl } = this.metadata;
const authCredentials = getAuthCredentials(baseUrl || getBaseUrl());
const authCredentials = getAuthCredentials(baseUrl || getBaseUrl(), this.metadata);
const accessToken = await authService.getAccessToken(authCredentials);
if (accessToken === '') {
throw 'User cancelled login flow.';
Expand Down
12 changes: 12 additions & 0 deletions Composer/packages/types/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// Licensed under the MIT License.

export type AuthParameters = {
/** What login endpoint to use
*
* ex: https://login.microsoftonline.com/common
*/
authority?: string;

/** (Web) Client ID of the AAD app that the user is authenticating against. */
clientId?: string;
/** (Web) List of OAuth scopes that will be granted once the user has authenticated. */
Expand All @@ -16,6 +22,12 @@ export type AuthParameters = {
};

export type ElectronAuthParameters = {
/** What login endpoint to use
*
* ex: https://login.microsoftonline.com/common
*/
authority?: string;

/**
* The resource for which we want to get a token for.
*
Expand Down
6 changes: 6 additions & 0 deletions extensions/pvaPublish/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ export const AUTH_CREDENTIALS = {
scopes: ['9315aedd-209b-43b3-b149-2abff6a95d59/.default'],
targetResource: '9315aedd-209b-43b3-b149-2abff6a95d59',
},
GCC_HIGH: {
clientId: COMPOSER_1P_APP_ID,
scopes: ['69c6e40c-465f-4154-987d-da5cba10734e/.default'],
targetResource: '69c6e40c-465f-4154-987d-da5cba10734e',
},
};

export const BASE_URLS = {
INT: 'https://bots.int.customercareintelligence.net/',
PPE: 'https://bots.ppe.customercareintelligence.net/',
PROD: 'https://powerva.microsoft.com/',
GCC: 'https://gcc.api.powerva.microsoft.us/',
GCC_HIGH: 'https://high.api.powerva.microsoft.us/',
};
8 changes: 4 additions & 4 deletions extensions/pvaPublish/src/node/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const publish = async (
logger.log('Starting publish to Power Virtual Agents.');
// authenticate with PVA
const base = baseUrl || getBaseUrl();
const creds = getAuthCredentials(base);
const creds = getAuthCredentials(base, tenantId);
const accessToken = await getAccessToken(creds);

// write the .zip to a buffer in memory
Expand Down Expand Up @@ -162,7 +162,7 @@ export const getStatus = async (
try {
// authenticate with PVA
const base = baseUrl || getBaseUrl();
const creds = getAuthCredentials(base);
const creds = getAuthCredentials(base, tenantId);
const accessToken = await getAccessToken(creds);

// check the status for the publish job
Expand Down Expand Up @@ -223,7 +223,7 @@ export const history = async (
try {
// authenticate with PVA
const base = baseUrl || getBaseUrl();
const creds = getAuthCredentials(base);
const creds = getAuthCredentials(base, tenantId);
const accessToken = await getAccessToken(creds);

// get the publish history for the bot
Expand Down Expand Up @@ -257,7 +257,7 @@ export const pull = async (
try {
// authenticate with PVA
const base = baseUrl || getBaseUrl();
const creds = getAuthCredentials(base);
const creds = getAuthCredentials(base, tenantId);
const accessToken = await getAccessToken(creds);

// fetch zip containing bot content
Expand Down
14 changes: 14 additions & 0 deletions extensions/pvaPublish/src/node/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ describe('should return the proper PVA base URL for the environment', () => {
Object.assign(process.env, { COMPOSER_PVA_PUBLISH_ENV: 'GCC' });
expect(getBaseUrl()).toBe(BASE_URLS.GCC);
});

it('gcc high', () => {
Object.assign(process.env, { COMPOSER_PVA_PUBLISH_ENV: 'GCC-HIGH' });
expect(getBaseUrl()).toBe(BASE_URLS.GCC_HIGH);
});
});

describe('it should return the proper PVA auth parameters for the base URL', () => {
Expand Down Expand Up @@ -71,4 +76,13 @@ describe('it should return the proper PVA auth parameters for the base URL', ()
const url = 'https://gcc.api.powerva.microsoft.us/api/botmanagement/v1';
expect(getAuthCredentials(url)).toEqual(AUTH_CREDENTIALS.GCC);
});

it('gcc high', () => {
const url = 'https://high.api.powerva.microsoft.us/api/botmanagement/v1';
const tenantId = '1234-6789-abcd-efgh';
expect(getAuthCredentials(url, tenantId)).toEqual({
...AUTH_CREDENTIALS.GCC_HIGH,
authority: `https://login.microsoftonline.us/${tenantId}`,
});
});
});
13 changes: 12 additions & 1 deletion extensions/pvaPublish/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const getBaseUrl = () => {
return url;
}

case 'gcc-high': {
const url = BASE_URLS.GCC_HIGH;
logger.log('gcc high pva publish env detected, operation using PVA url: ', url);
return url;
}

default: {
const url = BASE_URLS.PROD;
logger.log('No pva publish env detected, operation using PVA url: ', url);
Expand All @@ -46,7 +52,7 @@ export const getBaseUrl = () => {
* Looks at the base URL for a request and returns the necessary authentication parameters
* to get an access token for the resource.
*/
export const getAuthCredentials = (baseUrl = '') => {
export const getAuthCredentials = (baseUrl = '', tenantId?: string) => {
if (baseUrl) {
const host = new URL(baseUrl).host;

Expand All @@ -56,6 +62,11 @@ export const getAuthCredentials = (baseUrl = '') => {
return AUTH_CREDENTIALS.PPE;
} else if (host === 'gcc.api.powerva.microsoft.us') {
return AUTH_CREDENTIALS.GCC;
} else if (host === 'high.api.powerva.microsoft.us') {
return {
...AUTH_CREDENTIALS.GCC_HIGH,
authority: `https://login.microsoftonline.us/${tenantId}`,
};
}
}
// fall back to prod
Expand Down

0 comments on commit 2a99641

Please sign in to comment.