自動處理權杖產生程序

您正在查看 Apigee Edge 說明文件。
查看 Apigee X 說明文件
資訊

將 SAML 與 Edge API 搭配使用時,您從 SAML 宣告取得 OAuth2 存取權及更新權杖的程序稱為「通行碼」流程。透過密碼流程,您可以透過瀏覽器取得動態密碼,然後用於取得 OAuth2 權杖。

不過,您的環境可能支援自動執行常見開發工作,例如測試自動化或持續整合/持續部署 (CI/CD)。如要在啟用 SAML 的情況下自動執行這些工作,您需要能取得及更新 OAuth2 權杖的方法,而不必從瀏覽器複製/貼上密碼。

關於電腦使用者

Apigee Edge 可在已啟用 SAML 的機構中支援電腦使用者機器使用者通常用於自動化作業,且無法由真人使用者直接存取。

機器使用者不必指定密碼即可取得 OAuth2 權杖。這表示您可以使用 Edge API,完全自動化取得及更新 OAuth2 權杖的程序。

自動化權杖產生程序的步驟

如要自動化權杖產生程序,請按照下列指示操作:

步驟 說明
1 在 SAML 身分識別區域中建立電腦使用者
2 將必要角色指派給 Edge 機構中的機器使用者
3 取得電腦使用者的 OAuth2 權杖

影片:請觀看短片,瞭解如何利用機器使用者憑證自動存取 Apigee Edge API。

管理 SAML 身分識別區域的機器使用者

Apigee 提供機器使用者管理指令列介面 (CLI),可用來建立及管理機器使用者帳戶。 以下各節將說明使用裝置使用者管理 CLI 的步驟。

使用 CLI

如要使用電腦使用者管理 CLI,請先下載並解壓縮下列檔案: usermgmt.tar.gz(1)

呼叫 CLI 的格式如下:

usermgmt_platform [command] [flags]

下表摘要說明支援的平台,以及呼叫裝置使用者管理 CLI 的對應指令。 (執行檔位於 usermgmt 目錄中)。

平台 32 位元 64 位元
Linux usermgmt_linux_386 usermgmt_linux_amd64
Mac usermgmt_darwin_386 usermgmt_darwin_amd64
Windows usermgmt_windows_386 usermgmt_windows_amd64

下表摘要說明可指定的指令

指令 更多資訊
create 在身分區域中建立機器使用者
delete 刪除身分可用區中的機器使用者
help 取得使用 CLI 的相關協助
list 列出識別可用區中的所有機器使用者
reset 重設身分區域中機器使用者的密碼

您也可以選擇傳送下列其中一個標記,顯示指定指令的說明:-h--help

登入 CLI

您在 24 小時內首次執行 CLI 時,系統會提示您輸入 zoneadmin 帳戶憑證。

Enter your Apigee credentials
Username: zoneadmin-username
Password: zoneadmin-password
If your user is opted with MFA, enter MFA code. Otherwise press enter to skip.
MFA: mfa-code_or_enter_to_skip

機器使用者管理 CLI 會將存取權杖儲存在本機電腦中,因此您每 24 小時只需登入一次。

使用 CLI 取得協助

使用 usermgmt_platform help 指令顯示 CLI 使用資訊。如要查看支援的平台清單,請參閱使用 CLI

usermgmt_platform help

系統會顯示下列說明資訊:

A command-line interface (CLI) to manage machine user accounts to automate
Apigee identity zone management. Use the CLI to create, list, delete,
and reset the password for machine users.

Usage:
  usermgmt [flags]
  usermgmt [command]

Available Commands:
  create  Creates a machine users in an identity zone.
  delete  Deletes a machine users in an identity zone.
  help    Help about any command
  list    Lists the machine users in an identity zone.
  reset   Resets the password for a machine user in an identity zone.

Flags:
  -h, --help               help for usermgmt

Use "usermgmt [command] --help" for more information about a command.

同時傳送指令和指令列上的 -h--help 標記,以顯示特定指令的說明。

舉例來說,如要取得清單指令的相關說明:

usermgmt_platform list -h

系統會顯示下列說明資訊:

Lists the machine users in an identity zone.

Usage:
  usermgmt list [flags]

Flags:
  -h, --help   help for list

在身分可用區建立機器使用者

使用 usermgmt_platform create 指令在身分可用區中建立機器使用者。如要查看支援的平台清單,請參閱使用 CLI

  1. 輸入下列指令:
    usermgmt_platform create

    系統隨即顯示身分識別區域清單:

    myzone1
    myzone2
  2. 在提示訊息中輸入可用區名稱:
    Enter a zone name: myzone1
  3. 輸入電腦使用者的使用者名稱:
    Create a Machine User
    Username: machineuser1@mycompany.com
  4. 輸入電腦使用者的密碼。在系統提示時重新輸入密碼。
    Password: password
    Re-enter password: password 

    已建立使用者。

    Created machine user machineuser1@mycompany.com

列出身分可用區中的所有機器使用者

使用 usermgmt_platform list 指令列出身分可用區中的所有機器使用者。如要查看支援的平台清單,請參閱使用 CLI

  1. 輸入下列指令:
    usermgmt_platform list
    系統會顯示身分可用區清單:
    myzone1
    myzone2
  2. 在提示訊息中輸入可用區名稱:
    Enter a zone name: myzone1

    顯示識別資訊可用區中的機器使用者清單會顯示:

    Machine users in the zone:
    machineuser1@mycompany.com
        

為身分區域中的電腦使用者重設密碼

使用 usermgmt_platformreset 指令重設身分區域中機器使用者的密碼。如要查看支援的平台清單,請參閱使用 CLI

  1. 輸入下列指令:
    usermgmt_platform reset

    系統隨即顯示身分識別區域清單:

    myzone1
    myzone2
  2. 在提示訊息中輸入可用區名稱:
    Enter a zone name: myzone1
  3. 輸入要重設密碼的電腦使用者使用者名稱:
    Reset User Password
    Enter the username for the machine user
    Username: machineuser1@mycompany.com
  4. 輸入電腦使用者的新密碼。在系統提示時重新輸入密碼。
    Enter the new password: password
    Re-enter password: password

    系統會重設密碼。

    Reset password for machine user machineuser1@mycompany.com

刪除身分可用區中的機器使用者

使用 usermgmt_platform delete 指令刪除身分可用區中的機器使用者。如要查看支援的平台清單,請參閱使用 CLI

  1. 輸入下列指令:
    usermgmt_platform delete
    系統會顯示身分可用區清單:
    myzone1
    myzone2
  2. 在提示訊息中輸入可用區名稱:
    Enter a zone name: myzone1
  3. 輸入要刪除的電腦使用者使用者名稱:
    Delete User
    Enter the username for the machine user
    Username: machineuser1@mycompany.com 

    已刪除電腦使用者。

    Deleted user machineuser1@mycompany.com

將必要角色指派給 Edge 機構中的機器使用者

透過 UI,將機器使用者新增至已啟用 SAML 的 Edge 機構,並指派必要的角色 (例如機構管理員),如「新增使用者」所述。

取得機器使用者的 OAuth2 權杖

您可以自動化權杖產生程序,並為具有 acurl(1)get_token(1) 公用程式的機器使用者處理權杖快取,詳情請參閱 機器使用者適用的 OAuth2SAML 可用區中的機器使用者

如何透過 curl 手動取得機器使用者的 OAuth2 權杖:

  1. 使用你偏好的網址編碼工具,對電腦使用者的使用者名稱和密碼進行編碼。

    警告:請使用「內部」網址編碼工具,確保機器使用者憑證不會遭到盜用。

  2. 呼叫 SAML 權杖端點,產生初始存取和更新權杖,如以下範例所示:
    curl -H "Content-Type: application/x-www-form-urlencoded;charset=utf-8" \
      -H "accept: application/json;charset=utf-8" \
      -H "Authorization: Basic ZWRnZWNsaTplZGdlY2xpc2VjcmV0" -X POST \
      https://zoneName.login.apigee.com/oauth/token -s \
      -d 'grant_type=password&username=machineusername&password=machineuserpassword'

    如要進行授權,請在 Authorization 標頭中傳遞保留的 OAuth2 用戶端憑證 ZWRnZWNsaTplZGdlY2xpc2VjcmV0。呼叫會列印存取權和重新整理權杖至 stdout

  3. 將存取權杖傳送至 Edge Management API 呼叫做為不記名標頭:
    curl -H "Authorization: Bearer ACCESS_TOKEN" \
      https://api.enterprise.apigee.com/v1/organizations/orgName
  4. 存取權杖過期後,您可以將更新權杖傳送至 SAML 權杖端點,藉此重新整理權杖,如以下範例所示:
    curl -H "Content-Type:application/x-www-form-urlencoded;charset=utf-8" \
      -H "Accept: application/json;charset=utf-8" \
      -H "Authorization: Basic ZWRnZWNsaTplZGdlY2xpc2VjcmV0" -X POST \
      https://zoneName.login.apigee.com/oauth/token \
      -d 'grant_type=refresh_token&refresh_token=REFRESH_TOKEN'

(1) Copyright 2023 Google LLC
usermgmtacurlget_token 工具是依據 Google Cloud Platform 使用規範的協議提供,依據包括《服務專屬條款》,列載於 https://cloud.google.com/terms/service-terms