|
606 | 606 | # 10. Check existing VPC Endpoints (PrivateLinks) in the task VPC. |
607 | 607 | # If there is any VPC Endpoints configured for the task VPC, we assume you would need an additional SSM PrivateLink to be configured. (yellow) |
608 | 608 | # TODO: In the ideal world, the script should simply check if the task can reach to the internet or not :) |
| 609 | +requiredEndpoint="com.amazonaws.${AWS_REGION}.ssmmessages" |
609 | 610 | taskNetworkingAttachment=$(echo "${describedTaskJson}" | jq -r ".tasks[0].attachments[0]") |
610 | | -taskVpcId="" |
| 611 | +taskSubnetId=$(echo "${describedTaskJson}" | jq -r ".tasks[0].attachments[0].details[] | select(.name==\"subnetId\") | .value") |
| 612 | +subnetJson=$(${AWS_CLI_BIN} ec2 describe-subnets --subnet-ids "${taskSubnetId}") |
611 | 613 | if [[ "x${taskNetworkingAttachment}" = "xnull" ]]; then |
612 | 614 | ## bridge/host networking (only for EC2) |
613 | 615 | taskVpcId=$(echo "${describedContainerInstanceJson}" | jq -r ".containerInstances[0].attributes[] | select(.name==\"ecs.vpc-id\") | .value") |
614 | 616 | else |
615 | 617 | ## awsvpc networking (for both EC2 and Fargate) |
616 | | - taskSubnetId=$(echo "${describedTaskJson}" | jq -r ".tasks[0].attachments[0].details[] | select(.name==\"subnetId\") | .value") |
617 | | - taskVpcId=$(${AWS_CLI_BIN} ec2 describe-subnets --subnet-ids "${taskSubnetId}" | jq -r ".Subnets[0].VpcId") |
| 618 | + taskVpcId=$(echo "${subnetJson}" | jq -r ".Subnets[0].VpcId") |
618 | 619 | fi |
619 | | -## List Vpc Endpoints |
620 | | -vpcEndpointsJson=$(${AWS_CLI_BIN} ec2 describe-vpc-endpoints \ |
621 | | - --filters Name=vpc-id,Values="${taskVpcId}") |
622 | | -vpcEndpoints=$(echo "${vpcEndpointsJson}" | tr -d '\n' | jq -r ".VpcEndpoints[]") |
| 620 | +## Obtain the ownerID of subnet's owner to check if the subnet is shared via AWS RAM (which check-ecs-exec.sh doesn't support today) |
| 621 | +subnetOwnerId=$(echo "${subnetJson}" | jq -r ".Subnets[0].OwnerId") |
623 | 622 | printf "${COLOR_DEFAULT} VPC Endpoints | " |
624 | | -if [[ "x${vpcEndpoints}" = "x" ]]; then |
625 | | - printf "${COLOR_GREEN}SKIPPED ${COLOR_DEFAULT}(${taskVpcId} - No additional VPC endpoints required)\n" |
| 623 | +if [[ ! "x${ACCOUNT_ID}" = "x${subnetOwnerId}" ]]; then |
| 624 | + ## Shared Subnets (VPC) are not supported in Amazon ECS Exec Checker |
| 625 | + printf "${COLOR_RED}CHECK FAILED${COLOR_YELLOW}\n" |
| 626 | + printf " Amazon ECS Exec Checker doesn't support VPC endpoint validation for AWS RAM shared VPC/subnets.\n" |
| 627 | + printf " Contact your administrator to find if the following resources require to have an additional VPC endpoint.\n" |
| 628 | + printf " - Resources: ${taskVpcId} and ${taskSubnetId}\n" |
| 629 | + printf " - VPC Endpoint: ${requiredEndpoint}${COLOR_DEFAULT}\n" |
626 | 630 | else |
627 | | - # Check whether an ssmmessages VPC endpoint exists |
628 | | - vpcEndpoints=$(echo "${vpcEndpointsJson}" | tr -d '\n' | jq -r ".VpcEndpoints[].ServiceName") |
629 | | - printf "\n" |
630 | | - ssmsessionVpcEndpointExists=false |
631 | | - requiredEndpoint="com.amazonaws.${AWS_REGION}.ssmmessages" |
632 | | - for vpe in $vpcEndpoints; do |
633 | | - if [[ "x${vpe}" = "x${requiredEndpoint}" ]]; then |
634 | | - ssmsessionVpcEndpointExists=true |
635 | | - break |
636 | | - fi |
637 | | - done |
| 631 | + ## List Vpc Endpoints |
| 632 | + vpcEndpointsJson=$(${AWS_CLI_BIN} ec2 describe-vpc-endpoints \ |
| 633 | + --filters Name=vpc-id,Values="${taskVpcId}") |
| 634 | + vpcEndpoints=$(echo "${vpcEndpointsJson}" | tr -d '\n' | jq -r ".VpcEndpoints[]") |
| 635 | + if [[ "x${vpcEndpoints}" = "x" ]]; then |
| 636 | + printf "${COLOR_GREEN}SKIPPED ${COLOR_DEFAULT}(${taskVpcId} - No additional VPC endpoints required)\n" |
| 637 | + else |
| 638 | + # Check whether an ssmmessages VPC endpoint exists |
| 639 | + vpcEndpoints=$(echo "${vpcEndpointsJson}" | tr -d '\n' | jq -r ".VpcEndpoints[].ServiceName") |
| 640 | + printf "\n" |
| 641 | + ssmsessionVpcEndpointExists=false |
| 642 | + for vpe in $vpcEndpoints; do |
| 643 | + if [[ "x${vpe}" = "x${requiredEndpoint}" ]]; then |
| 644 | + ssmsessionVpcEndpointExists=true |
| 645 | + break |
| 646 | + fi |
| 647 | + done |
638 | 648 |
|
639 | | - printf " Found existing endpoints for ${taskVpcId}:\n" |
640 | | - for vpe in $vpcEndpoints; do |
641 | | - if [[ "x${vpe}" = "x${requiredEndpoint}" ]]; then |
642 | | - printf " - ${COLOR_GREEN}${vpe}${COLOR_DEFAULT}\n" |
643 | | - else |
644 | | - printf " - ${COLOR_DEFAULT}${vpe}\n" |
| 649 | + printf " Found existing endpoints for ${taskVpcId}:\n" |
| 650 | + for vpe in $vpcEndpoints; do |
| 651 | + if [[ "x${vpe}" = "x${requiredEndpoint}" ]]; then |
| 652 | + printf " - ${COLOR_GREEN}${vpe}${COLOR_DEFAULT}\n" |
| 653 | + else |
| 654 | + printf " - ${COLOR_DEFAULT}${vpe}\n" |
| 655 | + fi |
| 656 | + done |
| 657 | + if [[ "x${ssmsessionVpcEndpointExists}" = "xfalse" ]]; then |
| 658 | + printf " SSM PrivateLink \"${COLOR_YELLOW}${requiredEndpoint}${COLOR_DEFAULT}\" not found. You must ensure your task has proper outbound internet connectivity." |
645 | 659 | fi |
646 | | - done |
647 | | - if [[ "x${ssmsessionVpcEndpointExists}" = "xfalse" ]]; then |
648 | | - printf " SSM PrivateLink \"${COLOR_YELLOW}${requiredEndpoint}${COLOR_DEFAULT}\" not found. You must ensure your task has proper outbound internet connectivity." |
649 | 660 | fi |
650 | 661 | fi |
651 | 662 |
|
|
0 commit comments