Skip to content

Commit d4fb93c

Browse files
authored
Merge branch 'main' into merge-back/2.56.1
2 parents 9329a73 + 861774b commit d4fb93c

File tree

239 files changed

+4973
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+4973
-296
lines changed

packages/@aws-cdk/aws-iam/lib/grant.ts

+51-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export interface CommonGrantOptions {
2424
* The resource ARNs to grant to
2525
*/
2626
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>>;
2734
}
2835

2936
/**
@@ -160,6 +167,7 @@ export class Grant implements IDependable {
160167
const statement = new PolicyStatement({
161168
actions: options.actions,
162169
resources: options.resourceArns,
170+
conditions: options.conditions,
163171
});
164172

165173
const addedToPrincipal = options.grantee.grantPrincipal.addToPrincipalPolicy(statement);
@@ -224,17 +232,27 @@ export class Grant implements IDependable {
224232
/**
225233
* The statement that was added to the principal's policy
226234
*
227-
* Can be accessed to (e.g.) add additional conditions to the statement.
235+
* @deprecated Use `principalStatements` instead
228236
*/
229237
public readonly principalStatement?: PolicyStatement;
230238

239+
/**
240+
* The statements that were added to the principal's policy
241+
*/
242+
public readonly principalStatements = new Array<PolicyStatement>();
243+
231244
/**
232245
* The statement that was added to the resource policy
233246
*
234-
* Can be accessed to (e.g.) add additional conditions to the statement.
247+
* @deprecated Use `resourceStatements` instead
235248
*/
236249
public readonly resourceStatement?: PolicyStatement;
237250

251+
/**
252+
* The statements that were added to the principal's policy
253+
*/
254+
public readonly resourceStatements = new Array<PolicyStatement>();
255+
238256
/**
239257
* The options originally used to set this result
240258
*
@@ -243,14 +261,26 @@ export class Grant implements IDependable {
243261
*/
244262
private readonly options: CommonGrantOptions;
245263

264+
private readonly dependables = new Array<IDependable>();
265+
246266
private constructor(props: GrantProps) {
247267
this.options = props.options;
248268
this.principalStatement = props.principalStatement;
249269
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+
}
250279

280+
const self = this;
251281
Dependable.implement(this, {
252282
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)));
254284
},
255285
});
256286
}
@@ -282,6 +312,24 @@ export class Grant implements IDependable {
282312
construct.node.addDependency(this);
283313
}
284314
}
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+
}
285333
}
286334

287335
function describeGrant(options: CommonGrantOptions) {

packages/@aws-cdk/aws-iam/test/grant.test.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Template } from '@aws-cdk/assertions';
1+
import { Template, Match } from '@aws-cdk/assertions';
22
import { CfnResource, Resource, Stack } from '@aws-cdk/core';
33
import { Construct } from 'constructs';
44
import * as iam from '../lib';
@@ -97,6 +97,34 @@ describe('Grant dependencies', () => {
9797
expectDependencyOn('RoleDefaultPolicy5FFB7DAB');
9898
expectDependencyOn('BuckarooPolicy74174DA4');
9999
});
100+
101+
test('can combine two grants', () => {
102+
// GIVEN
103+
const role1 = new iam.Role(stack, 'Role1', {
104+
assumedBy: new iam.ServicePrincipal('bla.amazonaws.com'),
105+
});
106+
const role2 = new iam.Role(stack, 'Role2', {
107+
assumedBy: new iam.ServicePrincipal('bla.amazonaws.com'),
108+
});
109+
110+
// WHEN
111+
const g1 = iam.Grant.addToPrincipal({
112+
actions: ['service:DoAThing'],
113+
grantee: role1,
114+
resourceArns: ['*'],
115+
});
116+
const g2 = iam.Grant.addToPrincipal({
117+
actions: ['service:DoAThing'],
118+
grantee: role2,
119+
resourceArns: ['*'],
120+
});
121+
122+
(g1.combine(g2)).applyBefore(resource);
123+
124+
// THEN
125+
expectDependencyOn('Role1DefaultPolicyD3EF4D0A');
126+
expectDependencyOn('Role2DefaultPolicy3A7A0A1B');
127+
});
100128
});
101129

102130
function applyGrantWithDependencyTo(principal: iam.IPrincipal) {
@@ -108,8 +136,8 @@ function applyGrantWithDependencyTo(principal: iam.IPrincipal) {
108136
}
109137

110138
function expectDependencyOn(id: string) {
111-
Template.fromStack(stack).hasResource('CDK::Test::SomeResource', (props: any) => {
112-
return (props?.DependsOn ?? []).includes(id);
139+
Template.fromStack(stack).hasResource('CDK::Test::SomeResource', {
140+
DependsOn: Match.arrayWith([id]),
113141
});
114142
}
115143

packages/@aws-cdk/aws-lambda/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,55 @@ const fn = new lambda.Function(this, 'MyFunction', {
764764
See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html)
765765
to learn more about AWS Lambda's X-Ray support.
766766

767+
## Lambda with AWS Distro for OpenTelemetry layer
768+
769+
To have automatic integration with XRay without having to add dependencies or change your code, you can use the
770+
[AWS Distro for OpenTelemetry Lambda (ADOT) layer](https://aws-otel.github.io/docs/getting-started/lambda).
771+
Consuming the latest ADOT layer can be done with the following snippet:
772+
773+
```ts
774+
import {
775+
AdotLambdaExecWrapper,
776+
AdotLayerVersion,
777+
AdotLambdaLayerJavaScriptSdkVersion,
778+
} from 'aws-cdk-lib/aws-lambda';
779+
780+
const fn = new lambda.Function(this, 'MyFunction', {
781+
runtime: lambda.Runtime.NODEJS_18_X,
782+
handler: 'index.handler',
783+
code: lambda.Code.fromInline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
784+
adotInstrumentation: {
785+
layerVersion: AdotLayerVersion.fromJavaScriptSdkLayerVersion(AdotLambdaLayerJavaScriptSdkVersion.LATEST),
786+
execWrapper: AdotLambdaExecWrapper.REGULAR_HANDLER,
787+
},
788+
});
789+
```
790+
791+
To use a different layer version, use one of the following helper functions for the `layerVersion` prop:
792+
793+
* `AdotLayerVersion.fromJavaScriptSdkLayerVersion`
794+
* `AdotLayerVersion.fromPythonSdkLayerVersion`
795+
* `AdotLayerVersion.fromJavaSdkLayerVersion`
796+
* `AdotLayerVersion.fromJavaAutoInstrumentationSdkLayerVersion`
797+
* `AdotLayerVersion.fromGenericSdkLayerVersion`
798+
799+
Each helper function expects a version value from a corresponding enum-like class as below:
800+
801+
* `AdotLambdaLayerJavaScriptSdkVersion`
802+
* `AdotLambdaLayerPythonSdkVersion`
803+
* `AdotLambdaLayerJavaSdkVersion`
804+
* `AdotLambdaLayerJavaAutoInstrumentationSdkVersion`
805+
* `AdotLambdaLayerGenericSdkVersion`
806+
807+
For more examples, see our [the integration test](test/integ.lambda-adot.ts).
808+
809+
If you want to retrieve the ARN of the ADOT Lambda layer without enabling ADOT in a Lambda function:
810+
811+
```ts
812+
declare const fn: lambda.Function;
813+
const layerArn = lambda.AdotLambdaLayerJavaSdkVersion.V1_19_0.layerArn(fn.stack, fn.architecture);
814+
```
815+
767816
## Lambda with Profiling
768817

769818
The following code configures the lambda function with CodeGuru profiling. By default, this creates a new CodeGuru

0 commit comments

Comments
 (0)