1
- import { createHash } from 'crypto' ;
2
1
import * as fs from 'fs' ;
3
2
import * as path from 'path' ;
4
3
import * as cxapi from '../../../cx-api' ;
@@ -16,6 +15,7 @@ import { Stage, StageSynthesisOptions } from '../stage';
16
15
import { IPolicyValidationPluginBeta1 } from '../validation' ;
17
16
import { ConstructTree } from '../validation/private/construct-tree' ;
18
17
import { PolicyValidationReportFormatter , NamedValidationPluginReport } from '../validation/private/report' ;
18
+ import { FileSystem } from '../fs' ;
19
19
20
20
const POLICY_VALIDATION_FILE_PATH = 'policy-validation-report.json' ;
21
21
const VALIDATION_REPORT_JSON_CONTEXT = '@aws-cdk/core:validationReportJson' ;
@@ -90,7 +90,7 @@ function getAssemblies(root: App, rootAssembly: CloudAssembly): Map<string, Clou
90
90
*/
91
91
function invokeValidationPlugins ( root : IConstruct , outdir : string , assembly : CloudAssembly ) {
92
92
if ( ! App . isApp ( root ) ) return ;
93
- const hash = computeChecksumOfFolder ( outdir ) ;
93
+ let hash : string | undefined ;
94
94
const assemblies = getAssemblies ( root , assembly ) ;
95
95
const templatePathsByPlugin : Map < IPolicyValidationPluginBeta1 , string [ ] > = new Map ( ) ;
96
96
visitAssemblies ( root , 'post' , construct => {
@@ -111,6 +111,11 @@ function invokeValidationPlugins(root: IConstruct, outdir: string, assembly: Clo
111
111
// eslint-disable-next-line no-console
112
112
console . log ( 'Performing Policy Validations\n' ) ;
113
113
}
114
+
115
+ if ( templatePathsByPlugin . size > 0 ) {
116
+ hash = FileSystem . fingerprint ( outdir ) ;
117
+ }
118
+
114
119
for ( const [ plugin , paths ] of templatePathsByPlugin . entries ( ) ) {
115
120
try {
116
121
const report = plugin . validate ( { templatePaths : paths } ) ;
@@ -126,7 +131,7 @@ function invokeValidationPlugins(root: IConstruct, outdir: string, assembly: Clo
126
131
} ,
127
132
} ) ;
128
133
}
129
- if ( computeChecksumOfFolder ( outdir ) !== hash ) {
134
+ if ( FileSystem . fingerprint ( outdir ) !== hash ) {
130
135
throw new Error ( `Illegal operation: validation plugin '${ plugin . name } ' modified the cloud assembly` ) ;
131
136
}
132
137
}
@@ -162,21 +167,6 @@ function invokeValidationPlugins(root: IConstruct, outdir: string, assembly: Clo
162
167
}
163
168
}
164
169
165
- function computeChecksumOfFolder ( folder : string ) : string {
166
- const hash = createHash ( 'sha256' ) ;
167
- const files = fs . readdirSync ( folder , { withFileTypes : true } ) ;
168
-
169
- for ( const file of files ) {
170
- const fullPath = path . join ( folder , file . name ) ;
171
- if ( file . isDirectory ( ) ) {
172
- hash . update ( computeChecksumOfFolder ( fullPath ) ) ;
173
- } else if ( file . isFile ( ) ) {
174
- hash . update ( fs . readFileSync ( fullPath ) ) ;
175
- }
176
- }
177
- return hash . digest ( ) . toString ( 'hex' ) ;
178
- }
179
-
180
170
const CUSTOM_SYNTHESIS_SYM = Symbol . for ( '@aws-cdk/core:customSynthesis' ) ;
181
171
182
172
/**
0 commit comments