Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ec2): VPC flow log ECS record fields don't work #33341

Open
2 tasks
isker opened this issue Feb 7, 2025 · 4 comments
Open
2 tasks

(ec2): VPC flow log ECS record fields don't work #33341

isker opened this issue Feb 7, 2025 · 4 comments
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud feature-request A feature should be added or improved. p2

Comments

@isker
Copy link
Contributor

isker commented Feb 7, 2025

Describe the feature

VPC flow logs support various ECS fields if one of the participants in the log record is an ECS task.

https://docs.aws.amazon.com/vpc/latest/userguide/flow-log-records.html

The documentation for each of these fields says:

To include this field in your subscription, you need permission to call ecs:XXX.

Where the exact permission varies. I'm guessing "you" means the flow logs service principal vpc-flow-logs.amazonaws.com, though I'm honestly not certain. Regardless, CDK generates no such grants, so these ECS fields are always empty even when ECS is involved in the flow.

Use Case

I want to investigate NAT gateway usage by ECS tasks. Without these records working, I have to correlate task ENI IP addresses to those appearing in the flow logs.

Proposed Solution

If any of the ECS LogFormats are used, FlowLog should grant the appropriate permissions.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.178.1

Environment details (OS name and version, etc.)

N/A

@isker isker added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2025
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Feb 7, 2025
@jacklin213
Copy link
Member

Currently to create Flow logs that support version 7 fields (ECS metadata) with the correct permissions you would have to use the FlowLogs construct similar to the following

const flowLog = new FlowLog(stack, 'FlowLogs', {
    resourceType: FlowLogResourceType.fromVpc(someVpc),
    destination: FlowLogDestination.toCloudWatchLogs(someLogGroup),
    logFormat: [
	LogFormat.ECS_CLUSTER_ARN,
	LogFormat.ECS_CLUSTER_NAME,
	LogFormat.ECS_CONTAINER_INSTANCE_ARN,
        LogFormat.ECS_CONTAINER_INSTANCE_ID,
	LogFormat.ECS_CONTAINER_ID,
	LogFormat.ECS_SECOND_CONTAINER_ID,
	LogFormat.ECS_SERVICE_NAME,
	LogFormat.ECS_TASK_DEFINITION_ARN,
	LogFormat.ECS_TASK_ARN,
	LogFormat.ECS_TASK_ID        
    ]
});
flowLog.iamRole?.addToPrincipalPolicy(
    new PolicyStatement({
        actions: [
            'ecs:ListClusters',  // Required for ECS_CLUSTER_ARN | ECS_CLUSTER_NAME | ECS_CONTAINER_INSTANCE_ARN | ECS_CONTAINER_INSTANCE_ID | ECS_CONTAINER_ID | ECS_SECOND_CONTAINER_ID | ECS_SERVICE_NAME | ECS_TASK_DEFINITION_ARN | ECS_TASK_ARN | ECS_TASK_ID
	    'ecs:ListServices', //  Required for ECS_SERVICE_NAME
	    'ecs:ListTaskDefinitions' // Required for ECS_TASK_DEFINITION_ARN
        ],
        effect: Effect.ALLOW,
	resources: ['*']
    }),
    new PolicyStatement({
        actions: [
	    'ecs:ListContainerInstances' // Required for ECS_CONTAINER_INSTANCE_ARN | ECS_CONTAINER_INSTANCE_ID 
        ],
        effect: Effect.ALLOW,
	resources: [...]  // This part would need to be scoped down to your cluster resource arn
    }),
    new PolicyStatement({
        actions: [
	    'ecs:ListTasks' // Required for ECS_TASK_ARN | ECS_TASK_ID
        ],
        effect: Effect.ALLOW,
	resources: [...]  // This part would need to be scoped down to your container-instance resource arn
    })
)

Note: For demonstration purposes I have modified the role that is auto created by the FlowLogs construct, but probably would be better to use your own custom role


In present day LogFormat is essentially just static strings so any permissions granting from the FlowLogs construct itself would have to happen within the constructor most likely around

if (props.logFormat) {
customLogFormat = props.logFormat.map(elm => {
return elm.value;
}).join(' ');
}

The permissions for ecs:ListClusters, ecs:ListServices and ecs:ListTaskDefinitions will be fairly straight forward since they just support wildcard resources *. Source: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonelasticcontainerservice.html

I think the part that would be hard to incorporate into the existing FlowLogs construct is the scoping of the resource element for permissions such as ecs:ListTasks and ecs:ListContainerInstances as it would require some form of passing ECS resource attributes into the construct.

The other thing to think about is the mapping of LogFormat to IAM permissions

@isker
Copy link
Contributor Author

isker commented Feb 10, 2025

Thanks for the write up.

  • I think that would only work for CloudWatch today because the other destinations don’t result in a Role being created.
  • ListServices and ListTasks can actually be scoped to a cluster with the ecs:cluster condition key, as the doc you linked shows.
  • Are you sure all the scopeable actions would have to be restricted to specific clusters? Multiple clusters can be running in a VPC, and the set of clusters that need to be enumerated might not even be clear to the caller constructing the FlowLog. Are we obligated to produce the most restrictive policies possible, even with read-only actions like these?

@pahud
Copy link
Contributor

pahud commented Feb 10, 2025

Proposed Solution
If any of the ECS LogFormats are used, FlowLog should grant the appropriate permissions.

Thank you for the report @isker . Can you share a minimal code snippet as well as what permissions you expect to be auto generated? Meanwhile, we welcome PRs from the community.

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 10, 2025
@isker
Copy link
Contributor Author

isker commented Feb 10, 2025

The policy @jacklin213 sketched above looks good, though implementation would get a lot easier if we did not need to scope grants to particular resources (i.e. we use wildcard for everything).

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

3 participants