[AWS Config] AWS Config Remediation์„ ํ™œ์šฉํ•˜์—ฌ ์ผ์ • ๊ธฐ๊ฐ„ ๋ฏธ์‚ฌ์šฉ AWS IAM User์—๊ฒŒ Notification ๋ฉ”์ผ ๋ฐœ์†ก ์ž๋™ํ™”ํ•˜๊ธฐ

2023. 10. 8. 17:28ใ†AWS

โ˜„๏ธ To-Be Architecture

์•„ํ‚คํ…์ฒ˜์˜ ์ „์ฒด Flow๋Š” ๊ฐ„๋‹จํ•˜๋‹ค.

โ‘  ์ผ์ • ๊ธฐ๊ฐ„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์•”ํ˜ธ ๋˜๋Š” access key๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” IAM user ์ถ”์ถœ ( AWS Config )
โ‘ก IAM user์— ๋Œ€ํ•œ Email ๋ฐœ์†ก ( AWS SES )
โ‘ ์— ๋Œ€ํ•œ โ‘ก์˜ ๋™์ž‘์„ ์ž๋™ํ™”ํ•˜๊ธฐ ์œ„ํ•ด AWS Config์˜ Remediation ๊ธฐ๋Šฅ๊ณผ AWS Systems Manager์˜ Automation์„ ์ด์šฉํ•œ๋‹ค.

 

์œ„ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ ˆ์ฐจ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

  1. AWS Config ๊ทœ์น™ ์„ค์ •
  2. Amazon SES ์‚ฌ์ „ ์„ค์ •
  3. SES๋กœ ์ด๋ฉ”์ผ์„ ๋ฐœ์†กํ•˜๊ธฐ ์œ„ํ•œ AWS SSM Document ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ
  4. 1์—์„œ ์„ค์ •ํ•œ Config ๊ทœ์น™์— 3์˜ Document๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” Config Remediation ์„ค์ •

1.  AWS Config์—์„œ ๊ทœ์น™ ์ถ”๊ฐ€ํ•˜๊ธฐ

AWS Config > Add Rule

AWS Managed rule ์ค‘ iam-user-unused-credentials-check ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•˜์—ฌ AWS IAM(Identity and Access Management) ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •๋œ ์ผ ๋‚ด์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ์•”ํ˜ธ ๋˜๋Š” ํ™œ์„ฑ ์•ก์„ธ์Šค ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž.

Evaluation mode

๊ณ„์ •์— config rule์„ ์ถ”๊ฐ€ํ•  ๋•Œ, AWS Config๊ฐ€ ๋ฆฌ์†Œ์Šค๋ฅผ ํ‰๊ฐ€ํ•  ์‹œ๊ธฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Config Rule์— ๋”ฐ๋ผ AWS Config๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ฐฐํฌ๋˜๊ธฐ ์ „, ๋˜๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ฐฐํฌ๋œ ํ›„, ๋˜๋Š” ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘์— ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • proactive evaluation : ๋ฐฐํฌ๋˜๊ธฐ ์ „ ๋ฆฌ์†Œ์Šค๋ฅผ ํ‰๊ฐ€
  • detective evaluation : ๋ฐฐํฌ๋œ ํ›„ ๋ฆฌ์†Œ์Šค๋ฅผ ํ‰๊ฐ€

trigger type

AWS Config rule์ด ๋ฆฌ์†Œ์Šค๋ฅผ ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ํ‰๊ฐ€ํ• ์ง€ ๋ช…์‹œํ•œ๋‹ค. ๋ฆฌ๋กœ์Šค๋Š” ๊ตฌ์„ฑ ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ ๋˜๋Š” ์ฃผ๊ธฐ์  ์Šค์ผ€์ค„์„ ์ •ํ•ด ํ‰๊ฐ€๋  ์ˆ˜๋„ ์žˆ๋‹ค.

AWS Config Rule์„ ์„ค์ •ํ•˜๋ฉด ํ•ด๋‹น rule์„ ์ค€์ˆ˜ํ•˜์ง€ ๋ชปํ•˜๋Š”(non-compliant) ๋ฆฌ์†Œ์Šค๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

Remediate

Config์˜ ๊ธฐ๋Šฅ์œผ๋กœ AWS Config๊ฐ€ Non-Compliant ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ์ง€ํ•˜๋ฉด AWS Systems Manager Automation ๋ฌธ์„œ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•˜์—ฌ Non-Compliant ๋ฆฌ์†Œ์Šค๋ฅผ ์›ํ•˜๋Š” ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. AWS์—์„œ ์ œ๊ณตํ•˜๋Š” Document๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  custom ํ•˜๊ฒŒ ๋งŒ๋“  SSM Document๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

AWS Config Rule > Action > Manage remediation์„ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น rule์— ๋Œ€ํ•œ ์ˆ˜๋™ ๋˜๋Š” ์ž๋™ remediation ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, service์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ Config rule์— ๋Œ€ํ•ด์„œ๋งŒ remediation์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง€๊ธˆ์€ Remediate์— ์—ฐ๊ฒฐํ•  Document๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— AWS Systems Manager์—์„œ ๋ฉ”์ผ ๋ฐœ์†ก ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ํ›„, ์„ค์ •ํ•œ๋‹ค.


2. Amazon SES ์„ค์ •ํ•˜๊ธฐ

2-1. ์ด๋ฉ”์ผ ์ฃผ์†Œ ํ™•์ธ

Amazon SES๋ฅผ ํ†ตํ•ด ๋ณธ์ธ์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ์—์„œ ์ด๋ฉ”์ผ์„ ๋ณด๋‚ด๋ ค๋ฉด ๋จผ์ € ํ•ด๋‹น ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ํ™•์ธํ•˜์—ฌ ์ด๋ฉ”์ผ ์ฃผ์†Œ์˜ ์†Œ์œ ์ž์ž„์„ Amazon SES์— ์ธ์ฆํ•ด์•ผ ํ•œ๋‹ค. ์ž๊ฒฉ์ฆ๋ช… ์ƒ์„ฑ ํ›„ 5๋ถ„ ๋‚ด์— ํ™•์ธ ์ด๋ฉ”์ผ์„ ์ˆ˜์‹ ํ•ด์•ผ ํ•œ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ ๋…ธ๋ž€์ƒ‰ ์นธ์— ํ•ด๋‹นํ•˜๋Š” ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด SES console์—์„œ ์ด๋ฉ”์ผ ์ธ์ฆ์ด verified ๋˜์—ˆ๋‹ค๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋œฐ ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ณ„์† ์‹œ๋„ํ•ด๋„ ์•„๋ž˜ ๊ฐ€์ด๋“œ ๋ฌธ์„œ๋งŒ ๋– ์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ค„ ์•Œ์•˜๋‹ค.

๋งํฌ๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ € ๊ฐ€์ด๋“œ ๋ฌธ์„œ๋กœ ์—ด๋ฆฐ๋‹ค๋Š” ๊ฒŒ ์ •๋ง ์ด์ƒํ•˜์ง€๋งŒ, SES ์ฝ˜์†”์—์„œ ํ•ด๋‹น ์ด๋ฉ”์ผ์ด verified ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Send test email์„ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ์ด๋ฉ”์ผ์„ ๋ฐœ์†กํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ด๋ฉ”์ผ์€ ์ธ์ฆํ•œ ์ด๋ฉ”์ผ ์ฃผ์†Œ์—์„œ ๋ฐœ์†ก๋œ ๋ฉ”์ผ์„ ์ฒจ๋ถ€ํŒŒ์ผ๋กœ ํฌ์›Œ๋”ฉํ•œ ํ˜•ํƒœ๋กœ ์ „์†ก๋œ๋‹ค.

2-2. SES Sandbox ํ•ด์ œ

AWS๋Š” ์‹ ๊ทœ AWS Account๋ฅผ Amazon SES Sandbox์— ๋ฐฐ์น˜ํ•œ๋‹ค. AWS Account๊ฐ€ Sandbox ์ƒํƒœ์ธ ๊ฒฝ์šฐ, SES์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํ™•์ธ๋œ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋กœ๋งŒ ๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. AWS Account๊ฐ€ SES Sandbox ์ƒํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, ์ˆ˜์‹ ์ž์˜ ์ฃผ์†Œ ๋˜๋Š” ๋„๋ฉ”์ธ์ด ์ธ์ฆ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€์— ๊ด€๊ณ„์—†์ด ์ˆ˜์‹ ์ž์—๊ฒŒ ์ด๋ฉ”์ผ์„ ๋ฐœ์†กํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ sandbox ์ƒํƒœ ํ•ด์ œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๋”๋ณด๊ธฐ

๐Ÿ“ Request ์›๋ฌธ

- Region : ap-northeast-2

- Use Case: When I receive a request to create an iam user, I will enter the requestor's business email in the user's tag. A notification email will be sent to IAM Users who have not used their credentials for more than 90 days. Therefore, I need a function to send it to an unauthorized mail address.

The email transmission logic using Amazon SES will be included in AWS Systems Manager's document and performed through the automation function. Automation action will be configured to be logged to cloudwatch logs.

The frequency of sending e-mails is currently expected to be 1 to 10 per day. This was calculated by expecting a user who did not access for 90 days among IAM users.

In the body of the mail to be sent, we will make it possible to block the receipt of the mail, including "If you want to block the reception, please give me an email separately" with the administrator's email.

Please move this account out of AWS SES Sandbox.

SES Sandbox ํ•ด์ œ ์„ฑ๊ณตํ•œ ์ƒํƒœ


3. Systems Manager Automation Document ์ƒ์„ฑ

1์—์„œ ์ƒ์„ฑํ•œ AWS Config Rule์˜ Remediation์œผ๋กœ ์ง€์ •ํ•  AWS Systems Manager Documents๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

AWS Systems Manager ์ฝ˜์†” > Create Automation Document

Document ์ƒ์„ฑ ์‹œ, ํƒ€์ž…์€ Automation์„ ์„ ํƒํ•œ๋‹ค. Automation์€ AWS ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ workflow(์œ ์ง€๋ณด์ˆ˜ ๋ฐ ๋ฐฐํฌ ๋“ฑ)๋ฅผ ์ƒ์„ฑ, ๊ด€๋ฆฌ ๋ฐ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” AWS Systems Manager์˜ ๊ธฐ๋Šฅ์ด๋‹ค. SSM Automation์€ AWS SSM Document๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž๋™ํ™” ์›Œํฌํ”Œ๋กœ์šฐ์˜ ๋‹จ๊ณ„์™€ ์ž‘์—…์„ ์ •์˜ํ•œ๋‹ค. ๋ฐ˜๋ณต ์ž‘์—…์„ ์ž๋™ํ™”ํ•˜๊ณ  ์šด์˜ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ด๋ฉฐ ์ธํ”„๋ผ ์ „๋ฐ˜์—์„œ ์ผ๊ด€๋œ ์ ˆ์ฐจ ์‹คํ–‰์„ ๋ณด์žฅํ•œ๋‹ค. 

 

๐Ÿ’ก ์ฐธ๊ณ 
AWS๋Š” AWS SSM Document ์ด๋ฆ„์— ์ ‘๋‘์‚ฌ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด aws, amazon, amzn์„ ์˜ˆ์•ฝํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— document ์ƒ์„ฑ ์‹œ ์ ‘๋‘์‚ฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

Input Parameter๋กœ SSM Automation์—๊ฒŒ ๋ถ€์—ฌํ•  AutomationAssumeRole, Config Rule์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” IAM ์œ ์ €์˜ ID, ๋ฉ”์ผ ๋ฐœ์†ก์„ ์œ„ํ•œ ๋ฐœ์‹ ์ž ์ด๋ฉ”์ผ๊ณผ ๋ฉ”์ผ์— ํ‘œ๊ธฐ๋˜๋Š” ๋ฐœ์‹ ์ž ์ด๋ฆ„์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋Š” 4. Config Remediation ์„ค์ •์—์„œ ์ž…๋ ฅํ•œ๋‹ค. 

Document๋Š” IAM User์˜ ์ •๋ณด๋ฅผ ์–ป์–ด์˜ค๋Š” ๋‹จ๊ณ„, ๊ทธ๋ฆฌ๊ณ  AWS SES๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ด๋ฉ”์ผ ์ฃผ์†Œ๋กœ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•˜๋Š” ๋‹จ๊ณ„์˜ ์ด ๋‘ ๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑํ•˜์˜€๋‹ค.

3-1. (Step 1) GetUserInfo

from time import sleep
import boto3

THROTTLE_PERIOD = 0.05


def get_username(iam_client, iam_user_id):
  paginator = iam_client.get_paginator("list_users")
  page_iterator = paginator.paginate()
  for page in page_iterator:
    for user in page["Users"]:
      if user["UserId"] == iam_user_id:
        return user["UserName"]
    sleep(THROTTLE_PERIOD)


def get_user_email_tag(iam_client, iam_username):
    tags = iam_client.list_user_tags(UserName=iam_username)
    email_tag = list(filter(lambda tag: tag['Key'].lower() == 'email', tags["Tags"]))
    return email_tag


def handler(event, context):
  iam_client = boto3.client("iam")
  iam_user_id = event["IAMUserId"]

  iam_username = get_username(iam_client, iam_user_id)

  if iam_username is None:
    error_message = f"AWS IAM USER ID, {iam_user_id} DOES NOT EXIST."
    raise Exception(error_message)

  email_tag = get_user_email_tag(iam_client, iam_username)

  if len(email_tag) == 1:
    return {"UserName":iam_username, "UserEmail": email_tag[0]['Value']}
  else:
    error_message = f"AWS IAM User {iam_username} DOES NOT HAVE AN EMAIL-RELATED TAGS."
    raise Exception(error_message)

Boto3 Docs: IAM > Client > list_users (2023.07.02 ๊ธฐ์ค€)์— ๋”ฐ๋ฅด๋ฉด user์˜ tag๋Š” ๋ฆฌํ„ด๋˜์ง€ ์•Š๋Š”๋‹ค. ์‹ค์ œ๋กœ resourceID๋ฅผ ํ†ตํ•ด list_users๋กœ ๋ฆฌํ„ด ๋ฐ›์€ user๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

{
  "Path": "/",
  "UserName": {IAM-USER-NAME},
  "UserId": "{IAM-USER-ID}",
  "Arn": "arn:aws:iam::{ACCOUNT-ID}:user/{IAM-USER-NAME}",
  "CreateDate": "2022-05-31 04:09:05+00:00",
  "PasswordLastUsed": "2023-07-02 07:24:36+00:00"
}

๋”ฐ๋ผ์„œ get_username ํ•จ์ˆ˜์—์„œ userName์„ ๋ฐ›๊ณ  get_user_email_tag ํ•จ์ˆ˜์—์„œ list_user_tags๋ฅผ ์ด์šฉํ•ด tag๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•˜๋‹ค. ํ•ด๋‹น SSM Automation ๋ฌธ์„œ๋Š” IAM ์œ ์ €์— "email" ํ‚ค์˜ ํƒœ๊ทธ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ฉ”์ผ ๋ฐœ์†ก๊นŒ์ง€ ์‹คํ–‰๋˜๋‹ˆ ๋ฐ˜๋“œ์‹œ ์‚ฌ์ „์— IAM ์œ ์ €์˜ ํƒœ๊ทธ๋กœ email์„ ์ž…๋ ฅํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

3-2. (Step 2) SendEmail

import json
import boto3
import base64
from botocore.exceptions import ClientError

def sendEmail(sender_email, sender_name, user_email, user_name):
  CHARSET = "utf-8"
  sendername_bytes = sender_name.encode(CHARSET)
  sendername_b64 = base64.b64encode(sendername_bytes)
  sendername_b64_str = str(sendername_b64)
  SENDER_NAME = f"=?{CHARSET}?B?{sendername_b64_str[2:-1]}?="
  SENDER = f"{SENDER_NAME} <{sender_email}>"
  RECIPIENT = user_email
  AWS_REGION = "ap-northeast-2"
  SUBJECT = "=====mail subject====="
  BODY_TEXT = (f"======body text======")
  BODY_HTML = f"""<html>
  <head></head>
  <body>
    <p>AWS IAM ์œ ์ € {user_name}์— ??์ผ ๋™์•ˆ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ์•”ํ˜ธ ๋˜๋Š” ํ™œ์„ฑ ์•ก์„ธ์Šค ํ‚ค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ์น˜ ๋ฐ”๋ž๋‹ˆ๋‹ค.</p>
  </body>
  </html>"""

  client = boto3.client('ses',region_name=AWS_REGION)

  try:
    response = client.send_email(
      Destination={
        'ToAddresses': [
          RECIPIENT,
        ],
      },
      Message={
        'Body': {
          'Html': {
            'Charset': CHARSET,
            'Data': BODY_HTML,
          },
          'Text': {
            'Charset': CHARSET,
            'Data': BODY_TEXT,
          },
        },
        'Subject': {
          'Charset': CHARSET,
          'Data': SUBJECT,
        },
      },
      Source=SENDER
    )
  except ClientError as e:
    print(e.response['Error']['Message'])
    raise e
  else:
    print("Email sent! Message ID:"),
    print(json.dumps(response, indent=2))


def handler(event, context):
  sender_email = event["SenderEmail"]
  sender_name = event["SenderName"]
  user_email = event["UserEmail"]
  user_name = event["UserName"]
  sendEmail(sender_email, sender_name, user_email, user_name)

๋ฐœ์‹ ์ž ์ด๋ฆ„์„ ํ•œ๊ธ€๋กœ ํ‘œ๊ธฐํ•˜๋ฉด ๊นจ์ง€๋Š” ํ˜„์ƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ฝ”๋”ฉ ํ›„ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ๋‹จ๊ณ„๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ… ์ฐธ์กฐ.

 

[Amazon SES] SES ๋ฉ”์ผ ๋ฐœ์†ก ์‹œ ํ•œ๊ธ€ ๋ฐœ์‹ ์ž ์ด๋ฆ„ ๊นจ์ง ํ˜„์ƒ

โ˜„๏ธ ๋ฌธ์ œ AWS SES API SendEmail๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ๊ธ€๋กœ ์ž…๋ ฅํ•œ ๋ฐœ์‹ ์ž ์ด๋ฆ„์ด ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€ ์•Š์•˜๋‹ค. ๐ŸŒŽ ํ™˜๊ฒฝ ์ฝ”๋“œ ์‹คํ–‰ ํ™˜๊ฒฝ AWS Systems Automation Document Action : aws:execut

hyeon-joo.tistory.com

3-3. IAM Role ์ƒ์„ฑ

SSM Document๊ฐ€ IAM ์œ ์ € ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ , SES๋ฅผ ํ†ตํ•ด ๋ฉ”์ผ์„ ๋ฐœ์†กํ•˜๊ธฐ ์œ„ํ•œ ๊ถŒํ•œ์ด ์žˆ๋Š” IAM Role์ด ํ•„์š”ํ•˜๋‹ค.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "iam:ListUsers",
                "iam:ListUserTags",
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

 

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "ssm.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

3-4. Debugging

Automation Document๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด์„œ Config์—์„œ manage remediate๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค. document๋ฅผ ์ƒˆ ๋ฒ„์ „์œผ๋กœ ๋ฐœํ–‰ํ•˜๊ณ  ๋ฐ”๋กœ remediate๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ƒˆ ๋ฒ„์ „์„ default ๋ฒ„์ „์œผ๋กœ ์ง€์ •ํ–ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ƒˆ ๋ฒ„์ „์˜ document๊ฐ€ ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ๋Š” SSM Document์—์„œ ์ž„์‹œ Resource ID๋ฅผ ๊ฐ€์ ธ์™€ Execute automation์„ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ฉฐ ๋””๋ฒ„๊น…ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

Role ์„ค์ •์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์•„ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ. Automation Document์— assumeRole ๊ฐ’์„ ํ™•์ธํ•˜์ž.


4. Config Remediation ์„ค์ •

AWS Config Rule > Manage remediation

1์—์„œ ์„ค์ •ํ•œ Config ๊ทœ์น™์— Config Remediation ์„ค์ •ํ•œ๋‹ค.

AWS Config Rule > Remedation Action ์„ค์ • (1)

Remediation Method์—์„œ Automatic remediation์„ ์„ ํƒํ•˜๊ณ , Remediation action์œผ๋กœ๋Š” 3์—์„œ ์ƒ์„ฑํ•œ AWS SSM Automation Document๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

AWS Config Rule > Remedation Action ์„ค์ • (2)

์—ฐ๊ฒฐํ•œ SSM Document์˜ ๋‚ด์šฉ์— ๋”ฐ๋ผ Resource ID parameter, Parameters๋ฅผ ์ž…๋ ฅํ•˜๋Š” ํ™”๋ฉด์ด ๋ณ€๊ฒฝ๋œ๋‹ค. Config Remediation ์„ค์ • ์‹œ parameters ๊ฐ’์„ ์ง€์ •ํ•˜์—ฌ  SSM Document์˜ Input Parameter์— ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค. Resource ID Parameter๋ฅผ ์ด์šฉํ•˜์—ฌ Config rule์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๋ฆฌ์†Œ์Šค์˜ ID๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์•„๋ž˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜ ๋˜๋Š” n/a๋ฅผ ์„ ํƒํ•˜์—ฌ ์•„๋ฌด๊ฒƒ๋„ ์ง€์ •ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ํ•ด๋‹น Config Rule์€ ์ผ์ • ๊ธฐ๊ฐ„ ๋ฏธ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ™œ์„ฑํ™”๋œ ์•ก์„ธ์Šค ํ‚ค๊ฐ€ ์žˆ๋Š” IAM User๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Resource ID๋Š” IAM User์˜ ID๊ฐ€ ๋˜๋ฉฐ, ์ด๋ฅผ SSM Document์˜ IAMUserId ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. AutomationAssumeRole์—๋Š” 3-3์—์„œ ์ƒ์„ฑํ•œ IAM Role์˜ ARN์„ ์ž…๋ ฅํ•˜๊ณ , SenderName์—๋Š” ๋ฉ”์ผ ๋ฐœ์‹ ์ž ์ด๋ฆ„, SenderEmail์—๋Š” 2-1์—์„œ ์ธ์ฆ๋ฐ›์€ ๋ฐœ์‹ ์ž ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.


5. ์™„์„ฑ

์ด๋กœ์จ ์ผ์ • ๊ธฐ๊ฐ„ ๋ฏธ์‚ฌ์šฉ IAM ์‚ฌ์šฉ์ž์—๊ฒŒ noti ๋ฉ”์ผ ๋ฐœ์†ก ์ž๋™ํ™” ์„ฑ๊ณต~! ๐Ÿ˜‡

๋”๋ณด๊ธฐ

๐Ÿ’ฐ ๋น„์šฉ ๐Ÿ’ฐ

IAM ์œ ์ €์˜ ์ˆ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ SES๋Š” ์›” 300๊ฑด ํ…์ŠคํŠธ๋กœ ๋ณด๋‚ธ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ 1๋‹ฌ๋Ÿฌ ๋ฏธ๋งŒ์œผ๋กœ ๋น„์šฉ์ด ๊ณผ๊ธˆ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๊ณ 

SSM Automation ์˜ˆ์ƒ ๋น„์šฉ ๋˜ํ•œ 1๋‹ฌ๋Ÿฌ ๋ฏธ๋งŒ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค. (2023.07 ๊ธฐ์ค€, AWS Cacluator ์ด์šฉ) SES + SSM๋งŒ ํ•ฉ์ณ์„œ๋Š” ์›” 1๋‹ฌ๋Ÿฌ ์ •๋„๋กœ ์ €๋ ดํ•˜๊ฒŒ ์ž๋™ํ™” ๊ตฌํ˜„์— ์„ฑ๊ณตํ•˜์˜€๋‹ค.


๐Ÿšจ ์ œ์•ฝ ์‚ฌํ•ญ ๐Ÿšจ

๋ฐœ์‹ ์ž ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•˜์˜€์œผ๋‚˜, ์‹ค์ œ๋กœ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•˜๋Š” ์ฃผ์ฒด๋Š” AWS SES ์„œ๋น„์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์› ๋ฉ”์ผ ๋ฐœ์†ก์ž์˜ ๋„๋ฉ”์ธ์ด @amazonses.com ์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”์ผ ์ˆ˜์‹  ์ธก์˜ ์ •์ฑ…์— ๋”ฐ๋ผ ๋ฐœ์‹ ์ž ๋ณ€์กฐ ์ฐจ๋‹จ ์ •์ฑ…์œผ๋กœ ์ŠคํŒธ ์ฒ˜๋ฆฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋˜๋Š” ์•„๋ž˜ ์บก์ฒ˜ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์ฃผ์˜ ๋ฌธ๊ตฌ๊ฐ€ ํ•จ๊ป˜ ์ถœ๋ ฅ๋œ๋‹ค.


์œ„์—์„œ ์„ค๋ช…ํ•œ ์ „์ฒด ๋‹จ๊ณ„๋ฅผ Cloudformation template ํŒŒ์ผ๋กœ ๊ตฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ•„์š”ํ•˜์‹  ๋ถ„๋“ค์€ ์•„๋ž˜ yaml ํŒŒ์ผ์„ ์ด์šฉํ•ด ์†์‰ฝ๊ฒŒ workflow๋ฅผ ๋ฐฐํฌํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค~๐Ÿ€

remediation-unused-credentials.yaml
0.01MB

 

 

 

 

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ