You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(iam): missing validation for actions added post instantiation of a policy statement (#21906)
## Bug Description
The validation for actions/nonActions currently only exists in the constructor of the PolicyStatement class as shown below -
https://github.com/aws/aws-cdk/blob/56ba2ab2c2d9240b76ece17c3296488a63f0b232/packages/%40aws-cdk/aws-iam/lib/policy-statement.ts#L88-L95
The above validation is missing when we add an action/nonAction post instantiation of the IAM policy statement leading to discrepancy in the behaviour.
The following snippet doesn't throw any error -
```typescript
const statement = new iam.PolicyStatement({ resources: ['*'] });
statement.addActions('action');
statement.addNonActions('nonaction');
```
## Solution
- Refactored the validation in the constructor into a separate private method called `validatePolicyActions()`
- Executing this new validation method in the `addActions()` and `addNonActions()`
- Fixed existing unit tests which assumed the above behaviour
fixes#21821
----
### All Submissions:
* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)
### Adding new Unconventional Dependencies:
* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)
### New Features
* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
thrownewError(`Action '${action}' is invalid. An action string consists of a service namespace, a colon, and the name of an action. Action names can include wildcards.`);
94
-
}
95
-
}
96
-
97
89
this._sid=props.sid;
98
90
this._effect=props.effect||Effect.ALLOW;
99
91
@@ -154,6 +146,7 @@ export class PolicyStatement {
154
146
if(actions.length>0&&this._notAction.length>0){
155
147
thrownewError('Cannot add \'Actions\' to policy statement if \'NotActions\' have been added');
156
148
}
149
+
this.validatePolicyActions(actions);
157
150
this._action.push(...actions);
158
151
}
159
152
@@ -170,6 +163,7 @@ export class PolicyStatement {
170
163
if(notActions.length>0&&this._action.length>0){
171
164
thrownewError('Cannot add \'NotActions\' to policy statement if \'Actions\' have been added');
172
165
}
166
+
this.validatePolicyActions(notActions);
173
167
this._notAction.push(...notActions);
174
168
}
175
169
@@ -233,6 +227,16 @@ export class PolicyStatement {
233
227
}
234
228
}
235
229
230
+
privatevalidatePolicyActions(actions: string[]){
231
+
// In case of an unresolved list of actions return early
thrownewError(`Action '${action}' is invalid. An action string consists of a service namespace, a colon, and the name of an action. Action names can include wildcards.`);
.toThrow(`Action '${invalidAction}' is invalid. An action string consists of a service namespace, a colon, and the name of an action. Action names can include wildcards.`);
.toThrow(`Action '${invalidAction}' is invalid. An action string consists of a service namespace, a colon, and the name of an action. Action names can include wildcards.`);
225
+
});
226
+
218
227
test('multiple identical entries render to a scalar (instead of a singleton list)',()=>{
0 commit comments