Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role.addToPolicy does not exist for imported roles #24195

Open
jatinmehrotra opened this issue Feb 16, 2023 · 3 comments
Open

Role.addToPolicy does not exist for imported roles #24195

jatinmehrotra opened this issue Feb 16, 2023 · 3 comments
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. p2

Comments

@jatinmehrotra
Copy link

Describe the bug

I am trying to import the role, however just like #8307 addTopolicy does not exist on imported role.

Expected Behavior

addToPolicy should have been working on the import role.

Current Behavior

The method does not exist

Reproduction Steps

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3'
import { Effect, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { IRole } from 'aws-cdk-lib/aws-iam';

export class CdkStackoverflowStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);


    const statement = new PolicyStatement({
      effect: Effect.ALLOW,
      actions: ['execute-api:Invoke'],
      resources: [
        //SOME RESOURCE
      ]
    })
    const role_name = 'my_role_for_ec2_testing'
    const role = Role.fromRoleName(this, 'Role', role_name)
  }

  role.addToPolicy(statement)




}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.44.0

Framework Version

No response

Node.js Version

16.17.0

OS

macos

Language

Typescript

Language Version

No response

Other information

No response

@jatinmehrotra jatinmehrotra added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 16, 2023
@github-actions github-actions bot added the @aws-cdk/aws-iam Related to AWS Identity and Access Management label Feb 16, 2023
@pahud
Copy link
Contributor

pahud commented Feb 16, 2023

This is related to #8307 but not duplicated. I am leaving it open as p2 and all upvotes on this issue would help us prioritize. Thank you.

@pahud pahud added p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 16, 2023
@peterwoodworth
Copy link
Contributor

Generally, reports like this aren't bugs because imported resources are known to have limited functionalities, and we also don't claim to support this method on imported roles in our documentation.

From the developer guide:

Although you can use an external resource anywhere you'd use a similar resource defined in your AWS CDK app, you cannot modify it. For example, calling addToResourcePolicy (Python: add_to_resource_policy) on an external s3.Bucket does nothing.

However, I looked a little bit into this and it looks like the ImportedRole that we return actually does have this method: this is its implementation

public addToPolicy(statement: PolicyStatement): boolean {
return this.addToPrincipalPolicy(statement).statementAdded;
}
public addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult {
if (!this.defaultPolicy) {
const useUniqueName = FeatureFlags.of(this).isEnabled(IAM_IMPORTED_ROLE_STACK_SAFE_DEFAULT_POLICY_NAME);
const defaultDefaultPolicyName = useUniqueName
? `Policy${Names.uniqueId(this)}`
: 'Policy';
const policyName = this.defaultPolicyName ?? defaultDefaultPolicyName;
this.defaultPolicy = new Policy(this, policyName, useUniqueName ? { policyName } : undefined);
this.attachInlinePolicy(this.defaultPolicy);
}
this.defaultPolicy.addStatements(statement);
return { statementAdded: true, policyDependable: this.defaultPolicy };
}

As you can see, it's just calling addToPrincipalPolicy(). The implementation on the Role construct is different - at the moment I'm unsure if the functionality of the addToPolicy method is possible on the imported resource

@jatinmehrotra
Copy link
Author

jatinmehrotra commented Feb 17, 2023

@peterwoodworth @pahud I have a different interpretation to this it is not supported because fromRoleName returns an IRole interface. This interface does not have a addToPolicy method.

That is why we cannot access this method.
Is my understanding correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. p2
Projects
None yet
Development

No branches or pull requests

3 participants