設定 VM 內的細微存取權


在 Compute Engine 中,您可以使用 OS 登入功能授予使用者 VM 存取權,並限制使用者存取 sudo。如要控管哪些使用者可以在 VM 中查看或執行特定檔案和可執行檔,可以使用 OS 政策推出政策,自動集中管理 VM 叢集中的檔案。

本教學課程說明如何使用 OS Login 群組和 OS 政策,授予沒有 sudo 存取權的一群使用者,在多個 VM 上查看 auth.log 檔案 (/var/log/auth.log) 的權限。

在大多數預設設定中,auth.log 檔案權限會設為 640,且檔案屬於 adm 群組。如果使用者未獲派 roles/compute.osAdminLogin IAM 角色,就不會加入 adm 群組,也無法取得 sudo 存取權,因此無法讀取檔案。

本教學課程著重於管理特定檔案的使用者權限,但您可以修改工作流程,並用於設定 VM 上其他檔案或可執行檔的權限。

目標

在本教學課程中,您將瞭解如何:

  • 如何建立 OS 登入 Linux 群組
  • 如何使用 OS 政策指派作業,一次變更多個 VM 中檔案的群組權限

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project.

  3. 安裝或更新至最新版 Google Cloud CLI
  4. 請確認您是貴機構的 Cloud Identity 群組管理員。如果您不是群組管理員,請請貴機構的群組管理員為您建立 OS Login Linux 群組
  5. 啟用 VM 管理員

準備環境

您必須先建立一些 VM,才能授予 auth.log 檔案的存取權。

使用 gcloud compute instances bulk create 指令建立三個 VM:

gcloud compute instances bulk create \
    --name-pattern=tutorial-# \
    --zone=us-west1-a \
    --count=3 \
    --labels=vm=tutorial \
    --metadata=enable-oslogin=TRUE

畫面會顯示類似以下的訊息:

`Bulk create request finished with status message: [VM instances created: 3, failed: 0.]`

設定 auth.log 檔案的群組權限

如要授予使用者 auth.log 檔案的讀取權,但不想授予 roles/compute.osAdminLogin IAM 角色,請將檔案所屬的群組變更為使用者所屬的 Linux 群組。

建立 OS 登入 Linux 群組

您可以建立OS 登入 Linux 群組,新增啟用 OS 登入的每個 VM 上顯示的補充 Linux 群組。建立 OS 登入 Linux 群組 (而非本機補充 Linux 群組) 後,您就能以統一方式控管所有 VM 上的 Linux 群組。

如要建立 OS 登入 Linux 群組,請使用 gcloud beta identity groups create 指令

gcloud beta identity groups create logaccess@ORGANIZATION_DOMAIN \
    --organization=ORGANIZATION_DOMAIN \
    --posix-group=gid=12345,name=logaccess

ORGANIZATION_DOMAIN 替換為與貴機構相關聯的網域 (example.com)。

建立 OS 政策指派作業

您可以使用 OS 政策,集中管理 VM 設定並自動執行相關作業。您可以使用 OS 政策指派作業,將 OS 政策套用至 VM。建立 OS 政策指派作業,並搭配可變更 auth.log 檔案預設群組的 OS 政策,即可一次變更多個 VM 中的 auth.log 檔案預設群組,不必一次變更一個 VM。

如要建立 OS 政策指派作業,請按照下列步驟操作:

  1. 在工作站上開啟終端機。

  2. 執行下列指令,建立 OS 政策指派作業 .yaml 檔案:

    touch tutorial-os-policy-assignment.yaml
    
  3. 在文字編輯器中開啟 tutorial-os-policy-assignment.yaml 檔案,並新增下列規格:

    # OS policy assignment that sets the /var/log/auth.log group to logaccess.
    osPolicies:
     - id: log-access-policy
       mode: ENFORCEMENT
       resourceGroups:
           resources:
             - id: grant-log-access
               exec:
                 validate:
                   # Checks if the group is logaccess. If yes, exits  with code 100. If no,
                   # exits with code 101 and proceeds to the `enforce` step.
                   script:
                     if stat -c '%G' /var/log/auth.log | grep -q 'logaccess'; then exit 100; else exit 101; fi
                   interpreter: SHELL
                 enforce:
                   # Changes the group to logaccess and exits with code 100.
                   script:
                     chgrp logaccess /var/log/auth.log && exit 100
    instanceFilter:
     inclusionLabels:
       - labels:
           vm: tutorial
    rollout:
     disruptionBudget:
       fixed: 10
     minWaitDuration: 30s
    

將 OS 政策指派作業套用至 VM

如要將 OS 政策指派項目套用至 VM,請按照下列步驟操作:

  1. 使用 gcloud compute os-config os-policy-assignments create 指令套用 OS 政策:

    gcloud compute os-config os-policy-assignments create log-access-assignment \
       --location=us-west1-a \
       --file=log-access-os-policy-assignment.yaml \
       --async
    

    系統會根據 tutorial-os-policy-assignment.yaml 檔案中提及的推出規格,將 OS 政策指派作業推出至 VM。

  2. 確認 OS 政策指派作業已成功推出至 VM,且 OS 政策指派作業已使用 gcloud compute os-config os-policy-assignment-reports list 指令成功更新預設群組:

    gcloud compute os-config os-policy-assignment-reports list --location=us-west1-a
    

將使用者加入 OS 登入 Linux 群組

將使用者新增至 OS 登入 Linux 群組後,使用者就會在所有已啟用 OS 登入功能的 VM 中,沿用該群組的權限。在本教學課程中,您新增至 logaccess 群組的任何使用者,都能在沒有 roles/compute.osAdminLogin IAM 角色的情況下查看 auth.log 檔案。

請參閱 Cloud Identity 說明文件,瞭解如何將使用者新增至 logaccess 群組

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除個別資源

使用 gcloud compute instances delete 指令刪除您建立的 VM:

gcloud compute instances delete tutorial-1 tutorial-2 tutorial-3 \
    --zone=us-west1-a

後續步驟