AWS/ECS

ECS Security

백셀건전지 2023. 9. 25. 09:38

Shard Responsibility Model

  • AWS는 Cloud'의' 보안에 책임이 있고, 사용자는 Cloud'안의' 보안에 책임이 있다.

ECS Task IAM role

  • IAM Role을 task 에 할당할 때, trust policy의 service에 "ecs-tasks.amazonaws.com" 설정해서 각 task가 EC2의 IAM role을 상속받지 않게 하라
  • When you add a task role to a task definition, the Amazon ECS container agent automatically creates a token with a unique credential ID
  • This token and the role credentials are then added to the agent's internal cache. The agent populates the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI in the container with the URI of the credential ID 

  • 아래의 명령어로 container 내의 temporary role credential 조회 가능
    • curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
    • output에는 AWS 리소스 접근할 때 사용하는 access Key ID, Secret Access Key, credential이 유효하다는 token 등의 정보가 담겨 있음
{
	"RoleArn": "arn:aws:iam::123456789012:role/SSMTaskRole-SSMFargateTaskIAMRole-DASWWSF2WGD6",
	"AccessKeyId": "AKIAIOSFODNN7EXAMPLE",
	"SecretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
	"Token": "IQoJb3JpZ2luX2VjEEM/Example==",
	"Expiration": "2021-01-16T00:51:53Z"
}

Task Execution Role

  • used to grant the Amazon ECS container agent permission to call specific AWS API actions on your behalf

Amazon EC2 container instance role

  • Amazon ECS 컨테이너 에이전트는 Amazon ECS 클러스터의 각 Amazon EC2 인스턴스에서 실행되는 컨테이너
  • 이 컨테이너는 운영 체제에서 사용할 수 있는 init 명령을 사용하여 Amazon ECS 외부에서 초기화되며, 결과적으로 task role로부터 권한을 받지 못해 EC2에 있는 ECS 권한을 추가해야.
  • AmazonEC2ContainerServiceforEC2Role policy를 ecsInstanceRole에 추가해주어야 instance가 cluster에 join이 가능

Service Linked Role

  • ECS가 다른 서비스 API를 호출할 권한을 얻게 하는 role
  • ex) network interface 추가/삭제, target group register/deregister, create/delete scaling policies

IAM Role 구성 권장 사항

  • EC2 Metadata 접근 권한을 막아라
    • bridge mode로 실행되는 task가 EC2 metadata 에 접근하는 것을 막기 위해, 아래의 명령어를 user data 에 입력
    • awsvpc mode인 task는 /etc/ecs/ecs.config 파일의 ECS_AWSVPC_BLOCK_IMDS 를 true로 변경
sudo yum install -y iptables-services; sudo iptables --insert FORWARD 1 --in-interface docker+ --destination 192.0.2.0/32 --jump DROP
  • awsvpc mode 사용
    • restrict the flow of traffic between different tasks or between your tasks and other services that run within your Amazon VPC
    • adds an additional layer of security
    • provides task-level network isolation for tasks that run on Amazon EC2
    • It is the default mode on AWS Fargate
    • the only network mode that you can use to assign a security group to tasks
  • IAM Access Advisor 사용
    • review the results produced by IAM Access Advisor, and then remove actions that were never used or haven't been used recently
  • AWS CloudTrail 사용

Network Security

  • 통신 간 암호화
    • Service Mesh: App Mesh의 envoy proxy간에 TLS 암호화 구성 가능. ex) virtual node, virtual gateway. TLS 인증서는 ACM이나 private 인증서 사용
    • Nitro instance 사용: C5n, G4, I3en, M5dn, M5n, P3dn, R5dn, and R5n 인스턴스 타입은 자동으로 암호화됨. TG, LB로 라우팅는 경우엔 X
    • ALB의 SNI(Server Name Indication) 사용: SNI 사용 시 하나의 listener에 여러 개의 안전한 app 붙일 수 있음. ACM에서 LB를 위한 인증서 발행하고 그 인증서를 listener의 certificate list 에 등록하는 것을 추천. 여러개 등록해도 ALB가 smart certificate selection.
    • TLS 인증서로 종단간 암호화: task로 TLS 인증서 deploy해야. Self-signed도 되고 trusted certificate authority 도 가능.
  • Task Networking
    • awsvpc mode로 사용해서 task를 위한 SG 사용하자
  • Service Mesh 와 MTLS
    • App Mesh의 Virtual node가 mesh 바깥의 service 와 통신해야 한다면, mesh 내의 virtual node에 ALLOW_ALL outbound filter 설정하여 사용.
  • AWS PrivateLink
    • ensures that calls to the Amazon ECS service stay within the Amazon VPC and do not traverse the internet
  • Amazon ECS container agent setting
    • ECS_AWSVPC_BLOCK_IMDS / ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST
      • ECS Task가 EC2 metadata에 접근하는 것을 차단
    • HTTP_PROXY
      • agent가 internet에 연결하기 위해 HTTP Proxy로 route하는 것
  • 추천 사항
    • network 암호화 가능하면 해라
    • awsvpc mode 사용
    • network traffic이 고립되어야 하는 환경에는 VPC를 분리해서 ECS 구성하고, AWS 서비스간 통신은 VPC Endpoint 구성해서 사용
    • VPC에 IGW 구성하기 어려우면 AWS PrivateLink 구성 
    • VPC Flow log 사용하여 traffic 분석

Secrets Management

  • Secret들은 SSM Parameter store 나 Secrets manager 통해서 seamless하게 container에 주입됨.
    • secrets container definition parameter에 사용되는 환경 변수
    • secretOptions : logging platform이 인증을 필요로 할때
    • repositoryCredentials: container registry가 인증을 필요로 할 때 사용하는 parameter
  • Param Store과 Secret manager 차이는, secret manager가 secret 자동 rotatin, 랜덤 secret 생성, AWS account간 secret 공유 등의 기능이 있음.
  • 추천 사항
    • docker inspect 명령어로 환경변수가 로그에 찍힌 것이 유출될 수 있으니, 암호화된 S3 버킷에 저장하고 task role 사용으로 접근 통제해라
    • Secretes Manager에서 secret 읽어오고 shard volume에 write 하는 sidecar container 사용해라. sidecar container는 읽어오고 바로 종료되도록 구성

Logging & Monitoring

  • CloudWatch Logs나 Kinesis Data Firehose plugin 지원하는 fluentbit 사용
  • Firehose Destination은 S3, OpenSearch, Amazon Redshift 등
  • Fluentd보다 리소스 사용량 낮으므로 fluentbit 사용

Custom Log routing - FireLens for Amazon ECS

  • AWS 서비스나 AWS Partner Network 에 로그 전송 가능
  • Fluentd와 Fluent bit 모두 지원
  • Fargate와 EC2 모두 가능
  • AWS CloudFormation template 에서 사용 가능
  • bridge mode에서 사용할 때에는, FireLens가 컨테이너 보다 먼저 시작해야.

Fargate Security

  • Ephemeral storage 암호화를 위해 KMS 사용
  • Fargate는 각 task가 20GiB의 ephemeral stoage 할당 받고, ephemeralStorage parameter 수정해서 200GiB까지 가능. AES-256 암호화 알고리즘으로 암호화 가능
  • SYS_PSTRACE: 커널 시스템 호출 추적을 위한 기능

 

'AWS > ECS' 카테고리의 다른 글

VPC Lattice  (0) 2023.09.22
ECS Capacity Providers  (0) 2023.09.17
App Mesh 정리  (0) 2023.09.17
ECS Networking 정리  (0) 2023.09.17
ECS Monitoring 정리  (0) 2023.09.15