Skip to content

Narrowing the Serverless IAM Deployment Policy #1439

Closed

Description

I’ve been spending time recently trying to remove Admin rights as a requirement for sls deployments. Still a work in progress, but so far I have this policy that I can attach to any “serverless-agent” AWS user, so that the serverless-agent user is empowered enough to deploy:

    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:Describe*",
                "cloudformation:List*",
                "cloudformation:Get*",
                "cloudformation:PreviewStackUpdate"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:CreateStack",
                "cloudformation:UpdateStack",
                "cloudformation:DeleteStack"
            ],
            "Resource": "arn:aws:cloudformation:*:*:stack/${project}*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:Get*",
                "lambda:List*",
                "lambda:CreateFunction"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:AddPermission",
                "lambda:CreateAlias",
                "lambda:DeleteFunction",
                "lambda:InvokeFunction",
                "lambda:PublishVersion",
                "lambda:RemovePermission",
                "lambda:Update*"
            ],
            "Resource": "arn:aws:lambda:*:*:function:${project}*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:*"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "apigateway:GET"
            ],
            "Resource": [
                "arn:aws:apigateway:*::/restapis"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "apigateway:GET",
                "apigateway:POST",
                "apigateway:PUT",
                "apigateway:DELETE"
            ],
            "Resource": [
                "arn:aws:apigateway:*::/restapis/*/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "arn:aws:iam::*:role/*"
        },
        {
            "Effect": "Allow",
            "Action": "kinesis:*",
            "Resource": "arn:aws:kinesis:*:*:stream/${project}*"
        },
        {
            "Effect": "Allow",
            "Action": "iam:*",
            "Resource": "arn:aws:iam::*:role/${project}*"
        },
        {
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": "arn:aws:sqs:*:*:${project}*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:Put*",
                "events:Remove*",
                "events:Delete*"
            ],
            "Resource": "arn:aws:events:*:*:rule/${project}*"
        }
    ]
}

Right now, I'm focused on a single policy that can deploy to all stages. But some enterprises may need this IAM policy to allow dev and staging deployments, but limit who can deploy to production. So, I've also been experimenting with adding "${stage}" to some of the resource ARNs, but don't have it fully worked out yet. For example:

"Resource": "arn:aws:events:*:*:rule/${project}-${stage}*"

There are still a few places where the permissions could be narrowed further. Specifically, the REST API section allows delete of ALL apis right now. And the lambda permissions are too broad. But I’ve had some annoying technical issues trying to narrow those two sections.

The API Gateway policy is still broad because you must have the 'api-id' in the ARN. But you don't know that until a deployment generates it. So on the surface, seems like a chicken/egg problem to me, but maybe there is a way to supply that api-id, instead of having AWS generate it.

And the lambda permissions are still broad because I can't see the particular Arn it is trying to manipulate to add an event mapping to a lambda, and the obvious ARNs don't work. Maybe there is a way to show the ARN being accessed in serverless, when the deployment fails so that I can add it to the policy, but no luck so far.

As I said, still a work in progress, so use with caution. Will post back any further 'narrowing' as I figure it.

Activity

  1. ghost changed the title [-]Serverless IAM Deployment Policy[/-] [+]Narrowing the Serverless IAM Deployment Policy[/+] on Jun 29, 2016
  2. depado commented on Jul 4, 2016

    @depado

    Thanks for the great work, I'll look into that, I also need to narrow down the IAM policy.

  3. pgasiorowski commented on Jul 4, 2016

    @pgasiorowski

    Thanks for sharing. I'm going to give it a go a provide some feedback.
    My first impressions it that the policy could be narrowed further by including the ${region} in appropriate Resource sections. What do you think?

  4. tobyhede commented on Jul 5, 2016

    @tobyhede
    Contributor

    This is really good, thanks

  5. pgasiorowski commented on Jul 5, 2016

    @pgasiorowski

    Here is one change that I did to make it work for me:

        {
          "Effect": "Allow",
          "Action": [
            "apigateway:GET"
          ],
          "Resource": [
            "arn:aws:apigateway:*::/restapis"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "apigateway:GET",
            "apigateway:POST",
            "apigateway:PUT",
            "apigateway:DELETE"
          ],
          "Resource": [
            "arn:aws:apigateway:*::/restapis/*/*"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "apigateway:GET",
            "apigateway:HEAD",
            "apigateway:OPTIONS"
            "apigateway:PATCH",
            "apigateway:POST",
            "apigateway:PUT",
            "apigateway:DELETE"
          ],
          "Resource": [
            "arn:aws:apigateway:*::/restapis",
            "arn:aws:apigateway:*::/restapis/*"
          ]
        },

    2nd-level POST /api/v1/resource didn't work with your IAM but the above would handle it.
    It also enables pushing missing Head, Patch and Options methods.

    Going further with the restrictions, adding ${region} to all Resources with the exception of * and arn:aws:iam:* seems to work fine eg:

    "arn:aws:apigateway:${region}::/restapis",
    "arn:aws:apigateway:${region}::/restapis/*"
    "arn:aws:lambda:${region}::function:${project}*"
    

    And the lambda permissions are still broad because I can't see the particular Arn it is trying to manipulate to add an event mapping to a lambda, and the obvious ARNs don't work.

    This one is particularly nasty but it doesn't seem to work without it.

  6. cknowles commented on Jul 6, 2016

    @cknowles

    You may be able to change PassRole to just role/${project}* to restrict that part further? I found it would also be helpful if serverless populated a variable with the ID of the Gateway to use to restrict the apigateway permissions. Some duplicate effort in #588.

  7. ecoruh commented on Jul 8, 2016

    @ecoruh

    This is great, thanks. I got it working except one bit. We are using serverless-client-s3 plugin for deploying s3 buckets.

    Anyone knows what the minimum set of permissions are for sls client deploy to work?. I tried this in my policy:

        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "s3:ListBuckets",
                    "s3:ListObjects",
                    "s3:DeleteObjects",
                    "s3:CreateBucket",
                    "s3:PutBucketWebsite",
                    "s3:PutBucketTagging",
                    "s3:PutBucketPolicy",
                    "s3:PutObject"
                ],
                "Resource": "*"
            }
    

    I scanned the plugin source code to come up with the s3 function list and added them into the policy. But I received this error:

    AccessDenied: Access Denied
        at Request.extractError (/usr/local/lib/node_modules/serverless/node_modules/aws-sdk/lib/services/s3.js:350:35)
        at Request.callListeners (/usr/local/lib/node_modules/serverless/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    ...
    From previous event:
        at ServerlessProviderAws.request (/usr/local/lib/node_modules/serverless/lib/ProviderAws.js:72:10)
        at ClientDeploy._processDeployment (/home/eric/BuildAgent/work/3a164dd9bff1550c/ucr/node_modules/serverless-client-s3/index.js:134:24)
    

    This line seems to yield Access Denied:

          return _this.aws.request('S3', 'listBuckets', {}, _this.evt.options.stage, _this.evt.options.region)
    

    even though I had listBuckets in my permission list. What am I missing?

    note: I shall refine Resource further once I get it working..

  8. cknowles commented on Jul 8, 2016

    @cknowles

    I think try s3:ListAllMyBuckets. Some of the others don't exist, it's confusing as the AWS CLI/SDK does not use the same names as the IAM actions - see these docs. s3:ListBucket with no s is the action to list the content of a bucket.

  9. pgasiorowski commented on Jul 9, 2016

    @pgasiorowski

    This is absolutely outstanding research and I think it makes a lot of sense to split into multiple policies for multi-user deployments. I yet need to try 1.0-alpha but do you think these policies would also be appropriate for CloudFormation-based deployments?

  10. ploopaltar commented on Jul 18, 2016

    @ploopaltar

    So we are using these permissions for the s3 client:

    Permissions

    s3:GetObject
    s3:GetObjectVersion
    s3:PutObject
    s3:DeleteObject
    s3:CreateBucket
    s3:DeleteBucket
    s3:ListBucket
    s3:ListBucketVersions
    s3:ListAllMyBuckets
    s3:GetBucketVersioning
    s3:PutBucketVersioning
    s3:GetBucketLocation
    s3:PutBucketPolicy
    s3:GetBucketWebsite
    s3:PutBucketWebsite
    s3:DeleteBucketWebsite

  11. str3tch commented on Jul 19, 2016

    @str3tch

    Hi Ron,

    Yes, we need the s3 permissions to deploy our serverless application as we are using the serverless-client-s3 plugin to deploy files to s3, along with the lambda code. I think this is a quite common use case. So having these permissions helps us.

    cheers,
    Chris

  12. 113 remaining items

  13. uclaeagit commented on Apr 19, 2019

    @uclaeagit

    Well then the path to get to production-ready is a lot of trial and error as you find out what permissions you need.

  14. eedwards-sk commented on Aug 13, 2019

    @eedwards-sk

    I'm a little surprised a project with this many commits and contributions lacks ops documentation for production hardening.

    production policy support should ideally be capable of:

    • running under roles pertaining to each service (cloudwatch, ec2, etc)

    • scoping resources as finely as possible, e.g. using conditions that restrict operations based on tags where possible, and based on naming patterns otherwise

    Allowing a service account s3:DeleteBucket with "*" is a really bad idea, for example. I suspect there are numerous users with this exact configuration in production.

  15. driskell commented on Sep 24, 2019

    @driskell

    Is there any further improvements on this?

    The majority of policies seem to include things like iam:AttachRolePolicy which although restricted to the current role can be used to add the Administrators policy. Things like iam:PassRole alongside lambda:CreateFunction are also problematic as you can just pass the Administrators role to the lambda function.

    I'm tempted to say it should be documented that it cannot be secured effectively when using CloudFormation - and that a dedicated Sub Organisation with reasonable limits imposed should be used to limit impact.

    I guess the best approach could be for the CloudFormation template to be uploaded manually initially, and for that template to automatically include necessary permissions for UPDATE of resources only, as I would assume that can then bypass things like iam:xxx and lambda:CreateFunction since it should theoretically only need lambda:UpdateFunctionCode?

    This is my reference for the escalation issues: https://github.com/RhinoSecurityLabs/AWS-IAM-Privilege-Escalation/blob/master/README.md

  16. erikerikson commented on Sep 24, 2019

    @erikerikson
    Contributor

    @driskell -it's good that you're keeping an eye out for security. You might be pleased to know that you are able to restrict the roles that can be passed when using the PassRole provision. See: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html (if you don't want to read all the text, search for "example 1").

  17. linjunpop commented on Dec 30, 2019

    @linjunpop

    It would be great if some plugin can generate the policies from a serverless service.

  18. cmendes0101 commented on Dec 31, 2019

    @cmendes0101

    It would be great if some plugin can generate the policies from a serverless service.

    There is this tool https://github.com/dancrumb/generator-serverless-policy

    It's mentioned in a blog from Serverless.com Blog but its not perfect https://serverless.com/blog/abcs-of-iam-permissions/

  19. sachintha97 commented on Aug 16, 2020

    @sachintha97

    We also created a tool that will help to generate AWS IAM policies: https://github.com/Open-SL/serverless-permission-generator
    Please let us know your thoughts.

  20. joshuaquek commented on Feb 4, 2021

    @joshuaquek

    Awesome, this has been working great for me @sachintha97

    Direct link to the working policy generator tool: https://open-sl.github.io/serverless-permission-generator/

  21. driskell commented on Feb 4, 2021

    @driskell

    @joshuaquek @sachintha97 The generator appears to grant iam PassRole for all roles - along with CreateFunction for any function name - so it seems to offer little additional security as one can use the role to create function with Administrator permissions. Granted, it will make exploitation a little more time-consuming.

    Ideally that PassRole would be limited as @erikerikson pointed out to only allow passing of the roles created by the CloudFormation. Additionally it is not limiting policy attachments so could still attach an Administrator policy. There's also no limitation on the API Gateway modifications allowing potential hijacking of other gateways.

    Ideally the tool should note that this does not provide secure CloudFormation deployments. I don't think it wise to put such a tool into the public domain as it is as I worry many people will assume it secures the CloudFormation deployment in ways it does not. It does make privilege escalation harder, but by no means secured.

  22. BrandonE commented on Mar 24, 2021

    @BrandonE

    I successfully used the instructions on this article after making these modifications.

  23. driskell commented on Mar 24, 2021

    @driskell

    @BrandonE The modifications made are adding IAM permissions that allow creation of administrator users. So may be defeating the purpose of the original article. It looks like you'd just be able to add a new administrator to serverless.yml and the deployment would still accept it and create that user. I sometimes think it may be more secure to just use the Administrator user, as it's clear what access is given, where with many of these minimal privileges policies it hides the fact that it provides little protection from a genuine attack, so could lead into false sense of security.

  24. BrandonE commented on Mar 24, 2021

    @BrandonE

    @driskell The additions allow you to manage roles, not manage IAM users, no?

  25. driskell commented on Mar 24, 2021

    @driskell

    @BrandonE I guess depending on the restrictions put on place on them but I'm not sure it can be restricted sufficiently. With a "*" as Resource for sure it would let you attach the Administrator policy to any role using PutRolePolicy and the IAM user already is able to assume the deployment role. There might be a way around it with permissions boundaries etc but none of that is mentioned in the articles and I haven't personally worked it out, as I think even with boundaries on all the 3 roles, the CloudFormation still needs to create the Lambda Role and a Function to run as that and so the serverless.yml can inject anything with any permissions still.

    I think closest I got was allowing only updates to existing stuff since CloudFormation won't need permissions to things that don't change but it adds a barrier to adding new functions and things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions