Package: wonk
The Policy WonkWonk is a tool for combining a set of AWS policy files into smaller compiledpolicy sets.Table of ContentsContents:The Policy WonkTable of ContentsRationalePolicies are limited resourcesYour roles share a lot of common permissionsThings got really complicated when you weren’t lookingInstallationUsageFetching policiesCombining policiesBuilding configured policy statementsThe detailsBreaking up is hard to doPolicy setsTerraforming combined policiesLimitationsCopyrightHistoryRationaleWonk can help you in several situations.Policies are limited resourcesYou want to give the people in your organization the AWS permissions needed to do their job. Amazon has helpfully created several hundred policies likeAmazonRDSReadOnlyAccessandAmazonS3FullAccessthat you can assign to users, groups, or roles. This is super convenient… up until it’s not.AWS hasIAM quotaslike:Policies may not be more than 6,144 characters long.You can’t attach more than 10 groups or roles to a user.You can’t attach more than 10 policies to a single group, role, or user.If you’re logging into AWS with SSO, each user like gets exactly 1 role assigned to them.What if your backend engineers log in with Okta and they need11policies to do their job?Wonk to the rescue! You can combine them into one big policy with a command line like:$ wonk combine -p MyPolicy AWSPolicy1.json AWSPolicy2.jsonwhich reads the contents ofAWSPolicy1.jsonandAWSPolicy2.jsonand merges them into aMyPolicy_1.jsonfile.Your roles share a lot of common permissionsPerhaps you have one role for backend engineers, and another role for backend engineers who are on call this week and need some additional permissions. You really don’t want to maintain two policies that are nearly identical, though.In this case, you could put all of the standard permissions in one policy, all of the additional on-call permissions in another, then combine them:$ wonk combine -p BackendOnCall Backend.json OnCall.jsonThings got really complicated when you weren’t lookingBeyond just combining a file or 2 as needed, you want some help managing multiple roles with lots of policies. Say you’re setting up policies for both frontend and backend engineers and each of them have special on-call roles that share some extra debugging permissions. Each role uses a combination of some AWS-managed policies with some that you’ve written yourself.Wonk loves you and wants you to be happy.First, it assumes a directory layout like this:├── wonk.yaml
├── managed
│   ├── AWSPolicy1.json
│   └── AWSPolicy2.json
├── local
│   ├── BackendECSReadOnly.json
│   ├── FrontendCloudWatchReadOnly.json
│   └── OnCall.json
└── combined
    ├── Backend_1.json
    ├── Frontend_1.json
    ├── BackendOnCall_1.json
    └── FrontendOnCall_1.jsonwhere themanageddirectory is full of policy files that you’ve downloaded from AWS (maybe using thewonk fetchcommand), thelocaldirectory has policies you’ve written yourself, andcombinedhas the output files that Wonk creates for you.Youcouldwrite a bunch ofwonk combinecommand lines, maybe in a shell script or a Makefile. Alternatively, you could write awonk.yamlfile like this:policy_sets:
  OnCall:
    abstract: true
    local:
      - OnCall

  Backend:
    managed:
      - AWSPolicy1
      - arn:path:to:your:policy/AWSPolicy2
    local:
      - BackendECSReadOnly

  BackendOnCall:
    inherits:
      - Backend
      - OnCall

  Frontend:
    managed:
      - AWSPolicy3
    local:
      - FrontendCloudWatchReadOnly

  FrontendOnCall:
    inherits:
      - Frontend
      - OnCalland then tell Wonk to build them all for you:$ wonk build --allwhich fetches any missing managed policies, then creates a set of combined policies named after their YAML configurations. Wonk skips building combined policies for abstract policy sets.A managed policyFoois fetched by the ARNarn:aws:iam::aws:policy/Foo. However, some Amazon policies don’t follow that convention. In that case, you can give an ARN instead of a policy name and that ARN will be fetched instead (and the policy’s name will be derived from the ARN). You could also do that if you want to fetch your own policy from Amazon instead of maintaining it locally.Installationpip install wonkAlternatively: clone this repo and runpoetry install.UsageFetching policiesUsewonk fetchto retrieve a policy from AWS by name or by ARN and write it to stdout. Each of these commands emit the same output:$ wonk fetch --arn "arn:aws:iam::aws:policy/AWSLambdaFullAccess"
$ wonk fetch --name AWSLambdaFullAccess
$ wonk fetch --profile my_aws_profile_name --name AWSLambdaFullAccessCombining policiesUsewonk combineto combine multiple policies into a policy set:$ wonk combine -p Foo policy1.json policy2.jsonBuilding configured policy statementsThewonk buildcommand interprets awonk.yamlfile as described in the example above and builds the requested policy set(s).To build one named policy set:$ wonk build --policy-set BackendOnCallTo build all defined policy sets:$ wonk build --allThe detailsSounds simple, right? Well, not quite. Remember, IAM quotas limit managed policies to 6,144 characters. You can put a few more characters on an inline policy directly on a role, but that’s not best practice and you don’t really want to go down that path. Instead, Wonk uses a few tricks to try to make policies fit inside their size limit:It strips allSidkeys from statements, per Amazon’s recommendations.It discard duplicate actions.It removes all “shadowed” actions. For instance, if a statement has actionsFoo:SomeActionandFoo:*, it discardsFoo:SomeActionbecauseFoo:*already has it covered. Similarly,Foo:Get*will shadowFoo:GetSomething, soFoo:GetSomethingwill be removed.Wonk tries to make the generated policies as human-readable as possible, but will format them very tersely if necessary. You can always usejqto reformat its outputs for viewing.Note: actions are always grouped by similar principals, resources, conditions, etc. If two statements have different conditions, say, they are processed separately.Breaking up is hard to doWonk does whatever it can to make a policy fit within that magic 6,144 character limit, but sometimes that just can’t be done. If you try to combine 30 different non-overlapping policies, there’s a decent chance that the end result simply can’t be shrunk enough. A careful reader might have noticed that all of the command examples specify an output “base” instead of a specific filename, and an outputFooends up creating a file namedFoo_1.json. This is because in the case that Wonk can’t pack everything into a separate file, it creates asetof as few output policies as possible to include all of the actions. The general process is this:Try to make everything fit.If there are any statements with so many actions that they can’t be shrunk into the size limit, split them up into equal-size chunks that do fit.Now we have the case of fittingMstatements intoNpolicies, of which there can’t be more than 10 because of the AWS limits. That looks a lot like theknapsack problem, and indeed it is. Wonk uses Google’sSCIP constraint solverto pack all of the statements into as few policies as possible.Ifnoneof this is sufficient, Wonk raises an exception and quits.Policy setsThe end result of many Wonk operations is a collection of files, apolicy set, named<base>_1.jsonthrough<base>_N.jsonwhere N <= 10. This is different from most utilities which operate on individual files, but Wonk can’t know how many files it will be creating in advance.Why 10? Because AWS usually won’t allow you to attach more than 10 policies to a user, group, or role. Since policy sets work together like one giant policy and can’t be split up, Wonk won’t create a policy set that can’t actually be attached to anything. If you’re bumping up against this limit, consider creating 2 policy sets and applying them to 2 distinct but related groups (likeBackend_1andBackend_2), then putting each relevant user into both groups. Alternatively, if your policies cover 99 actions likeService:OnePermissionandService:Anotheron a service that only has 100 possible actions, and you’ve done your due diligence and don’t mind giving your users access to that 100th action, consider adding aService:*action to a local policy. That will replace all those individual actions with the single wildcard. Likewise, if you mean to give your users access to all of the variousService:GetThisandService:GetThatactions, you can cover them all at once withService:Get*. This also has the nice side effect of documenting that you actually intend to allow access to all of theGet*actions.Terraforming combined policiesReasonably recent modern versions of Terraform supportfilesetandfor_eachsyntax. You can define a single policy resource that exactly expands out to a whole set of policies, then attach them all at once to a group or role:resource "aws_iam_policy" "Frontend" {
  for_each    = fileset(path.module, "combined/Frontend_*.json")
  name        = split(".", basename(each.value))[0]
  description = "Frontend users need to do stuff"
  policy      = file(each.value)
}

resource "aws_iam_group_policy_attachment" "Frontenders__Frontend" {
  for_each   = aws_iam_policy.Frontend
  group      = data.aws_iam_group.frontenders.group_name
  policy_arn = each.value.arn
}LimitationsAs of this writing, Wonk is usable but not finished. It’s missing a few nice features:Wonk doesn’t consider action shadowing when one statement has restrictions but another doesn’t. For example, given two statement blocks:{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "Foo:Something",
            "Resource": "arn:aws:service::my_resource"
        },
        {
            "Effect": "Allow",
            "Action": "Foo:Something"
        }
    ]
}the second statement is broader than the first, so the first could be safely removed. Right now it isn’t.CopyrightThe Policy Wonk is copyright 2021-2023 Amino, Inc. and distributed under the terms of the Apache-2.0 License.History0.7.12023-03-23: Wonk will now create the ‘combined’ policy set output directory if it doesn’t already exist. Relax PyYAML constraint to >=5.0, upgrade and re-lock all loosely pinned dependencies. Require poetry >= 1.3.0.7.02023-03-20: Add ‘abstract’ option to policy sets (default false). Wonk skips building combined policies for abstract policy sets.0.6.12022-05-20: Important bugfix! Policies large enough to require splitting weren’t being processed correctly, causing an exception. This would not have caused data corruption, just a runtime failure.0.6.02022-05-18: Make multiple compression passes over statements, if needed, to provide better results. Deterministically compute policy IDs from a hash of their contents, rather than using a random ID. Internal refactoring makes data flows easier to understand and reason about.0.5.42022-03-24: Upgrade ortools to 9.3.10497.0.5.32021-12-07: More robust shadowed action detection.0.5.22021-09-24: First public release.