Skip to content

Commit 2be0b63

Browse files
authored
chore: replace a real IRole with a type intersection (#35770)
This replaces a real `IRole` instance with a type intersection (`IRoleRef & IGrantable`, which is good enough for 90% of cases). Just on StateMachine, to test it out. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fd5ff76 commit 2be0b63

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@types/prettier": "2.6.0",
2222
"@yarnpkg/lockfile": "^1.1.0",
2323
"aws-sdk-js-codemod": "^2.4.5",
24-
"cdk-generate-synthetic-examples": "^0.2.29",
24+
"cdk-generate-synthetic-examples": "^0.2.32",
2525
"conventional-changelog-cli": "^2.2.2",
2626
"fs-extra": "^9.1.0",
2727
"graceful-fs": "^4.2.11",

packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export interface StateMachineProps {
117117
*
118118
* @default A role is automatically created
119119
*/
120-
readonly role?: iam.IRole;
120+
readonly role?: iam.IRoleRef & iam.IGrantable;
121121

122122
/**
123123
* Maximum run time for this state machine
@@ -427,11 +427,6 @@ export class StateMachine extends StateMachineBase {
427427
*/
428428
public static readonly PROPERTY_INJECTION_ID: string = 'aws-cdk-lib.aws-stepfunctions.StateMachine';
429429

430-
/**
431-
* Execution role of this state machine
432-
*/
433-
public readonly role: iam.IRole;
434-
435430
/**
436431
* The name of the state machine
437432
* @attribute
@@ -455,6 +450,11 @@ export class StateMachine extends StateMachineBase {
455450
*/
456451
public readonly stateMachineRevisionId: string;
457452

453+
/**
454+
* Execution role of this state machine
455+
*/
456+
private readonly _role: iam.IRoleRef & iam.IGrantable;
457+
458458
constructor(scope: Construct, id: string, props: StateMachineProps) {
459459
super(scope, id, {
460460
physicalName: props.stateMachineName,
@@ -476,7 +476,7 @@ export class StateMachine extends StateMachineBase {
476476
this.validateLogOptions(props.logs);
477477
}
478478

479-
this.role = props.role || new iam.Role(this, 'Role', {
479+
this._role = props.role || new iam.Role(this, 'Role', {
480480
assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
481481
});
482482

@@ -494,7 +494,7 @@ export class StateMachine extends StateMachineBase {
494494
}
495495

496496
if (props.encryptionConfiguration instanceof CustomerManagedEncryptionConfiguration) {
497-
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
497+
this._role.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
498498
effect: iam.Effect.ALLOW,
499499
actions: [
500500
'kms:Decrypt', 'kms:GenerateDataKey',
@@ -513,7 +513,7 @@ export class StateMachine extends StateMachineBase {
513513
}));
514514

515515
if (props.logs && props.logs.level !== LogLevel.OFF) {
516-
this.role.addToPrincipalPolicy(new iam.PolicyStatement({
516+
this._role.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
517517
effect: iam.Effect.ALLOW,
518518
actions: [
519519
'kms:GenerateDataKey',
@@ -540,10 +540,10 @@ export class StateMachine extends StateMachineBase {
540540
const resource = new CfnStateMachine(this, 'Resource', {
541541
stateMachineName: this.physicalName,
542542
stateMachineType: props.stateMachineType ?? undefined,
543-
roleArn: this.role.roleArn,
543+
roleArn: this._role.roleRef.roleArn,
544544
loggingConfiguration: props.logs ? this.buildLoggingConfiguration(props.logs) : undefined,
545545
tracingConfiguration: this.buildTracingConfiguration(props.tracingEnabled),
546-
...definitionBody.bind(this, this.role, props, graph),
546+
...definitionBody.bind(this, this._role.grantPrincipal, props, graph),
547547
definitionSubstitutions: props.definitionSubstitutions,
548548
encryptionConfiguration: buildEncryptionConfiguration(props.encryptionConfiguration),
549549
});
@@ -569,15 +569,27 @@ export class StateMachine extends StateMachineBase {
569569
* The principal this state machine is running as
570570
*/
571571
public get grantPrincipal() {
572-
return this.role.grantPrincipal;
572+
return this._role.grantPrincipal;
573+
}
574+
575+
/**
576+
* Execution role of this state machine
577+
*
578+
* Will throw if the Role object that was given does not implement IRole
579+
*/
580+
public get role(): iam.IRole {
581+
if (!isIRole(this._role)) {
582+
throw new ValidationError(`The role given to this StateMachine is not an IRole, but ${this._role.constructor.name}`, this);
583+
}
584+
return this._role;
573585
}
574586

575587
/**
576588
* Add the given statement to the role's policy
577589
*/
578590
@MethodMetadata()
579591
public addToRolePolicy(statement: iam.PolicyStatement) {
580-
this.role.addToPrincipalPolicy(statement);
592+
this._role.grantPrincipal.addToPrincipalPolicy(statement);
581593
}
582594

583595
private validateStateMachineName(stateMachineName: string) {
@@ -846,3 +858,9 @@ export class ChainDefinitionBody extends DefinitionBody {
846858
};
847859
}
848860
}
861+
862+
function isIRole(x: iam.IRoleRef): x is iam.IRole {
863+
const xx = x as iam.IRole;
864+
return (!!xx.addManagedPolicy && !!xx.addToPrincipalPolicy && !!xx.assumeRoleAction && !!xx.attachInlinePolicy
865+
&& !!xx.grant && !!xx.policyFragment);
866+
}

packages/awslint/bin/awslint.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
/* eslint-disable no-console */
33
import * as child_process from 'child_process';
44
import * as path from 'path';
5+
import { JsiiFeature } from '@jsii/spec';
56
import * as chalk from 'chalk';
67
import * as fs from 'fs-extra';
78
import * as reflect from 'jsii-reflect';
89
import * as yargs from 'yargs';
910
import { ALL_RULES_LINTER, DiagnosticLevel, RuleFilterSet } from '../lib';
1011

12+
const FEATURES: JsiiFeature[] = ['intersection-types'];
13+
1114
let stackTrace = false;
1215

1316
async function main() {
@@ -247,7 +250,7 @@ main().catch(e => {
247250

248251
async function loadModule(dir: string) {
249252
const ts = new reflect.TypeSystem();
250-
await ts.load(dir, { validate: false }); // Don't validate to save 66% of execution time (20s vs 1min).
253+
await ts.load(dir, { validate: false, supportedFeatures: FEATURES }); // Don't validate to save 66% of execution time (20s vs 1min).
251254
// We run 'awslint' during build time, assemblies are guaranteed to be ok.
252255

253256
// We won't load any more assemblies. Lock the typesystem to benefit from performance improvements.

packages/awslint/lib/rules/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ apiLinter.add({
139139
return;
140140
}
141141

142+
if (type.intersectionOfTypes) {
143+
// Type intersections are okay
144+
return;
145+
}
146+
142147
throw new Error(`invalid type reference: ${type.toString()}`);
143148
}
144149
},

scripts/run-rosetta.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ time $ROSETTA extract \
7373

7474
if $infuse; then
7575
echo "💎 Generating synthetic examples for the remainder" >&2
76-
time npx cdk-generate-synthetic-examples@^0.1.292 \
76+
time npx cdk-generate-synthetic-examples \
7777
$(cat $jsii_pkgs_file)
7878

7979
time $ROSETTA extract \

yarn.lock

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,14 +3494,6 @@
34943494
"@jridgewell/resolve-uri" "^3.1.0"
34953495
"@jridgewell/sourcemap-codec" "^1.4.14"
34963496

3497-
"@jsii/check-node@1.113.0":
3498-
version "1.113.0"
3499-
resolved "https://registry.npmjs.org/@jsii/check-node/-/check-node-1.113.0.tgz#13075880ea626ba8738ee3a220fa6d83fe710091"
3500-
integrity sha512-6iPLiQiSVn8/D89ycIpj78cMfmxOIU/F9RUTVYwLqKPw4cxpR+BCC4N83WKyGkZxhOxULLa9f5q+rkWq/vAMpA==
3501-
dependencies:
3502-
chalk "^4.1.2"
3503-
semver "^7.7.2"
3504-
35053497
"@jsii/check-node@1.114.1":
35063498
version "1.114.1"
35073499
resolved "https://registry.npmjs.org/@jsii/check-node/-/check-node-1.114.1.tgz#9ff05988721cdee3d295b80a131e10e449e0571d"
@@ -3533,7 +3525,7 @@
35333525
dependencies:
35343526
ajv "^8.17.1"
35353527

3536-
"@jsii/spec@1.115.0", "@jsii/spec@^1.114.1":
3528+
"@jsii/spec@1.115.0", "@jsii/spec@^1.114.1", "@jsii/spec@^1.115.0":
35373529
version "1.115.0"
35383530
resolved "https://registry.npmjs.org/@jsii/spec/-/spec-1.115.0.tgz#be0818bd31509687cb6445873d1ec65e8d24bd24"
35393531
integrity sha512-qnicuByM0G5L6ZF2yO/e5cHwT6pb5E0aUvgHBycFPHBkgf5yjtvm+Wtk6q61srNuRASYI25BiTOonyABNeRjlw==
@@ -3547,13 +3539,6 @@
35473539
dependencies:
35483540
ajv "^8.17.1"
35493541

3550-
"@jsii/spec@^1.113.0":
3551-
version "1.113.0"
3552-
resolved "https://registry.npmjs.org/@jsii/spec/-/spec-1.113.0.tgz#cc9960a69c285466088c370f8e47d07c5ae73fb4"
3553-
integrity sha512-OAQNfJHzMmE42ySJpelOFFKCgnh6hxcKLnmgtaYEzsFW9UxH9gc945FFOXff52GbhUcigGElCqJ3MclrbgXoGw==
3554-
dependencies:
3555-
ajv "^8.17.1"
3556-
35573542
"@lerna/create@8.2.4":
35583543
version "8.2.4"
35593544
resolved "https://registry.npmjs.org/@lerna/create/-/create-8.2.4.tgz#59a050f58681e9236db38cc5bcc6986ae79d1389"
@@ -6442,13 +6427,13 @@ case@1.6.3, case@^1.6.3:
64426427
resolved "https://registry.npmjs.org/case/-/case-1.6.3.tgz#0a4386e3e9825351ca2e6216c60467ff5f1ea1c9"
64436428
integrity sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ==
64446429

6445-
cdk-generate-synthetic-examples@^0.2.29:
6446-
version "0.2.29"
6447-
resolved "https://registry.npmjs.org/cdk-generate-synthetic-examples/-/cdk-generate-synthetic-examples-0.2.29.tgz#5dae1ec8f43b6797259e081eb6c6aa69e16158cd"
6448-
integrity sha512-JP3/2wL0drTcQLgoEJeewWCneBYEcOWmLtwPvvScvXNt3lRApOfhBGcqplM7w/YS2pgKAzort2w2tSzFBTmlzA==
6430+
cdk-generate-synthetic-examples@^0.2.32:
6431+
version "0.2.32"
6432+
resolved "https://registry.npmjs.org/cdk-generate-synthetic-examples/-/cdk-generate-synthetic-examples-0.2.32.tgz#e79900e3b1f6ecc7fdb5141086561eb847fd9184"
6433+
integrity sha512-Uj7s8dHNv2xUM3E9iLMeSaNwWp1oJ3PmW3fZQ3wlyhUoAielOJP2eH3aMsY81FNy7vJa5KxvGZjWHi101stuWQ==
64496434
dependencies:
6450-
"@jsii/spec" "^1.113.0"
6451-
jsii-reflect "^1.113.0"
6435+
"@jsii/spec" "^1.115.0"
6436+
jsii-reflect "^1.115.0"
64526437
yargs "^17.7.2"
64536438

64546439
cdk8s-plus-27@2.9.5:
@@ -10121,17 +10106,17 @@ jsii-reflect@1.116.0, jsii-reflect@^1.116.0:
1012110106
oo-ascii-tree "^1.116.0"
1012210107
yargs "^17.7.2"
1012310108

10124-
jsii-reflect@^1.113.0:
10125-
version "1.113.0"
10126-
resolved "https://registry.npmjs.org/jsii-reflect/-/jsii-reflect-1.113.0.tgz#35f6e2c9e285710edbebeb4c405b8c1fde0e9cba"
10127-
integrity sha512-YS0ewXfjFGTuvp8Rrd40yhPc9yYIoF2zMu8xZpWbdwCtgBabAfzra5mwNPeLBWvR4qI4PHE+A2qEGarx3Tufnw==
10109+
jsii-reflect@^1.115.0:
10110+
version "1.115.0"
10111+
resolved "https://registry.npmjs.org/jsii-reflect/-/jsii-reflect-1.115.0.tgz#debe523fa2de0ba020d54d41a2f7b0e0bc8ef048"
10112+
integrity sha512-svWvulZ8IH035sLR0aEt3UiN4Ejqh99zSBfVTFsr3bjBfDZqcxIlExFI/fMb7O+a7XwT0WdTakTuX/aajQt3cg==
1012810113
dependencies:
10129-
"@jsii/check-node" "1.113.0"
10130-
"@jsii/spec" "^1.113.0"
10114+
"@jsii/check-node" "1.115.0"
10115+
"@jsii/spec" "1.115.0"
1013110116
chalk "^4"
1013210117
fs-extra "^10.1.0"
10133-
oo-ascii-tree "^1.113.0"
10134-
yargs "^16.2.0"
10118+
oo-ascii-tree "^1.115.0"
10119+
yargs "^17.7.2"
1013510120

1013610121
jsii-rosetta@~5.9.9:
1013710122
version "5.9.9"
@@ -11757,10 +11742,10 @@ onetime@^5.1.0, onetime@^5.1.2:
1175711742
dependencies:
1175811743
mimic-fn "^2.1.0"
1175911744

11760-
oo-ascii-tree@^1.113.0:
11761-
version "1.113.0"
11762-
resolved "https://registry.npmjs.org/oo-ascii-tree/-/oo-ascii-tree-1.113.0.tgz#69b40e2b52d812be78141119d3010f62f3d198fe"
11763-
integrity sha512-9hGp+3S8qy0MSdBzp5pX2448Iv+w6QyXI6KBVihdt+Sb8nw1MxNu6ErMadTAXmyfCwZzZoEpn9hybTHEQuSJcQ==
11745+
oo-ascii-tree@^1.115.0:
11746+
version "1.115.0"
11747+
resolved "https://registry.npmjs.org/oo-ascii-tree/-/oo-ascii-tree-1.115.0.tgz#60a28f29cc449cd4274140f5d939c08a18c5ac8b"
11748+
integrity sha512-KbZpipKiCQ5Ws2GryfOUWBxuVpVYosqAi85mtplgMyhQuueOZAO8qeTmNqYYHUxBHOz9O+kKCCjidEkLmCDhLQ==
1176411749

1176511750
oo-ascii-tree@^1.116.0:
1176611751
version "1.116.0"

0 commit comments

Comments
 (0)