Prerequisites
The Chainloop Go SDK for WASM policies is built on top of the Extism Go PDK, which provides the WebAssembly plugin interface. Required tools:TinyGo (0.30.0 or later)
TinyGo (0.30.0 or later)
TinyGo compiles Go code to WebAssembly with minimal binary size.
Go (1.21 or later)
Go (1.21 or later)
For dependency management and module support.
github.com/chainloop-dev/chainloop/labs/wasm-policy-sdk/go- Chainloop WASM Policy SDKgithub.com/extism/go-pdk- Extism Plugin Development Kit (auto-installed)
Project Setup
Create go.mod
Create policy.yaml
Complete Example
Here’s a complete policy that validates SBOM components:API Quick Reference
The Go SDK provides functions for:- Execution:
Run()- Entry point wrapper - Material Extraction:
GetMaterialJSON(),GetMaterialString(),GetMaterialBytes() - Arguments:
GetArgs(),GetArgString(),GetArgStringDefault() - Results:
Success(),Fail(),Skip(),OutputResult(),AddViolation(),HasViolations() - Logging:
LogInfo(),LogDebug(),LogWarn(),LogError() - HTTP Requests:
HTTPGet(),HTTPGetJSON(),HTTPPost(),HTTPPostJSON() - Artifact Discovery:
Discover(),DiscoverByDigest()
Building
Basic Build
Build Flags
-target=wasi- WebAssembly System Interface target-o policy.wasm- Output file name
Typical File Sizes
- Simple policy: ~770KB
- SBOM policy: ~770KB
- HTTP policy: ~793KB
Testing
Create Test Script
test.sh:
TinyGo Compatibility
TinyGo has some limitations compared to standard Go: Supported:- ✅ Flat structs with simple types
- ✅ Slices and maps with string keys
- ✅ json.Unmarshal for parsing
- ✅ String manipulation
- ✅ Basic math operations
- ⚠️ Generics (limited support)
- ❌ Reflection (limited)
- ❌ Goroutines (limited)
Best Practices
- Keep types simple - Use flat structs with concrete types
- Validate early - Check input format before complex logic
- Return specific violations - Include field names and values in messages
- Use skip for non-applicable cases - Don’t fail policies for wrong material types
- Test with real data - Use actual SBOMs/attestations from your projects
- Log validation progress - Use logging for debugging
- Handle errors gracefully - Always check error returns
