티스토리 뷰

카테고리 없음

AWS Security Token Service(STS)

미니대왕님 2021. 3. 17. 13:44

AWS Security Token Service(STS) 는 임시적인 제한된 credential을 제공하기 위해서 사용되며 IAM user혹은 Federated User을 위해서 사용된다.

사용이 필요한 이유와 use cases.

1) 코드 안에 embed token이 필요없다.

2) lifetime을 제한할 수 있으며 15분에서 36시간 까지 가능하다.

3) Identity federation 회사 AD, Web, Google, Facebook 접속을 가능하게 한다.

4) EC2 instance의 앱에서 접근이 가능하게 한다.

세팅

Step1

IAM User를 만든다.

Access Type으로는 Programmatic access만 체크를 하면 되며 여기서는 특정 그룹이나 policy를 붙이지 않아도 된다.

Step2

Roles을 만들고 두번째인 Another AWS account을 선택한다.

Policy를 붙인다. (AmazonS3ReadOnlyAccess)

Review and create role

Step3:

Trust Relationships 탭으로 가서

The current trust relation only allow root account to assume this role

Modify it with the arn of the user(myteststsuser) we have just created

Step4

Add inline policy to the user we have created

Service: STS

Action: AssumeRole

Resource: ARN of the role we created earlier

This is making our user assume the role

Step5:

Testing

$ aws configure --profile ststestprofile

AWS Access Key ID [None]: XXXXXXXX

AWS Secret Access Key [None]: XXXXXX

Default region name [None]: us-west-2

Default output format [None]: json

Also, export this profile for the time being

$ export AWS_PROFILE=ststestprofile

아래와 같은 코드를 통해서 access key id, access key, token값을 얻어낸다. 아래서 expiration date가 있는 것을 확인하자.

$ aws sts assume-role --role-arn arn:aws:iam::XXXXXX:role/sts-s3-read-only --role-session-name "mytestsession"

{

"AssumedRoleUser": {

"AssumedRoleId": "XXXXXXX:mytestsession",

"Arn": "arn:aws:sts::XXXXXXX:assumed-role/sts-s3-read-only/mytestsession"

},

"Credentials": {

"SecretAccessKey": "XXXXXXX",

"SessionToken": "XXXXXXX",

"Expiration": "2018-12-18T06:47:21Z",

"AccessKeyId": "XXXXXXXXX"

}

}

export가 된 아래 값을 확인하고, 아래 같은 temporary한 키값으로 여길 수 있다.

export AWS_ACCESS_KEY_ID="XXXXXXX"

export AWS_SECRET_ACCESS_KEY="XXXXXXX"

export AWS_SECURITY_TOKEN="XXXXXXX"

Try to access S3 bucket

$ aws s3 ls

2018-12-13 20:53:05 mytestXXXXXX

OR

$ aws s3 cp bucketest s3://mytestXXXXXX

upload failed: ./bucketest to s3://mytestXXXXXX/bucketest An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

 

 

댓글