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

support inline_policy and permissions boundary #20

Merged
merged 16 commits into from
Jun 11, 2024

Conversation

hacker65536
Copy link
Contributor

@hacker65536 hacker65536 commented Mar 19, 2024

This module looks great and I would love to use it.
I have modified the code a bit and would be happy to review it if you would like.

@mbuotidem
Copy link

This is a much needed addition due to the inherent difficulty of using customer managed policies.

@matiasbertani
Copy link
Contributor

This would be a great feature to have.

@novekm
Copy link
Collaborator

novekm commented May 9, 2024

/do-e2e-tests

Copy link

End to end test has been scheduled

Copy link

E2E tests in progress

aws-ia-automator-prod[bot]
aws-ia-automator-prod bot previously approved these changes May 9, 2024
Copy link

@aws-ia-automator-prod aws-ia-automator-prod bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E2E tests completed successfully

@hacker65536 hacker65536 changed the title support inline_policy support inline_policy and permissions boundary May 20, 2024
@hacker65536 hacker65536 force-pushed the inlinepolicy branch 6 times, most recently from e8b7303 to 00a6ffa Compare May 20, 2024 09:37
@hacker65536
Copy link
Contributor Author

@novekm
I have updated files to support inline policy and permissions boundary.
Please review this when you have time.

@novekm
Copy link
Collaborator

novekm commented May 22, 2024

Hi there - initially I went down the path of adding support for inline policies, however stopped as it's not an AWS best practice. It's recommended to use AWS Managed Policies, or Customer Managed policies where possible see this doc for more info on this. However, I acknowledge the current limitation with IAM IdC that the customer managed policy must exist in each account before you can actually use it outlined in this answer on AWS re:Post.

Unfortunately, there isn't a simple native way to deploy a Terraform configuration to multiple accounts, regions, etc. at once (something like CloudFormation StackSets). This means that with the current functionality of the module, you would need to first provision the customer managed policy in each account before referencing it in the module. Terraform Stacks seems to be something that will aid in these type of use cases once GA.

In the meantime, I'll review the PR for adding support for inline policies when I get some time

@novekm
Copy link
Collaborator

novekm commented May 22, 2024

@hacker65536 I see you have been doing a few additional commits to the PR. Let me know when it is ready for review

@hacker65536 hacker65536 force-pushed the inlinepolicy branch 2 times, most recently from 4331899 to 9fb26eb Compare May 23, 2024 06:13
@hacker65536
Copy link
Contributor Author

@novekm
I have done some testing with the following code. It seems to work fine.
Please review it when you have time.

example code
data "aws_organizations_organization" "org" {}
data "aws_iam_policy_document" "restrictAccessInlinePolicy" {
  statement {
    sid = "Restrict"
    actions = [
      "*",
    ]
    effect = "Deny"
    resources = [
      "*",
    ]
    condition {
      test     = "NotIpAddress"
      variable = "aws:SourceIp"
      values = [
        // replace with your own IP address
        "0.0.0.0/0",
      ]
    }
    condition {
      test     = "Bool"
      variable = "aws:ViaAWSService"
      values = [
        "false"
      ]
    }
    condition {
      test     = "StringNotLike"
      variable = "aws:userAgent"
      values = [
        "*exec-env/CloudShell*"
      ]
    }
  }
}

locals {
  active_accounts = [for a in data.aws_organizations_organization.org.accounts : a if a.status == "ACTIVE"]
  tags = {
    "Owner" = "SRE Team"
  }
}


module "aws-iam-identity-center" {
  //source = "aws-ia/iam-identity-center/aws"
  //source = "./terraform-aws-iam-identity-center"
  source = "git::https://github.com/hacker65536/terraform-aws-iam-identity-center?ref=4a55c75"


  sso_groups = {
    // sections 
    SectionSre : {
      group_name        = "SectionSre"
      group_description = "Section SRE"
    }
  }
  sso_users = {
    testuser1 : {
      group_membership = ["SectionSre", "AWSControlTowerAdmins"]
      user_name        = "testuser1"
      given_name       = "aaa"
      family_name      = "bbb"
      email            = "[email protected]"
    }
  }

  permission_sets = {

    AdministratorAccess = {
      description          = "Provides full access to AWS services and resources",
      session_duration     = "PT3H",
      aws_managed_policies = ["arn:aws:iam::aws:policy/AdministratorAccess"]
      inline_policy        = data.aws_iam_policy_document.restrictAccessInlinePolicy.json
      tags                 = local.tags
    },
    PowerUserAccess = {
      description          = "Provides full access to AWS services and resources, but does not allow management of Users and groups",
      session_duration     = "PT3H",
      aws_managed_policies = ["arn:aws:iam::aws:policy/PowerUserAccess"]
      //inline_policy        = data.aws_iam_policy_document.restrictAccessInlinePolicy.json
      tags = local.tags
    },
    ViewOnlyAccess = {
      description          = "This policy grants permissions to view resources and basic metadata across all AWS services",
      session_duration     = "PT3H",
      aws_managed_policies = ["arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"]
      //inline_policy        = data.aws_iam_policy_document.restrictAccessInlinePolicy.json
      managed_policy_arn = "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"
      permissions_boundary = {
        managed_policy_arn = "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"
        /*
        customer_managed_policy_reference = {
          name = "existing_policy_name"
          path = "/service-role/"
        }
        */
      }
      tags = local.tags
    },
    ReadOnlyAccess = {
      description          = "This policy grants permissions to view resources and basic metadata across all AWS services",
      session_duration     = "PT3H",
      aws_managed_policies = ["arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"]
      //inline_policy        = data.aws_iam_policy_document.restrictAccessInlinePolicy.json
      managed_policy_arn = "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"
      tags               = local.tags
    },
  }
  account_assignments = {
    SectionSre : {
      principal_name = "SectionSre"
      principal_type = "GROUP"
      permission_sets = [
        "AdministratorAccess",
        "PowerUserAccess",
        "ViewOnlyAccess",
        // existing permission set
        "AWSAdministratorAccess",
      ]
      account_ids = [
        // replace with your own account id
        "111111111111",
        "222222222222",
      ]
      //account_ids     = toset(local.active_accounts[*].id)
    },
    testuser1 : {
      principal_name = "testuser1"
      principal_type = "USER"
      permission_sets = [
        "AdministratorAccess",
        "PowerUserAccess",
        "ViewOnlyAccess",
        // existing permission set
        "AWSAdministratorAccess",
      ]
      account_ids = [
        // replace with your own account id
        "111111111111",
      ]
    },

  }
}

@novekm
Copy link
Collaborator

novekm commented Jun 3, 2024

Hi @hacker65536, I'm testing the PR and all seems to be well with the inline policy, however it's still mentioning the forced replacements as with the current version of the module. Is the same showing on your end? This is what I'm getting after adding an additional group:

# module.aws-iam-identity-center.aws_identitystore_group_membership.sso_group_membership["testuser1_AWSControlTowerAdmins"] must be replaced
-/+ resource "aws_identitystore_group_membership" "sso_group_membership" {
      ~ group_id          = "xx" # forces replacement -> (known after apply) # forces replacement
      ~ id                = "xx/xx" -> (known after apply)
      ~ membership_id     = "xx" -> (known after apply)
        # (2 unchanged attributes hidden)
    }

  # module.aws-iam-identity-center.aws_identitystore_group_membership.sso_group_membership["testuser1_SectionSre"] must be replaced
-/+ resource "aws_identitystore_group_membership" "sso_group_membership" {
      ~ group_id          = "xx" # forces replacement -> (known after apply) # forces replacement
      ~ id                = "xx" -> (known after apply)
      ~ membership_id     = "xx" -> (known after apply)
        # (2 unchanged attributes hidden)
    }

Plan: 3 to add, 0 to change, 2 to destroy.

@hacker65536
Copy link
Contributor Author

Same situation.
Perhaps the problem can be solved.
Plz give me a moment.

@hacker65536 hacker65536 force-pushed the inlinepolicy branch 2 times, most recently from e62cc55 to 06b4006 Compare June 6, 2024 02:11
@hacker65536
Copy link
Contributor Author

@novekm
I've simplified references to resources defined within this module and to resources defined outside of this module.
How about this?

@novekm
Copy link
Collaborator

novekm commented Jun 7, 2024

Hi @hacker65536, doing some testing today but initial tests look good. Will update later today

@novekm novekm merged commit 46c0969 into aws-ia:main Jun 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants