Share
Embed
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
JAWS-UG CDK支部#18の資料です
https://jawsug-cdk.connpass.com/event/336392/
tomoki10
December 18, 2024
Transcript
23 slides
-
ࣗݾհ !UPNPLJ !UPNPLJ • ࠤ౻ஐथ • ϏδωεςΫϊϩδʔ෦ • ΞʔΩςΫτνʔϜ •
JAWS-UG CDKࢧ෦ ӡӦ • ͖ͳAWSαʔϏε -BNCEB $%, !UNL -
࣍ ͢͜ͱ • ҎԼͷΞοϓσʔτͷৼΓฦΓ • AWS CDK Solutions Constructs Factories
• CloudFormation HooksͷΞοϓσʔτ • CDKར༻ऀଆͷηΩϡϦςΟͷશମ૾ ಘΒΕΔใ • CDKηΩϡϦςΟͷશମ૾ͱγϑτϨϑτͷಈ͖ -
AWS CDK Solutions Constructsͱ • CDKͷConstructΛ֦ுͨ͠ͷͰɺΑ͋͘ΔΞʔΩςΫνϟΛύλʔϯͱͯ͠ ఏڙ͍ͯ͠Δ • ͍ΘΏΔL3 Constructͷύλʔϯू
• ྫ͑ҎԼͷΑ͏ʹAPI Gateway→DynamoDBͷߏΛ؆୯ʹ࡞ΕΔ Amazon DynamoDB Amazon API Gateway -
AWS CDK Solutions ConstructsͷҰ෦ • aws-alb-fargate • aws-alb-lambda • aws-apigateway-dynamodb
• aws-apigateway-iot • aws-apigateway-kinesisstreams • aws-apigateway-lambda • aws-apigateway- sagemakerendpoint • aws-apigateway-sqs • aws-apigatewayv2websocket- sqs • aws-cloudfront-apigateway- lambda • aws-cloudfront-apigateway • aws-cloudfront-mediastore • aws-cloudfront-s3 • aws-cognito-apigateway- lambda • aws-constructs-factories • aws-dynamodbstreams-lambda- elasticsearch-kibana • aws-dynamodbstreams-lambda • aws-eventbridge- kinesisfirehose-s3 • aws-eventbridge- kinesisstreams • aws-eventbridge-lambda • aws-eventbridge-sns • aws-eventbridge-sqs • aws-eventbridge-stepfunctions • aws-fargate-dynamodb • aws-fargate-eventbridge • aws-fargate-kinesisfirehose • aws-fargate-kinesisstreams • aws-fargate-opensearch • aws-fargate-s3 • aws-fargate-secretsmanager • aws-iot-sqs • etc… -
AWS CDK Solutions ConstructsͷҰ෦ • L3 ConstructͳͷͰॊೈੑ͘ͳΔ • ʮ্ཱ͕ͪ͛ૣ͍ˡˠޙ͔Βࡉ͔͍෦͕มߋ͠ʹ͍͘ʯͷτϨʔυΦϑ •
มߋ͢Δ߹ɺL2 ConstructͰରԠͯ͠ͳ͍ϓϩύςΟΛݺͼग़͢Α͏ʹΤ εέʔϓϋονͷར༻͕ඞཁ • cdk-nagͳͲΛͬͨηΩϡϦςΟରԠͷͨΊʹɺ্هͷΤεέʔϓϋον͕ඞཁ ʹͳΔ߹͋Δ • ֊͕૿͑ΔͷͰςετ/σόοά͕ෳࡶԽͯ͠ٯʹ͕͔͔࣌ؒΔՄೳੑ • Solution Constructࣗମͷֶशίετ͕ඞཁʹͳΔ -
ͬ͘͟Γ༁ https://aws.amazon.com/jp/blogs/devops/instant-well-architected-cdk-resources-with-solutions-constructs-factories/ ʹΘͨΓɺAWS Solutions ConstructsɺAmazon S3όέοτ͕AWS LambdaؔΛτϦΨʔ͢ΔͳͲɺ 2ͭҎ্ͷAWSαʔϏεΛ࿈ܞ͢ΔখنͰΈ߹ΘͤՄೳͳύλʔϯΛఏڙ͢Δ͜ͱͰɺઍਓͷAWS Cloud Development
Kit (CDK)Ϣʔβʔͷwell-architectedͳϫʔΫϩʔυͷ࡞ΛՃ͖ͯ͠·ͨ͠ɻ͜ͷؒɺ طଘͷSolutions Constructʹ߹க͠ͳ͍ϢʔεέʔεΛ͓࣋ͭ٬༷͔Βɺݸʑͷwell-architectedͳϦιʔεΛ ࡞͍ͨ͠ͱ͍͏ཁ͕دͤΒΕ͍ͯ·ͨ͠ɻSolutions Constructs FactoriesΛ༻͢Δ͜ͱͰɺΫϥ ΠΞϯτɺSolutions Constructs͕େنͳύλʔϯΛߏ͢Δࡍʹ༻͍ͯ͠Δಉ͡෦ίʔυΛ༻͠ ͯɺwell-architectedͳݸผϦιʔεΛ࡞Ͱ͖·͢ɻAWS CDKΛ༻ͯ͠୯ҰͷAWSϦιʔεΛσϓϩΠ͢ Δ͜ͱଟ͘ͷ߹؆୯ͳ࡞ۀͰ͕͢ɺͯ͢ͷϕετϓϥΫςΟεʹैͬͯͦͷϦιʔεΛσϓϩΠ͢Δʹ ɺΑΓଟ͘ͷࣝͱ࿑ྗ͕ඞཁͰ͢ɻྫ͑ɺదʹߏ͞ΕͨS3όέοτʹɺόʔδϣχϯάɺ҉߸Խɺ ΞΫηεϩάهɺTLSίʔϧͷΈΛڐՄ͢ΔόέοτϙϦγʔɺϥΠϑαΠΫϧϙϦγʔؚ͕·Ε͍ͯΔ ͖Ͱ͢ɻAWS Solutions ConstructsͷS3BucketFactory()ϝιουɺS3ΞΫηεϩάΛอ࣋͢ΔՃͷόέο τΛؚΉɺͯ͢ͷϕετϓϥΫςΟε͕ߏ͞Εͨશͳwell-architectedͳCDK S3όέοτΛ࣮͠·͢ɻ -
ͦΕͧΕͷߏͷ֓೦ਤ Solutions Constructs L2 ConstructΛଋͶͯར༻ Solution Constructs Factories ୯ମͷL2ΛΑΓηΩϡΞʹͯ͠ར༻ Solution
Construct L2 Construct L2 Construct Solutions Constructs Factories L2 Construct L2 Construct Secure L2 Construct Secure L2 Construct Ϣʔβར༻෦ CDK෦ߏ෦ -
ίʔυྫ import * as cdk from 'aws-cdk-lib'; import { Construct
} from 'constructs'; // Add this import statement: import { ConstructsFactories } from '@aws-solutions-constructs/aws- constructs-factories'; export class FactoriesBlogStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Add these two lines const factories = new ConstructsFactories(this, 'constructs- factories'); const response = factories.s3BucketFactory('default-bucket', {}); response.s3Bucket.bucketArn; } } -
AWS CDK Solutions Constructsͷྫ • s3BucketFactoryͰS3 BucketΛ࡞ͬͨ߹ҎԼ͕ࣗಈͰઃఆ͞ΕΔ • TLSΞΫηε͕༗ޮ •
ΞΫηε ϩά͕༗ޮ • όʔδϣϯཧ͕༗ޮ • ͯ͢ͷύϒϦοΫ ACL ͱϙϦγʔ͕ϒϩοΫ • AWS ϚωʔδυαʔόʔαΠυ҉߸Խ • 90ޙʹඇ࠷৽όʔδϣϯΛS3 GlacierʹҠߦ͢ΔϥΠϑαΠΫϧϙϦγʔ -
13 • ैདྷΞΧϯτͷCloudFormation(Cfn) ͷ࡞/ߋ৽/আૢ࡞ʹରͯ͠ɺΞΫγϣϯ ϦιʔεݕࠪΛՃͰ͖ͨHooks • ैདྷΧελϜHookΛ͏߹ɺCfnͱͯ͠ HookΛ࡞͠ɺCloudFormation Registryʹ ొ͢Δඞཁ͕͋ͬͨ
• ࠓճͷΞοϓσʔτͰLambdaΛHook ͱͯ͠ར༻͢Δ͜ͱ͕ՄೳʹͳΓɺςετ σϓϩΠΛଞͷ։ൃϓϩηεͱ߹ΘͤΔ ͜ͱ͕Մೳʹͳͬͨʂ AWS CloudFormation Hooks ͕ΧελϜ AWS Lambda ؔͷαϙʔτΛ։࢝ https://aws.amazon.com/jp/blogs/devops/proactively-validate-your-aws-cloudformation-templates-with-aws-lambda/ -
14 Hookʹઃఆͨ͠LambdaʹඈΜͰ͘ΔΠϕϯτྫ { "clientRequestToken": "XXXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "awsAccountId": "111111111111", "stackId": “arn:…”, …
"actionInvocationPoint": "CREATE_PRE_PROVISION", "requestData": { "targetName": "AWS::S3::Bucket", "targetType": "AWS::S3::Bucket", "targetLogicalId": “Bucket”, … "targetModel": { "resourceProperties": { "PublicAccessBlockConfiguration": { "RestrictPublicBuckets": true, "IgnorePublicAcls": true }, "BucketName": "XXXXXXXXXXXXXXXXXXXXXXXXXXX", "VersioningConfiguration": { "Status": "Enabled" }, … https://aws.amazon.com/jp/blogs/devops/proactively-validate-your-aws-cloudformation-templates-with-aws-lambda/ -
15 ΠϕϯτΛॲཧ͢ΔLambdaͷྫ def lambda_handler(event, context): … payload = { "clientRequestToken":
clientRequestToken, "hookStatus": "SUCCESS", "errorCode": None, "message": message, "callbackContext": None, "callbackDelaySeconds": 0, } LOGGER.debug(payload) return payload … https://aws.amazon.com/jp/blogs/devops/proactively-validate-your-aws-cloudformation-templates-with-aws-lambda/ -
16 • ैདྷϓϩάϥϛϯάݴޠͰͷ࣮͕ ඞཁͩͬͨCfn Hooks • ࠓճͷΞοϓσʔτͰGuard DSLΛ ॻ͘͜ͱͰHookͷ࡞͕Մೳʹʂ •
Lambdaͷཧ͕ෆཁͰɺPolicy as CodeΛ࣮ݱͰ͖Δ • S3্ʹDSLΛஔ͍ͯ༻ CloudFormation Guard υϝΠϯݻ༗ͷݴޠΛ༻ͯ͠ AWS CloudFormation Hooks Λ࡞ https://aws.amazon.com/about-aws/whats-new/2024/11/author-aws-cloudformation-hooks-cloudformation-guard-domain-specific-language/ -
let aws_lambda_functions_inside_vpc = Resources.*[ Type == 'AWS::Lambda::Function' Metadata.cfn_nag.rules_to_suppress not exists
or Metadata.cfn_nag.rules_to_suppress.*.id != "W89" Metadata.guard.SuppressedRules not exists or Metadata.guard.SuppressedRules.* != "LAMBDA_INSIDE_VPC" ] rule LAMBDA_INSIDE_VPC when %aws_lambda_functions_inside_vpc !empty { %aws_lambda_functions_inside_vpc.Properties.VpcConfig.SecurityGroupIds !empty %aws_lambda_functions_inside_vpc.Properties.VpcConfig.SubnetIds !empty << Violation: All AWS Lambda Functions must be configured with access to a VPC … >> } ྫɿVPC LambdaҎ֎Λېࢭ͢Δ߹ͷྫ https://github.com/aws-cloudformation/aws-guard-rules-registry/blob/main/rules/aws/lambda/lambda_inside_vpc.guard CloudFormation Guard υϝΠϯݻ༗ͷݴޠΛ༻ͯ͠ AWS CloudFormation Hooks Λ࡞ 17 -
ηΩϡϦςΟͷγϑτϨϑτͱ ։ൃϥΠϑαΠΫϧͷૣ͍ஈ֊ͰηΩϡϦςΟΛΈࠐΜͰɺͷૣظൃݟ खΓίετͷݮɺ্࣭Λࢦ͢ߟ͑ํ Develop Build&Test Pre-Deployment Operations ιϑτΣΞ։ൃͷϥΠϑαΠΫϧ ίίΑΓ ઌͷϓϩηεͰࢭΊΔ
ࢀߟɿhttps://www.youtube.com/watch?v=7cYzYWcDyiM -
ϑΣʔζ͝ͱʹؔ࿈͢ΔϥΠϒϥϦ/αʔϏε • SCP • Declarative- policies • CloudFormation Hooks •
Solutions Constructs Factories • assertions • Aspects • cdk-nag • cfn-nag • Checkov • cfn-guard Develop Build&Test Pre-Deployment Operations • SecurityHub • Config • RCP -
ϑΣʔζ͝ͱʹؔ࿈͢ΔϥΠϒϥϦ/αʔϏε Develop Build&Test Pre-Deployment Operations ։ൃ࣌ͱσϓϩΠલͷγϑτϨϑτ͢ΔͨΊͷίʔυ/αʔϏε͕ڧԽʂ • SCP • Declarative
policies • CloudFormation Hooks • Solutions Constructs Factories • assertions • Aspects • cdk-nag • cfn-nag • Checkov • cfn-guard • SecurityHub • Config • RCP -
·ͱΊ • ҎԼͷΞοϓσʔτͷৼΓฦΓΛ͠·ͨ͠ • AWS CDK Solutions Constructs Factories͕ηΩϡΞʹίϯετϥΫτΛ࡞ Δ৽͍͠Solutions
Constructs • CloudFormation Hooks͕Ξοϓσʔτ͠ɺLambdaDSLͷར༻͕Մೳʹ • CDKηΩϡϦςΟͷશମͰηΩϡϦςΟγϑτϨϑτͷಈ͖͕ڧԽ • Solutions Constructs FactoriesͰ։ൃதʹηΩϡϦςΟରࡦ • CloudFormation HooksͰpre-deploymentͷ࣌ͷηΩϡϦςΟରࡦ͕ڧԽ