การเขียนปลั๊กอิน governance ของคุณเอง
หน้า.wasm แล้วPlugin ใน
หน้าPlugin และ
เกี่ยว<guest> คือ<workspace> คือ
สิ่งที่คุณต้องมี
- ชุด
เครื่อง มือ Go ซึ่ง คอมไพล์ เป็น WebAssembly โดย ไม่ ต้อง มี เครื่อง มือ เพิ่ม - สำเนา
ที่ เช็กเอาต์ ของ ชุด พัฒนา ปลั๊กอิน ซึ่ง มี โครง ร่าง และ ของ แพ็กเกจ ABI ที่ ชุด พัฒนา นี้ สร้าง อยู่ บน นั้น: go.modของสำเนา ของ คุณ ชี้ ไป ทั้ง สอง - ไบนารี
aggrega
ชุดrust/examples/governance-adapter-skeleton และtypescript/examples/governance-adapter-skeleton แต่ละaggrega ทุก
รูปทรงของผลงานที่เสร็จแล้ว
ปลั๊กอิน.wasm และPlugin ที่
ไดเรกทอรี
category-guard/ your copy of the skeleton: the Go module that compiles to the .wasm .gitignore README.md the skeleton's own guide go.mod go.sum main.go the describe() self-report and the shared plumbing evaluator.go the seam, where the rule goes annotations.go the annotation-transform role labels.go the label-transform role verdicts.go the ir-verdict and render-verdict roles build.sh annotation-payload.json label-payload.json ir-verdict-payload.json render-verdict-payload.json my-payload.json a copy you edit in section 4 wrong-payload.json a copy you break in section 5 overlay.yaml the identity you supply to the entity generator dist/governance-adapter-skeleton-go.wasm the compiled guestshop/ the workspace the plugin is attached in catalog/component/example/compute/api/1.0.0/component.yaml catalog/component/example/devtool/db-console/1.0.0/component.yaml catalog/recipe/compose/api/1.0.0/recipe.yaml catalog/recipe/compose/db-console/1.0.0/recipe.yaml catalog/plugin/governance/category-guard/0.1.0/plugin.yaml catalog/plugin/governance/category-guard/0.1.0/governance--category-guard.wasm aggregate.yaml manifest.yaml release.yaml .aggrega-config.yamlกฎdevtool หมวด
1. คัดลอกโครงร่าง
cp -r /path/to/aggrega-pdk/go/examples/governance-adapter-skeleton category-guardcd category-guardgo.mod ของreplace สอง
module example.com/category-guard
go 1.26.0
require ( github.com/aggrega-ai/aggrega-abi/go v0.0.0 github.com/aggrega-ai/aggrega-pdk/go v0.0.0 github.com/extism/go-pdk v1.1.3)
replace github.com/aggrega-ai/aggrega-pdk/go => /path/to/aggrega-pdk/go
replace github.com/aggrega-ai/aggrega-abi/go => /path/to/aggrega-abi/gogo.sum ของ
2. บิลด์ แล้วเรียกแต่ละบทบาทด้วยมือ
บิลด์
./build.shWrote <guest>/dist/governance-adapter-skeleton-go.wasmaggrega plugin run-local โหลดdescribe() ของ--type governance ว่า
aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export transform_annotations --with @annotation-payload.json{ "injections": []}run-local อ่าน
aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export mutate --with @label-payload.json{ "patches": []}aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_ir --with @ir-verdict-payload.json{ "results": [ { "severity": "info", "message": "this adapter has no policy engine yet: the seam received rules.policy (43 bytes) and some settings" } ]}aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_render --with @render-verdict-payload.json{ "results": [ { "severity": "info", "message": "this adapter has no policy engine yet: the seam received rules.policy (43 bytes) and some settings" } ]}คำinfo หนึ่งrules.policy ใต้ sources และsettings หมายเหตุ
นี่ir-verdict-payload.json ฉบับ
{ "graph": { "apiVersion": "aggrega.ai/v1beta1", "kind": "IR", "metadata": { "aggregate_name": "shop" }, "spec": { "nodes": { "api": { "component": "example/compute/api", "resolved_version": "1.0.0", "labels": { "component.aggrega.ai/type": "server", "component.aggrega.ai/category": "compute" } } }, "edges": [], "values_mapping": {}, "infra_mapping": {}, "feature_values_mapping": {}, "provenance": { "schema_version": "aggrega.ai/v1beta1", "compiler_version": "0.0.0", "aggregate_version": "1.0.0", "manifest_version": "1.0.0", "release_version": "1.0.0" } } }, "precalculated_paths": [], "sources": [ { "path": "rules.policy", "content": "policy text, handed to the engine verbatim\n" } ], "settings": { "max_nodes": 50 }}เป็นir-verdict: กราฟsources กับ settings ของverdict_render คือ RenderPolicyInput ของ
3. เขียนกฎหนึ่งข้อ
กฎevaluator.go ซึ่งEvaluate ด้วยdeny, warn และ audit — และhalt, warn และ info ส่วนannotations และ labels ดังdeny
แทนที่ evaluator.go ด้วย:
package main
import ( "encoding/json" "fmt" "slices" "sort")
// Program is one policy program the attachment carried.type Program struct { Path string Text string}
// categoryLabel is the label a catalog Component states its category in.const categoryLabel = "component.aggrega.ai/category"
// Evaluate refuses every component whose category the attachment's settings// list under forbidden_categories.func Evaluate(programs []Program, settings json.RawMessage, payload json.RawMessage, decision string) ([]json.RawMessage, error) { if decision != denyDecision { return nil, nil }
var config struct { ForbiddenCategories []string `json:"forbidden_categories"` } if len(settings) > 0 { if err := json.Unmarshal(settings, &config); err != nil { return nil, fmt.Errorf("reading the settings: %w", err) } }
var input struct { Graph struct { Spec struct { Nodes map[string]struct { Labels map[string]string `json:"labels"` } `json:"nodes"` } `json:"spec"` } `json:"graph"` } if err := json.Unmarshal(payload, &input); err != nil { return nil, fmt.Errorf("reading the build graph: %w", err) }
// A Go map has no order, so the node ids are sorted: identical input must // yield an identical sequence of decisions. ids := make([]string, 0, len(input.Graph.Spec.Nodes)) for id := range input.Graph.Spec.Nodes { ids = append(ids, id) } sort.Strings(ids)
var decisions []json.RawMessage for _, id := range ids { category := input.Graph.Spec.Nodes[id].Labels[categoryLabel] if !slices.Contains(config.ForbiddenCategories, category) { continue } message, err := json.Marshal(fmt.Sprintf( "component %q is a %s component, and this workspace ships none", id, category)) if err != nil { return nil, err } decisions = append(decisions, message) } return decisions, nil}Program คง
- อ่าน
เพย์โหลด โหนดภาย ใต้ ชื่อ ที่ wire ตั้ง ให้ อยู่ ที่ graph.spec.nodesโดยใช้ id ของ โหนด เป็น คีย์ แต่ละ โหนด มี labelsของตน — เส้น สิ่งทาง เดียว กับ ที่ ที่ ให้เพย์โหลด ของ แต่ละ บทบาท เปิด ให้ อ่าน นโยบาย Rego เป็น input.graph.spec.nodesกฎที่ เขียน สำหรับ บทบาท นี้ ไม่ อ่าน สิ่ง ใด เลย ที่ บทบาท อื่น: เพย์โหลด render-verdictไม่มีกราฟ การ วน ซ้ำ จึง ไม่ พบ โหนด ที่ นั่น - สิ่ง
ที่ หมวดห้าม เป็น เรื่อง ของ ผู้ ดำเนิน การ ไม่ใช่ ของ ปลั๊กอิน มา จาก settingsของการ แนบ .wasmเดียวกันจึง ใช้ กับ พื้นที่ ทำงาน ที่ ห้าม devtoolและพื้นที่ ที่ ไม่ ห้าม สิ่ง ใด - เรียง
ลำดับ เอาต์พุตก่อน ตอบ ของ บิลด์ เหมือน กัน ทุก ไบต์ ใน ทุก การ รัน ที่ อินพุต เหมือน กัน และ เกสต์ ที่ ตอบ ตาม ลำดับ ของ แมป ใน Go จะ ทำลาย สิ่ง นั้น จาก ภายใน - ล้ม
เหลว การอย่าง เสียง ดัง ตั้ง ค่า หรือ เพย์โหลด ที่ อ่าน ไม่ ได้ จะ ส่ง คืน ข้อ ผิด พลาด แทน รายการ ว่าง และ บทบาท เปลี่ยน ข้อ ผิด พลาด นั้น เป็นการ เรียก ที่ ล้ม เหลว รายการ ว่าง คือ นโยบาย ที่ ไม่ พบ สิ่ง ที่ จะ พูด การ เรียก ที่ ล้ม เหลว คือ การ เรียก ที่ ไม่ ได้ รัน และ บิลด์ แยก สอง อย่าง นี้ ออก จาก กัน ได้ ก็ ต่อ เมื่อ เกสต์ ทำ เช่น นั้น
กฎprograms เลย กฎsources
สุดท้าย ให้main.go เปลี่ยนdescriptor():
Description: "Refuse a build that carries a component of a category the attachment forbids.",ตัวPlugin
4. บิลด์อีกครั้ง แล้วอ่านคำตอบของกฎ
./build.shWrote <guest>/dist/governance-adapter-skeleton-go.wasmเรียก
aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export transform_annotations --with @annotation-payload.jsonaggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export mutate --with @label-payload.jsonaggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_ir --with @ir-verdict-payload.jsonaggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_render --with @render-verdict-payload.json{ "injections": []}{ "patches": []}{ "results": []}{ "results": []}หมายเหตุsettings ของresults ว่าง
หากir-verdict-payload.json ไปmy-payload.json และsettings ของ
"settings": { "forbidden_categories": [ "compute" ] }aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_ir --with @my-payload.json{ "results": [ { "severity": "halt", "message": "component \"api\" is a compute component, and this workspace ships none" } ]}การdeny มาhalt ที่halt หยุด
5. เมื่อเพย์โหลดไม่เข้ากัน
คำlabels ของwrong-payload.json:
"labels": [ "server", "compute" ]aggrega plugin run-local --plugin dist/governance-adapter-skeleton-go.wasm \ --type governance --export verdict_ir --with @wrong-payload.jsonunder key "api": expected JSON object, found [คำ
การ
6. ประกาศเป็นเอนทิตี Plugin ในแค็ตตาล็อกของพื้นที่ทำงานของคุณ
ฟิลด์overlay.yaml:
apiVersion: aggrega.ai/v1beta1kind: Pluginmetadata: name: category-guardaggrega plugin gen-catalog --plugin dist/governance-adapter-skeleton-go.wasm \ --overlay overlay.yaml --output-dir ../shop/catalog/plugingenerated ../shop/catalog/plugin/governance/category-guard/0.1.0/plugin.yamlเอนทิตี
# Generated from the plugin by `aggrega plugin gen-catalog`. You may narrow its capability lists; edit nothing else here.apiVersion: aggrega.ai/v1beta1kind: Pluginmetadata: name: category-guard version: 0.1.0 pluginType: governance description: Refuse a build that carries a component of a category the attachment forbids.spec: abiVersion: v1beta1 exports: - name: transform_annotations - name: mutate - name: verdict_ir - name: verdict_renderversion คือbuild.sh ของ0.1.0 เว้นAGGREGA_PLUGIN_VERSION ระบุpluginType และdescribe() รายงาน ปลั๊กอิน
ขณะ.wasm อยู่governance/category-guard คือ governance--category-guard.wasm:
cp dist/governance-adapter-skeleton-go.wasm \ ../shop/catalog/plugin/governance/category-guard/0.1.0/governance--category-guard.wasmคัด.wasm ตาม.wasm is not on this machine (ไม่
7. พื้นที่ทำงานที่ปลั๊กอินเฝ้าตรวจ
พื้นที่devtool
catalog/component/example/compute/api/1.0.0/component.yaml:
apiVersion: aggrega.ai/v1beta1kind: Componentmetadata: name: example/compute/api version: 1.0.0 description: The shop's HTTP API. labels: component.aggrega.ai/type: server component.aggrega.ai/category: computespec: schema: infra: port: type: integer default: 8080 description: The port the API listens on.catalog/component/example/devtool/db-console/1.0.0/component.yaml:
apiVersion: aggrega.ai/v1beta1kind: Componentmetadata: name: example/devtool/db-console version: 1.0.0 description: A database administration console in the browser. labels: component.aggrega.ai/type: server component.aggrega.ai/category: devtoolspec: schema: infra: port: type: integer default: 8081 description: The port the console listens on.catalog/recipe/compose/api/1.0.0/recipe.yaml:
apiVersion: aggrega.ai/v1beta1kind: Recipemetadata: name: api version: 1.0.0 description: Runs the API as a Docker Compose service.spec: capability: compose implements: example/compute/api:1.0.0 workload: | apiVersion: aggrega.ai/v1beta1 kind: Workload containers: main: image: "registry.example.com/shop/api:1.0.0" ports: http: {{ .infra.port }}catalog/recipe/compose/db-console/1.0.0/recipe.yaml:
apiVersion: aggrega.ai/v1beta1kind: Recipemetadata: name: db-console version: 1.0.0 description: Runs the database console as a Docker Compose service.spec: capability: compose implements: example/devtool/db-console:1.0.0 workload: | apiVersion: aggrega.ai/v1beta1 kind: Workload containers: main: image: "adminer:4" ports: http: {{ .infra.port }}aggregate.yaml:
apiVersion: aggrega.ai/v1beta1kind: Aggregatemetadata: name: shop version: "1.0.0" description: The shop's API.spec: components: api: type: example/compute/apimanifest.yaml:
apiVersion: aggrega.ai/v1beta1kind: Manifestmetadata: name: shop-manifest version: "1.0.0" description: Binds each component type to its Docker Compose recipe.spec: mappings: components: type: example/compute/api:1.0.0: api:1.0.0 example/devtool/db-console:1.0.0: db-console:1.0.0release.yaml:
apiVersion: aggrega.ai/v1beta1kind: Releasemetadata: name: shop-staging environment: staging version: "1.0.0"spec: aggregate: shop manifest: shop-manifest components: api: {}8. แนบและบิลด์
ผู้.aggrega-config.yaml:
workspace: catalog_dirs: - catalogauto_pull: trueplugins: governance: - uri: governance/category-guard:0.1.0 role: ir-verdict with: settings: forbidden_categories: - devtooluri ระบุir-verdict โฮสต์verdict_ir ด้วยauto_pull ให้
cd ../shopaggrega build --release release.yaml warning: plugin "governance/category-guard" resolved from local with no recorded digest to check it against, so it runs unverified; it is read from beside its Plugin entity in a catalog directory on every build, and a pull records its digest only for a plugin attached at a transform role; otherwise publish the plugin and reference the published entity to have its digest recordedPull complete: release.lock (entities=7, source_artifacts=0, external_inputs=0)[… omitted: a line reporting total build time, which differs on every run]Build completed: 1 file written to <workspace>/distร้านdevtool กฎdist/compose/docker-compose.yaml
คำ
9. วางการละเมิดไว้
เพิ่ม
spec: components: api: type: example/compute/api console: type: example/devtool/db-console components: api: {} console: {}aggrega build --release release.yaml[… omitted: the unverified-plugin warning, and the pull's lines saying the Release could not be rendered to discover its external inputs, which carry the refusal below][POLICY_ERROR E0005] policy plugin "category-guard" rejected the build: component "console" is a devtool component, and this workspace ships none caused by: policy violationบิลด์dist/ ข้อความ
สิ่งที่ควรเปลี่ยนต่อไป
- กฎ
เพิ่ม กฎเติม หรือ ภาษา นโยบาย ที่ เขียน ด้วย Go ถูก คอมไพล์ รวม อยู่ ใน เกสต์ หาก ต้องการ โหลด กฎ จาก ไฟล์ แทน ให้ อ่าน programsในEvaluate—sourcesของการ ABI เป็นแนบ แต่ละ รายการ มีชื่อ ฐาน และ ข้อความ — แล้ว ส่ง ให้ เอนจิน ของ Aggrega เอง อธิบายเกสต์ ที่ ห่อ เอนจิน นโยบาย ภายนอก - บทบาท
อื่น Evaluateเดียวกันรองรับ ทุก บทบาท การ ตอบ annotationsหรือlabelsทำให้ปลั๊กอิน บทบาทกำหนด รูป บิลด์ ที่ บทบาท การ แปลง รูป ทรง ของ การ ตัดสิน ใจ อยู่ ใน README ของ โครง ร่าง และ สิ่ง ที่ แต่ละ บทบาท เปลี่ยน ได้ อยู่ ใน ทั้ง สี่ - เอ็กซ์พอร์ต
น้อย ปลั๊กอินลง ที่ รองรับ เฉพาะ ir-verdictลบไฟล์ บทบาท อื่น และ รายการ ของ ไฟล์ เหล่า นั้น ใน descriptor()การแนบ ที่ ชี้ บทบาท ไป ยัง เอ็กซ์พอร์ต ที่ เกสต์ ไม่ ได้ ประกาศ จะ ถูก ปฏิเสธ โดย ระบุ ชื่อ ก่อน ที่ เกสต์ จะ รัน - การ
ตั้ง เกสต์ค่าที่ ระบุ ชนิด ประกาศ JSON Schema สำหรับ การ ตั้ง ค่า ของ ตน ใน describe()ได้ และโฮสต์ การจะ ตรวจ สอบ ความ ถูก ต้อง ของ การ ตั้ง ค่า ของ การ แนบ กับ สคีมา นั้น ก่อน ที่ เกสต์ จะ รัน ดู ตั้ง ค่าที่ ระบุ ชนิด - การ
เผย ซึ่งแพร่ เปลี่ยน คู่มือการ จัด วาง ไดเรกทอรี แค็ตตาล็อก ให้ เป็น อาร์ติแฟกต์ ที่ ปัก หมุด ด้วย ไดเจสต์ ซึ่ง Release หา ค่า และ ยืนยัน นัก ครอบคลุมพัฒนา ปลั๊กอิน วงจร ชีวิต นั้น
ข้อมูลอ้างอิงที่เกี่ยวข้อง
- ซ็อกเก็ต
การ — บทบาทกำกับ ดูแล ทั้ง สี่ เพย์โหลด ของ บทบาท ระดับ ความ รุนแรง ของ คำ ตัดสิน และ วิธี ที่ ผู้ ดำเนิน การ แนบ ปลั๊กอิน - สัญญา
ของ Plugin — รูปทรง อินพุต และ เอาต์พุต ของ ทุก ซ็อกเก็ต และ การ เข้า รหัส ที่ แต่ละ รูป ทรง ใช้ - คู่มือ
นัก — เอนทิตีพัฒนา ปลั๊กอิน Wasm Pluginพื้นผิว ความ สามารถ และ วงจร ชีวิต การ เผย แพร่ - การ
เขียน — บทผู้ ให้ บริการ แนะนำ ที ละ ขั้น ตอน แบบ เดียวกัน สำหรับ เกสต์ ผู้ ให้ บริการ - การ
อ่าน — ธรรมเนียมเอาต์พุต ใน หน้า เหล่า นี้ ที่ บล็อก เอาต์พุต ข้าง ต้น ปฏิบัติ ตาม