From 672094dd129a4678f00aa0aa0387b2e6131a2dd6 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 21 Nov 2023 07:02:38 -0500 Subject: [PATCH] Fetch clients if not defined in client grants YAML handler (#865) Fetching clients for client grants YAML handler if they don't exist in assets Co-authored-by: Will Vedder --- src/context/yaml/handlers/clientGrants.ts | 9 ++++- src/context/yaml/index.ts | 3 +- test/context/yaml/clientGrants.test.js | 49 ++++++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/context/yaml/handlers/clientGrants.ts b/src/context/yaml/handlers/clientGrants.ts index aa5422908..d864f66ad 100644 --- a/src/context/yaml/handlers/clientGrants.ts +++ b/src/context/yaml/handlers/clientGrants.ts @@ -17,10 +17,17 @@ async function parse(context: YAMLContext): Promise { } async function dump(context: YAMLContext): Promise { - const { clientGrants, clients } = context.assets; + let { clientGrants, clients } = context.assets; if (!clientGrants) return { clientGrants: null }; + if (clients === undefined) { + clients = await context.mgmtClient.clients.getAll({ + paginate: true, + include_totals: true, + }); + } + // Convert client_id to the client name for readability return { clientGrants: clientGrants.map((grant) => { diff --git a/src/context/yaml/index.ts b/src/context/yaml/index.ts index d7a30ccc8..715c4ccc6 100644 --- a/src/context/yaml/index.ts +++ b/src/context/yaml/index.ts @@ -7,6 +7,7 @@ import { wrapArrayReplaceMarkersInQuotes, Auth0, } from '../../tools'; +import pagedClient from '../../tools/auth0/client'; import log from '../../logger'; import { isFile, toConfigFn, stripIdentifiers, formatResults, recordsSorter } from '../../utils'; @@ -29,7 +30,7 @@ export default class YAMLContext { this.configFile = config.AUTH0_INPUT_FILE; this.config = config; this.mappings = config.AUTH0_KEYWORD_REPLACE_MAPPINGS || {}; - this.mgmtClient = mgmtClient; + this.mgmtClient = pagedClient(mgmtClient); this.disableKeywordReplacement = false; //@ts-ignore because the assets property gets filled out throughout diff --git a/test/context/yaml/clientGrants.test.js b/test/context/yaml/clientGrants.test.js index e513d9a3e..43d9fb4eb 100644 --- a/test/context/yaml/clientGrants.test.js +++ b/test/context/yaml/clientGrants.test.js @@ -40,11 +40,58 @@ describe('#YAML context client grants', () => { it('should dump client grants', async () => { const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmtClient()); const clientGrants = [ - { audience: 'https://test.myapp.com/api/v1', client_id: 'My M2M', scope: ['update:account'] }, + { + audience: 'https://test.myapp.com/api/v1', + client_id: 'client-id', + scope: ['update:account'], + }, ]; context.assets.clientGrants = clientGrants; const dumped = await handler.dump(context); expect(dumped).to.deep.equal({ clientGrants }); }); + + it('should dump client grants and replace client ID with client name if clients in assets', async () => { + const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmtClient()); + const clientGrants = [ + { + audience: 'https://test.myapp.com/api/v1', + client_id: 'client-id-1', + scope: ['update:account'], + }, + ]; + context.assets.clientGrants = clientGrants; + context.assets.clients = [{ client_id: 'client-id-1', name: 'Client 1' }]; + + const dumped = await handler.dump(context); + const expected = (() => { + const ret = clientGrants; + ret[0].client_id = 'Client 1'; + return { clientGrants: ret }; + })(); + expect(dumped).to.deep.equal(expected); + }); + + it('should dump client grants and replace client ID with client name even if clients not in assets', async () => { + const mockMgmt = mockMgmtClient(); + mockMgmt.clients.getAll = () => [[{ client_id: 'client-id-1', name: 'Client 1' }]]; + const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmt); + const clientGrants = [ + { + audience: 'https://test.myapp.com/api/v1', + client_id: 'client-id-1', + scope: ['update:account'], + }, + ]; + context.assets.clientGrants = clientGrants; + + const dumped = await handler.dump(context); + const expected = (() => { + const ret = clientGrants; + ret[0].client_id = 'Client 1'; + return { clientGrants: ret }; + })(); + expect(dumped).to.deep.equal(expected); + }); });