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

Issues porting MFA policy example to the CDK #3128

Closed
1 task done
joelhegg opened this issue Jun 28, 2019 · 4 comments
Closed
1 task done

Issues porting MFA policy example to the CDK #3128

joelhegg opened this issue Jun 28, 2019 · 4 comments
Assignees
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management feature-request A feature should be added or improved. management/tracking Issues that track a subject or multiple issues

Comments

@joelhegg
Copy link

  • I'm submitting a ...

    • 📚 construct library gap
  • Please tell us about your environment:

    • CDK CLI Version: 0.36.0
    • Module Version: 0.36.0
    • OS: [OSX Mojave]
    • Language: [Java]
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

I needed to use https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_aws_my-sec-creds-self-manage.html in the CDK. I ran into a number of dead ends along the way. Some are known issues, some are new. I figured it would be useful to share this real world example that ties them all together.

  1. Can't just port the policy to the CDK

Due to #964, there's no straight forward way to express NotAction in the CDK. Since I'm using Java, the "hack" one person gave would be more involved.

  1. Unclear how to attach an inline policy through a CfnInclude

I ended up just putting the example policy in a .json file along withCfnInclude like so:

ObjectMapper mapper = new ObjectMapper();
new CfnInclude(this, "force-mfa", CfnIncludeProps.builder()
    .withTemplate((ObjectNode)mapper.readTree(this.getClass().getResourceAsStream("/force-mfa.json")))
    .build());

My initial inclination was to use Group.attachInlinePolicy, but I couldn't figure out how to go from the CfnInclude to an actual Policy object. It feels like there should be a straight forward way to deserialize a CloudFormation template into a CDK object to allow for better interop.

  1. Difficulties using custom managed policies

Since inline policies weren't going to work for me, I switched to a custom managed policy by prepending the example json with this:

{
  "Resources": {
    "forcemfa": {
      "Type": "AWS::IAM::ManagedPolicy",
      "Properties": {
        "ManagedPolicyName": "force-mfa",
        "PolicyDocument": 

I ran into #2974 trying to use GroupProps.withManagedPolicyArns. I thought I might just adapt ManagedPolicy.fromAwsManagedPolicyName like so:

    IManagedPolicy fromCustomManagedPolicyName(String managedPolicyName) {
        return () -> Lazy.stringValue((IResolveContext ctx) -> {
            Stack stack = Stack.of(ctx.getScope());
            return stack.formatArn(ArnComponents.builder()
                    .withService("iam")
                    .withRegion("")
                    .withAccount(stack.getAccount())
                    .withResource("policy")
                    .withResourceName(managedPolicyName)
                    .build());
        });
    }

That gave me this error though. I'm sure there must be another step I'm not aware of to enable resolution with this code. It wasn't immediately clear from reading through the CDK code and documentation.

An exception occured while executing the Java class. Resolution error: Resolution error: Resolution error: Resolution error: JSII kernel assumption violated, undefined is not an object.
Object creation stack:
  at new LazyBase (.../node_modules/@aws-cdk/core/lib/lazy.js:30:44)
  at new LazyString (.../node_modules/@aws-cdk/core/lib/lazy.js:46:9)
  at Function.stringValue (.../node_modules/@aws-cdk/core/lib/lazy.js:13:39)

What ended up working is something like this:

Group group = new Group(this, "my-group", GroupProps.builder()
    .withGroupName("my-group")
    .withManagedPolicyArns(Arrays.asList(
        ManagedPolicy.fromAwsManagedPolicyName("AWSCodeCommitPowerUser"),
        (IManagedPolicy) () -> String.format("arn:aws:iam::%s:policy/force-mfa", this.getAccount())
    ))
    .build());

Incidentally, I think the cast usage makes that a little uglier than it should be. This seems to be caused by GroupProps using an Object/any where Group uses IManagedPolicy.

@joelhegg joelhegg added the needs-triage This issue or PR still needs to be triaged. label Jun 28, 2019
@NGL321 NGL321 added feature-request A feature should be added or improved. gap @aws-cdk/aws-iam Related to AWS Identity and Access Management management/tracking Issues that track a subject or multiple issues and removed needs-triage This issue or PR still needs to be triaged. labels Jul 1, 2019
@NGL321
Copy link
Contributor

NGL321 commented Jul 1, 2019

Hi @joelhegg,

Thank you for posting this with so much detail. It seems that this is a fairly broad gap that is being encountered rather frequently, so due to the detail of this post I am going to mark this as the tracking issue for the policy handling gap.

@NGL321
Copy link
Contributor

NGL321 commented Jul 1, 2019

#3112 is also relevant here

@NGL321 NGL321 mentioned this issue Jul 1, 2019
5 tasks
@sdole
Copy link

sdole commented Jul 12, 2019

Hello, I saw a blog saying that the cdk is now GA for Typescript and Python. Does it mean these gaps have been fixed?

@rix0rrr
Copy link
Contributor

rix0rrr commented Jan 23, 2020

I believe all pain points have been addressed by now.

@rix0rrr rix0rrr closed this as completed Jan 23, 2020
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 feature-request A feature should be added or improved. management/tracking Issues that track a subject or multiple issues
Projects
None yet
Development

No branches or pull requests

4 participants