(Working with STS directly is a bit of a blind spot for me.) I have a job processing app built on Step Functions, which a bunch of our other apps use for various tasks. Many of those tasks produce new files (resized images, copies of source files, etc). Currently, the clients are free to define what the S3 destination for those files is, but realistically they can only include destinations that the states (Lambdas, mostly) have permissions for.
One thought I had was that if the client app could include an IAM role in the job definition, and at the point where something is actually making a call to S3, it assumes that role and uses those credentials rather than the Lambda’s own execution role.
As I said, STS is a blindspot. But I think this should be possible, and would go something like this:
ClientAccount hosts ClientApp. ClientApp creates jobs, and includes an IAM role in the job called ClientRole, which has write access to some S3 bucket in ClientAccount.
ProcessorAccount hosts the ProcessorApp step function, which includes a ResizeLambda. When ResizeLambda gets a job that contains a role, it makes a call to STS, gets temporary credentials for that role, and then makes the S3 calls using those credentials.
Assuming that is all correct, I’m not sure that a) it’s a good idea, or b) what the ClientRole actually looks like. It obviously needs the S3 permissions, but as far as allowing some arbitrary Lambda(s) in another account to assume it, is it just a trust relationship? If so, what would that look like?
I don’t think it’s generally advisable to have an untrusted caller pass an IAM role name to a function that would use it as a parameter to sts:AssumeRole.
Since we’re talking about S3 buckets, using a different role isn’t typically necessary. S3 buckets have their own resource-level permissions that can be used to grant access to them. So you can add the Lambda function’s existing execution role to the Bucket Policy of each bucket that the function is allowed to write to. By doing this, you also can avoid granting explicit S3-related policies to the Lambda function’s execution role.
Even more so if the bucket is the client’s (outside your own account).
Only asking for a bucket policy is easier to do and secure rather than convoluted IAM & STS operations.
I think I’m following what you’re trying to do and if so one option would be to do the role assumption programmatically inside of lambda. Meaning the lambda function would have its own iam role which you’re not going to be able to modify during runtime but when the client initiates the workflow they can pass in as a parameter the name of an iam role to assume together with the S3 bucket of their choosing. Programmatically in the lambda function you’d pull that role name and S3 bucket name via lambda’s variables and assume the role accordingly. Even from a security perspective I don’t see a problem here because you’re not exposing the code to the clients so they can’t arbitrarily change it they’re only passing runtime context. Hope that helps.
I would suggest the client pass on a pre-signed s3 url. That way only the client needs iam access to the bucket, and the lambda can just to a simple put to the pre-signed url.
I’ve implemented similar Lamda/AssumeRole patterns. Although like others commenters said, you might be better just using S3 Bucket policies for your use-case, but the process is …
Lambda execution Role needs the sts:AssumeRole permission. Ideally limited to just the Roles it would be allowed to assume.
Role(s) for the Lambda to assume via STS. These need an assume_role_policy to allow the Lambda Role to assume it. Here it’s much more important to limit the assume role policy to just the Lambda ARN - it’s a much bigger security risk to allow any Role to assume that Role and do whatever it can do.
Code in the Lambda to assume the Role. That’s pretty straight forward.
Your security consideration is that if you have a lot of ClientAccounts giving your Lambda the ability to do “things” then however the ClientApp declares which Role it needs is open to be hacked to declare a Role for another client. If the ProcessorApp is getting it’s jobs in a way that, for example, it knows the account id that the job is coming from - then the Lambda could contain a look-up from account ids to Roles and you’d be protected from spoofing.
Ok, that makes sense and seems like the way to go. But… the way things are right now, each Lambda for the various states in the Step Function has its own IAM role, which means any bucket that wants to grant this access would need a pretty beefy bucket policy, and it’s very likely that it would get outdated over time as the state machine grows.
Some of the roles could easily be consolidated to a single role, but others make less sense to unify (i.e., I don’t necessarily want all the other Lambdas to get the permissions they have).
So assuming that I need to maintain multiple execution roles for the Lambdas, but I want to be able to publish a single role to which buckets grant access, what do you think the best option in? At that point does it make sense to have a execution role for each Lambda, and another S3 role that they all share and is only used for the S3 API calls? That would get managed entirely internally, so it removes the untrusted actor.
One idea I can think of would be to create an IAM role that has no policies of its own, and allow your various Lambda execution roles to assume that satellite role. That satellite role can then be trusted by the various S3 buckets via their respective policies.
Prior to interacting with S3, your Lambda functions can call sts:AssumeRole to assume the satellite role. Then, using the returned credentials, they can access the S3 buckets.