OktaでSAML連携しているAWSアカウントにcliでログインするokta-awscli使ってみた

OktaでSAML連携しているAWSアカウントへのawscliアクセスに利用できるツールをみつけたのでやってみました。簡単に設定できていいです。
2020.02.23

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、臼田です。

みなさん、SSOしてますか?(挨拶

今回はOktaでSAML連携しているAWSアカウントに対してawscliを利用する方法を調査したので紹介します。

okta-awscliとは

Oktaからawscliを利用するための公式ライブラリはありませんが、公式のヘルプに下記情報がまとまっています。

Integrating the Amazon Web Services Command Line Interface Using Okta

コミュニティソリューションとしてGoやPythonの実装があり、そのうちの一つがokta-awscliです。

下記のようないろんな認証方式にも対応しています。

  • テナント全体のMFAサポート
  • アプリケーションごとのMFAサポート(バージョン0.4.0で追加)
  • Okta検証
  • Okta Verifyプッシュサポート
  • Google Authenticator
  • YubiKey(ライブラリpython-u2flib-hostが必要)

設定も簡単で使いやすかったので紹介します。

やってみた

インストール

pipからインストールできます。

$ pip install okta-awscli

コンフィグ設定

~/.okta-awsにコンフィグファイルを作成します。

サンプルは下記のようになっています。

[default]
base-url = <your_okta_org>.okta.com

## The remaining parameters are optional.
## You will be prompted for them, if they're not included here.
username = <your_okta_username>
password = <your_okta_password> # Only save your password if you know what you are doing!
factor   = <your_preferred_mfa_factor> # Current choices are: GOOGLE or OKTA
role     = <your_preferred_okta_role> # AWS role name (match one of the options prompted for by "Please select the AWS role" when this parameter is not specified
app-link = <app_link_from_okta> # Found in Okta's configuration for your AWS account.
duration = 3600 # duration in seconds to request a session token for, make sure your accounts (both AWS itself and the associated okta application) allow for large durations. default: 3600

パスワードなどは書きたくないので私は下記だけ設定しました。

[default]
base-url = <your_okta_org>.okta.com

## The remaining parameters are optional.
## You will be prompted for them, if they're not included here.
username = <your_okta_username>

必要な内容はインタラクティブに補完されます。

クレデンシャル取得

それでは実行します。実行するとパスワード入力を求められるので入力します。

$ okta-awscli
Enter password:

続いてMFAの確認があるのでアプリプッシュを選択します(環境に合わせて選択)

Registered MFA factors:
1: Okta Verify - Push
2: Okta Verify
Please select the MFA factor: 1
Waiting for push verification...

利用できるAWS Appが表示されるため選択します。

Available apps:
1: AWS - ********
2: AWS - ********
Please select AWS app: 2

利用できるIAM Roleが表示されるため選択します。

1: arn:aws:iam::999999999999:role/your-role
Please select the AWS role: 1

アクセスキー等が出力されました。

export AWS_ACCESS_KEY_ID=ASXXXXXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=zdXXXXXXXXXXXXXXXXXXXXXXX
export AWS_SESSION_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

これをそのまま実行して環境変数に設定すると、利用できるようになります。

aws configure listaws sts get-caller-identityを実行して適切に設定されていることが確認できます。

まとめ

okta-awscliを利用してOktaでSAML連携しているAWSアカウントに対してawscliを実行できました。

profile登録もできますので日常使いもはかどりますね。