針對渲染輸出的原則:一條內部規則,適用每個目標,兩種引擎
一條內部規則附加在 render-verdict 角色,判定建置為每個目標寫出的檔案,並拒絕在每個目標上刻意放入的違規。這條規則有兩個子句:
每個容器映像檔都指定固定版本——不是
latest的標籤,就是摘要。每個資料庫執行個體都以加密方式儲存其資料。
這個工作區透過 Helm、Docker Compose、Terraform、CloudFormation 和 Azure Resource Manager(arm 提供者)為 Kubernetes 建置一個資料庫。render-verdict 角色會在每個目標都渲染完畢之後,判定建置所寫出的檔案,而規則讀取的是各目標自己的輸出形狀:Kubernetes 物件、Compose 檔案與 Azure Resource Manager 範本中的映像檔,以及 Terraform 檔案與 CloudFormation 範本中的 RDS 執行個體。每個目標都有一個通過的 Release,以及一個刻意放入違規的 Release,規則會拒絕後者。
這條規則寫了兩次,分別對應編譯進 CLI 的兩種原則引擎——Rego 與 CEL——而這兩份在此的每次建置上結果一致。Releases 旁的一支指令碼會在兩種引擎下建置每個 Release,並檢查每個結果。
aggrega build你的原則
接入編譯器
通過
已寫出的檔案
可供檢閱與部署
失敗
建置已中止
原則可以中止建置並說明該修正什麼。它絕不會為了讓檔案通過而修改你的檔案。
它建置什麼
| 實例 | 目錄 Component | 它在這裡是什麼 |
|---|---|---|
db | core/datastore/postgres | 訂單資料庫 |
report | core/compute/task | 讀取 db 的報表工作,以手動啟動:它帶有 core/manual-trigger,因此 Release 部署時不會啟動任何執行 |
Azure Resource Manager 的建置有自己的 Aggregate aggregate-arm.yaml,其中是同一個資料庫,以及一個讀取它、型別為 core/compute/worker 的 report。arm 提供者能實現 worker,但不能實現上述工作所帶有的 core/manual-trigger,因此共用的 Aggregate 無法在那裡建置。該檔案中的資料庫名為 orders-db,因為彈性伺服器採用其元件的名稱,而伺服器名稱至少要有三個字元。
report 透過它的 database 連線讀取資料庫的位址、連接埠與名稱,分別為 DB_HOST、DB_PORT 和 DB_NAME。除了 Azure Resource Manager 的建置之外,每次建置的 Aggregate 都是同一個檔案;每個 Release 都指名選定目標的 Manifest:
| Manifest | db | report |
|---|---|---|
manifest-helm.yaml | helm/postgres,一個 StatefulSet | helm/task,一個保持暫停的 CronJob |
manifest-compose.yaml | compose/postgres,一個服務 | compose/task,一個位於 profile 之後的服務 |
manifest-terraform.yaml | terraform/postgres-rds,一個 RDS 執行個體 | terraform/task-ecs,一個 Amazon ECS 工作定義 |
manifest-cloudformation.yaml | cloudformation/postgres-rds,一個 RDS 執行個體 | cloudformation/task-ecs,一個 Amazon ECS 工作定義 |
manifest-arm.yaml | arm/postgres-flexible-server,一個 Azure Database for PostgreSQL 彈性伺服器 | arm/worker-container-apps,一個 Azure Container Apps 應用程式 |
Kubernetes、Compose、Terraform 和 CloudFormation 的 Recipes 只靠目錄就能渲染:Kubernetes 資料庫是合成出來的,而不是以 chart 的形式取得,Terraform 資料庫的主體由其 Recipe 撰寫,Terraform 工作的模組則放在它的 Recipe 旁邊。因此這些建置可離線執行。arm Recipes 包裝了 Azure Verified Modules 範本,建置會在每個 Recipe 所固定的提交處取得這些範本,所以該目標的建置需要網路。
Releases
| Release | 目標 | 規則發現什麼 | 建置 |
|---|---|---|---|
release-helm.yaml | Kubernetes | report 執行 registry.example.com/orders-report:1.4.2 | 完成 |
release-helm-unpinned.yaml | Kubernetes | report 以標籤 latest 執行同一個映像檔 | 中止 |
release-compose.yaml | Docker Compose | report 執行 registry.example.com/orders-report:1.4.2 | 完成 |
release-compose-unpinned.yaml | Docker Compose | report 執行沒有標籤的映像檔 | 中止 |
release-terraform.yaml | Terraform | 執行個體以 Release 所指名的金鑰加密 | 完成 |
release-terraform-unencrypted.yaml | Terraform | Release 沒有指名金鑰,因此執行個體未加密 | 中止 |
release-cloudformation.yaml | CloudFormation | Release 開啟了 StorageEncrypted | 完成 |
release-cloudformation-unencrypted.yaml | CloudFormation | Release 讓它保持關閉 | 中止 |
release-arm.yaml | Azure Resource Manager | report 執行 registry.example.com/orders-report:1.4.2 | 完成 |
release-arm-unpinned.yaml | Azure Resource Manager | report 執行沒有標籤的映像檔 | 中止 |
映像檔由 Release 決定:core/compute/task 與 core/compute/worker 不封裝任何軟體,所以由 Release 在 infra.image 之下指名執行時所用的映像檔,包含標籤。在兩個 AWS 目標上,資料庫的加密也由 Release 決定。Terraform Recipe 讀取 infra.overrides.kms_key_id,並在它有設定時,於金鑰旁渲染 storage_encrypted = true;CloudFormation Recipe 讀取 infra.overrides.StorageEncrypted 與 KmsKeyId,使用資源屬性自己的名稱。兩者都沒設定的 Release 會渲染出完全沒有加密屬性的執行個體,刻意放入違規的 Releases 就是如此。
Azure Resource Manager 目標沒有可刻意放入的加密違規:彈性伺服器一律會對其靜態資料加密,所以範本中沒有任何設定可供規則發現缺少。那裡刻意放入的違規是未固定的映像檔,而規則的加密子句在該範本中什麼都讀不到。
Kubernetes 和 Compose 的資料庫執行映像檔 postgres:16,這是固定標籤,所以規則會判定它們,並讓它們在每個 Release 中通過。
附加規則
.aggrega-config.yaml 將 hub 指名為它的登錄庫,沒有指名目錄資料夾,也沒有治理附加項目。規則由建置以 --config 指名的檔案附加:Rego 引擎用 governance-rego.yaml,CEL 引擎用 governance-cel.yaml。Rego 的內容如下:
plugins: governance: - uri: governance/rego:0.9.0 role: render-verdict with: sources: - policies/house-rule.rego--config 為該次執行指名全域設定檔,取代本機原本會讀取的那一個,而這個工作區的 .aggrega-config.yaml 仍會疊加在它之上。工作區檔案中的 plugins.governance 清單會取代所指名檔案所帶有的清單,這就是工作區檔案不宣告任何清單的原因。0.9.0 是標準程式庫目錄中每個引擎之 Plugin 實體的版本。兩個引擎都編譯進 CLI——aggrega plugin list 會在內嵌的內建項目中列出 governance/rego 和 governance/cel——因此這裡沒有任何建置會取得外掛程式。
render-verdict 原則所讀取的內容
位於 render-verdict 的原則會在 input.artifacts 讀取建置所寫出的檔案,每個檔案一個項目,依路徑排序。每個項目帶有:
path——檔案在dist/之下的路徑,例如terraform/modules/db/main.tf;content——檔案的文字,與寫出時相同;format——目標的提供者為該檔案宣告的格式:Compose 檔案為yaml,CloudFormation 範本為json,每個 Terraform 檔案為hcl,包括其 Recipe 所寫出的資料庫主體。沒有任何宣告時,它是空字串,這裡的 Kubernetes 檔案就是如此;documents——解析成結構的檔案內容,以文件清單表示:這裡每個檔案各一份;parse_error——只出現在無法解析的檔案上,用來取代documents。
除了這些檔案之外,input.sources 存放附加項目自己的原則檔案,input.settings 存放它的設定——這裡是空物件,因為這個附加項目沒有宣告任何設定。
aggrega build --release <release> --dry-run --policy-input <dir> 會把每個角色收到的酬載寫入 <dir>,且不寫入其他任何東西。在 render-verdict 時,該檔案是引擎收到的內容,而不是原則讀取的內容:其中每個檔案都帶有它的 path 與 content,以及其提供者宣告的 format,引擎在收到酬載後,才會從內容解析出 documents。因此該檔案列出規則將會看到的檔案,但不含它們解析後的文件。在 release-terraform.yaml 的建置填好鎖定檔之後:
aggrega build --release release-terraform.yaml --config governance-rego.yaml --dry-run --policy-input payloadsjq -c '[.artifacts[].path]' payloads/render-verdict.json# ["terraform/aggrega.modules.tf","terraform/modules/db/main.tf","terraform/modules/report/aggrega.provenance.tf","terraform/modules/report/main.tf","terraform/modules/report/variables.tf"]規則在 documents 內讀取的路徑,是在建置中檢查的,此時引擎已經解析了它們。這些 Recipes 寫出的檔案都能成功解析,所以這裡沒有任何檔案帶有 parse_error。這條規則的每個子句都讀取 documents,因此無法解析的檔案會未經檢查就通過;不能讓這種情況發生的規則,要加上一個子句,拒絕帶有 parse_error 的檔案。
每個子句如何讀取其目標的形狀
每個子句都指名它所讀取的形狀,而其他形狀的檔案不帶有該子句所指名的內容:在 Rego 中,路徑是未定義的,子句不成立。因此一條規則可以跑過每個目標的輸出,而每個子句只會找到屬於自己的那一部分。
Compose 寫出一個檔案,其文件在 services 之下以名稱為每個服務建立鍵:
deny contains msg if { some artifact in input.artifacts some doc in artifact.documents some name, service in doc.services unpinned(service.image) msg := sprintf("%v: service %v runs %v, an image with no tag or the tag latest", [artifact.path, name, service.image])}Kubernetes 物件把它們的容器放在 pod 範本中,CronJob 把該範本放在比其他工作負載深一層的位置,即 spec.jobTemplate.spec 之下:
pod_template(doc) := doc.spec.jobTemplate.spec.template if doc.kind == "CronJob"
pod_template(doc) := doc.spec.template if doc.kind != "CronJob"原生 HCL 的 Terraform 把區塊巢狀放在其型別之下,再放在每個標籤之下,而最後一個標籤存放區塊清單,因為型別與標籤相同的區塊會收集成同一份清單。所以執行個體的屬性位於其名稱之下的一個索引處,而子句會走訪該清單:
deny contains msg if { some artifact in input.artifacts native_hcl(artifact) some doc in artifact.documents some name, blocks in doc.resource.aws_db_instance some block in blocks not block.storage_encrypted == true msg := sprintf("%v: the RDS instance %v stores its data unencrypted", [artifact.path, name])}這裡的清單比起尋找某個值的規則更重要,因為這個子句是針對缺少而拒絕。若寫成針對單一區塊——some name, block in doc.resource.aws_db_instance——則 block 就是那份清單,block.storage_encrypted 在它上面是未定義的,而 not 對任何未定義的東西都成立:子句連 release-terraform.yaml 也會拒絕,儘管其執行個體已加密,訊息還和刻意放入違規的 Release 相同。
.tf.json 檔案會依其 Recipe 寫出的樣子讀取,名稱直接存放區塊本身,所以為其中一種形式寫的規則讀不到另一種。這就是子句先讀取 format 的原因:native_hcl 接受格式為 hcl 的檔案,或格式為空且名稱以 .tf 結尾的檔案——沒有任何提供者為其宣告格式的檔案是依名稱解析的,所以規則也依名稱判斷它,而不是略過它。這裡建置的每個 Terraform 檔案都宣告為 hcl,所以這裡沒有任何檔案是依名稱判斷的。這些 Recipes 不寫出 .tf.json 檔案,所以規則不會讀到其他形式。各角色的酬載揭露什麼說明了這兩種形式。
CloudFormation 寫出一個範本,其資源在 Resources 之下以邏輯 ID 為鍵,每個資源都有它的 Type 和 Properties:
deny contains msg if { some artifact in input.artifacts some doc in artifact.documents some id, resource in doc.Resources resource.Type == "AWS::RDS::DBInstance" not resource.Properties.StorageEncrypted == true msg := sprintf("%v: the RDS instance %v stores its data unencrypted", [artifact.path, id])}沒有 StorageEncrypted 屬性的執行個體,或該屬性設為 true 以外任何值的執行個體,都會被拒絕:not … == true 對兩者都成立。
Azure Resource Manager 寫出一個範本,其 resources 是以符號名稱為鍵的物件。容器應用程式是型別為 Microsoft.App/containerApps 的資源,其容器位於 properties.template 之下;包裝模組的 Recipe 則寫出巢狀部署,也就是型別為 Microsoft.Resources/deployments 的資源,它在 containers 參數中接收其容器。這裡的 worker-container-apps 輸出是第二種形式,而規則兩種都會讀取:
deny contains msg if { some artifact in input.artifacts some doc in artifact.documents is_object(doc.resources) some resource in doc.resources resource.type == "Microsoft.Resources/deployments" some container in resource.properties.parameters.containers.value unpinned(container.image) msg := sprintf("%v: container %v runs %v, an image with no tag or the tag latest", [artifact.path, container.name, container.image])}arm 提供者一律把 resources 寫成那種物件。is_object 讓子句不去碰建置中頂層 resources 存放清單的其他任何文件。
CEL 規則為何寫法不同
policies/house-rule.cel 陳述相同的兩個子句,並在這裡的每次建置上得出相同的判定。它在三個地方的寫法不同,每一處都是出於引擎的緣故。
**它在讀取每個鍵之前都先測試。**在 CEL 中,讀取值所沒有的鍵是一種求值錯誤,而不是未定義的路徑,而錯誤會中止建置。每個目標的檔案都缺少別的目標所帶有的鍵——Kubernetes 物件沒有 services,Compose 檔案沒有 kind——所以每個子句都先用 has() 測試它所讀取的鍵:
// Docker Compose: a service whose image names no fixed version.input.artifacts .filter(a, has(a.documents) && a.documents.exists(d, has(d.services) && d.services.exists(s, has(d.services[s].image) [… omitted: the image test, and the rest of the clause]沒有 has(d.services) 測試時,aggrega build --release release-helm.yaml --config governance-cel.yaml --auto-pull 不論 Kubernetes 物件執行什麼映像檔都會中止,以 1 結束,且不寫出 dist/,而回報該錯誤的那一行會指名原則檔案與鍵:
render-verdict: plugin "governance/cel:0.9.0": call "verdict_render": wasm: plugin "cel" call to verdict_render: policy source "house-rule.cel" could not be evaluated: No such key: services**它每個檔案只回報一個違規者。**決策鍵會收集訊息,而 CEL 運算式無法把它為每個檔案建立的清單合併成一份清單。所以每個 CEL 子句在每個檔案中只回報一個違規者:pod 範本的第一個容器,或者在檔案以名稱為鍵的服務、執行個體與資源之中,名稱排序最前的那一個。推導式走訪對應表時沒有固定順序,所以依名稱選擇才能讓訊息在每次建置之間保持相同。Rego 規則會回報每個違規者。同一個 Compose 檔案中若有兩個未固定的服務,Rego 引擎會以兩則訊息中止,CEL 引擎則只以一則訊息中止,針對名稱排序最前的服務;若只有一個,如這裡的每個 Release,兩者會以相同的訊息中止。
**它會重複自己。**CEL 沒有為子運算式命名的方法,所以使用兩次的測試就寫兩次,映像檔測試在每個子句中都完整寫出,而不是像 Rego 的 unpinned 那樣被呼叫。
這兩個檔案是刻意的近似重複,各自依上述規則獨立撰寫,而不是由其中一個產生另一個,這樣當引擎之間意見不一致的建置,就會顯示出該分歧。
判定絕不會改變輸出
位於 render-verdict 的原則會接受建置或拒絕建置。它拿到建置所寫出的檔案並回傳判定;它回傳的任何東西都不會套用到這些檔案上。所以對照組不論有沒有附加規則,建置出的檔案都逐位元組相同,而 check.sh 在每次執行時都會比較這兩棵樹。被拒絕的建置完全不會寫出 dist/。要修正拒絕,要改的是 Release——固定的標籤、加密金鑰——然後下一次建置會再次被判定。
草擬範例
你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example policy-render-verdict它接著會印出要接著執行的命令。第一個會切換到新資料夾:
cd policy-render-verdict建置
aggrega build --release release-terraform.yaml --config governance-rego.yaml --auto-pullaggrega build --release release-terraform-unencrypted.yaml --config governance-rego.yaml --auto-pull # halts; exits 1把 governance-rego.yaml 換成 governance-cel.yaml,就能在 CEL 引擎上執行同一條規則;把 Release 檔名中的 terraform 換成 helm、compose、cloudformation 或 arm,它們刻意放入違規的 Releases 是 release-helm-unpinned.yaml、release-compose-unpinned.yaml、release-cloudformation-unencrypted.yaml 和 release-arm-unpinned.yaml。
**標準程式庫目錄在這裡供應每個 Component、Recipe、Feature 和 FeatureRecipe,並帶有兩個引擎的 Plugin 實體。**它們從 hub.aggrega.ai 解析,那是 .aggrega-config.yaml 所指名的預設登錄庫。若以 --catalog 指定目錄簽出(取得方式),就不會從登錄庫取得任何東西,所以不需要其他東西的建置可離線執行。--auto-pull 會在建置期間填入每個 Release 的鎖定檔;鎖定檔與渲染輸出是產生出來的,並非隨附。
Terraform 對照組建置完成。刻意放入違規的 Release 則中止:
[… omitted: the lines printed while the lockfile is resolved, which carry the refusal below once more and say the render that discovers external inputs could not finish][POLICY_ERROR E0005] policy plugin "rego" rejected the build: terraform/modules/db/main.tf: the RDS instance db stores its data unencrypted caused by: policy violation每則訊息都指名規則所判定的檔案(以它在 dist/ 之下的路徑表示)以及其中的違規者。其他刻意放入違規的 Releases 也以同樣方式中止:
policy plugin "rego" rejected the build: helm/report-cronjob.yaml: container main runs registry.example.com/orders-report:latest, an image with no tag or the tag latestpolicy plugin "cel" rejected the build: compose/docker-compose.yaml: service report runs registry.example.com/orders-report, an image with no tag or the tag latestpolicy plugin "cel" rejected the build: cloudformation/cloudformation-stack.json: the RDS instance Db stores its data unencryptedpolicy plugin "rego" rejected the build: arm/arm-template.json: container main runs registry.example.com/orders-report, an image with no tag or the tag latest每一行都是該建置輸出中 [POLICY_ERROR E0005] 之後的那一行,從左邊界開始顯示。第一則和最後一則來自 Rego 引擎,另外兩則來自 CEL 引擎,各自指名回報它的引擎;另一個引擎會以自己的名稱印出相同的訊息。
搭配 AI 助理
以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。
只使用 Aggrega 工具,不要自行建立任何資料夾。如果工作區資料夾中還沒有 `policy-render-verdict` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `policy-render-verdict`,且不指定 `dir`:它會把範例寫入新的 `policy-render-verdict` 資料夾。接著為每個 Release 各呼叫一次 `cli-build` 工具(它會執行 `aggrega build` 並接受其 `--config` 旗標),依序為 `release-terraform.yaml` 和 `release-terraform-unencrypted.yaml`,每次都將 `workspace_root` 設為 `policy-render-verdict`、`release` 設為該檔案、`auto_pull` 設為 true、`config` 設為 `governance-rego.yaml`。第二次本來就應該被拒絕,所以請把拒絕訊息讀給我聽。告訴我第一次建置寫出了哪些檔案;它們位於 `policy-render-verdict/dist/` 之下。檢查每次建置
這個自我檢查在文件存放庫的檢出中執行,其中有它所使用的共用建置輔助程式;aggrega draft --example 所寫出的副本不帶該輔助程式。
./check.shcheck.sh 先對每個對照組各建置一次,不附加任何原則,作為參照,然後透過 build-release 輔助工具,在每個引擎下建置每個 Release。對照組必須完成,並寫出與其參照逐位元組相同的檔案;刻意放入違規的 Release 必須以 E0005 和預期的訊息中止,除此之外沒有其他拒絕,且不寫出 dist/。兩個引擎都要符合同一則訊息。它每次建置印出一行,若有任何結果不同就以 1 結束,並印出該建置的輸出:
ENGINE RELEASE EXIT EXPECTED VERDICT--------------------------------------------------------------------------------------------------------------none release-helm.yaml 0 reference OKnone release-compose.yaml 0 reference OKnone release-terraform.yaml 0 reference OKnone release-cloudformation.yaml 0 reference OKnone release-arm.yaml 0 reference OKrego release-helm.yaml 0 pass OK: completed, byte-identical to the build with no policyrego release-helm-unpinned.yaml 1 halt OK: E0005, the expected sentencerego release-compose.yaml 0 pass OK: completed, byte-identical to the build with no policyrego release-compose-unpinned.yaml 1 halt OK: E0005, the expected sentencerego release-terraform.yaml 0 pass OK: completed, byte-identical to the build with no policyrego release-terraform-unencrypted.yaml 1 halt OK: E0005, the expected sentencerego release-cloudformation.yaml 0 pass OK: completed, byte-identical to the build with no policyrego release-cloudformation-unencrypted.yaml 1 halt OK: E0005, the expected sentencerego release-arm.yaml 0 pass OK: completed, byte-identical to the build with no policyrego release-arm-unpinned.yaml 1 halt OK: E0005, the expected sentencecel release-helm.yaml 0 pass OK: completed, byte-identical to the build with no policycel release-helm-unpinned.yaml 1 halt OK: E0005, the expected sentencecel release-compose.yaml 0 pass OK: completed, byte-identical to the build with no policycel release-compose-unpinned.yaml 1 halt OK: E0005, the expected sentencecel release-terraform.yaml 0 pass OK: completed, byte-identical to the build with no policycel release-terraform-unencrypted.yaml 1 halt OK: E0005, the expected sentencecel release-cloudformation.yaml 0 pass OK: completed, byte-identical to the build with no policycel release-cloudformation-unencrypted.yaml 1 halt OK: E0005, the expected sentencecel release-arm.yaml 0 pass OK: completed, byte-identical to the build with no policycel release-arm-unpinned.yaml 1 halt OK: E0005, the expected sentence
check: every build ended as expected.它使用 ./build-release.sh 所解析出的二進位檔,所以由 AGGREGA_BIN 決定,而 -- 之後的引數會傳到每次建置。當這個目錄中有 dist/ 時,它拒絕啟動,因為它會把每次建置的樹移開來比較,而且不會留下任何一棵;這些建置會填入這個目錄的鎖定檔,與任何建置一樣。它的結束狀態就是整個判定。
編譯器不解讀的地方
編譯器渲染每個目標的檔案,以其提供者所宣告的格式把它們當作文字交給附加的引擎,並依每個判定所帶的嚴重程度採取行動。它不知道映像檔標籤是什麼、latest 會移動,也不知道 RDS 執行個體應該加密:這些意義屬於規則。關於角色、酬載與決策鍵,請見治理插槽。
檔案
aggregate.yaml——資料庫,以及連接到它的報表工作。aggregate-arm.yaml——同一個資料庫與一個報表 worker,用於 Azure Resource Manager 的建置。manifest-helm.yaml、manifest-compose.yaml、manifest-terraform.yaml、manifest-cloudformation.yaml、manifest-arm.yaml——在單一目標上,將每個元件型別與 Features 繫結到各自的 Recipes。release-<target>.yaml、release-<target>-unpinned.yaml、release-<target>-unencrypted.yaml——每個目標一個對照組和一個刻意放入的違規。.aggrega-config.yaml— 預設登錄庫;沒有治理附加項目。governance-rego.yaml、governance-cel.yaml——透過各引擎附加在render-verdict的規則,各以--config指名。policies/house-rule.rego、policies/house-rule.cel——規則,每個引擎各一份。build-release.sh— 建置一個 Release,並回顯等效的aggrega build命令。check.sh——在每個引擎下建置每個 Release,並檢查每個結果。