Here's the complete setup - account A will refer to the account where the Jenkins master operates and the EKS cluster running the slaves, account B will refer to the account we are provisioning resources into where we use a deployment role. Apologies as this is a bit lengthy.
- Master jenkins server runs in account A using a limited IAM role which has access to EKS cluster using the Jenkins k8s plugin.
- EKS nodes within nodegroups and Fargate profiles are configured with IAM permissions to be able to run Fluent Bit configurations and allow them to write their own logs to Cloudwatch which represent the API logs and console logs for the containers. This happens through regular IAM and is independent of roles assumed on pods. This is a standard Amazon logging configuration using Fluent Bit as a DaemonSet on the NodeGroup and the baked in implementation of Fluent Bit offered on the Fargate workers.
- AWS standard for giving k8s service accounts access to IAM roles is to create an OIDC connector between the target account (account B) and the k8s endpoint, not a local IAM role within account Account A at all. The role in Account B is configured with an Assume Role policy that allows a AssumeRoleWithWebIdentity for the k8s user by name, through the OIDC connector only. This role is given the deployment permissions for account B.
- Service Accounts are configured within a namespace on EKS containing an 'eks.amazonaws.com/role-arn' annotation which specifies the role that the service account should take on.
- podTemplate for the deployment job within Jenkins specifies namespace where the pod is to run and the service account name.
- When the pod is launched AWS calls (must be baked into the SDK to perform AssumeRoleWithWebIdentity following a hint) automatically match the role from the service account annotation and connect to the target account. Being challenged for a token, the k8s endpoint issues the token to the OIDC connector allowing the k8s user to assume the role. This happens somewhat 'automagically' once the configuration is right.
Note that this configuration is not something that is only needed for federated accounts, this is Amazon's model for assigning roles to EKS service accounts regardless and in a single account scenario the OIDC connector and the ROLE would be in the same account, the mechanism is the same though, one uses AssumeRoleWithWebIdentity and this happens automatically with the federated principal coming from k8s rather than IAM.
In this configuration, either cross account or within the same account we get the same experience, the run completes but, the master log is produced (lacking agent activity as noted in script areas etc.) and log streams are created for the agent runs, these are empty because the agent appears not to be able to write to them.
Noting standard AWS Fluent Bit configurations produce logs perfectly both for the node group and the EKS cluster but this does not offer job logging for Jenkins of course.
That is how this plugin works already, or is supposed to. See docs. The agents are supplied a token good only for sending logs to this build (or job at least). They need not be granted any particular permission outside of that, or indeed be running in AWS at all. The trust model is conservative:
I certainly have not tested this specific scenario, nor am I any kind of expert on IAM. I relied on studying AWS documentation and experimenting with the limited types of authentication available to me through my employer’s account.