限制 VM 的安全殼層金鑰


本文說明如何從 VM 移除及封鎖 SSH 金鑰,防止使用者存取虛擬機器 (VM) 執行個體。

事前準備

  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

      1. After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      2. Set a default region and zone.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

          After installing the Google Cloud CLI, initialize it by running the following command:

          gcloud init

          If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

移除安全殼層金鑰

您可以從使用 OS 登入功能的 VM使用中繼資料型 SSH 金鑰的 VM 移除 SSH 金鑰。

從使用 OS 登入功能的 VM 移除 SSH 金鑰

使用 OS 登入功能的 VM 會接受與 Google 帳戶相關聯的安全殼層 (SSH) 金鑰。您可以使用 Google Cloud CLI 或 OS Login API,從使用者帳戶移除公開 SSH 金鑰。如果您是貴機構的管理員,可以使用 Directory API 從使用者帳戶中移除 SSH 金鑰。Compute Engine 會自動從 Google 帳戶中移除過期的金鑰。

gcloud

如要從帳戶中移除公開安全殼層金鑰,請按照下列步驟操作:

  1. 如果您不知道要移除哪個金鑰,請執行 gcloud compute os-login describe-profile 指令,查看與帳戶相關聯的所有金鑰:

    gcloud compute os-login describe-profile
    
  2. 複製要刪除金鑰的 fingerprint 值。

  3. 使用 gcloud compute os-login ssh-keys remove 指令從帳戶中移除金鑰:

    gcloud compute os-login ssh-keys remove --key=KEY
    

    KEY 替換成要移除的公開 SSH 金鑰,或是要移除的金鑰的 OS Login 指紋。

REST

如要從帳戶中移除公開安全殼層金鑰,請按照下列步驟操作:

  1. 如果不知道要移除哪個金鑰,請使用 users.getLoginProfile 方法查看與帳戶相關聯的所有金鑰:

    GET https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/loginProfile
    

    ACCOUNT_EMAIL 替換為與帳戶相關聯的電子郵件地址。

  2. 複製要刪除金鑰的 fingerprint 值。

  3. 使用 users.sshPublicKeys.delete 方法從帳戶中移除金鑰

    DELETE https://oslogin.googleapis.com/v1/users/ACCOUNT_EMAIL/sshPublicKeys/FINGERPRINT
    

    更改下列內容:

    • ACCOUNT_EMAIL:與帳戶相關聯的電子郵件地址
    • FINGERPRINT:要移除的金鑰的 SHA-256 指紋

從使用中繼資料型金鑰的 VM 移除 SSH 金鑰

您可以使用Google Cloud 控制台、gcloud CLI 或 Compute Engine API,從專案或執行個體中繼資料移除公開 SSH 金鑰。

從特定使用者的中繼資料中移除最後一個金鑰,或是特定使用者的中繼資料中最後一個金鑰過期後,Compute Engine 會刪除 VM 上的使用者 ~/.ssh/authorized_keys 檔案。

注意:如果您管理中繼資料中的 SSH 金鑰,可能會導致專案成員無法連線至 VM。此外,您也可能無意中授予使用者 (包括專案外部使用者) VM 的存取權。詳情請參閱手動管理金鑰的風險一節。

從專案中繼資料移除公開金鑰

從專案中繼資料移除公開 SSH 金鑰,即可移除專案中所有 VM 的存取權。

使用 gcloud CLI 和 Compute Engine API 從中繼資料移除金鑰時,您必須先擷取現有金鑰清單,編輯金鑰清單以移除不需要的金鑰,然後使用要保留的金鑰清單覆寫舊金鑰,如下一節所述。

主控台

如要使用Google Cloud 控制台從專案中繼資料移除公開安全殼層金鑰,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Metadata」頁面。

    前往「中繼資料」

  2. 按一下「SSH keys」(SSH 金鑰) 分頁標籤。

  3. 按一下頁面頂端的「編輯」圖示

  4. 找出要移除的安全殼層金鑰,然後按一下金鑰旁邊的刪除按鈕。

    針對要移除的每個 SSH 金鑰重複上述步驟。

  5. 按一下 [儲存]

gcloud

如要使用 gcloud CLI 從專案中繼資料移除公開 SSH 金鑰,請按照下列步驟操作:

  1. 執行 gcloud compute project-info describe 指令,取得專案的中繼資料:

    gcloud compute project-info describe
    

    輸出結果會與下列內容相似:

    ...
    metadata:
      ...
      - key: ssh-keys
        value: |-
          cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF
          baklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}
    ...
    
  2. 複製 ssh-keys 中繼資料值。

  3. 在工作站上建立並開啟新的文字檔案。

  4. 在檔案中貼上剛才複製的安全殼層金鑰清單,然後刪除要從專案中繼資料中移除的金鑰。

  5. 儲存並關閉檔案。

  6. 執行 gcloud compute project-info add-metadata 指令,設定全專案 ssh-keys 值:

    gcloud compute project-info add-metadata --metadata-from-file=ssh-keys=KEY_FILE
    

    請將 KEY_FILE 替換為下列其中一個值:

    • 如果您專案已有安全殼層金鑰,請輸入您在上一個步驟中建立的檔案路徑
    • 如果專案沒有現有的 SSH 金鑰,則為新公開 SSH 金鑰檔案的路徑

REST

如要使用 Compute Engine API 從專案中繼資料移除公開安全殼層金鑰,請執行下列步驟:

  1. 使用 projects.get 方法從中繼資料取得 fingerprintssh-keys 值。

    GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
    

    PROJECT_ID 替換為您的專案 ID。

    回應類似以下內容:

    ...
    "fingerprint": "utgYE_XWtE8=",
    "items": [
     {
      "key": "ssh-keys",
      "value": "cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF\nbaklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}"
     }
    ]
    ...
    
  2. 複製 SSH 金鑰值清單,然後刪除要移除的金鑰。

  3. 使用 projects.setCommonInstanceMetadata 移除 SSH 金鑰。

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/setCommonInstanceMetadata
    
    {
    "items": [
     {
      "key": "ssh-keys",
      "value": "EXISTING_SSH_KEYS"
     }
    ]
    "fingerprint": "FINGERPRINT"
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • EXISTING_SSH_KEYS:要保留的安全殼層金鑰清單
    • FINGERPRINTprojects.get 要求回應中的 fingerprint

從執行個體中繼資料移除公開 SSH 金鑰

從執行個體中繼資料移除公開安全殼層金鑰,即可移除單一 VM 的存取權。

使用 gcloud CLI 和 Compute Engine API 從中繼資料移除金鑰時,您必須先擷取現有金鑰清單,編輯金鑰清單以移除不需要的金鑰,然後使用要保留的金鑰清單覆寫舊金鑰,如下一節所述。

主控台

如要使用Google Cloud console 從執行個體中繼資料移除公開安全殼層金鑰,請執行下列步驟:

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 按一下要移除金鑰的 VM 名稱。

  3. 按一下 [編輯]

  4. 在「SSH Keys」(安全殼層金鑰) 專區中,點選「Show and edit」(顯示與編輯)。該區段會展開並顯示所有執行個體層級公開安全殼層金鑰。

  5. 在要移除的 SSH 金鑰旁,按一下「刪除」按鈕

    針對要移除的每個 SSH 金鑰重複上述步驟。

  6. 按一下 [儲存]

gcloud

如要使用 gcloud CLI 從執行個體中繼資料移除公開 SSH 金鑰,請執行下列步驟:

  1. 執行 gcloud compute instances describe 指令,取得 VM 的中繼資料:

    gcloud compute instances describe VM_NAME
    

    VM_NAME 替換為要新增或移除公開 SSH 金鑰的 VM 名稱。

    輸出結果會與下列內容相似:

    ...
    metadata:
    ...
    - key: ssh-keys
     value: |-
       cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF
       baklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}
    ...
    
  2. 複製 ssh-keys 中繼資料值。

  3. 在本機工作站上建立並開啟一個新的文字檔案。

  4. 在檔案中貼上剛才複製的安全殼層金鑰清單,然後移除要刪除的金鑰。

  5. 儲存並關閉檔案。

  6. 執行 gcloud compute project-info add-metadata 指令,設定全專案 ssh-keys 值:

    gcloud compute instances add-metadata VM_NAME --metadata-from-file ssh-keys=KEY_FILE
    

    更改下列內容:

    • VM_NAME:要移除安全殼層金鑰的 VM
    • KEY_FILE:包含所有專案安全殼層金鑰清單的檔案路徑

REST

如要使用 Compute Engine API 從執行個體中繼資料移除公開安全殼層金鑰,請按照下列步驟操作:

  1. 使用 instances.get 方法從中繼資料取得 fingerprintssh-keys 值。

    GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE:您要新增 SSH 金鑰的 VM 所在可用區
    • VM_NAME:要新增安全殼層金鑰的 VM

    回應類似以下內容:

    ...
    "fingerprint": "utgYE_XWtE8=",
    "items": [
    {
     "key": "ssh-keys",
     "value": "cloudysanfrancisco:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF\nbaklavainthebalkans:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQDx3FNVC8 google-ssh {"userName":"baklavainthebalkans","expireOn":"2021-06-14T16:59:03+0000"}"
    }
    ]
    ...
    
  2. 複製 SSH 金鑰值清單,然後刪除要移除的金鑰。

  3. 使用 instances.setMetadata 移除 SSH 金鑰。

    
    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata
    
    {
    "items": [
     {
      "key": "ssh-keys",
      "value": "EXISTING_SSH_KEYS
     }
    ]
    "fingerprint": "FINGERPRINT"
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • EXISTING_SSH_KEYSprojects.get 要求回應中的 ssh-keys 鍵值
    • FINGERPRINTinstances.get 要求回應中的 fingerprint

封鎖使用中繼資料安全殼層金鑰的 VM 專案安全殼層金鑰

您可以封鎖 VM 的專案 SSH 金鑰,防止 VM 接受儲存在專案中繼資料中的 SSH 金鑰。您可以在建立 VM 時建立 VM 後,封鎖 VM 的專案安全殼層金鑰。

在建立 VM 時,從 VM 封鎖專案安全殼層金鑰

您可以在建立 VM 時,使用Google Cloud 控制台、gcloud CLI 或 Compute Engine API,禁止 VM 使用專案 SSH 金鑰。

主控台

如要使用 Google Cloud 控制台建立執行個體,並禁止該執行個體接受儲存在專案中繼資料中的 SSH 金鑰,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「建立執行個體」

  2. 如要封鎖專案安全殼層金鑰,請按照下列步驟操作:

    1. 按一下導覽選單中的「Security」(安全性)

    2. 展開「管理存取權」部分。

    3. 如要停用 OS 登入,請清除「透過 IAM 權限控管 VM 存取權」核取方塊。

    4. 勾選「封鎖全專案安全殼層金鑰」核取方塊。

  3. 視需要指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。

  4. 如要建立並啟動執行個體,請按一下「建立」

gcloud

如要使用 gcloud CLI 建立 VM,並禁止該 VM 接受儲存在專案中繼資料中的 SSH 金鑰,請使用 gcloud compute instances create 指令

gcloud compute instances create VM_NAME \
    --metadata block-project-ssh-keys=TRUE

VM_NAME 替換為新 VM 的名稱。

REST

如要建立 VM,並使用 Compute Engine 封鎖 VM 接受專案中繼資料中儲存的 SSH 金鑰,請建構對 instances.insert 方法POST 要求:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances

更改下列內容:

  • PROJECT_ID:專案 ID
  • ZONE:VM 所在可用區

在要求主體中,於 items 屬性中提供使用者名稱和公開安全殼層金鑰:

...
{
 "items": [
    {
     "key": "block-project-ssh-keys",
     "value": TRUE
    }
   ]
}
...

在建立 VM 後封鎖 VM 的專案安全殼層金鑰

您可以在建立 VM 後,使用Google Cloud 控制台、gcloud CLI 或 Compute Engine API,封鎖 VM 的專案 SSH 金鑰。

主控台

如要使用 Google Cloud 控制台,禁止 VM 接受專案中繼資料儲存的安全殼層金鑰連線,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 按一下要封鎖專案 SSH 金鑰的 VM 名稱。

  3. 按一下 [編輯]

  4. 在「SSH Keys」(安全殼層金鑰) 下方,選取「Block project-wide SSH keys」(封鎖全專案安全殼層金鑰) 核取方塊。

  5. 安全殼層金鑰的連線設定編輯完成後,請按一下 [Save] (儲存)

gcloud

如要使用 gcloud CLI,禁止 VM 接受專案中繼資料中儲存的 SSH 金鑰連線,請執行下列操作:

執行 gcloud compute instances add-metadata 指令

gcloud compute instances add-metadata VM_NAME --metadata block-project-ssh-keys=TRUE

VM_NAME 替換為要封鎖全專案公開 SSH 金鑰的 VM 名稱。

REST

如要使用 Compute Engine API,禁止 VM 接受專案中繼資料中儲存的 SSH 金鑰連線,請按照下列步驟操作:

  1. 使用 instances.get 方法從中繼資料取得 fingerprint

    GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE:您要新增 SSH 金鑰的 VM 所在可用區
    • VM_NAME:要新增安全殼層金鑰的 VM

    回應類似以下內容:

    ...
    "fingerprint": "utgYE_XWtE8="
    ...
    
  2. 使用 instances.setMetadata 方法block-project-ssh-keys 設為 TRUE

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata
    
    {
    "items": [
     {
      "key": "block-project-ssh-keys",
      "value": TRUE
     }
    ]
    "fingerprint": "FINGERPRINT"
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE 是執行個體所在的區域
    • INSTANCE_NAME 是要封鎖全專案金鑰的執行個體。
    • FINGERPRINTinstances.get 要求的相關回應中 fingerprint 的值。

後續步驟