Repository navigation
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
[-]Serverless IAM Deployment Policy[/-][+]Narrowing the Serverless IAM Deployment Policy[/+]depado commentedon Jul 4, 2016
Thanks for the great work, I'll look into that, I also need to narrow down the IAM policy.
pgasiorowski commentedon Jul 4, 2016
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?
tobyhede commentedon Jul 5, 2016
This is really good, thanks
pgasiorowski commentedon Jul 5, 2016
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
*andarn:aws:iam:*seems to work fine eg:This one is particularly nasty but it doesn't seem to work without it.
cknowles commentedon Jul 6, 2016
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.ecoruh commentedon Jul 8, 2016
This is great, thanks. I got it working except one bit. We are using
serverless-client-s3plugin for deploying s3 buckets.Anyone knows what the minimum set of permissions are for
sls client deployto work?. I tried this in my policy: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:
This line seems to yield Access Denied:
even though I had listBuckets in my permission list. What am I missing?
note: I shall refine Resource further once I get it working..
cknowles commentedon Jul 8, 2016
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:ListBucketwith nosis the action to list the content of a bucket.pgasiorowski commentedon Jul 9, 2016
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?
ploopaltar commentedon Jul 18, 2016
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
str3tch commentedon Jul 19, 2016
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
113 remaining items
uclaeagit commentedon Apr 19, 2019
Well then the path to get to production-ready is a lot of trial and error as you find out what permissions you need.
eedwards-sk commentedon Aug 13, 2019
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:DeleteBucketwith"*"is a really bad idea, for example. I suspect there are numerous users with this exact configuration in production.driskell commentedon Sep 24, 2019
Is there any further improvements on this?
The majority of policies seem to include things like
iam:AttachRolePolicywhich although restricted to the current role can be used to add theAdministratorspolicy. Things likeiam:PassRolealongsidelambda:CreateFunctionare also problematic as you can just pass theAdministratorsrole 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:xxxandlambda:CreateFunctionsince it should theoretically only needlambda:UpdateFunctionCode?This is my reference for the escalation issues: https://github.com/RhinoSecurityLabs/AWS-IAM-Privilege-Escalation/blob/master/README.md
erikerikson commentedon Sep 24, 2019
@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").
enricop89 commentedon Sep 27, 2019
@hybby Thanks mate! I followed your suggestions and created two main roles:
Deploy Role: https://gist.github.com/enricop89/80a36d6b73417d3ef40bfe00c09a8a35#file-serverless-deploy-role-json
CFRole: https://gist.github.com/enricop89/80a36d6b73417d3ef40bfe00c09a8a35#file-cloudformation-role-json
linjunpop commentedon Dec 30, 2019
It would be great if some plugin can generate the policies from a serverless service.
cmendes0101 commentedon Dec 31, 2019
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/
sachintha97 commentedon Aug 16, 2020
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.
joshuaquek commentedon Feb 4, 2021
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/
driskell commentedon Feb 4, 2021
@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.
BrandonE commentedon Mar 24, 2021
I successfully used the instructions on this article after making these modifications.
driskell commentedon Mar 24, 2021
@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.
BrandonE commentedon Mar 24, 2021
@driskell The additions allow you to manage roles, not manage IAM users, no?
driskell commentedon Mar 24, 2021
@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.
Widen Lambda IAM permissions to Get*/List* to stop deploy whack-a-mole