S3ã®VPC Endpointãä½æããå ´åãã¢ã¯ã»ã¹å ãPrivateãµããããã«ããã°å¼ç¤¾Godai Nakamuraã®blog(*1)ã«ããéãcurlçãå©ç¨ãã¦ã¤ã³ã¿ã¼ãããã®ä»»æã®ãµã¤ãã¸ã¢ã¯ã»ã¹ã¯ã§ããªãããS3ã¸ã¯VPC Endpointãä½æããäºã§ã¢ã¯ã»ã¹å¯è½ã¨ãªã£ãäºã確èªãããã¨ãã£ãã·ã³ãã«ãªè©±ã§æ¸ã¿ã¾ãã
ãããPublicãµããããã®å ´åã¯ãããã©ã«ãã«ã¼ã(0.0.0.0/0)ãIGWã«åãã¦ããããIGWçµç±ã§ãVPC Endpointçµç±ã§ãS3ã¸ã¢ã¯ã»ã¹åºæ¥ãããä¸ã®ææ³ã¯éç¨ãã¾ããã
å¾ã£ã¦å®éã«ã©ã®çµè·¯ã§ã¢ã¯ã»ã¹ããããã®ããè¦æ¥µããã«ã¯å°ãæ·±ã確èªãããå¿
è¦ãããã¾ãã
Publicãµããããã®ã«ã¼ããã¼ãã«ã®ä¾ (S3ã®VPC Endpointã®çµè·¯è¨å®æ¸ã¿) (*2)
ytamu@SilverMachine:~$ aws ec2 describe-route-tables --route-table-id=rtb-0b9d6fda75bce0adf --query 'RouteTables[].Routes[]' --output table ------------------------------------------------------------------------------------------------------------- | DescribeRouteTables | +----------------------+---------------------------+-------------------------+-------------------+----------+ | DestinationCidrBlock | DestinationPrefixListId | GatewayId | Origin | State | +----------------------+---------------------------+-------------------------+-------------------+----------+ | 10.0.0.0/16 | | local | CreateRouteTable | active | | 0.0.0.0/0 | | igw-01d45a55298df7dad | CreateRoute | active | | | pl-61a54008 | vpce-0a7e08b0a117fddb5 | CreateRoute | active | +----------------------+---------------------------+-------------------------+-------------------+----------+ ytamu@SilverMachine:~$ ytamu@SilverMachine:~$ date;aws ec2 describe-prefix-lists --prefix-list-ids=pl-61a54008 Thu May 16 07:40:22 UTC 2019 { "PrefixLists": [ { "Cidrs": [ "52.219.0.0/20", "54.231.224.0/21", "52.219.16.0/22", "52.219.68.0/22" ], "PrefixListId": "pl-61a54008", "PrefixListName": "com.amazonaws.ap-northeast-1.s3" } ] } ytamu@SilverMachine:~$
ãã®ã«ã¼ããã¼ãã«ãè¦ãç¬éãS3é¢é£ã®IPã¢ãã¬ã¹å¸¯ã4ã¤æå®ããã¦ããã®ã§ãã³ã²ã¹ããããã§VPC Endpointã«åã(IGWãçµç±ããªã)çã ãåé¡ãªãã§ãããã¨æãããããã¾ãããè¨å®å¾ã«ã¯å®éã«éä¿¡ãçºçããåä½ç¢ºèª(è£ã©ã)ã¯ãã¦ãããããã®ã§ãã
確èªæ¹æ³ã®1ã¤ã®ä¾ã¨ãã¦ãCloudTrailã®ãã°ãã確èªããã¨ãã£ãææ³ãããã®ã§ä»åã¡ã¢ä»£ããã«æ®ãã¾ãã
ããããããäºãä¸è¡ã§çºããã¨
ãå½è©²Publicãµããããã«ããEC2ã¤ã³ã¹ã¿ã³ã¹ããS3ã¸ä½ããã®ã¢ã¯ã·ã§ã³ãå®è¡ããCloudTrailã®å¯¾è±¡ãã°ã«ããCloudTrailEventã®å
容ããçµè·¯ãå¤æãããã§ãã
CloudTrailã«ã¤ãã³ããã°ã¨ãã¦æ®ãããããã°ã©ã®ãããªãªãã¬ã¼ã·ã§ã³ã§ãè¯ãç(*3)ã§ãããä»åã¯ä¸æçã«é©å½ãªS3bucketãä½æããã®ãã°ããçµè·¯ã確èªãã¾ãã
ç°¡ææ¤è¨¼ç°å¢ã®æ§æã¯ãã£ãã以ä¸ã§ãã
|
EC2(Amazon Linux2)ããS3bucketãä½æãã¦åé¤ãã¾ãã
(è¨å¤§ãªéã®CloudTrailã®ãã°ã追ãããããªãã®ã§åæã«dateãå©ãã¦ããã¨å¾ã«å¹¸ãã«ãªãã¾ã)
# S3bucketä½æ [ec2-user@ip-10-0-0-225 ~]$ date;aws s3 mb s3://via-vpce-test Thu May 16 07:52:32 UTC 2019 make_bucket: via-vpce-test [ec2-user@ip-10-0-0-225 ~]$
# S3bucketåé¤ (ãæé¤ãç®ç) [ec2-user@ip-10-0-0-225 ~]$ date;aws s3 rb s3://via-vpce-test Thu May 16 07:52:41 UTC 2019 remove_bucket: via-vpce-test [ec2-user@ip-10-0-0-225 ~]$
ããã¦ãIGWçµç±ã¨ãªãããã«ã¼ãã£ã³ã°ãã¼ãã«ãè¨å®ãç´ã(VPC Endpointã¨Publicãµããããã®é¢é£ã¥ããå¤ã)ãä»åº¦ã¯ä¸ã®å 容ãS3bucketå: via-igw-testã¨ãã¦å度å®æ½ãã¾ãã(ãã¡ãã¯å·®å確èªããªãã¦è¯ããªãå¥ã«ãããªãã¦è¯ãã§ã)
# S3ã®VPC Endpointãã Publicãµããããã®ã«ã¼ããã¼ãã«ãRemove ytamu@SilverMachine:~$ aws ec2 modify-vpc-endpoint --vpc-endpoint-id vpce-0a7e08b0a117fddb5 --remove-route-table-ids=rtb-0b9d6fda75bce0adf --reset-policy { "Return": true } ytamu@SilverMachine:~$
å®æ½ãå®äºãããCloudTrailããS3bucketãä½æãã対象ãã°ãçªãæ¢ãã¾ãã
ytamu@SilverMachine:~$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=CreateBucket
"EventName": "CreateBucket" ãã°ã対象æ¥æåå¾ã§è¦ã¤ããçã§ãã
ããã¼ã¸ãã³ã³ã½ã¼ã«ãå©ç¨ãã¦ç¢ºèª(*5)ããæ¹ã楽ã ã¨æããã¾ãã
# CloudTrailã®ãã°åæ ã«ã¯å°ã
æéããããã®ã§ã¾ã åºåããã¦ããªããã°çç²ã§ã飲ã¿ãªããå¾
ã¡ã¾ããããããã¦åç
§ãªã¼ã¸ã§ã³ãééãçç²ãå·ãã¦ãã¾ãäºãããã®ã§æ°ãã¤ãã¾ãããã<-èªåã¸ã®æã
S3bucketä½æãã対象ãã°ã®CloudTrailEventã®ä¸èº«ã確èªããã¨ä»¥ä¸ã®ãããªæãã«ãªã£ã¦ãã
"vpcEndpointId": "vpce-0a7e08b0a117fddb5" ã¨ãã£ãä»åä½æããVPC Endpointã®IDæ
å ±ã2ç®æè¦ã¦åãã¾ãã
CloudTrailEvent[]ã®ä¸ã JSONã®ãªãã«JSONã¨ãã£ãæãã§äººã®ç®ã«åªãããªããã¸ã¥ã¢ã«ãªçºãä»åã¯jqã³ãã³ããã¼ã«ãå©ç¨ãã¦æ´å½¢ãã¦ãã¾ãã
ã¾ããä¸æ¬æã§ããè²ã
ã¨ä¼ããæ¹ãããããªæ
å ±ãããããåºãã®ã§ä¸é¨çµæã XXXX ã¨ãã£ãå½¢ã§æåã§ç½®ãæãã¦ãã¾ãã
ytamu@SilverMachine:~$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventId,AttributeValue=3b44035b-5a47-433d-aee4-deb02606ee7c --query 'Events[].CloudTrailEvent[]' --output text |jq { "eventVersion": "1.05", "userIdentity": { "type": "AssumedRole", "principalId": "XXXXXXXXXXXXXXXXXXXXX:i-XXXXXXXXXXXXXXXXX", "arn": "arn:aws:sts::XXXXXXXXXXXX:assumed-role/S3FullAccess-Role/i-0942293cad73b36c3", "accountId": "XXXXXXXXXXXX", "accessKeyId": "XXXXXXXXXXXXXXXXXXXX", "sessionContext": { "attributes": { "mfaAuthenticated": "false", "creationDate": "2019-05-16T07:28:34Z" }, "sessionIssuer": { "type": "Role", "principalId": "XXXXXXXXXXXXXXXXXXXXX", "arn": "arn:aws:iam::XXXXXXXXXXXX:role/S3FullAccess-Role", "accountId": "XXXXXXXXXXXX", "userName": "S3FullAccess-Role" } } }, "eventTime": "2019-05-16T07:52:34Z", "eventSource": "s3.amazonaws.com", "eventName": "CreateBucket", "awsRegion": "ap-northeast-1", "sourceIPAddress": "10.0.0.225", "userAgent": "[aws-cli/1.16.102 Python/2.7.14 Linux/4.14.114-103.97.amzn2.x86_64 botocore/1.12.92]", "requestParameters": { "CreateBucketConfiguration": { "LocationConstraint": "ap-northeast-1", "xmlns": "http://s3.amazonaws.com/doc/2006-03-01/" }, "bucketName": "via-vpce-test", "host": [ "via-vpce-test.s3.ap-northeast-1.amazonaws.com" ] }, "responseElements": null, "additionalEventData": { "SignatureVersion": "SigV4", "CipherSuite": "ECDHE-RSA-AES128-GCM-SHA256", "AuthenticationMethod": "AuthHeader", "vpcEndpointId": "vpce-0a7e08b0a117fddb5" }, "requestID": "XXXXXXXXXXXXXXXX", "eventID": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "eventType": "AwsApiCall", "recipientAccountId": "XXXXXXXXXXXX", "vpcEndpointId": "vpce-0a7e08b0a117fddb5" } ytamu@SilverMachine:~$
ä»åãVPC Endpointçµç±ã¨IGWçµç±ã®CloudTrailEventã®å·®åã¯ä»¥ä¸ã®éãã§ããã
"vpcEndpointId"ã®æç¡ã®éãã ãã§ãªãã
"sourceIPAddress"ãVPC Endpointçµç±ã®å ´åã¯EC2ã®ãã©ã¤ãã¼ãIPã¢ãã¬ã¹ã§ãããIGWçµç±ã®å ´åã¯AWSã®ã°ãã¼ãã«IPã¢ãã¬ã¹ã¨ãªã£ã¦ããäºããããã¾ãã
ytamu@SilverMachine:~$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventId,AttributeValue=3b44035b-5a47-433d-aee4-deb02606ee7c --query 'Events[].CloudTrailEvent[]' --output text |jq > /tmp/via-vpce-test ytamu@SilverMachine:~$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventId,AttributeValue=81a05480-c6cd-4168-bca1-0929ce9c637b --query 'Events[].CloudTrailEvent[]' --output text |jq > /tmp/via-igw-test ytamu@SilverMachine:~$ diff -u /tmp/via-igw-test /tmp/via-vpce-test --- /tmp/via-igw-test 2019-05-16 22:57:04.000000000 +0900 +++ /tmp/via-vpce-test 2019-05-16 22:56:47.000000000 +0900 @@ -5,11 +5,11 @@ "principalId": "XXXXXXXXXXXXXXXXXXXX":i-XXXXXXXXXXXXXXXXX", "arn": "arn:aws:sts::XXXXXXXXXXXX:assumed-role/S3FullAccess-Role/i-XXXXXXXXXXXXXXXXX", "accountId": "XXXXXXXXXXXX", - "accessKeyId": "XXXXXXXXXXXXXXXXXXXX", + "accessKeyId": "XXXXXXXXXXXXXXXXXXXX", "sessionContext": { "attributes": { "mfaAuthenticated": "false", - "creationDate": "2019-05-16T12:51:42Z" + "creationDate": "2019-05-16T07:28:34Z" }, "sessionIssuer": { "type": "Role", @@ -20,30 +20,32 @@ } } }, - "eventTime": "2019-05-16T13:33:09Z", + "eventTime": "2019-05-16T07:52:34Z", "eventSource": "s3.amazonaws.com", "eventName": "CreateBucket", "awsRegion": "ap-northeast-1", - "sourceIPAddress": "54.199.166.59", + "sourceIPAddress": "10.0.0.225", "userAgent": "[aws-cli/1.16.102 Python/2.7.14 Linux/4.14.114-103.97.amzn2.x86_64 botocore/1.12.92]", "requestParameters": { "CreateBucketConfiguration": { "LocationConstraint": "ap-northeast-1", "xmlns": "http://s3.amazonaws.com/doc/2006-03-01/" }, - "bucketName": "via-igw-test", + "bucketName": "via-vpce-test", "host": [ - "via-igw-test.s3.ap-northeast-1.amazonaws.com" + "via-vpce-test.s3.ap-northeast-1.amazonaws.com" ] }, "responseElements": null, "additionalEventData": { "SignatureVersion": "SigV4", "CipherSuite": "ECDHE-RSA-AES128-GCM-SHA256", - "AuthenticationMethod": "AuthHeader" + "AuthenticationMethod": "AuthHeader", + "vpcEndpointId": "vpce-0a7e08b0a117fddb5" }, - "requestID": "XXXXXXXXXXXXXXXX", - "eventID": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", + "requestID": "XXXXXXXXXXXXXXXX", + "eventID": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "eventType": "AwsApiCall", - "recipientAccountId": "XXXXXXXXXXXX" + "recipientAccountId": "XXXXXXXXXXXX", + "vpcEndpointId": "vpce-0a7e08b0a117fddb5" } ytamu@SilverMachine:~$
åèURI
*1...VPC Endpointã使ã£ã¦S3ã«ã¢ã¯ã»ã¹ãã¦ã¿ã
http://blog.serverworks.co.jp/tech/2015/08/31/vpc-endpoint/
*2...ã²ã¼ãã¦ã§ã¤ VPC ã¨ã³ããã¤ã³ã
https://docs.aws.amazon.com/ja_jp/vpc/latest/userguide/vpce-gateway.html
*3...AWS CloudTrail ã使ç¨ã㦠Amazon S3 API ã³ã¼ã«ã®ãã°ãè¨é²ãã
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/cloudtrail-logging.html
*4...jq
https://stedolan.github.io/jq/
*5...CloudTrailã®åºæ¬
http://blog.serverworks.co.jp/tech/2018/03/05/cloudtrail_basic/