HashiCorp Vault

Vault 是以身分為基礎的 Secret 和加密管理系統。這項整合功能會收集 Vault 的稽核記錄。這項整合功能也會收集權杖、記憶體和儲存空間指標。

如要進一步瞭解 Vault,請參閱 Hashicorp Vault 說明文件。

必要條件

如要收集 Vault 遙測資料,請安裝 Ops Agent:

  • 如要查看指標,請安裝 2.18.2 以上版本。
  • 如要查看記錄,請安裝 2.18.1 以上版本。

這項整合功能支援 Vault 1.6 以上版本。

設定 Vault 執行個體

如要從 Vault 執行個體收集遙測資料,您必須在 HCL 或 JSON Vault 設定檔中,將 prometheus_retention_time 欄位設為非零值。

Full configuration options can be found at https://www.vaultproject.io/docs/configuration
telemetry {
  prometheus_retention_time = "10m"
  disable_hostname = false
}

此外,您必須以超級使用者身分啟用稽核記錄收集功能,並建立 prometheus-metrics ACL 政策。根權杖用於新增政策,該政策具有 /sys/metrics 端點的讀取功能。這項政策用於建立 Vault 權杖,並授予足夠權限來收集 Vault 指標。

如果是首次初始化 Vault,可以使用下列指令碼產生根權杖。否則,請參閱「使用解除密封金鑰產生根權杖」一文,瞭解如何產生根權杖。

export VAULT_ADDR=http://localhost:8200
# Create simple Vault initialization with 1 key share and a key threshold of 1.
vault operator init -key-shares=1 -key-threshold=1 | head -n3 | cat > .vault-init
VAULT_KEY=$(grep 'Unseal Key 1'  .vault-init | awk '{print $NF}')
VAULT_TOKEN=$(grep 'Initial Root Token:' .vault-init | awk '{print $NF}')
export VAULT_TOKEN
vault operator unseal $VAULT_KEY

# Enable audit logs.
vault audit enable file file_path=/var/log/vault_audit.log

# Create Prometheus ACL policy to access metrics endpoint.
vault policy write prometheus-metrics - << EOF
path "/sys/metrics" {
  capabilities = ["read"]
}
EOF

# Create an example token with the prometheus-metrics policy to access Vault metrics.
# This token is used as `$VAULT_TOKEN` in your Ops Agent configuration for Vault.
vault token create -field=token -policy prometheus-metrics > prometheus-token

設定 Vault 的 Ops Agent

按照「設定 Ops Agent」指南,新增必要元素,從 Vault 執行個體收集遙測資料,然後重新啟動代理程式。

範例設定

下列指令會建立設定,收集及擷取 Vault 的遙測資料:

# Configures Ops Agent to collect telemetry from the app. You must restart the agent for the configuration to take effect.

set -e

# Check if the file exists
if [ ! -f /etc/google-cloud-ops-agent/config.yaml ]; then
  # Create the file if it doesn't exist.
  sudo mkdir -p /etc/google-cloud-ops-agent
  sudo touch /etc/google-cloud-ops-agent/config.yaml
fi

# Create a back up of the existing file so existing configurations are not lost.
sudo cp /etc/google-cloud-ops-agent/config.yaml /etc/google-cloud-ops-agent/config.yaml.bak

# Create a Vault token that has read capabilities to /sys/metrics policy.
# For more information see: https://developer.hashicorp.com/vault/tutorials/monitoring/monitor-telemetry-grafana-prometheus?in=vault%2Fmonitoring#define-prometheus-acl-policy
VAULT_TOKEN=$(cat prometheus-token)


sudo tee /etc/google-cloud-ops-agent/config.yaml > /dev/null << EOF
metrics:
  receivers:
    vault:
      type: vault
      token: $VAULT_TOKEN
      endpoint: 127.0.0.1:8200
  service:
    pipelines:
      vault:
        receivers:
          - vault
logging:
  receivers:
    vault_audit:
      type: vault_audit
      include_paths: [/var/log/vault_audit.log]
  service:
    pipelines:
      vault:
        receivers:
          - vault_audit
EOF

如要讓這些變更生效,請重新啟動 Ops Agent:

Linux

  1. 如要重新啟動代理程式,請在執行個體上執行下列指令:
    sudo systemctl restart google-cloud-ops-agent
    
  2. 如要確定代理程式已重新啟動,請執行下列指令,並驗證「指標代理程式」和「Logging 代理程式」元件是否已啟動:
    sudo systemctl status "google-cloud-ops-agent*"
    

Windows

  1. 使用 RDP 或類似工具連線至執行個體,然後登入 Windows。
  2. 在 PowerShell 圖示上按一下滑鼠右鍵,然後選取「Run as Administrator」(以系統管理員身分執行),開啟具有管理員權限的 PowerShell 終端機。
  3. 如要重新啟動代理程式,請執行下列 PowerShell 指令:
    Restart-Service google-cloud-ops-agent -Force
    
  4. 如要確定代理程式已重新啟動,請執行下列指令,並驗證「指標代理程式」和「Logging 代理程式」元件是否已啟動:
    Get-Service google-cloud-ops-agent*
    

設定記錄收集

如要從 Vault 擷取記錄,您必須為 Vault 產生的記錄建立接收器,然後為新的接收器建立管道。

如要為 vault_audit 記錄檔設定接收器,請指定下列欄位:

欄位 預設 說明
exclude_paths 檔案系統路徑模式清單,用於從 include_paths 比對的集合中排除。
include_paths 檔案系統路徑清單,用於讀取每個檔案的結尾。路徑中可使用萬用字元 (*)。
record_log_file_path false 如果設為 true,則取得記錄項目的特定檔案路徑,會顯示在輸出記錄項目中,做為 agent.googleapis.com/log_file_path 標籤的值。使用萬用字元時,系統只會記錄取得記錄的檔案路徑。
type 這個值必須是 vault_audit。
wildcard_refresh_interval 60s include_paths 中萬用字元檔案路徑的重新整理間隔。以時間長度表示,例如 30s 或 2m。如果記錄檔的輪替速度快於預設間隔,這項屬性在記錄檔處理量較高時可能很有用。

記錄內容

logName 衍生自設定中指定的接收器 ID。LogEntry 內的詳細欄位如下。

vault_audit 記錄在 LogEntry 中包含下列欄位:

欄位 類型 說明
jsonPayload.auth struct
jsonPayload.auth.accessor 字串 這是用戶端權杖存取子的 HMAC。
jsonPayload.auth.client_token 字串 這是用戶端詞元 ID 的 HMAC。
jsonPayload.auth.display_name 字串 這是由驗證方法角色設定的顯示名稱,或是在建立密鑰時明確設定的顯示名稱。
jsonPayload.auth.entity_id 字串 這是權杖實體 ID。
jsonPayload.auth.metadata 物件 其中包含與 client_token 相關聯的中繼資料鍵/值組合清單。
jsonPayload.auth.policies 物件 其中會列出與 client_token 相關聯的政策。
jsonPayload.auth.token_type 字串
jsonPayload.error 字串 如果要求發生錯誤,錯誤訊息會包含在這個欄位的值中。
jsonPayload.request struct
jsonPayload.request.client_token 字串 這是用戶端詞元 ID 的 HMAC。
jsonPayload.request.client_token_accessor 字串 這是用戶端權杖存取子的 HMAC。
jsonPayload.request.data 物件 資料物件會包含鍵/值組合形式的密鑰資料。
jsonPayload.request.headers 物件 用戶端在要求中指定的其他 HTTP 標頭。
jsonPayload.request.id 字串 這是專屬要求 ID。
jsonPayload.request.namespace.id 字串
jsonPayload.request.operation 字串 這是與路徑功能相對應的操作類型,應為下列其中一項:create、read、update、delete 或 list。
jsonPayload.request.path 字串 要求的 Vault 作業路徑。
jsonPayload.request.policy_override 布林值 如果要求覆寫軟性強制政策,則為 true。
jsonPayload.request.remote_address 字串 傳送要求的用戶端 IP 位址。
jsonPayload.request.wrap_ttl 字串 如果權杖已包裝,系統會以數字字串的形式顯示設定的包裝存留時間值。
jsonPayload.response struct
jsonPayload.response.data.accessor 字串 這是用戶端權杖存取子的 HMAC。
jsonPayload.response.data.creation_time 字串 權杖建立時間的時間戳記,採用 RFC 3339 格式。
jsonPayload.response.data.creation_ttl 字串 權杖建立存留時間 (秒)。
jsonPayload.response.data.display_name 字串 這是由驗證方法角色設定的顯示名稱,或是在建立密鑰時明確設定的顯示名稱。
jsonPayload.response.data.entity_id 字串 這是權杖實體 ID。
jsonPayload.response.data.expire_time 字串 RFC 3339 格式的時間戳記,代表這個權杖的到期時間。
jsonPayload.response.data.explicit_max_ttl 字串 明確的權杖存留時間上限值 (以秒為單位,如未設定則為「0」)。
jsonPayload.response.data.id 字串 這是專屬的回應 ID。
jsonPayload.response.data.issue_time 字串 RFC 3339 格式的時間戳記。
jsonPayload.response.data.num_uses 數字 如果權杖有使用次數限制,這裡會顯示該值。
jsonPayload.response.data.orphan 布林值 布林值,代表權杖是否為孤立權杖。
jsonPayload.response.data.path 字串 要求的 Vault 作業路徑。
jsonPayload.response.data.policies 物件 其中會列出與 client_token 相關聯的政策。
jsonPayload.response.data.renewable 布林值 布林值,代表權杖是否為孤立權杖。
jsonPayload.type 字串 稽核記錄的類型。
severity 字串 (LogSeverity) 記錄項目層級 (已翻譯)。

設定指標收集

如要從 Vault 擷取指標,您必須為 Vault 產生的指標建立接收器,然後為新的接收器建立管道。

這個接收器不支援在設定中使用多個執行個體,例如監控多個端點。所有這類執行個體都會寫入相同的時間序列,Cloud Monitoring 無法區分。

如要為 vault 指標設定接收器,請指定下列欄位:

欄位 預設 說明
ca_file CA 憑證的路徑。用戶端會驗證伺服器憑證。如果留空,接收端會使用系統根 CA。
cert_file TLS 憑證的路徑,用於需要 mTLS 的連線。
collection_interval 60s 時間長度值,例如 30s 或 5m。
endpoint localhost:8200 Vault 使用的「hostname:port」。
insecure true 設定是否要使用安全的 TLS 連線。如果設為 false,則會啟用 TLS。
insecure_skip_verify false 設定是否略過憑證驗證。如果 insecure 設為 true,則不會使用 insecure_skip_verify 值。
key_file TLS 金鑰的路徑,用於需要 mTLS 的連線。
metrics_path /v1/sys/metrics 指標收集路徑。
token localhost:8200 用於驗證的權杖。
type 這個值必須是 vault。

監控的內容

下表列出 Ops Agent 從 Vault 執行個體收集的指標。

指標類型
種類、類型
受監控資源
標籤
workload.googleapis.com/vault.audit.request.failed
CUMULATIVE, INT64
gce_instance
 
workload.googleapis.com/vault.audit.response.failed
CUMULATIVE, INT64
gce_instance
 
workload.googleapis.com/vault.core.leader.duration
GAUGE, DOUBLE
gce_instance
 
workload.googleapis.com/vault.core.request.count
GAUGE, INT64
gce_instance
cluster
workload.googleapis.com/vault.memory.usage
GAUGE, DOUBLE
gce_instance
 
workload.googleapis.com/vault.storage.operation.delete.count
CUMULATIVE, INT64
gce_instance
storage
workload.googleapis.com/vault.storage.operation.delete.time
CUMULATIVE, DOUBLE
gce_instance
storage
workload.googleapis.com/vault.storage.operation.get.count
CUMULATIVE, INT64
gce_instance
storage
workload.googleapis.com/vault.storage.operation.get.time
CUMULATIVE, DOUBLE
gce_instance
storage
workload.googleapis.com/vault.storage.operation.list.count
CUMULATIVE, INT64
gce_instance
storage
workload.googleapis.com/vault.storage.operation.list.time
CUMULATIVE, DOUBLE
gce_instance
storage
workload.googleapis.com/vault.storage.operation.put.count
CUMULATIVE, INT64
gce_instance
storage
workload.googleapis.com/vault.storage.operation.put.time
CUMULATIVE, DOUBLE
gce_instance
storage
workload.googleapis.com/vault.token.count
GAUGE, INT64
gce_instance
cluster
namespace
workload.googleapis.com/vault.token.lease.count
GAUGE, INT64
gce_instance
 
workload.googleapis.com/vault.token.renew.time
GAUGE, INT64
gce_instance
 
workload.googleapis.com/vault.token.revoke.time
GAUGE, INT64
gce_instance
 

驗證設定

本節說明如何驗證 Vault 接收器是否設定正確。Ops Agent 可能需要一兩分鐘才會開始收集遙測資料。

如要確認 Vault 記錄檔是否已傳送至 Cloud Logging,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的 「Logs Explorer」頁面:

    前往「Logs Explorer」(記錄檔探索工具)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。

  2. 在編輯器中輸入下列查詢,然後點選「執行查詢」:
    resource.type="gce_instance"
    log_id("vault_audit")
    

如要確認 Vault 指標是否已傳送至 Cloud Monitoring,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Metrics Explorer」頁面:

    前往「Metrics Explorer」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 在查詢建立工具窗格的工具列中,選取名為 「PromQL」PromQL的按鈕。
  3. 在編輯器中輸入下列查詢,然後點選「執行查詢」:
    {"workload.googleapis.com/vault.memory.usage", monitored_resource="gce_instance"}
    

查看資訊主頁

如要查看 Vault 指標,必須先設定圖表或資訊主頁。Vault 整合功能包含一或多個資訊主頁。 設定整合作業且 Ops Agent 開始收集指標資料後,系統會自動安裝所有資訊主頁。

您也可以查看資訊主頁的靜態預覽畫面,不必安裝整合項目。

如要查看已安裝的資訊主頁,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Dashboards」(資訊主頁) 頁面:

    前往「Dashboards」(資訊主頁)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 選取「Dashboard List」(資訊主頁清單) 分頁標籤,然後選擇「Integrations」(整合) 類別。
  3. 按一下要查看的資訊主頁名稱。

如果您已設定整合,但尚未安裝資訊主頁,請確認 Ops Agent 是否正在執行。如果資訊主頁中的圖表沒有指標資料,資訊主頁就會安裝失敗。Ops Agent 開始收集指標後,系統就會為您安裝資訊主頁。

如要查看資訊主頁的靜態預覽畫面,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Integrations」(整合) 頁面:

    前往「Integrations」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 按一下「Compute Engine」部署平台篩選器。
  3. 找出 Vault 項目,然後按一下「查看詳細資料」。
  4. 選取「資訊主頁」分頁標籤,即可查看靜態預覽畫面。如果已安裝資訊主頁,請點選「View dashboard」(查看資訊主頁) 前往該頁面。

如要進一步瞭解 Cloud Monitoring 中的資訊主頁,請參閱「資訊主頁和圖表」。

如要進一步瞭解如何使用「Integrations」(整合) 頁面,請參閱「管理整合」。

安裝警告政策

警告政策會指示 Cloud Monitoring 在發生指定情況時通知您。Vault 整合包含一或多項警告政策,可供您使用。您可以在 Monitoring 的「Integrations」(整合) 頁面查看及安裝這些警告政策。

如要查看可用警告政策的說明並安裝,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Integrations」(整合) 頁面:

    前往「Integrations」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 找出 保管箱 項目,然後按一下「查看詳細資料」。
  3. 選取「快訊」分頁標籤。這個分頁會說明可用的警告政策,並提供安裝介面。
  4. 安裝警告政策。警告政策需要知道警告觸發時應將通知傳送至何處,因此您必須提供相關資訊,才能安裝。如要安裝警告政策,請按照下列步驟操作:
    1. 從可用警告政策清單中,選取要安裝的政策。
    2. 在「Configure notifications」(設定通知) 部分,選取一或多個通知管道。您可以選擇停用通知管道,但如果這麼做,警告政策就不會傳送通知。您可以在 Monitoring 中查看狀態,但不會收到通知。

      如要進一步瞭解通知管道,請參閱「管理通知管道」。

    3. 點選「建立政策」。

如要進一步瞭解 Cloud Monitoring 的警告政策,請參閱「警告簡介」。

如要進一步瞭解如何使用「Integrations」(整合) 頁面,請參閱「管理整合」。

後續步驟

如要瞭解如何使用 Ansible 安裝 Ops Agent、設定第三方應用程式,以及安裝範例資訊主頁,請觀看安裝 Ops Agent 以排解第三方應用程式的問題影片。