Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch RoleしてS3にアクセスできるようにする #18

Closed
wants to merge 4 commits into from

Conversation

ddddddO
Copy link
Contributor

@ddddddO ddddddO commented Sep 22, 2023

@orangekame3

こちらのツールで、Switch RoleしてS3をツリー出力したいと思い、一つだけオプションを追加いたしました🙇

以下のドキュメントを参考に、AWS上でSwitchRoleできるように準備し、動作確認しました!

確認が足りない等あれば、ご連絡お待ちしてます!

~/github.com/ddddddO/stree
08:52:01 > go run *.go show-tree-bucket -p dev_david           # dev_david 自体には当該バケット(show-tree-bucket)を見る権限は無い
2023/09/23 08:52:27 failed to fetch S3 object keys: AccessDenied: Access Denied
        status code: 403, request id: T956TAXXXXXXXXXX, host id: kGxQ/04c9rlssucDV4o3s0C0dPSfefmTLHLFXSgIGGBUcdNLH4dVxogu5H5X0ERxxxxxxxxxxxxx
exit status 1
~/github.com/ddddddO/stree
08:52:26 > go run *.go show-tree-bucket -p dev_david -s arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket             # dev_david は、-s(--switch-role)で指定されたロールに切り替えられる設定がされており、このロールは当該バケット(show-tree-bucket)の読み書きが出来る権限が備わっている
show-tree-bucket
├── demo.gif
├── first
│   └── chil
│       └── chilchil
│           ├── verify.png
│           └── web_example.gif
└── second

4 directories, 3 files
~/github.com/ddddddO/stree
08:52:47 > go run *.go show-tree-bucket -p tester_jane           # tester_jane 自体に当該バケット(show-tree-bucket)を見る権限は無い
2023/09/23 08:52:59 failed to fetch S3 object keys: AccessDenied: Access Denied
        status code: 403, request id: G0GHETXXXXXXXXXX, host id: LwzAOleZlz8tvYQ9RyIlnRyRJ7qcXCLd8A0aLuaBNFTtVagRYQ2iFvwrHPD/wy/xxxxxxxxxxxxx
exit status 1
~/github.com/ddddddO/stree
08:52:59 > go run *.go show-tree-bucket -p tester_jane -s arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket             # tester_jane は、-s(--switch-role)で指定されたロールに切り替えられる設定をしていないため、Switch Roleができない
2023/09/23 08:53:15 failed to fetch S3 object keys: AccessDenied: User: arn:aws:iam::820544363308:user/Jane is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket
        status code: 403, request id: a995bd81-53f4-459c-8138-xxxxxxxxxxxx
exit status 1
~/github.com/ddddddO/stree
08:53:15 > go run *.go show-tree-bucket -p default           # default は、show-tree-bucket バケットの作成者. デグレチェックのため実行
show-tree-bucket
├── demo.gif
├── first
│   └── chil
│       └── chilchil
│           ├── verify.png
│           └── web_example.gif
└── second

4 directories, 3 files
~/github.com/ddddddO/stree
08:53:33 >

取り込んでいただけると幸いです🙇

@orangekame3
Copy link
Owner

orangekame3 commented Sep 23, 2023

@ddddddO

PRありがとうございます!とても嬉しいです。
週末予定があり、詳細な確認が遅れそうなので取り急ぎコメントさせていただきます。

スイッチロール、検証していませんでした!
ありがとうございます。

こちら、検証していないので推測となりますがもしかするとスイッチロール自体はconfigファイルの設定(profileの指定)でなんとかなるかもしれません。

▼config

[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
source_profile = user1

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html

↑profile指定で、既存のNewSsessionして、プロファイル読み込んでいるところでロールの情報も読み込んでくれそうな気がしています。

一方で認証にMFAを利用する場合は既存の実装では不十分でstsのパッケージを利用する必要がありそうです。

↑MFAを利用する場合は--mfaフラグを用意しておいて--mfa利用時のみ標準入力を立ち上げてKeyを入力させてstsの適当なパッケージでsessionをラップしてあげるとよいのかなと思っています。
(Keyを入力時は、伏せ字にする)

このコメントですが、実証せずに記入しているので誤っていたらすみません。🙇

@ddddddO
Copy link
Contributor Author

ddddddO commented Sep 23, 2023

@orangekame3
お忙しいところ、見てくださりありがとうございます!!(すみません💦)

提示いただいた方法でやってみると、ツリー出力できました!(勉強になりました、configにRoleを追加すればよいのはためになります🙇)

↓ は、検証ログです🙋

~/github.com/ddddddO/stree
09:27:35 > go run *.go show-tree-bucket -r ap-northeast-1 -p S3ReadWriter_show-tree-bucket      #
show-tree-bucket
├── demo.gif
├── first
│   └── chil
│       └── chilchil
│           ├── verify.png
│           └── web_example.gif
└── second

4 directories, 3 files
~/github.com/ddddddO/stree
09:28:55 > vim ~/.aws/config
~/github.com/ddddddO/stree
09:30:41 > go run *.go show-tree-bucket -r ap-northeast-1 -p S3ReadWriter_show-tree-bucket      #
2023/09/23 09:31:05 failed to fetch S3 object keys: AccessDenied: User: arn:aws:iam::820544363308:user/Jane is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket
        status code: 403, request id: b808dc29-4c27-41f7-92ae-085b0axxxxxx
exit status 1
~/github.com/ddddddO/stree
09:31:05 >

①: この時点の ~/.aws/config の内容は以下(一部抜粋)

[profile dev_david]
output = json
region = ap-northeast-1
[profile S3ReadWriter_show-tree-bucket]
role_arn = arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket
source_profile = dev_david
  • S3ReadWriter_show-tree-bucket ロールは、show-tree-bucket バケットの読み書きが出来る権限が備わっている
  • dev_david は、S3ReadWriter_show-tree-bucket ロールに切り替えることが出来る設定をしている

②: この時点の ~/.aws/config の内容は以下(一部抜粋)

[profile tester_jane]
output = json
region = ap-northeast-1
[profile S3ReadWriter_show-tree-bucket]
role_arn = arn:aws:iam::820544363308:role/S3ReadWriter_show-tree-bucket
source_profile = tester_jane
  • S3ReadWriter_show-tree-bucket ロールは、show-tree-bucket バケットの読み書きが出来る権限が備わっている
  • tester_jane は、S3ReadWriter_show-tree-bucket ロールに切り替える出来る設定をしていない

今あるものでできるので、ちょっとこちらのPRは一旦Closeします🙇(mfaについてもありがとうございます!こちらは誰かに譲る or 気が向いたらやってみます!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants