透過直接存取存放區下載 Python 套件

將 Assured OSS 與 Security Command Center 整合後,Assured Open Source Software 套件會託管在您控管的專案中建立的 Artifact Registry 存放區。

本頁面說明如何連線至 Assured OSS 的 Artifact Registry 存放區,直接存取及下載 Python 套件。

本文僅適用於 Assured OSS 進階層級。如要瞭解免費方案,請參閱使用直接存放區存取權下載免費方案的 Python 套件

事前準備

  1. 將 Assured OSS 與 Security Command Center 整合

  2. 驗證連線:為要求的服務帳戶驗證與 Assured OSS 的連線。

  3. 安裝最新版 Google Cloud CLI

  4. 如果您先前已安裝 Google Cloud CLI,請執行下列指令,確認您使用的是最新版本:

    gcloud components update
    

設定驗證方法

您可以透過下列任一方法,向 Assured OSS Python 套件存放區進行驗證:

  • 使用金鑰圈進行驗證
  • 使用服務帳戶金鑰進行驗證

下列各節說明如何設定這些驗證方法。

使用金鑰圈進行驗證

如要使用 Python keyring 向 Artifact Registry 進行驗證,請參閱使用 keyring 進行驗證,以及憑證搜尋順序的相關資訊。建議您使用 Python 密鑰環進行驗證。

如要設定用於驗證的鍵環,請按照下列步驟操作:

  1. 安裝 keyring 程式庫:

    pip install keyring
    
  2. 安裝 Artifact Registry 後端:

    pip install keyrings.google-artifactregistry-auth
    
  3. 列出後端,確認安裝作業:

    keyring --list-backends
    

    清單應包含下列項目:

    • ChainerBackend(priority:10)
    • GooglePythonAuth(priority:9)
  4. 如要瞭解如何設定應用程式預設憑證,請參閱「設定驗證」。

    這個步驟可確保 Assured OSS 憑證輔助程式在連線至存放區時取得金鑰。

使用服務帳戶金鑰進行驗證

如果應用程式需要使用使用者名稱和密碼進行驗證,請使用服務帳戶金鑰進行驗證。

將套件存放區網址 https://us-python.pkg.dev/PROJECT_ID/assuredoss-python/simple 替換為網址 https://_json_key_base64:$BASE64_KEY@us-python.pkg.dev/PROJECT_ID/assuredoss-python/simple

PROJECT_ID 替換為您設定 Assured Open Source Software 時選取的專案 ID。

安裝套件

下列操作說明假設您使用 PyPI 做為存放區,下載依附元件。如果使用其他存放區,則必須採取不同步驟下載依附元件。

安裝 Assured OSS 中現有的套件

如要指定要下載的 Assured OSS Python 套件,請建立兩個 requirements.txt 檔案。範例檔案如下:

  • requirements-google.txt

    # Packages present in Google's Artifact Registry
    
    urllib3==1.26.11 --hash=sha256:1cffe1aa066363a75c856f261c8fce62d87f7c40ce0f46453ea12bf652b12a13
    jsonschema==4.13.0 --hash=sha256:29895bfe55b93b75552fbdd1e09aa0c82b7c1c9395d4f267e10c7d43cd31a74e
    
  • requirements-pypi.txt

    # Packages present in Google's Artifact Registry are mentioned here so that pip
    # downloads their dependencies from PyPI.
    
    urllib3==1.26.11
    jsonschema==4.13.0
    
    # Below this comment, add any package version which you need but is NOT
    # present in Google's Artifact Registry and therefore needs to be downloaded from
    # PyPI.
    

如要下載套件,請執行下列指令:

  • 如要下載 Assured OSS Artifact Registry 中的必要套件,請執行下列指令:

     pip install --require-hashes --requirement=requirements-google.txt \
         --index-url https://_json_key_base64:$BASE64_KEY@us-python.pkg.dev/PROJECT_ID/assuredoss-python repo/simple -v \
         --no-deps
    

    請考量下列事項:

    • --require-hashes 為選填欄位。如果包含雜湊值,則 requirements.txt 檔案會為所有套件和所有套件版本指定雜湊值。
    • -v 為選填欄位。如果指定,指令會提供更多輸出內容
  • 如要下載 Artifact Registry for Assured OSS 中沒有的必要套件,請執行下列指令:

     pip install --requirement=requirements-pypi.txt --index-url https://pypi.org/simple -v
    

    這個指令也會下載您使用先前指令下載的套件所缺少的依附元件。

列出 Assured OSS 中所有可用的 Python 套件

如要使用 API 取得 Artifact Registry 存放區中所有 Python 套件的清單,請參閱「列出 Assured OSS 中提供的所有 Python 套件」。

產生 requirements-google.txt 檔案

本節提供產生 requirements-google.txt 檔案的額外資訊,您需要這個檔案才能下載 Python 套件。如要下載雜湊並為環境產生 requirements-google.txt 檔案,可以使用下列任一選項:

  • 使用指令碼為所有構件產生單一需求檔案。

  • 為每個構件下載個別的需求檔案。

以下各節將進一步說明這些選項。

方法 1:使用指令碼為所有構件產生單一需求檔案

使用 generator.sh 指令碼,為 Linux 作業系統的 Assured OSS 產生所有 Python 套件的單一 requirements-google.txt 檔案 (連同雜湊)。套件必須符合 Python 版本、機器架構和作業系統等限制。接著,您可以刪除不需要的套件版本,並使用產生的檔案。

generator.sh 指令碼可透過下列兩種方式提供協助:

  • 這項工具會產生最新的 Python 套件版本清單,這些版本可搭配 Assured OSS 使用,並成功安裝在系統上。
  • 這會產生 requirements-google.txt 檔案和所有雜湊值。

必要指令碼及其 README.md 檔案位於 Cloud Storage 值區 (gs://cloud-aoss/utils/python-requirements-txt/v1.0),可使用 Google Cloud CLI 下載。

如要下載指令碼和 README.md 檔案,請按照下列步驟操作:

  1. 使用下列指令,透過服務帳戶進行驗證,存取 Cloud Storage bucket:

    gcloud auth activate-service-account --key-file KEY_FILE
    

    KEY_FILE 換成包含設定期間所提供服務帳戶憑證的檔案路徑。

  2. 使用下列指令將 generator.sh 下載至電腦:

    gcloud storage cp gs://cloud-aoss/utils/python-requirements-txt/v1.0/generator.sh PATH_TO_LOCAL_STORE --recursive
    

    PATH_TO_LOCAL_STORE 替換為要儲存下載檔案的本機路徑。

  3. 使用下列指令下載 README.md 檔案:

    gcloud storage cp gs://cloud-aoss/utils/python-requirements-txt/v1.0/README.md PATH_TO_LOCAL_STORE --recursive
    

    PATH_TO_LOCAL_STORE 替換為要儲存下載檔案的本機路徑。README.md 檔案包含如何使用指令碼的說明。

如要執行指令碼,請使用下列指令:

  • 如要產生 requirements-google.txt 檔案,請執行下列指令:

     chmod +x generator.sh
     ./generator.sh
    
  • 如要以 CSV 檔案取得套件資訊,請執行下列指令:

    chmod +x generator.sh
    ./generator.sh -i
    

方法 2:為每個必要構件下載 requirements.txt

您也可以為每個 Python 構件下載個別的 requirements.txt (內含雜湊) 檔案,然後將這些檔案合併為單一 requirements.txt 檔案。

構件雜湊值位於 Cloud Storage 值區中,可使用 gcloud CLI 下載。每個套件和版本的雜湊值位於 gs://cloud-aoss/python/PACKAGE_NAME/VERSION Cloud Storage bucket 位置。

如要下載 requirements.txt 檔案,請按照下列步驟操作:

  1. 使用下列指令,透過服務帳戶進行驗證,存取 Cloud Storage bucket:

    gcloud auth activate-service-account --key-file KEY_FILE
    

    KEY_FILE 替換為包含服務帳戶憑證的檔案路徑。

  2. 使用下列指令,將特定套件和版本的 requirements.txt 下載至本機電腦:

    gcloud storage cp gs://cloud-aoss/python/PACKAGE_NAME/VERSION PATH_TO_LOCAL_STORE --recursive
    

    更改下列內容:

    • PACKAGE_NAME:套件名稱
    • VERSION:套件版本
    • PATH_TO_LOCAL_STORE:要下載檔案的本機路徑

    指令範例:

    gcloud storage cp gs://cloud-aoss/python/bleach/5.0.0 /tmp/bleach --recursive

    requirements.txt 範例檔案:

    bleach==5.0.0 \
     --hash=sha256:6d286e765bfd3e309209cfa1d063e4d46afa966dea8cb97431c02b1e3067d812
    

    每個這類 requirements.txt 檔案的內容可以合併為單一 requirements-google.txt 檔案。

後續步驟