こんにちは、上野です。
みなさん、AWSアカウントの初期セットアップはどうやっていますでしょうか。私も以下のような記事を書きましたが、いざすべてのAWSアカウントで毎回やるとなると大変ですよね。
AWS Cloud Development Kit (CDK) を使用してAWSアカウントの初期セットアップができるBaseline Environment on AWS(BLEA) を使ってみたのでその紹介となります。
BLEAとは?
以下GitHubより
Baseline Environment on AWS(BLEA) は 単独の AWS アカウントまたは ControlTower で管理されたマルチアカウント環境で、セキュアなベースラインを確立するための リファレンス CDK テンプレート群です。
AWSアカウントの初期セットアップをCDKでかんたんに実施できるテンプレートですね。私も立場上AWSアカウントの初期セットアップ(主にセキュリティサービスのONと通知設定)をやることが多いのですが、このテンプレートはAWSのソリューションアーキテクトさん達が作られており、高品質かつ簡単に初期セットアップができそうなので試してみます。詳細な日本語説明があるのもありがたいポイントですね。
シングルアカウントでセットアップする
マルチアカウントになるとControl Towerのセットアップ部分もかかわってきて、少し大がかりになりそうなので、今回はシングルアカウント、かつセキュリティベースを有効にするガバナンスベース部分を使用してみます。
Chatbotの準備
BLEAでは通知にChatbot(Slack)が使用されるため、事前に通知するチャネルを用意してセットアップしておきます。
設定したChatbotのWorkSpace IDと、通知するチャネルIDをメモしておきます。 チャネルIDはSlack上でチャネルをコピーすると取得できます。(末尾のCで始まる部分)
認証情報の準備
SSOでログインできるまっさらなAWSアカウントを用意し、sso profileを使用します。
$ aws configure sso SSO start URL [None]: https://d-hogehoge.awsapps.com/start SSO Region [None]: ap-northeast-1 Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.ap-northeast-1.amazonaws.com/ Then enter the code: JBVN-AAAA tcgetpgrp failed: Not a tty #Account ID やメールアドレスは実際のものと異なります There are 6 AWS accounts available to you. Audit, xxxxx-audit@example.com (111111111111) > SecurityService, xxxxx-security@example.com (222222222222) ・・・ Using the account ID 222222222222 The only role available to you is: AWSAdministratorAccess Using the role name "AWSAdministratorAccess" CLI default client Region [ap-northeast-1]: CLI default output format [None]: CLI profile name [AWSAdministratorAccess-222222222222]: blea # cdk実行の都度profileの指定が面倒なため、環境変数に設定 $ export AWS_PROFILE="blea"
これで、AWS SSOの認証を使用してCDKを実行できるようになりました。
※以後、対象のAWSアカウントIDを222222222222
と表記します。
初期設定
$ git clone https://github.com/aws-samples/baseline-environment-on-aws.git Cloning into 'baseline-environment-on-aws'... awsremote: Enumerating objects: 572, done. remote: Counting objects: 100% (218/218), done. remote: Compressing objects: 100% (95/95), done. remote: Total 572 (delta 164), reused 123 (delta 123), pack-reused 354 Receiving objects: 100% (572/572), 6.19 MiB | 7.29 MiB/s, done. Resolving deltas: 100% (287/287), done. $ cd baseline-environment-on-aws $ npm ci added 897 packages, and audited 936 packages in 6m 70 packages are looking for funding run `npm fund` for details found 0 vulnerabilities
※手順にあるGit の pre-commit hook のセットアップは今回はDeployのみ試すので省略します。
usecases/base-standalone/cdk.json
の、securityNotifyEmailとslackNotifierの部分を自分の環境に合わせて書き換えます。slackの部分は先ほどChatbotを設定した際のIDです。
README同様にdev
のContextを使用するため、devの部分を変更します。
{ "app": "npx ts-node bin/blea-base-sa.ts", "context": { "dev": { "description": "Environment variables for Governance base ", "envName": "Development", "securityNotifyEmail": "notify-security@example.com★ここを書きかえる★", "slackNotifier": { "workspaceId": "T8XXXXXXX★ここを書きかえる★", "channelIdSec": "C01XXXXXXXX★ここを書きかえる★" } } } }
デプロイ
実際のAWSアカウントにデプロイしていきます。
今回は対象のAWSアカウントで初回になるので、cdk bootstrap
を実行しておきます。
$ cd usecases/base-standalone $ npm run build # 初回のAWSアカウントなのでbootstrap実施 $ cdk bootstrap -c environment=dev ⏳ Bootstrapping environment aws://222222222222/ap-northeast-1... Trusted accounts for deployment: (none) Trusted accounts for lookup: (none) Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'. Pass '--cloudformation-execution-policies' to customize. CDKToolkit: creating CloudFormation changeset... ✅ Environment aws://222222222222/ap-northeast-1 bootstrapped.
デプロイします。
ところどころで確認を行いy
を入力します。
※長いので一部省略しています。
# デプロイ $ cdk deploy --all -c environment=dev ✨ Synthesis time: 4.19s BLEA-BASE-Config This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-Config: deploying... [0%] start: Publishing 36075ea848737006a3434eedeb15413291aca130f1956f9310340b1c77ef02af:222222222222-ap-northeast-1 [100%] success: Published 36075ea848737006a3434eedeb15413291aca130f1956f9310340b1c77ef02af:222222222222-ap-northeast-1 BLEA-BASE-Config: creating CloudFormation changeset... ✅ BLEA-BASE-Config ✨ Deployment time: 61.22s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-Config/fb66faf0-c4a9-11ec-bd62-069357f2a419 ✨ Total time: 65.41s BLEA-BASE-Guardduty BLEA-BASE-Guardduty: deploying... [0%] start: Publishing 20d150fb12160c68b8bd4d4b37777292eb7f210de2d55195b2f0a70eca51550b:222222222222-ap-northeast-1 [100%] success: Published 20d150fb12160c68b8bd4d4b37777292eb7f210de2d55195b2f0a70eca51550b:222222222222-ap-northeast-1 BLEA-BASE-Guardduty: creating CloudFormation changeset... ✅ BLEA-BASE-Guardduty ✨ Deployment time: 49.98s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-Guardduty/20ab3ba0-c4aa-11ec-87a0-0ac2492c4c4f ✨ Total time: 54.17s BLEA-BASE-Iam This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-Iam: deploying... [0%] start: Publishing 3acc53bed48e7eb475fed7b08a9ae522a474c6ad0cd139a4847cea26f8626573:222222222222-ap-northeast-1 [100%] success: Published 3acc53bed48e7eb475fed7b08a9ae522a474c6ad0cd139a4847cea26f8626573:222222222222-ap-northeast-1 BLEA-BASE-Iam: creating CloudFormation changeset... ✅ BLEA-BASE-Iam ✨ Deployment time: 89.73s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-Iam/47694b10-c4aa-11ec-8131-06a885d6234b ✨ Total time: 93.92s BLEA-BASE-SecurityHub BLEA-BASE-SecurityHub: deploying... [0%] start: Publishing f4200b77d698fb1341cddddfeaf76c8460cb510b65944606ad83efdd5e89accf:222222222222-ap-northeast-1 [100%] success: Published f4200b77d698fb1341cddddfeaf76c8460cb510b65944606ad83efdd5e89accf:222222222222-ap-northeast-1 BLEA-BASE-SecurityHub: creating CloudFormation changeset... ✅ BLEA-BASE-SecurityHub ✨ Deployment time: 22.41s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-SecurityHub/7d09ee50-c4aa-11ec-be61-0ed0e815ba61 ✨ Total time: 26.6s BLEA-BASE-Trail This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-Trail: deploying... [0%] start: Publishing 67e8845ce0ea21d9dbbc206b954395a43f6e44b353c26de3782179a2f7ac7a0e:222222222222-ap-northeast-1 [100%] success: Published 67e8845ce0ea21d9dbbc206b954395a43f6e44b353c26de3782179a2f7ac7a0e:222222222222-ap-northeast-1 BLEA-BASE-Trail: creating CloudFormation changeset... ✅ BLEA-BASE-Trail ✨ Deployment time: 210.09s Outputs: BLEA-BASE-Trail.ExportsOutputRefCloudTrailLogGroup343A29D62364BB0C = BLEA-BASE-Trail-CloudTrailLogGroup343A29D6-dtYPsHozktHW Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-Trail/8e540c40-c4aa-11ec-893b-0af2d3d177e5 ✨ Total time: 214.29s BLEA-BASE-ConfigCtGuardrail BLEA-BASE-ConfigCtGuardrail: deploying... [0%] start: Publishing 12840d3d99129d76b361faa9124c0230792f738fe197e731176b0f0b118e423f:222222222222-ap-northeast-1 [100%] success: Published 12840d3d99129d76b361faa9124c0230792f738fe197e731176b0f0b118e423f:222222222222-ap-northeast-1 BLEA-BASE-ConfigCtGuardrail: creating CloudFormation changeset... ✅ BLEA-BASE-ConfigCtGuardrail ✨ Deployment time: 22.56s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-ConfigCtGuardrail/0c1a88c0-c4ab-11ec-ac6c-0a9a73c4dc63 ✨ Total time: 26.75s BLEA-BASE-ConfigRule This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-ConfigRule: deploying... [0%] start: Publishing f25369fe020cf55988d76e2fe790fcf69c8db173f65451900a250d60572a1f9d:222222222222-ap-northeast-1 [100%] success: Published f25369fe020cf55988d76e2fe790fcf69c8db173f65451900a250d60572a1f9d:222222222222-ap-northeast-1 BLEA-BASE-ConfigRule: creating CloudFormation changeset... ✅ BLEA-BASE-ConfigRule ✨ Deployment time: 88.46s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-ConfigRule/21da5050-c4ab-11ec-878f-06edf6608b89 ✨ Total time: 92.65s BLEA-BASE-SecurityAlarm This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-SecurityAlarm: deploying... [0%] start: Publishing 6b805b0ad014bc66075f39fe18f9b0afddefd1b27564b905730881f06bc751ce:222222222222-ap-northeast-1 [100%] success: Published 6b805b0ad014bc66075f39fe18f9b0afddefd1b27564b905730881f06bc751ce:222222222222-ap-northeast-1 BLEA-BASE-SecurityAlarm: creating CloudFormation changeset... ✅ BLEA-BASE-SecurityAlarm ✨ Deployment time: 100.14s Outputs: BLEA-BASE-SecurityAlarm.ExportsOutputRefSecurityAlarmTopicEE71C63385BAAF8E = arn:aws:sns:ap-northeast-1:222222222222:BLEA-BASE-SecurityAlarm-SecurityAlarmTopicEE71C633-127OAIPNKCRJB Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-SecurityAlarm/59dfe5a0-c4ab-11ec-8131-06a885d6234b ✨ Total time: 104.34s BLEA-BASE-ChatbotSecurity This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: #~~省略~~ Do you wish to deploy these changes (y/n)? y BLEA-BASE-ChatbotSecurity: deploying... [0%] start: Publishing 2a1342d7b0e266c7d059c8a11b52466e617e30f192aeda2747b4f2e5adccd247:222222222222-ap-northeast-1 [100%] success: Published 2a1342d7b0e266c7d059c8a11b52466e617e30f192aeda2747b4f2e5adccd247:222222222222-ap-northeast-1 BLEA-BASE-ChatbotSecurity: creating CloudFormation changeset... ✅ BLEA-BASE-ChatbotSecurity ✨ Deployment time: 62.49s Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-ChatbotSecurity/4c7d6cb0-c4ac-11ec-9b47-06ed5436c21d ✨ Total time: 66.66s
CDKによって計9個のスタックが作成されました。
ガバナンスベースのセットアップはこれで完了です。
作成されたリソースの動作確認
いくつか作成されたセキュリティリソースの動作を見ていきます。
Security Hubの検知ルールがEventBridge上で以下のように設定されています。
- ルール名:「BLEA-BASE-SecurityAlarm-BLEARuleSecurityHubXXXX」(XXX部分はランダム)
{ "detail-type": ["Security Hub Findings - Imported"], "source": ["aws.securityhub"], "detail": { "findings": { "Compliance": { "Status": ["FAILED"] }, "RecordState": ["ACTIVE"], "Workflow": { "Status": ["NEW", "NOTIFIED"] }, "Severity": { "Label": ["CRITICAL", "HIGH"] } } } }
以下のように、設定したSlackチャネルへ通知が飛んでいました。
※Organizations経由で作成したばかりのアカウントのためMFA未設定の警告が飛んでいます。
その他にも、EventBridgeのルールで以下のイベントが通知されるよう設定されていました。
- CloudTrailの設定変更("StopLogging", "DeleteTrail", "UpdateTrail")
- AWS Health のイベント
- Configルール「bb-default-security-group-closed」の非準拠通知(初期状態)
- GuardDuty検知(severity4以上)
- NetworkACLの変更("CreateNetworkAcl", "DeleteNetworkAcl"など)
- SecurityGroupの変更("AuthorizeSecurityGroupIngress", "AuthorizeSecurityGroupEgress", "RevokeSecurityGroupIngress", "RevokeSecurityGroupEgress")
なお、Configルール「bb-default-security-group-closed」では修復アクションも設定されていて、自動的にdefaultセキュリティグループのルールが削除されるようになっていました。
その他、基本的なBLEAの設定はREADMEに書いている以下のとおりです。
- CloudTrail による API のロギング
- AWS Config による構成変更の記録
- GuardDuty による異常なふるまいの検知
- SecurityHub によるベストプラクティスからの逸脱検知 (AWS Foundational Security Best Practice, CIS benchmark)
カスタマイズしてみる
一部変更してみます。
通知するConfigルールを増やす
「bb-default-security-group-closed」以外のConfigルールについては、lib/blea-config-ct-guardrail-stack.ts
で以下のようにCloudFormationテンプレートに書かれたConfigルールを読み込むよう設定されています。
// https://github.com/awslabs/aws-config-rules/tree/master/aws-config-conformance-packs new cfn_inc.CfnInclude(this, 'ConfigCtGr', { templateFile: 'cfn/AWS-Control-Tower-Detective-Guardrails.yaml', });
cfn/AWS-Control-Tower-Detective-Guardrails.yaml
に書かれているConfigルールから、Slack通知対象に追加したいConfigRuleNameをメモしておきます。今回はサンプルとしてCheckForRestrictedSshPolicy
(セキュリティグループのSSH公開検知)を通知対象に追加してみます。
lib/blea-security-alarm-stack.ts
のConfigルールの検知部分に、対象のルール名を追加すればOKです。
// --------------- ConfigRule Compliance Change Notification ----------------- // ConfigRule - Compliance Change // See: https://docs.aws.amazon.com/config/latest/developerguide/monitor-config-with-cloudwatchevents.html // See: https://aws.amazon.com/premiumsupport/knowledge-center/config-resource-non-compliant/?nc1=h_ls // If you want to add rules to notify, add rule name text string to "configRuleName" array. // Sample Rule 'bb-default-security-group-closed' is defined at lib/blea-config-rules-stack.ts new cwe.Rule(this, 'BLEARuleConfigRules', { description: 'CloudWatch Event Rule to send notification on Config Rule compliance changes.', enabled: true, eventPattern: { source: ['aws.config'], detailType: ['Config Rules Compliance Change'], detail: { configRuleName: ['bb-default-security-group-closed','CheckForRestrictedSshPolicy'], //★ここに追加 newEvaluationResult: { complianceType: ['NON_COMPLIANT'], }, }, }, targets: [new cwet.SnsTopic(secTopic)], });
これで再度デプロイします。(対象のスタックのみでも良いですが、先ほどと合わせて --allで実行します。)
$ npm run build > blea-base-standalone@1.0.0 build > tsc --build $ cdk deploy --all -c environment=dev # 他のStackは省略 BLEA-BASE-SecurityAlarm BLEA-BASE-SecurityAlarm: deploying... [0%] start: Publishing b1fb547e82c591f317bf2c7ccc8da33b29e1532d9a4b2fa71a6020cba7fbe2cd:222222222222-ap-northeast-1 [100%] success: Published b1fb547e82c591f317bf2c7ccc8da33b29e1532d9a4b2fa71a6020cba7fbe2cd:222222222222-ap-northeast-1 BLEA-BASE-SecurityAlarm: creating CloudFormation changeset... ✅ BLEA-BASE-SecurityAlarm ✨ Deployment time: 91.45s Outputs: BLEA-BASE-SecurityAlarm.ExportsOutputRefSecurityAlarmTopicEE71C63385BAAF8E = arn:aws:sns:ap-northeast-1:222222222222:BLEA-BASE-SecurityAlarm-SecurityAlarmTopicEE71C633-127OAIPNKCRJB Stack ARN: arn:aws:cloudformation:ap-northeast-1:222222222222:stack/BLEA-BASE-SecurityAlarm/59dfe5a0-c4ab-11ec-8131-06a885d6234b ✨ Total time: 96.22s
これでEventBridgeのルールも無事に更新されました。
試しに22番ポートが公開されたSGを作成すると・・
以下のようにConfigルールの通知が飛んできます。(SecurityHubも同様の検知をします。)
エントリポイントの修正
bin/blea-base-sa.ts
を修正することで、各スタックの中身以外の変更もできます。
たとえば以下のpjPrefix
を書きかえれば、各スタック名のPrefix変更が可能です。
const pjPrefix = 'BLEA-BASE';
不要なスタックを作成したくない場合、たとえばIAM関連のリソースは別途ルールがあって手動管理したいとなった場合、以下のようにコメントアウトすることでIAMのスタックを作成しないこともできます。
(一度作成した場合はcdk destroy BLEA-BASE-Iam -c environment=dev
が必要)
// IAMは作成しない! // new BLEAIamStack(app, `${pjPrefix}-Iam`, { env: getProcEnv() });
CDK(TypeScript)を理解している方であれば、このBLEAをアレンジして独自のアカウントセットアップコードを柔軟に書けそうです。
まとめ
BLEAの良いなと感じるところ
- CDK環境があれば高速でアカウント基本設定ができる
- AWS SAさんが作っているという安心感
- 手厚いREADME
- 自分でアレンジも可能、使わないとしてもコードの参考になる
紹介記事にも書かれていますが、学習用リソースとしても良さそうです。セキュリティ系のサービスは設定がシンプルなものもあるので、わかりやすいものからどう書くのかみながら学んでいくととっつきやすいのかなと思いました。
私もまだまだ勉強の身ですが、BLEAおよびCDKの便利さが広まれば幸いです。