From 52f0943207449fff19458e113c5dd9f33c0be146 Mon Sep 17 00:00:00 2001 From: Shuichi Ohsawa Date: Sun, 5 Mar 2017 19:06:53 +0900 Subject: [PATCH] Add ec2 roles credentials. --- README.ja.md | 31 +++++++++++++++++++++++++++++-- README.md | 32 ++++++++++++++++++++++++++++++-- report/s3.go | 15 +++++++++++---- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/README.ja.md b/README.ja.md index 0b08dcc5..c04f713f 100644 --- a/README.ja.md +++ b/README.ja.md @@ -1059,8 +1059,35 @@ With this sample command, it will .. 事前にAWS関連の設定を行う - S3バケットを作成 [Creating a Bucket](http://docs.aws.amazon.com/AmazonS3/latest/UG/CreatingaBucket.html) -- アクセスキーを作成し、S3バケットへのREAD/WRITE権限をつけておく [Managing Access Keys for IAM Users](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) -- security credentialsを設定 [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) +- いずれかの方法でS3リソースへアクセスする設定を行う + - 環境変数を設定 [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) + - Security Credentialsを設定 [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) + - サービス用のIAMロールを作成し、サービス(EC2, AWS Lambda)にアタッチ [Creating a Role to Delegate Permissions to an AWS Service](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html) +- 環境変数、Security Credentialsを設定する場合はアクセスキーを作成する [Managing Access Keys for IAM Users](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) + +IAMポリシーの例: + +``` +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListAllMyBuckets" + ], + "Resource": "arn:aws:s3:::*" + }, + { + "Effect": "Allow", + "Action": [ + "s3:PutObject" + ], + "Resource": "arn:aws:s3:::vuls/*" + } + ] +} +``` ``` $ vuls scan \ diff --git a/README.md b/README.md index 2684fc1b..6135ae00 100644 --- a/README.md +++ b/README.md @@ -1064,8 +1064,36 @@ With this sample command, it will .. ## Example: Put results in S3 bucket To put results in S3 bucket, configure following settings in AWS before reporting. - Create S3 bucket. see [Creating a Bucket](http://docs.aws.amazon.com/AmazonS3/latest/UG/CreatingaBucket.html) -- Create access key. The access key must have read and write access to the AWS S3 bucket. see [Managing Access Keys for IAM Users](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) -- Configure the security credentials. see [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) +- Configure access to S3 resources. You can do this in several ways: + - Configure the environment variables. see [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) + - Configure the security credentials. see [Configuring the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) + - Create an IAM role for the service and attach it to the service(EC2, AWS Lambda). [Creating a Role to Delegate Permissions to an AWS Service](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html) +- To configure environment variables, security credentials, create an access key. see [Managing Access Keys for IAM Users](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) + + +Example of IAM policy: + +``` +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListAllMyBuckets" + ], + "Resource": "arn:aws:s3:::*" + }, + { + "Effect": "Allow", + "Action": [ + "s3:PutObject" + ], + "Resource": "arn:aws:s3:::vuls/*" + } + ] +} +``` ``` $ vuls report \ diff --git a/report/s3.go b/report/s3.go index abf1f98c..1137324b 100644 --- a/report/s3.go +++ b/report/s3.go @@ -26,6 +26,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds" + "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" @@ -37,10 +39,15 @@ import ( type S3Writer struct{} func getS3() *s3.S3 { - return s3.New(session.New(&aws.Config{ - Region: aws.String(c.Conf.AwsRegion), - Credentials: credentials.NewSharedCredentials("", c.Conf.AwsProfile), - })) + Config := &aws.Config{ + Region: aws.String(c.Conf.AwsRegion), + Credentials: credentials.NewChainCredentials([]credentials.Provider{ + &credentials.EnvProvider{}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: c.Conf.AwsProfile}, + &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(session.New())}, + }), + } + return s3.New(session.New(Config)) } // Write results to S3