Log on to CloudSSO by using Alibaba Cloud CLI

更新时间:
复制 MD 格式

CloudSSO integrates with Alibaba Cloud CLI, allowing you to log on to the CloudSSO user portal from the command line. After logon, select a Resource Directory account and an access configuration to access Alibaba Cloud resources.

Background information

Alibaba Cloud CLI version 3.0.271 and later introduce a new CloudSSO credential type that streamlines the configuration process. This is the recommended method. The legacy method for older versions is still supported. You can check your current CLI version by running the aliyun version command.

For Alibaba Cloud CLI V3.0.271 and later

This method supports interactive and non-interactive configuration. Use interactive mode for quick setup, or non-interactive mode for automation and scripting.

Interactive mode

  1. Run the aliyun configure command to start the configuration. You can use --profile to create multiple named profiles for different accounts or access configurations.

  2. aliyun configure --profile SSOProfile --mode CloudSSO
  3. When prompted, enter your user logon URL.

    aliyun configure --profile SSOProfile --mode CloudSSO
    CloudSSO Sign In Url []: https://signin-******.alibabacloudsso.com/device/login
  4. A browser window opens automatically. Follow the prompts to complete the logon process.

    If a browser window does not open, copy the SignIn url and User code from the CLI output and complete the process manually.

    Example prompt:

    If the browser does not open automatically, use the following URL to complete the login process:
    
    SignIn url: https://signin-****.alibabacloudsso.com/device/code
    User code: *********
  5. After a successful logon, the CLI lists the Resource Directory accounts you can access. Select the account you want to use.

    Now you can login to your account with SSO configuration in the browser.
    You have successfully logged in.
    Please choose an account:
    1. <RD Management Account>
    2. AccountName
    Please input the account number: 1
  6. Next, select one of the available access configurations.

    Please choose an access configuration:
    1. AccessConfiguration1
    2. AccessConfiguration2
    Please input the access configuration number: 2
  7. When prompted, set a default region.

    Default Region Id []: cn-hangzhou
  8. Upon successful configuration, a Configure Done message and a welcome banner are displayed.

Non-interactive mode

You can run the aliyun configure set command with the following options to configure a CloudSSO credential non-interactively.

Option

Description

Example

profile

Name of the CloudSSO credential. Supports letters, digits, and the following characters: _ / + = . @ -.

SSOProfile

mode

Credential type. Set to CloudSSO.

CloudSSO

cloud-sso-sign-in-url

The user logon URL. You can find this on the Overview page of the CloudSSO console.

https://signin-******.alibabacloudsso.com/device/login

cloud-sso-account-id

The ID of the target Resource Directory account. You can find this on the Multi-account Permission Configuration page of the CloudSSO console.

012345678910****

cloud-sso-access-config

The ID of the access configuration. You can find this on the Access Configuration page of the CloudSSO console.

ac-012345678910abcde****

region

Default region for CLI requests. Set this to the region where your resources are located.

cn-hangzhou

Example command:

aliyun configure set \
  --profile SSOProfile \
  --mode CloudSSO \
  --cloud-sso-sign-in-url "https://signin-******.alibabacloudsso.com/device/login" \
  --cloud-sso-account-id "012345678910****" \
  --cloud-sso-access-config "ac-012345678910abcde****" \ 
  --region "cn-hangzhou"
aliyun configure set `
  --profile SSOProfile `
  --mode CloudSSO `
  --cloud-sso-sign-in-url "https://signin-******.alibabacloudsso.com/device/login" `
  --cloud-sso-account-id "012345678910****" `
  --cloud-sso-access-config "ac-012345678910abcde****" `
  --region "cn-hangzhou"
Note

After configuring a CloudSSO credential non-interactively, you must perform a one-time interactive logon to authorize the CLI. Run aliyun configure --profile <profileName> and complete the browser-based logon.

For Alibaba Cloud CLI earlier than V3.0.271

Procedure

Step 1: Install CLIs

You must install Alibaba Cloud CLI and CloudSSO CLI:

  • Install Alibaba Cloud CLI

  • Install CloudSSO CLI

    1. Install Node.js.

      Install the package management tool npm when you install Node.js.

      Note

      You must install Node.js 7.6.0 or later. Install the latest LTS version for best compatibility.

    2. Run the following command to install the CloudSSO CLI:

      npm i @alicloud/sso-cli -g

    CloudSSO CLI.

Step 2: Configure CloudSSO access

  1. Run the following command to configure CloudSSO access:

    acs-sso configure
  2. Enter signinUrl, which indicates the URL that is used to log on to the CloudSSO user portal.

    Note

    To obtain the signinUrl, log on to the CloudSSO console, go to the Overview page, and then find the User Logon URL section on the right.

    Sample request:

    acs-sso configure
    ? please input 'signinUrl': https://signin-******.alibabacloudsso.com/device/login

    Sample success response:

    configuration done!

Step 3: Log on to Alibaba Cloud as a CloudSSO user

Commonly used commands:

  • Default logon

    1. Run the following command:

      acs-sso login
    2. A browser window opens. Log on to the user portal, then close the browser.

      If the browser does not open, copy the logon URL and user code from the CLI output to complete the process.

      Example:

      If your default browser is not opened automatically, please use the following URL to finish the signin process.
      Signin URL: https://signin-****.alibabacloudsso.com/device/code
      User Code: *********
    3. If you have access to multiple Resource Directory accounts, the CLI prompts you to select an account and access configuration, then generates the AccessKey pair.

      Example:

      You have logged in.
      used account: test-account(191585963325****)
      used access configuration: TestAC(ac-x08xz11covd3cyzd****)
      {
        "mode": "StsToken",
        "access_key_id": "STS.****",
        "access_key_secret": "****",
        "sts_token": "****"
      }

    After logon, the selected account and access configuration are cached in the profile and reused for subsequent logons.

  • Logon by using logon profiles

    acs-sso login --profile sso

    Use the --profile option to distinguish between multiple Resource Directory accounts and access configurations. The preceding command uses the profile named sso.

    If you omit --profile, the profile named default is used.

  • Obtain logon configurations.

    acs-sso profile --list
  • Delete a specific logon configuration.

    Run the following command to delete the default logon configuration:

    acs-sso profile --delete --profile default

    Run the following command to delete the logon configuration named sso:

    acs-sso profile --delete --profile sso
  • Configure the mode of the output.

    Configure one of the following output modes:

    • External process mode (default): Uses --mode External in Alibaba Cloud CLI. Use an external program to get credentials.

      Example:

      {
        "mode": "StsToken",
        "access_key_id": "STS.NUyPeEoab****",
        "access_key_secret": "GBubpmh****",
        "sts_token": "CAIS****"
      }
    • Environment variable mode: You can use this mode by configuring the --env parameter. Example: acs-sso login --profile user1 --env.

      Example:

      export ALIBABACLOUD_ACCESS_KEY_ID=STS.NUyPeEoab****
      export ALIBABACLOUD_ACCESS_KEY_SECRET=GBubpmh****
      export SECURITY_TOKEN=CAIS****

      Environment variables can be used together with Alibaba Cloud tools such as Terraform. Example: `acs-sso login --profile user1 --env` && terraform plan.

      Environment variables can be used together with Alibaba Cloud CLI. Example: `acs-sso login --profile user1 --env` && aliyun ecs DescribeRegions.

Step 4: Access Alibaba Cloud resources

Example:

aliyun configure --mode External --profile sso
Configuring profile 'sso' in 'External' authenticate mode...
Process Command []: acs-sso login --profile sso
Default Region Id []: cn-shanghai
Default Output Format [json]: json (Only support json)
Default Language [zh|en] en: 
Saving profile[sso] ...Done.

The Process Command value acs-sso login --profile sso specifies the logon profile as sso. Use the same profile name for both Alibaba Cloud CLI and CloudSSO CLI to keep credentials aligned.

Sample success response:

Configure Done!!!
..............888888888888888888888 ........=8888888888888888888D=..............
...........88888888888888888888888 ..........D8888888888888888888888I...........
.........,8888888888888ZI: ...........................=Z88D8888888888D..........
.........+88888888 ..........................................88888888D..........
.........+88888888 .......Welcome to use Alibaba Cloud.......O8888888D..........
.........+88888888 ............. ************* ..............O8888888D..........
.........+88888888 .... Command Line Interface(Reloaded) ....O8888888D..........
.........+88888888...........................................88888888D..........
..........D888888888888DO+. ..........................?ND888888888888D..........
...........O8888888888888888888888...........D8888888888888888888888=...........
............ .:D8888888888888888888.........78888888888888888888O ..............

Verify Alibaba Cloud CLI access:

aliyun sts GetCallerIdentity --profile sso