|
1 | 1 | import * as path from 'path';
|
2 |
| -import { Match, Template } from '@aws-cdk/assertions'; |
| 2 | +import { Annotations, Match, Template } from '@aws-cdk/assertions'; |
3 | 3 | import { ProfilingGroup } from '@aws-cdk/aws-codeguruprofiler';
|
4 | 4 | import * as ec2 from '@aws-cdk/aws-ec2';
|
5 | 5 | import * as efs from '@aws-cdk/aws-efs';
|
@@ -435,6 +435,154 @@ describe('function', () => {
|
435 | 435 | // THEN
|
436 | 436 | Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
|
437 | 437 | });
|
| 438 | + |
| 439 | + describe('annotations on different IFunctions', () => { |
| 440 | + let stack: cdk.Stack; |
| 441 | + let fn: lambda.Function; |
| 442 | + let warningMessage: string; |
| 443 | + beforeEach(() => { |
| 444 | + warningMessage = 'AWS Lambda has changed their authorization strategy'; |
| 445 | + stack = new cdk.Stack(); |
| 446 | + fn = new lambda.Function(stack, 'MyLambda', { |
| 447 | + code: lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler')), |
| 448 | + handler: 'index.handler', |
| 449 | + runtime: lambda.Runtime.PYTHON_3_6, |
| 450 | + }); |
| 451 | + }); |
| 452 | + |
| 453 | + describe('permissions on functions', () => { |
| 454 | + test('without lambda:InvokeFunction', () => { |
| 455 | + // WHEN |
| 456 | + fn.addPermission('MyPermission', { |
| 457 | + action: 'lambda.GetFunction', |
| 458 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 459 | + }); |
| 460 | + |
| 461 | + // Simulate a workflow where a user has created a currentVersion with the intent to invoke it later. |
| 462 | + fn.currentVersion; |
| 463 | + |
| 464 | + // THEN |
| 465 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda', Match.stringLikeRegexp(warningMessage)); |
| 466 | + }); |
| 467 | + |
| 468 | + describe('with lambda:InvokeFunction', () => { |
| 469 | + test('without invoking currentVersion', () => { |
| 470 | + // WHEN |
| 471 | + fn.addPermission('MyPermission', { |
| 472 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 473 | + }); |
| 474 | + |
| 475 | + // THEN |
| 476 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda', Match.stringLikeRegexp(warningMessage)); |
| 477 | + }); |
| 478 | + |
| 479 | + test('with currentVersion invoked first', () => { |
| 480 | + // GIVEN |
| 481 | + // Simulate a workflow where a user has created a currentVersion with the intent to invoke it later. |
| 482 | + fn.currentVersion; |
| 483 | + |
| 484 | + // WHEN |
| 485 | + fn.addPermission('MyPermission', { |
| 486 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 487 | + }); |
| 488 | + |
| 489 | + // THEN |
| 490 | + Annotations.fromStack(stack).hasWarning('/Default/MyLambda', Match.stringLikeRegexp(warningMessage)); |
| 491 | + }); |
| 492 | + |
| 493 | + test('with currentVersion invoked after permissions created', () => { |
| 494 | + // WHEN |
| 495 | + fn.addPermission('MyPermission', { |
| 496 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 497 | + }); |
| 498 | + |
| 499 | + // Simulate a workflow where a user has created a currentVersion after adding permissions to the function. |
| 500 | + fn.currentVersion; |
| 501 | + |
| 502 | + // THEN |
| 503 | + Annotations.fromStack(stack).hasWarning('/Default/MyLambda', Match.stringLikeRegexp(warningMessage)); |
| 504 | + }); |
| 505 | + |
| 506 | + test('multiple currentVersion calls does not result in multiple warnings', () => { |
| 507 | + // WHEN |
| 508 | + fn.currentVersion; |
| 509 | + |
| 510 | + fn.addPermission('MyPermission', { |
| 511 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 512 | + }); |
| 513 | + |
| 514 | + fn.currentVersion; |
| 515 | + |
| 516 | + // THEN |
| 517 | + const warns = Annotations.fromStack(stack).findWarning('/Default/MyLambda', Match.stringLikeRegexp(warningMessage)); |
| 518 | + expect(warns).toHaveLength(1); |
| 519 | + }); |
| 520 | + }); |
| 521 | + }); |
| 522 | + |
| 523 | + test('permission on versions', () => { |
| 524 | + // GIVEN |
| 525 | + const version = new lambda.Version(stack, 'MyVersion', { |
| 526 | + lambda: fn.currentVersion, |
| 527 | + }); |
| 528 | + |
| 529 | + // WHEN |
| 530 | + version.addPermission('MyPermission', { |
| 531 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 532 | + }); |
| 533 | + |
| 534 | + // THEN |
| 535 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyVersion', Match.stringLikeRegexp(warningMessage)); |
| 536 | + }); |
| 537 | + |
| 538 | + test('permission on latest version', () => { |
| 539 | + // WHEN |
| 540 | + fn.latestVersion.addPermission('MyPermission', { |
| 541 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 542 | + }); |
| 543 | + |
| 544 | + // THEN |
| 545 | + // cannot add permissions on latest version, so no warning necessary |
| 546 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyLambda/$LATEST', Match.stringLikeRegexp(warningMessage)); |
| 547 | + }); |
| 548 | + |
| 549 | + describe('permission on alias', () => { |
| 550 | + test('of current version', () => { |
| 551 | + // GIVEN |
| 552 | + const version = new lambda.Version(stack, 'MyVersion', { |
| 553 | + lambda: fn.currentVersion, |
| 554 | + }); |
| 555 | + const alias = new lambda.Alias(stack, 'MyAlias', { |
| 556 | + aliasName: 'alias', |
| 557 | + version, |
| 558 | + }); |
| 559 | + |
| 560 | + // WHEN |
| 561 | + alias.addPermission('MyPermission', { |
| 562 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 563 | + }); |
| 564 | + |
| 565 | + // THEN |
| 566 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyAlias', Match.stringLikeRegexp(warningMessage)); |
| 567 | + }); |
| 568 | + |
| 569 | + test('of latest version', () => { |
| 570 | + // GIVEN |
| 571 | + const alias = new lambda.Alias(stack, 'MyAlias', { |
| 572 | + aliasName: 'alias', |
| 573 | + version: fn.latestVersion, |
| 574 | + }); |
| 575 | + |
| 576 | + // WHEN |
| 577 | + alias.addPermission('MyPermission', { |
| 578 | + principal: new iam.ServicePrincipal('lambda.amazonaws.com'), |
| 579 | + }); |
| 580 | + |
| 581 | + // THEN |
| 582 | + Annotations.fromStack(stack).hasNoWarning('/Default/MyAlias', Match.stringLikeRegexp(warningMessage)); |
| 583 | + }); |
| 584 | + }); |
| 585 | + }); |
438 | 586 | });
|
439 | 587 |
|
440 | 588 | test('Lambda code can be read from a local directory via an asset', () => {
|
|
0 commit comments