はじめに
OCI CLIの認証方式としてAPIキーを作成して、クライアントに保存しておく方法が一般的(と思ってます)ですが、これとは別の方法としてトークンベースの認証方法を確認します。
トークンの生成
今回はOCI CLIをインストール済みのMACをクライアントとして使用します。
CLIはインストール済みですが、.oci/config
は設定していません。
以下のコマンドを実行し、リージョンを指定します。
% oci session authenticate
Enter a region by index or name(e.g.
1: af-johannesburg-1, 2: ap-chiyoda-1, 3: ap-chuncheon-1, 4: ap-dcc-canberra-1, 5: ap-hyderabad-1,
6: ap-ibaraki-1, 7: ap-melbourne-1, 8: ap-mumbai-1, 9: ap-osaka-1, 10: ap-seoul-1,
11: ap-singapore-1, 12: ap-sydney-1, 13: ap-tokyo-1, 14: ca-montreal-1, 15: ca-toronto-1,
16: eu-amsterdam-1, 17: eu-dcc-dublin-1, 18: eu-dcc-dublin-2, 19: eu-dcc-milan-1, 20: eu-dcc-milan-2,
21: eu-dcc-rating-1, 22: eu-dcc-rating-2, 23: eu-dcc-zurich-1, 24: eu-frankfurt-1, 25: eu-frankfurt-2,
26: eu-jovanovac-1, 27: eu-madrid-1, 28: eu-madrid-2, 29: eu-marseille-1, 30: eu-milan-1,
31: eu-paris-1, 32: eu-stockholm-1, 33: eu-zurich-1, 34: il-jerusalem-1, 35: me-abudhabi-1,
36: me-dcc-muscat-1, 37: me-dubai-1, 38: me-jeddah-1, 39: mx-monterrey-1, 40: mx-queretaro-1,
41: sa-bogota-1, 42: sa-santiago-1, 43: sa-saopaulo-1, 44: sa-vinhedo-1, 45: uk-cardiff-1,
46: uk-gov-cardiff-1, 47: uk-gov-london-1, 48: uk-london-1, 49: us-ashburn-1, 50: us-chicago-1,
51: us-gov-ashburn-1, 52: us-gov-chicago-1, 53: us-gov-phoenix-1, 54: us-langley-1, 55: us-luke-1,
56: us-phoenix-1, 57: us-saltlake-2, 58: us-sanjose-1): 48
Please switch to newly opened browser window to log in!
You can also open the following URL in a web browser window to continue:
https://login.uk-london-1.oraclecloud.com/v1/oauth2/xxxxxxxxxx
ブラウザが立ち上がりますので、テナンシーを入力します。
サインインします。
MFAは何を設定しているかでこの画面は変わると思います。
サインインできると以下のように表示されますので、ブラウザを閉じます。
MACのコンソールに以下のように表示されます。.oci/config
ファイルが作成されます。
Completed browser authentication process!
Config written to: /Users/xxxxx/.oci/config
Try out your newly created session credentials with the following example command:
oci iam region list --config-file /Users/xxxxx/.oci/config --profile DEFAULT --auth security_token
確認
トークンを使用して認証できることを確認します。
% oci iam region list --config-file /Users/xxxxx/.oci/config --profile DEFAULT --auth security_token
{
"data": [
{
"key": "AMS",
"name": "eu-amsterdam-1"
},
・・・
トークンの有効期限を確認します。デフォルトは60分です。
% oci session validate --config-file .oci/config --profile DEFAULT --auth security_token
Session is valid until 2024-01-04 14:32:44
% date
2024年 1月 4日 木曜日 13時41分25秒 JST
ブラウザを使用しないトークンの生成と有効時間の設定
先ほどはトークン作成時にブラウザで認証しましたが、--no-browser
オプションを指定すると、ブラウザを使用しないでトークンを作成できます。
この場合は以下のどちらかで認証する必要があります。
- APIキー・ベース認証(OCI設定構成)
- セッション・トークン・ベース認証(ociセッション認証)
また、--session-expiration-in-minutes
オプションでトークンの有効時間を指定できます。最小は5分です。
以下は、先ほど作成したトークンベースの認証(--auth security_token
)でトークンを作成しています。
% oci session authenticate --no-browser --auth security_token --session-expiration-in-minutes
有効期限が先ほどとは違い、5分になっています。
% oci session validate --config-file .oci/config --profile no-browser --auth security_token
Session is valid until 2024-01-04 14:14:51
% date
2024年 1月 4日 木曜日 14時10分33秒 JST
参考
--no-browser
オプションを指定せずに--session-expiration-in-minutes
オプションを指定した場合、エラーにはならないのですが、有効期限はデフォルトの60分のままでした。
% oci session authenticate --session-expiration-in-minutes 5
・・・
% oci session validate --config-file .oci/config --profile test --auth security_token
Session is valid until 2024-01-04 15:12:16
% date
2024年 1月 4日 木曜日 14時12分43秒 JST