0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

1PasswordでAWS MFAコードを自動入力!AWSume + 1Password CLIでAWS認証をワンライナー化する手順

Posted at

はじめに

  • 大分前でしたが、いつもお世話になっているDeveloper IOにて、awsumeという認証に強いツールが紹介されていました
  • 時間が空いたので早速使ってみよう...としたのですが、仕様が地味に変わったのか、ご紹介されていた手順が少し異なっていたため、最新版として整理をしてみました

やりたいこと

  • AWS CLIでMFA認証を含めたIAMユーザーのログインをワンライナーで実行する
  • MFA認証に1Passwordを用いる

前提

手順

  1. awsumeをpipインストールする

    # awsumeのインストール
    pip install awsume
    
    # バージョン確認
    awsume --version
    
    実行例
    PS > pip install awsume
    Collecting awsume
    Downloading awsume-4.5.4-py3-none-any.whl.metadata (772 bytes)
    Requirement already satisfied: colorama in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from awsume) (0.4.6)
    Requirement already satisfied: boto3 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from awsume) (1.35.49)
    Collecting psutil (from awsume)
    Downloading psutil-7.0.0-cp37-abi3-win_amd64.whl.metadata (23 kB)
    Requirement already satisfied: pluggy in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from awsume) (1.6.0)
    Requirement already satisfied: pyyaml in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from awsume) (6.0.2)
    Requirement already satisfied: botocore<1.36.0,>=1.35.49 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from boto3->awsume) (1.35.99)
    Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from boto3->awsume) (1.0.1)
    Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from boto3->awsume) (0.10.4)
    Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from botocore<1.36.0,>=1.35.49->boto3->awsume) (2.9.0.post0)
    Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from botocore<1.36.0,>=1.35.49->boto3->awsume) (2.2.2)
    Requirement already satisfied: six>=1.5 in c:\users\xxxxx\appdata\local\programs\python\python312\lib\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.36.0,>=1.35.49->boto3->awsume) (1.16.0)
    Downloading awsume-4.5.4-py3-none-any.whl (39 kB)
    Downloading psutil-7.0.0-cp37-abi3-win_amd64.whl (244 kB)
    Installing collected packages: psutil, awsume
    Successfully installed awsume-4.5.4 psutil-7.0.0
    PS > awsume --version
    4.5.4
    PS >
    

    いくつかのドキュメントではHomebrewコマンドを用いたインストールを提案している場合があります
    ただし、後続する1PasswordのプラグインがHomebrew版のawsumeだとインストールエラーが出るため、pip版をインストールするようにしてください

  2. 1Passwordのプラグインをインストールする

    # awsume-1password-pluginのインストール
    pip install awsume-1password-plugin
    
    # 1passwordのインストール確認
    awsume --list-plugins
    
    実行例
    PS > pip install awsume-1password-plugin
    Collecting awsume-1password-plugin
    Downloading awsume_1password_plugin-1.2.4-py3-none-any.whl.metadata (263 bytes)
    Downloading awsume_1password_plugin-1.2.4-py3-none-any.whl (4.1 kB)
    Installing collected packages: awsume-1password-plugin
    Successfully installed awsume-1password-plugin-1.2.4
    PS > awsume --list-plugins
    1password
    PS >
    
  3. 1Password CLIをインストールする

    # 1Password CLI
    winget install 1password-cli
    
    # Powershellを再起動
    
    # 1passwordのインストール確認
    op --version
    
    実行例
    PS > winget install 1password-cli
    見つかりました 1Password CLI [AgileBits.1Password.CLI] バージョン 2.31.1
    このアプリケーションは所有者からライセンス供与されます。
    Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
    インストーラーハッシュが正常に検証されました
    アーカイブを展開しています...
    アーカイブが正常に展開されました
    パッケージのインストールを開始しています...
    パス環境変数が変更されました; 新しい値を使用するにはシェルを再起動してください。
    コマンド ライン エイリアスが追加されました: "op"
    インストールが完了しました
    PS >
    
    PS > op --version
    2.31.1
    PS >
    
  4. アクセスキー設定

    • 未設定の場合のみ
    # 設定コマンド
    aws configure
    
    # 設定ファイル確認
    cat $HOME\.aws\credentials
    cat $HOME\.aws\config
    
    実行例
    PS > aws configure
    AWS Access Key ID [********************]:********************
    AWS Secret Access Key [********************]:********************
    Default region name [ap-northeast-1]:ap-northeast-1
    Default output format [json]:json
    PS >
    
    PS > cat $HOME\.aws\credentials
    [awsume-mfa-user]
    aws_access_key_id = ********************
    aws_secret_access_key = ****************************************
    PS > cat $HOME\.aws\config
    [profile awsume-mfa-user]
    region = ap-northeast-1
    output = json
    PS >
    
  5. MFA設定する

    notepad $HOME\.aws\config
    
    以下を追加
    mfa_serial = <MFAデバイスのARN>
    
    実行例
    PS > cat $HOME\.aws\config
    [profile awsume-mfa-user]
    mfa_serial = arn:aws:iam::xxxxxxxxxxxxxx:mfa/awsume-mfa-user
    region = ap-northeast-1
    output = json
    PS >
    
  6. awsume設定する

    notepad $HOME\.awsume\config.yaml
    
    # 以下を追加
    1password: 
      "<MFAデバイスのARN>": "<1Passwordの項目名>"
    
    実行例
    PS > cat $HOME\.awsume\config.yaml
    colors: true
    fuzzy-match: false
    role-duration: 0
    
    1password:
      "arn:aws:iam::xxxxxxxxxxxxxx:mfa/awsume-mfa-user": "AWS(awsume-mfa-user)" 
    PS >
    
  7. awsume設定する

    • 設定項目
      • サインインアドレス
      • メールアドレス
      • シークレットキー
      • 1Passwordのパスワード(いつも使っているやつ)
    • 設定項目の入手工程(1PasswordをDesktopにインストールしている場合)
      1. 1Password Desktopを開く
      2. 画面左上、「アカウント」をクリック
      3. リストされているアカウント名をクリック
      4. 必要な値をコピーしておく
    # 認証する
    Invoke-Expression $(op signin)
    
    # 認証を確認する
    op whoami
    
    実行例
    PS > Invoke-Expression $(op signin)
    No accounts configured for use with 1Password CLI.
    
    You can either:
    - Turn on the 1Password desktop app integration to sign in with the accounts you've added to the app: https://developer.1password.com/docs/cli/app-integration/ for details.
    - Add an account manually with 'op account add' and sign in by entering your password on the command line. See 'op account add --help' for details.
    - Authenticate using a 1Password service account by setting the 'OP_SERVICE_ACCOUNT_TOKEN' environment variable to your service account token. Learn more: https://developer.1password.com/docs/service-accounts/
    - Use 1Password CLI with a Connect server by setting the 'OP_CONNECT_HOST' and 'OP_CONNECT_TOKEN' environment variables to your Connect host and token, respectively. Learn more: https://developer.1password.com/docs/connect/
    
    Do you want to add an account manually now? [Y/n] Y  ←※入力項目
    Enter your sign-in address (example.1password.com): https://xxx.1password.com/  ←※入力項目
    Enter the email address for your account on my.1password.com/: xxxxxxxxx@xxxxx.xxxxx  ←※入力項目
    Enter the Secret Key for xxxxxxxxx@xxxxx.xxxxx on my.1password.com: xx-xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx  ←※入力項目
    Enter the password for xxxxxxxxx@xxxxx.xxxxx at my.1password.com:  ←※入力項目
    PS > op whoami
    URL:          https://xxx.1password.com/
    Email:        xxxxxxxxx@xxxxx.xxxxx
    User ID:      XXXXXXXXXXXXXXXXXXXXX
    User Type:    HUMAN
    PS >
    
  8. ログインで使用するプロファイル名を確認する

    awsume --list-profiles
    
    実行例
    PS > awsume --list-profiles
    Listing...
    
    ===============================AWS Profiles===============================
    PROFILE          TYPE  SOURCE  MFA?  REGION          PARTITION  ACCOUNT
    awsume-mfa-user  User  None    Yes   ap-northeast-1  aws        xxxxxxxxxxxxxx
    PS >
    
  9. 接続する

    # 接続コマンド
    awsume <プロファイル名>
    
    # 接続できたことの確認
    aws sts get-caller-identity
    
    実行例
    PS > awsume awsume-mfa-user
    Obtained MFA token from 1Password item: AWS(awsume-mfa-user)
    Session token will expire at 2025-06-23 08:15:11
    PS >
    
    PS > aws sts get-caller-identity
    {
        "UserId": "AIDA3VAYNP5DAFNC5SZZM",
        "Account": "xxxxxxxxxxxxxx",
        "Arn": "arn:aws:iam::xxxxxxxxxxxxxx:user/awsume-mfa-user"
    }
    
    PS >
    

おわりに

  • 今回はアクセスキー/シークレットーキーをローカルに設定する方法でご紹介しました
  • ただし、昨今はこのスタイルは非推奨となっています
  • SSO(Identity Center)を認証として使うawsumeのコマンドがあるそうなので、この辺りも近いうち調査をしてみたいなぁと考えていますのでお楽しみに!!

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?