在我們先前的文章中,使用 Terraform 管理 Jamf 設定:簡介,我們介紹了基礎設施即代碼的基本概念,以及如何開始使用 Terraform 和 Jamf Pro。如果您還沒有閱讀過,我們建議您從那裡開始,因為它涵蓋了 Terraform 的基礎、安裝、專案結構和狀態管理。
這次,我們將重點放在 Jamf Protect。我們已建立了專用的 Terraform provider,讓您可以將 Jamf Protect 產品設定作為代碼進行管理 - 計劃、操作設定、例外集等等。
為什麼需要 Jamf Protect 的專用 provider?
就像 Jamf Pro 一樣,Jamf Protect 本身是一個獨立的產品,也是 Jamf Platform 系列的一部分。它具有自己的 API 表面(基於 GraphQL)以及與 Jamf Pro 和其他 Jamf Platform 微服務(例如 Blueprints 和 Compliance Benchmarks)不同的資源集。端點安全計劃、遙測設定和可移除儲存策略等都存在於 Jamf Protect 中,需要自己的 provider 來正確管理。
如果您一直通過主控台管理 Jamf Protect 設定,您會知道在租戶間重新建立設定或回滾變更需要大量手動工作。使用此 provider,您的整個 Jamf Protect 設定可以存放在 Git 中、進行代碼審查,並在各個環境中一致地套用。
它涵蓋了什麼?
該 provider 提供 16 個資源和 18 個資料來源。以下是它涵蓋的一些主要領域:
威脅偵測與防禦
- 自訂防止清單 - 依據 Team ID、檔案雜湊、CDHash 或簽署 ID 的允許/封鎖清單。
- 例外集 - 分析和端點安全控制的例外。
端點設定
- 計劃 - 完整的端點安全設定,包括威脅防禦模式、通訊協定、報告間隔和裝置資訊收集。
- 操作設定 - 警示資料豐富化和報告端點(HTTP、Kafka、雲端)。
- 遙測 - 事件和日誌收集:安全事件、身份驗證、檔案完整性監控、效能指標等。
裝置控制
- 可移除儲存控制集 - USB 裝置策略,包括依據廠商 ID、產品 ID 或序號的覆蓋。
操作設定
- 資料轉發 - 將資料路由到 Amazon S3、Microsoft Sentinel 或雲端端點。
- 資料保留 - 租戶範圍的保留策略。
- 變更管理 - 設定凍結。
- 統一日誌篩選器 - 使用 NSPredicate 篩選器的 macOS 統一日誌收集。
存取控制
- 使用者、群組、角色和 API 客戶端 - 在代碼中管理存取和權限。
所有資源都支持完整的 CRUD 操作和 terraform import。
值得注意的是,jamfprotect_change_management、jamfprotect_data_forwarding 和 jamfprotect_data_retention 是單例資源 - 它們管理租戶範圍的設定,而不是個別可識別的物件。
開始使用
如果您按照介紹文章進行操作,您應該已經安裝了 Terraform 並了解了專案的結構。此處的步驟類似。
1. 在 Jamf Protect 中建立 API 客戶端
前往 Jamf Protect 主控台中的 Administration > API Clients,並建立具有您想要管理的資源所需權限的新 API 客戶端。
2. 設定 provider
將 provider 新增至您的 Terraform 設定:
terraform {
required_providers {
jamfprotect = {
source = "Jamf-Concepts/jamfprotect"
version = "~> 0.1.0"
}
}
}
provider "jamfprotect" {
url = "https://your-tenant.protect.jamfcloud.com"
client_id = var.jamfprotect_client_id
client_secret = var.jamfprotect_client_secret
}
如果您想保持 provider 區塊乾淨,也可以使用環境變數:
```bash
export JAMFPROTECT_URL="https://your-tenant.protect.jamfcloud.com"
export JAMFPROTECT_CLIENT_ID="your-client-id"
export JAMFPROTECT_CLIENT_SECRET="your-client-secret"
與 Jamf Pro provider 一樣,請確保這些認證不會進入您的 Git 儲存庫。使用 `terraform.tfvars` 檔案並將其新增至您的 `.gitignore`。
### 3. 定義您的資源
以下是定義要收集的資料和警示傳送位置的操作設定範例:
```hcl
resource "jamfprotect_action_configuration" "default" {
name = "Default Action Config"
alert_data_collection = {
binary_included_data_attributes = ["Sha256", "Signing Information"]
download_event_included_data_attributes = ["File"]
file_included_data_attributes = ["Sha256", "Signing Information"]
file_system_event_included_data_attributes = ["File", "Process"]
gatekeeper_event_included_data_attributes = ["Blocked Process"]
group_included_data_attributes = ["Name"]
keylog_register_event_included_data_attributes = ["Source Process"]
process_included_data_attributes = ["Args", "Signing Information", "Binary", "User", "Parent"]
process_event_included_data_attributes = ["Process"]
screenshot_event_included_data_attributes = ["File"]
synthetic_click_event_included_data_attributes = ["Process"]
user_included_data_attributes = ["Name"]
}
jamf_protect_cloud_endpoint = {
collect_alerts = ["low", "medium", "high"]
collect_logs = ["telemetry"]
}
}
一個可移除儲存策略,可以封鎖除核准的加密磁碟機以外的所有 USB 儲存裝置:
```hcl
resource "jamfprotect_removable_storage_control_set" "strict" {
name = "Strict USB Policy"
description = "Block all removable storage except encrypted devices."
default_permission = "Prevent"
default_local_notification_message = "Removable storage devices are not permitted."
override_encrypted_devices = [
{
permission = "Read and Write"
},
]
}
例外集以從威脅防禦中排除已知的 IT 管理工具:
```hcl
resource "jamfprotect_exception_set" "it_admin_tools" {
name = "IT Admin Tools"
description = "Exceptions for trusted IT administration software."
exceptions = [
{
type = "Override Endpoint Threat Prevention"
sub_type = "Process"
rules = [
{
rule_type = "Team ID"
value = "EQHXZ8M8AV"
},
]
},
]
}
您也可以使用資料來源來參考現有資源,而不是建立它們。在這裡,我們查詢租戶中已存在的 Jamf Managed Default Exceptions 集:
```hcl
data "jamfprotect_exception_sets" "all" {}
locals {
jamf_managed_default_exceptions = [
for es in data.jamfprotect_exception_sets.all.exception_sets :
es if es.name == "Jamf Managed Default Exceptions"
][0]
}
然後可以在計劃中將這些資源綁定在一起,該計劃是部署到端點的設定:
```hcl
resource "jamfprotect_plan" "endpoint_security" {
name = "Endpoint Security Plan"
description = "Standard endpoint security plan with threat prevention."
action_configuration = jamfprotect_action_configuration.default.id
removable_storage_control_set = jamfprotect_removable_storage_control_set.strict.id
exception_sets = [
jamfprotect_exception_set.it_admin_tools.id,
local.jamf_managed_default_exceptions.uuid,
]
endpoint_threat_prevention = "Block and report"
advanced_threat_controls = "Block and report"
tamper_prevention = "Block and report"
reporting_interval = 1440
compliance_baseline_reporting = true
auto_update = true
communications_protocol = "MQTT:443"
log_level = "Error"
report_architecture = true
report_hostname = true
report_serial_number = true
report_model_name = true
report_os_version = true
}
該計劃按 ID 參考其他資源,因此 Terraform 了解相依性圖並以正確的順序建立所有內容。
### 4. 套用您的設定
您在介紹中學到的相同命令也適用於此處:
```bash
terraform init # 初始化並下載 provider
terraform plan # 檢查將建立、變更或銷毀的內容
terraform apply # 套用變更(將要求您確認)
## 使現有租戶置於管理之下
如果您已經有一個具有現有設定的 Jamf Protect 租戶,您無需從頭開始。provider 中的每個資源都支持 `terraform import`,因此您可以將現有資源置於 Terraform 管理之下,而無需重新建立它們。
### 使用 Terraform query 發現現有資源
如果您執行的是 Terraform 1.14+,provider 支持[列表資源](https://developer.hashicorp.com/terraform/language/block/tfquery/list) - 一項新功能,可讓您直接從 Terraform 查詢現有基礎設施。這對於在匯入前發現租戶中已有的內容非常有用。
建立查詢檔案(例如 `discover.tfquery.hcl`):
```hcl
list "jamfprotect_plan" "all" {
provider = jamfprotect
include_resource = true
}
list "jamfprotect_exception_set" "custom" {
provider = jamfprotect
config {
name_prefix = "Custom"
}
}
然後執行:
```bash
terraform query -generate-config-out=generated.tf
Terraform 將查詢您的 Jamf Protect 租戶,傳回相符的資源及其 ID,並將資源區塊和匯入區塊都產生到 `generated.tf` 中。provider 中的每個列表資源都在 `config` 區塊中支持 `name_prefix` 篩選器(或用於使用者的 `email_prefix`),因此您可以將結果範圍縮小到您要尋找的內容。
除以下單例資源外,所有資源類型都支持列表查詢:
- `jamfprotect_change_management`
- `jamfprotect_data_forwarding`
- `jamfprotect_data_retention`
### 匯入資源
擁有所需的 ID 後,您可以使用匯入區塊將它們置於管理之下。使用 Terraform 1.5+:
```hcl
import {
to = jamfprotect_plan.endpoint_security
id = "your-plan-uuid"
}
執行 `terraform plan` 以產生匯入資源的設定,根據需要進行優化,從那時起它就作為代碼進行管理。如需有關大量匯入資源的詳細資訊,請參閱 HashiCorp 的[大量匯入文件](https://developer.hashicorp.com/terraform/language/v1.14.x/import/bulk)。
## 與其他 Jamf provider 搭配使用
此 provider 設計用於與 Jamf 生態系統中的其他 Terraform provider 搭配使用。您可以將它與以下項目一起使用:
- [Jamf Platform Provider](https://registry.terraform.io/providers/Jamf-Concepts/jamfplatform/latest) - 用於透過 Jamf Platform API 管理 Jamf Platform 中的資源(例如 Blueprints 和 Compliance Benchmarks)。
- [Deployment Theory Jamf Pro Provider](https://registry.terraform.io/providers/deploymenttheory/jamfpro/latest) - 用於透過其 Classic 和 Pro API 管理 Jamf Pro 產品中的資源。
在這些 provider 之間,您可以從單個 Terraform 專案中將完整的 Jamf 環境作為代碼進行管理。
## 背景
此 provider 最初由 [James Smith (@smithjw)](https://github.com/smithjw) 建立,他慷慨地將原始碼捐贈給了 [Jamf Concepts](https://github.com/Jamf-Concepts)。感謝 James 為這一切提供的基礎工作。
該 provider 是 MPL 2.0 許可下的開源軟體,建立在 HashiCorp 的 Terraform Plugin Framework(Protocol v6)之上。它包括整個單元和接受度測試,我們歡迎貢獻 - 錯誤報告、功能請求和拉取請求。
## 下一步
這是 v0.1.x 版本,我們正在積極收集反饋。如果您試用它,我們希望聽到什麼有效以及什麼無效。在 [GitHub 儲存庫](https://github.com/Jamf-Concepts/terraform-provider-jamfprotect)上開啟 issue 或在 Jamf Nation 上聯繫我們。
## 參考資料
- [GitHub 儲存庫](https://github.com/Jamf-Concepts/terraform-provider-jamfprotect)
- [Terraform Registry](https://registry.terraform.io/providers/Jamf-Concepts/jamfprotect)
- [使用 Terraform 管理 Jamf 設定:簡介](https://trusted.jamf.com/docs/managing-jamf-configuration-with-terraform-an-introduction)
- [Terraform 文件](https://developer.hashicorp.com/terraform)