@@ -24,6 +24,13 @@ export interface CommonGrantOptions {
24
24
* The resource ARNs to grant to
25
25
*/
26
26
readonly resourceArns : string [ ] ;
27
+
28
+ /**
29
+ * Any conditions to attach to the grant
30
+ *
31
+ * @default - No conditions
32
+ */
33
+ readonly conditions ?: Record < string , Record < string , unknown > > ;
27
34
}
28
35
29
36
/**
@@ -160,6 +167,7 @@ export class Grant implements IDependable {
160
167
const statement = new PolicyStatement ( {
161
168
actions : options . actions ,
162
169
resources : options . resourceArns ,
170
+ conditions : options . conditions ,
163
171
} ) ;
164
172
165
173
const addedToPrincipal = options . grantee . grantPrincipal . addToPrincipalPolicy ( statement ) ;
@@ -224,17 +232,27 @@ export class Grant implements IDependable {
224
232
/**
225
233
* The statement that was added to the principal's policy
226
234
*
227
- * Can be accessed to (e.g.) add additional conditions to the statement.
235
+ * @deprecated Use `principalStatements` instead
228
236
*/
229
237
public readonly principalStatement ?: PolicyStatement ;
230
238
239
+ /**
240
+ * The statements that were added to the principal's policy
241
+ */
242
+ public readonly principalStatements = new Array < PolicyStatement > ( ) ;
243
+
231
244
/**
232
245
* The statement that was added to the resource policy
233
246
*
234
- * Can be accessed to (e.g.) add additional conditions to the statement.
247
+ * @deprecated Use `resourceStatements` instead
235
248
*/
236
249
public readonly resourceStatement ?: PolicyStatement ;
237
250
251
+ /**
252
+ * The statements that were added to the principal's policy
253
+ */
254
+ public readonly resourceStatements = new Array < PolicyStatement > ( ) ;
255
+
238
256
/**
239
257
* The options originally used to set this result
240
258
*
@@ -243,14 +261,26 @@ export class Grant implements IDependable {
243
261
*/
244
262
private readonly options : CommonGrantOptions ;
245
263
264
+ private readonly dependables = new Array < IDependable > ( ) ;
265
+
246
266
private constructor ( props : GrantProps ) {
247
267
this . options = props . options ;
248
268
this . principalStatement = props . principalStatement ;
249
269
this . resourceStatement = props . resourceStatement ;
270
+ if ( this . principalStatement ) {
271
+ this . principalStatements . push ( this . principalStatement ) ;
272
+ }
273
+ if ( this . resourceStatement ) {
274
+ this . resourceStatements . push ( this . resourceStatement ) ;
275
+ }
276
+ if ( props . policyDependable ) {
277
+ this . dependables . push ( props . policyDependable ) ;
278
+ }
250
279
280
+ const self = this ;
251
281
Dependable . implement ( this , {
252
282
get dependencyRoots ( ) {
253
- return props . policyDependable ? Dependable . of ( props . policyDependable ) . dependencyRoots : [ ] ;
283
+ return Array . from ( new Set ( self . dependables . flatMap ( d => Dependable . of ( d ) . dependencyRoots ) ) ) ;
254
284
} ,
255
285
} ) ;
256
286
}
@@ -282,6 +312,24 @@ export class Grant implements IDependable {
282
312
construct . node . addDependency ( this ) ;
283
313
}
284
314
}
315
+
316
+ /**
317
+ * Combine two grants into a new one
318
+ */
319
+ public combine ( rhs : Grant ) {
320
+ const combinedPrinc = [ ...this . principalStatements , ...rhs . principalStatements ] ;
321
+ const combinedRes = [ ...this . resourceStatements , ...rhs . resourceStatements ] ;
322
+
323
+ const ret = new Grant ( {
324
+ options : this . options ,
325
+ principalStatement : combinedPrinc [ 0 ] ,
326
+ resourceStatement : combinedRes [ 0 ] ,
327
+ } ) ;
328
+ ret . principalStatements . splice ( 0 , ret . principalStatements . length , ...combinedPrinc ) ;
329
+ ret . resourceStatements . splice ( 0 , ret . resourceStatements . length , ...combinedRes ) ;
330
+ ret . dependables . push ( ...this . dependables , ...rhs . dependables ) ;
331
+ return ret ;
332
+ }
285
333
}
286
334
287
335
function describeGrant ( options : CommonGrantOptions ) {
0 commit comments