Skip to content

Commit cae85e7

Browse files
authored
Merge pull request #468 from auth0/DXCDT-84-resource-exclusion
DXCDT-84: Resource Exclusion
2 parents b897e41 + 6e0c953 commit cae85e7

File tree

8 files changed

+83
-34
lines changed

8 files changed

+83
-34
lines changed

Diff for: src/context/directory/index.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { loadFileAndReplaceKeywords, Auth0 } from '../../tools';
33

44
import cleanAssets from '../../readonly';
55
import log from '../../logger';
6-
import handlers from './handlers';
6+
import handlers, { DirectoryHandler } from './handlers';
77
import {
88
isDirectory, isFile, stripIdentifiers, toConfigFn
99
} from '../../utils';
10-
import { Assets, Auth0APIClient, Config } from '../../types'
10+
import { Assets, Auth0APIClient, Config, AssetTypes } from '../../types'
1111

1212
type KeywordMappings = { [key: string]: (string | number)[] | string | number }
1313

@@ -78,14 +78,17 @@ export default class DirectoryContext {
7878
// Copy clients to be used by handlers which require converting client_id to the name
7979
// Must copy as the client_id will be stripped if AUTH0_EXPORT_IDENTIFIERS is false
8080
//@ts-ignore because assets haven't been typed yet TODO: type assets
81-
this.assets.clientsOrig = [...this.assets.clients];
81+
this.assets.clientsOrig = [...this.assets.clients || []];
8282

8383
// Optionally Strip identifiers
8484
if (!this.config.AUTH0_EXPORT_IDENTIFIERS) {
8585
this.assets = stripIdentifiers(auth0, this.assets);
8686
}
8787

88-
await Promise.all(Object.entries(handlers).map(async ([name, handler]) => {
88+
await Promise.all(Object.entries(handlers).filter(([handlerName]: [AssetTypes, DirectoryHandler<any>]) => {
89+
const excludedAssetTypes = this.config.AUTH0_EXCLUDED || []
90+
return !excludedAssetTypes.includes(handlerName)
91+
}).map(async ([name, handler]) => {
8992
try {
9093
await handler.dump(this);
9194
} catch (err) {

Diff for: src/context/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const nonPrimitiveProps: (keyof Config)[] = [
1717
'AUTH0_EXCLUDED_CONNECTIONS',
1818
'AUTH0_EXCLUDED_RESOURCE_SERVERS',
1919
'AUTH0_EXCLUDED_DEFAULTS',
20+
'AUTH0_EXCLUDED',
2021
'EXCLUDED_PROPS',
2122
'INCLUDED_PROPS'
2223
];

Diff for: src/context/yaml/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import log from '../../logger';
77
import {
88
isFile, toConfigFn, stripIdentifiers, formatResults, recordsSorter
99
} from '../../utils';
10-
import handlers from './handlers';
10+
import handlers, { YAMLHandler } from './handlers';
1111
import cleanAssets from '../../readonly';
12-
import { Assets, Config, Auth0APIClient } from '../../types'
12+
import { Assets, Config, Auth0APIClient, AssetTypes } from '../../types'
1313

1414
type KeywordMappings = { [key: string]: (string | number)[] | string | number }
1515

@@ -101,7 +101,10 @@ export default class YAMLContext {
101101
throw new Error(`Problem loading tenant data from Auth0 ${err}${extraMessage}`);
102102
}
103103

104-
await Promise.all(Object.entries(handlers).map(async ([name, handler]) => {
104+
await Promise.all(Object.entries(handlers).filter(([handlerName]: [AssetTypes, YAMLHandler<any>]) => {
105+
const excludedAssetTypes = this.config.AUTH0_EXCLUDED || []
106+
return !excludedAssetTypes.includes(handlerName)
107+
}).map(async ([name, handler]) => {
105108
try {
106109
const data = await handler.dump(this);
107110
if (data) {

Diff for: src/tools/auth0/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ export default class Auth0 {
3939
this.config = config;
4040
this.assets = assets;
4141

42-
this.handlers = Object.values(handlers).map((h) => {
43-
//@ts-ignore because prompts don't appear to have been universally implemented yet
44-
const handler = new h.default({ client: this.client, config: this.config });
45-
return handler
46-
});
42+
this.handlers = Object.values(handlers).map((handler) => {
43+
//@ts-ignore because class expects `type` property but gets directly injected into class constructors
44+
return new handler.default({ client: this.client, config: this.config });
45+
}).filter((handler) => {
46+
const excludedAssetTypes = config('AUTH0_EXCLUDED') || []
47+
return !excludedAssetTypes.includes(handler.type)
48+
})
4749
}
4850

4951
async runStage(stage: Stage): Promise<void> {

Diff for: src/types.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,27 @@ export type Config = {
115115
AUTH0_CLIENT_SECRET: string
116116
AUTH0_INPUT_FILE: string
117117
AUTH0_ALLOW_DELETE: boolean
118+
AUTH0_EXCLUDED: AssetTypes[]
118119
EXTENSION_SECRET: string
119120
AUTH0_ACCESS_TOKEN?: string
120121
AUTH0_BASE_PATH?: string
121122
AUTH0_AUDIENCE?: string
122123
AUTH0_API_MAX_RETRIES?: number
123124
AUTH0_KEYWORD_REPLACE_MAPPINGS?: { [key: string]: string[] | string }
124-
AUTH0_EXCLUDED_RULES?: string[]
125-
AUTH0_EXCLUDED_CLIENTS?: string[]
126-
AUTH0_EXCLUDED_DATABASES?: string[]
127-
AUTH0_EXCLUDED_CONNECTIONS?: string[]
128-
AUTH0_EXCLUDED_RESOURCE_SERVERS?: string[]
129-
AUTH0_EXCLUDED_DEFAULTS?: string[]
130125
AUTH0_EXPORT_IDENTIFIERS?: boolean
131126
AUTH0_CONNECTIONS_DIRECTORY?: string
132127
EXCLUDED_PROPS?: {
133128
[key: string]: string[]
134129
}
135130
INCLUDED_PROPS?: {}
136131
AUTH0_IGNORE_UNAVAILABLE_MIGRATIONS?: boolean
132+
// Eventually deprecate:
133+
AUTH0_EXCLUDED_RULES?: string[]
134+
AUTH0_EXCLUDED_CLIENTS?: string[]
135+
AUTH0_EXCLUDED_DATABASES?: string[]
136+
AUTH0_EXCLUDED_CONNECTIONS?: string[]
137+
AUTH0_EXCLUDED_RESOURCE_SERVERS?: string[]
138+
AUTH0_EXCLUDED_DEFAULTS?: string[]
137139
}// TODO: replace with a more accurate representation of the Config type
138140

139141
export type Asset = { [key: string]: any }

Diff for: test/tools/auth0/index.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect } from 'chai';
2+
import Auth0 from '../../../src/tools/auth0'
3+
import { Auth0APIClient, Assets } from '../../../src/types'
4+
5+
const mockEmptyClient = {} as Auth0APIClient
6+
const mockEmptyAssets = {} as Assets
7+
8+
describe("#Auth0 class", () => {
9+
10+
describe("#resource exclusion", () => {
11+
it('should exclude handlers listed in AUTH0_EXCLUDED from Auth0 class', () => {
12+
13+
const auth0WithoutExclusions = new Auth0(mockEmptyClient, mockEmptyAssets, () => []);
14+
15+
const AUTH0_EXCLUDED = ['rules', 'organizations', 'connections']
16+
const auth0WithExclusions = new Auth0(mockEmptyClient, mockEmptyAssets, () => AUTH0_EXCLUDED);
17+
18+
expect(auth0WithoutExclusions.handlers.length).to.equal(auth0WithExclusions.handlers.length + AUTH0_EXCLUDED.length) // Number of handlers is reduced by number of exclusions
19+
20+
const areAllExcludedHandlersAbsent = auth0WithExclusions.handlers.some((handler) => {
21+
return AUTH0_EXCLUDED.includes(handler.type)
22+
})
23+
24+
expect(areAllExcludedHandlersAbsent).to.be.false;
25+
})
26+
27+
it('should not exclude any handlers if AUTH0_EXCLUDED is undefined', () => {
28+
const AUTH0_EXCLUDED = undefined
29+
const auth0 = new Auth0(mockEmptyClient, mockEmptyAssets, () => AUTH0_EXCLUDED);
30+
31+
expect(auth0.handlers.length).to.be.greaterThan(0)
32+
})
33+
})
34+
})

Diff for: test/tools/auth0/validator.tests.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { expect } from 'chai';
22
import Auth0 from '../../../src/tools/auth0';
33
import constants from '../../../src/tools/constants';
44

5+
const mockConfigFn = () => { };
6+
57
describe('#schema validation tests', () => {
68
const client = {
79
rules: {
@@ -33,13 +35,13 @@ describe('#schema validation tests', () => {
3335
};
3436

3537
const checkPassed = (data, done) => {
36-
const auth0 = new Auth0(client, data, {});
38+
const auth0 = new Auth0(client, data, mockConfigFn);
3739

3840
auth0.validate().then(passedCb(done), failedCb(done));
3941
};
4042

4143
const checkRequired = (field, data, done) => {
42-
const auth0 = new Auth0({}, data, {});
44+
const auth0 = new Auth0({}, data, mockConfigFn);
4345

4446
auth0
4547
.validate()
@@ -50,7 +52,7 @@ describe('#schema validation tests', () => {
5052
};
5153

5254
const checkEnum = (data, done) => {
53-
const auth0 = new Auth0({}, data, {});
55+
const auth0 = new Auth0({}, data, mockConfigFn);
5456

5557
auth0
5658
.validate()
@@ -61,7 +63,7 @@ describe('#schema validation tests', () => {
6163
};
6264

6365
const checkTypeError = (field, expectedType, data, done) => {
64-
const auth0 = new Auth0({}, data, {});
66+
const auth0 = new Auth0({}, data, mockConfigFn);
6567

6668
auth0
6769
.validate()
@@ -77,7 +79,7 @@ describe('#schema validation tests', () => {
7779
anything: 'anything'
7880
} ];
7981

80-
const auth0 = new Auth0({}, { branding: data }, {});
82+
const auth0 = new Auth0({}, { branding: data }, mockConfigFn);
8183

8284
auth0.validate().then(failedCb(done), passedCb(done, 'should be object'));
8385
});
@@ -125,7 +127,7 @@ describe('#schema validation tests', () => {
125127
audience: 'audience'
126128
} ];
127129

128-
const auth0 = new Auth0({}, { clientGrants: data }, {});
130+
const auth0 = new Auth0({}, { clientGrants: data }, mockConfigFn);
129131

130132
auth0.validate().then(failedCb(done), passedCb(done, 'should be array'));
131133
});
@@ -155,7 +157,7 @@ describe('#schema validation tests', () => {
155157
name: ''
156158
} ];
157159

158-
const auth0 = new Auth0({}, { clients: data }, {});
160+
const auth0 = new Auth0({}, { clients: data }, mockConfigFn);
159161

160162
auth0
161163
.validate()
@@ -235,7 +237,7 @@ describe('#schema validation tests', () => {
235237
anything: 'anything'
236238
} ];
237239

238-
const auth0 = new Auth0({}, { emailProvider: data }, {});
240+
const auth0 = new Auth0({}, { emailProvider: data }, mockConfigFn);
239241

240242
auth0.validate().then(failedCb(done), passedCb(done, 'should be object'));
241243
});
@@ -490,7 +492,7 @@ describe('#schema validation tests', () => {
490492
anything: 'anything'
491493
} ];
492494

493-
const auth0 = new Auth0({}, { prompts: data }, {});
495+
const auth0 = new Auth0({}, { prompts: data }, mockConfigFn);
494496

495497
auth0.validate().then(failedCb(done), passedCb(done, 'should be object'));
496498
});
@@ -545,7 +547,7 @@ describe('#schema validation tests', () => {
545547
name: '-rule-'
546548
} ];
547549

548-
const auth0 = new Auth0({}, { rules: data }, {});
550+
const auth0 = new Auth0({}, { rules: data }, mockConfigFn);
549551

550552
auth0
551553
.validate()
@@ -595,7 +597,7 @@ describe('#schema validation tests', () => {
595597
value: 'value'
596598
} ];
597599

598-
const auth0 = new Auth0({}, { rulesConfigs: data }, {});
600+
const auth0 = new Auth0({}, { rulesConfigs: data }, mockConfigFn);
599601

600602
auth0
601603
.validate()
@@ -626,7 +628,7 @@ describe('#schema validation tests', () => {
626628
name: '-hook-'
627629
} ];
628630

629-
const auth0 = new Auth0({}, { hooks: data }, {});
631+
const auth0 = new Auth0({}, { hooks: data }, mockConfigFn);
630632

631633
auth0
632634
.validate()
@@ -668,7 +670,7 @@ describe('#schema validation tests', () => {
668670
anything: 'anything'
669671
} ];
670672

671-
const auth0 = new Auth0({}, { tenant: data }, {});
673+
const auth0 = new Auth0({}, { tenant: data }, mockConfigFn);
672674

673675
auth0.validate().then(failedCb(done), passedCb(done, 'should be object'));
674676
});
@@ -686,7 +688,7 @@ describe('#schema validation tests', () => {
686688
it('should fail validation if migrations is not an object', (done) => {
687689
const data = '';
688690

689-
const auth0 = new Auth0({}, { migrations: data }, {});
691+
const auth0 = new Auth0({}, { migrations: data }, mockConfigFn);
690692

691693
auth0.validate().then(failedCb(done), passedCb(done, 'should be object'));
692694
});
@@ -696,7 +698,7 @@ describe('#schema validation tests', () => {
696698
migration_flag: 'string'
697699
};
698700

699-
const auth0 = new Auth0({}, { migrations: data }, {});
701+
const auth0 = new Auth0({}, { migrations: data }, mockConfigFn);
700702

701703
auth0.validate().then(failedCb(done), passedCb(done, 'should be boolean'));
702704
});

Diff for: test/utils.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
toConfigFn
2323
} from '../src/utils';
2424

25+
const mockConfigFn = () => { };
26+
2527
describe('#utils', function() {
2628
it('should check if directory exist', () => {
2729
const dirExist = path.join(testDataDir, 'utils', 'isdir');
@@ -95,7 +97,7 @@ describe('#utils', function() {
9597
rulesConfigs: [ { key: 'test', value: 'test' } ]
9698
};
9799

98-
const auth0 = new Auth0(mockMgmtClient(), {}, {});
100+
const auth0 = new Auth0(mockMgmtClient(), {}, mockConfigFn);
99101

100102
expect(stripIdentifiers(auth0, assets)).to.deep.equal({
101103
clients: [

0 commit comments

Comments
 (0)