A template repository to quickly scaffold a Kubewarden policy written with Go language

go-policy-template

This is a template repository that can be used to to quickly scaffold a Kubewarden policy written with Go language.

Don't forget to checkout Kubewarden's official documentation for more information about writing policies.

Introduction

Thist repository contains a working policy written in Go.

The policy looks at the name of a Kubernetes resource and rejects the request if the name is on a deny list.

The deny list is configurable by the user via the runtime settings of the policy. The configuration of the policy is expressed via this structure:

{
  "denied_names": [ "badname1", "badname2" ]
}

Code organization

The code that takes care of parsing the settings can be found inside of the settings.go file.

The actual validation code is defined inside of the validate.go file.

The main.go contains only the code which registers the entry points of the policy.

Implementation details

DISCLAIMER: WebAssembly is a constantly evolving topic. This document describes the status of the Go ecosystem at April 2021.

Currently the official Go compiler cannot produce WebAssembly binaries that can be run outside of the browser. Because of that, Kubewarden Go policies can be built only with the TinyGo compiler.

TinyGo doesn't yet support all the Go features (see here to see the current project status). Currently its biggest limitation is the lack of a fully supported reflect package. Among other things, that leads to the inability to use the encoding/json package against structures and user defined types.

Kubewarden policies need to process JSON data like the policy settings and the actual request received by Kubernetes. However it's still possible to write a Kubewarden policy by using some 3rd party libraries.

This is a list of libraries that can be useful when writing a Kubewarden policy:

  • Parsing JSON: queries against JSON documents can be written using the gjson library. The library features a powerful query language that allows quick navigation of JSON documents and data retrieval.
  • Mutating JSON: changing the contents of a JSON document can be done using the sjson library.
  • Generic set implementation: using Set data types can significantly reduce the amount of code inside of a policy, see the union, intersection, difference,... operations provided by a Set implementation. The mapset can be used when writing policies.

Last but not least, this policy takes advantage of helper functions provided by Kubewarden's Go SDK.

Testing

This policy comes with a set of unit tests implemented using the Go testing framework.

As usual, the tests are defined inside of the _test.go files. Given these tests are not part of the final WebAssembly binary, the official Go compiler can be used to run them. Hence they can take advantage of the encoding/json package to reduce some testing boiler plate.

The unit tests can be run via a simple command:

make test

It's also important the test the final result of the TinyGo compilation: the actual WebAssembly module.

This is done by a second set of end-to-end tests. These tests use the policicy-testdrive cli provided by the Kubewarden project to load and execute the policy.

The e2e tests are implemented using bats: the Bash Automated Testing System.

The end-to-end tests are defined inside of the e2e.bats file and can be run via this commmand:

make e2e-tests

Automation

This project contains the following GitHub Actions:

  • e2e-tests: this action builds the WebAssembly policy, installs the bats utility and then runs the end-to-end test
  • unit-tests: this action runs the Go unit tests
  • release: this action builds the WebAssembly policy and pushes it to a user defined OCI registry (ghcr is a perfect candidate)
Comments
  • Revert

    Revert "Use upstream gjson"

    This reverts this merged PR: https://github.com/kubewarden/go-policy-template/pull/22/files

    Unfortunately one of the dependencies of gjson is not compatible with TinyGo. Building the template policy leads to the following build time failure:

    -: blocking operation in exported function: __guest_call
    
    traceback:
    __guest_call
    main.validate
    fmt.Sprintf
    (*fmt.pp).doPrintf
    (*fmt.pp).printArg
    (*fmt.pp).printValue
    internal/fmtsort.Sort
    sort.Stable
    sort.stable
    sort.insertionSort
    (sort.Interface).Less
    (*github.com/tidwall/pretty.byKeyVal).Less
    (*github.com/tidwall/pretty.byKeyVal).isLess
    github.com/tidwall/pretty.parsestr
    encoding/json.Unmarshal
    (*encoding/json.decodeState).unmarshal
    (*encoding/json.decodeState).value
    (*encoding/json.decodeState).object
    encoding/json.cachedTypeFields
    (*sync.Map).LoadOrStore
    (*sync.Mutex).Lock
    make: *** [Makefile:4: policy.wasm] Error 1
    

    CC @floriankoch

  • Update module github.com/kubewarden/policy-sdk-go to v0.2.1

    Update module github.com/kubewarden/policy-sdk-go to v0.2.1

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | github.com/kubewarden/policy-sdk-go | require | minor | v0.1.3 -> v0.2.1 |


    Release Notes

    kubewarden/policy-sdk-go

    v0.2.1

    Compare Source

    v0.2.0

    Compare Source


    Configuration

    πŸ“… Schedule: Branch creation - "before 3am on Monday" (UTC), Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    β™» Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    πŸ”• Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

  • Use native Go types to interact with kubernetes

    Use native Go types to interact with kubernetes

    Do not merge: this is for internal demo purposes still

    This takes advantage of the work I've done to have Kubernetes data types compatible with TinyGo.

  • Fix building with vanilla gjson

    Fix building with vanilla gjson

    This template has been broken since we merged https://github.com/kubewarden/go-policy-template/pull/22

    Building the policy via make was broken because of the following reasons:

    That's because:

    • The vendored files were not updated
    • The containerized build relied on tinygo 0.18.0, which didn't support some of the features requested by the vanilla gjson

    This PR address both issues by:

    • Update the vendored directory and doing a cleanup of it via go mod tidy
    • Upgrade to latest release of tinygo: 0.23.0

    Updating to tinygo 0.23 causes also a compilation error inside of the container because the git executable is not found. That's because upstream Go, since release 1.18, embeds the git repository details inside of the final binaries, as reported inside of the release notes:

    The go command now embeds version control information in binaries. It includes the currently checked-out revision, commit time, and a flag indicating whether edited or untracked files are present. Version control information is embedded if the go command is invoked in a directory within a Git, Mercurial, Fossil, or Bazaar repository, and the main package and its containing main module are in the same repository. This information may be omitted using the flag -buildvcs=false.

    We don't want to add the git binary to the official tinygo container images, hence we're turning off this feature. It's important to note that tinygo build command doesn't support the buildvcs flag. However setting the flag via the GOFLAGS environment variable works.

    As a final note, in order to prevent this kind of breakages from happening again, this PR enables the GitHub action that runs the tests and e2e tests against the reference policy contained inside of this repository

  • Return error in the Valid() method.

    Return error in the Valid() method.

    Updates the Valid() method in the Settings type. Thus, the caller can get a explanation why the settings are not valid and print a proper error message.

  • Fix the owner label

    Fix the owner label

    The sample ingress has owner: some-owner but it'll be rejected by the tests in the tutorial unexpectedly. e.g.: https://docs.kubewarden.io/writing-policies/go/04-validation.html

    func TestAcceptRequestWithConstraintLabel(t *testing.T) {
        constrainedLabels := make(map[string]*RegularExpression)
        re, err := CompileRegularExpression(`^team-`)
        (snip)
        constrainedLabels["owner"] = re
    
  • Add metadata.yml file to the template repo

    Add metadata.yml file to the template repo

    The project should be created with a metadata.yml file that looks like:

    rules:
    - apiGroups: [""]
      apiVersions: ["v1"]
      resources: ["pods"]
      operations: ["CREATE", "UPDATE"]
    mutating: false
    labels:
      production: false
    annotations:
      name.castelli.hello: world
      io.kubewarden.policy.title: psp-apparmor
      io.kubewarden.policy.description: Replacement for the Kubernetes Pod Security Policy that controls the usage of AppArmor profiles
      io.kubewarden.policy.author: Flavio Castelli
      io.kubewarden.policy.url: https://github.com/kubewarden/psp-apparmor
      io.kubewarden.policy.source: https://github.com/kubewarden/psp-apparmor
      io.kubewarden.policy.license: Apache-2.0
      io.kubewarden.policy.usage: |
        This policy works by defining a whitelist of allowed AppArmor profiles. Pods are then inspected at creation and update time, to ensure only approved profiles are used.
    
        When no AppArmor profile is defined, Kubernetes will leave the final choice to the underlying container runtime. This will result in using the default AppArmor profile provided by Container Runtime. Because of that, the default behaviour of this policy is to accept workloads that do not have an AppArmor profile specified.
    
        The policy can be configured with the following data structure:
        ```yaml
        # list of allowed profiles
        allowed_profiles:
        - runtime/default
        - localhost/my-special-workload
        ```
    
  • Update module github.com/wapc/wapc-guest-tinygo to v0.3.3

    Update module github.com/wapc/wapc-guest-tinygo to v0.3.3

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | github.com/wapc/wapc-guest-tinygo | require | patch | v0.3.2 -> v0.3.3 |


    Release Notes

    wapc/wapc-guest-tinygo

    v0.3.3

    Compare Source


    Configuration

    πŸ“… Schedule: Branch creation - "before 3am on Monday" (UTC), Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    β™» Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    πŸ”• Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

  • Update module github.com/wapc/wapc-guest-tinygo to v0.3.2

    Update module github.com/wapc/wapc-guest-tinygo to v0.3.2

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | github.com/wapc/wapc-guest-tinygo | require | patch | v0.3.1 -> v0.3.2 |


    Release Notes

    wapc/wapc-guest-tinygo

    v0.3.2

    Compare Source


    Configuration

    πŸ“… Schedule: Branch creation - "before 3am on Monday" (UTC), Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    β™» Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    πŸ”• Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

  • Create SBOM file for each policy

    Create SBOM file for each policy

    It would be great to have a SBOM file for each policy release.

    Action items

    • Figure out how to automate the SBOM creation -> a user cloning our template should get this action automatically configured and enabled
    • Propagate this change to our existing policies
  • Update module go to 1.19

    Update module go to 1.19

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | go (source) | golang | minor | 1.17 -> 1.19 |


    Release Notes

    golang/go

    v1.19.0

    v1.18.5

    v1.18.4

    v1.18.3

    v1.18.2

    v1.18.1

    v1.18.0


    Configuration

    πŸ“… Schedule: Branch creation - "before 3am on Monday" (UTC), Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    β™» Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    πŸ”• Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

  • Dependency Dashboard

    Dependency Dashboard

    This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

    This repository currently has no open or pending branches.

    Detected dependencies

    github-actions
    .github/workflows/test.yml
    • kubewarden/github-actions v1
    gomod
    go.mod
    • go 1.19
    • github.com/kubewarden/strfmt v0.1.2
    • github.com/francoispqt/onelog v0.0.0-20190306043706-8c2bb31b10a4@8c2bb31b10a4
    • github.com/kubewarden/k8s-objects v1.24.0-kw3
    • github.com/kubewarden/policy-sdk-go v0.2.3
    • github.com/mailru/easyjson v0.7.7
    • github.com/wapc/wapc-guest-tinygo v0.3.3

    • [ ] Check this box to trigger a request for Renovate to run again on this repository
Automated-gke-cilium-networkpolicy-demo - Quickly provision and tear down a GKE cluster with Cilium enabled for working with Network Policy.

Automated GKE Network Policy Demo Before running the automation, make sure you have the correct variables in env-automation/group_vars/all.yaml. There

Jan 1, 2022
Prevent Kubernetes misconfigurations from ever making it (again 😀) to production! The CLI integration provides policy enforcement solution to run automatic checks for rule violations. Docs: https://hub.datree.io
Prevent Kubernetes misconfigurations from ever making it  (again 😀) to production! The CLI integration provides policy enforcement solution to run automatic checks for rule violations.  Docs: https://hub.datree.io

What is Datree? Datree helps to prevent Kubernetes misconfigurations from ever making it to production. The CLI integration can be used locally or in

Jan 1, 2023
Kubernetes Native Policy Management
Kubernetes Native Policy Management

Kyverno Kubernetes Native Policy Management Kyverno is a policy engine designed for Kubernetes. It can validate, mutate, and generate configurations u

Jan 2, 2023
octant plugin for kubernetes policy report
octant plugin for kubernetes policy report

Policy Report octant plugin [Under development] Resource Policy Report Tab Namespace Policy Report Tab Policy Report Navigation Installation Install p

Aug 7, 2022
Sign Container Images with cosign and Verify signature by using Open Policy Agent (OPA)
 Sign Container Images with cosign and Verify signature by using Open Policy Agent (OPA)

Sign Container Images with cosign and Verify signature by using Open Policy Agent (OPA) In the beginning, I believe it is worth saying that this proje

Nov 30, 2022
runtime - an abstraction library on top of the Open Policy Agent (OPA)

runtime - an abstraction library on top of the Open Policy Agent (OPA) Introduction The "runtime" project is a library that sits on top of OPA. The go

Nov 7, 2022
A plugin for running Open Policy Agent (OPA) in AWS Lambda as a Lambda Extension.

opa-lambda-extension-plugin A custom plugin for running Open Policy Agent (OPA) in AWS Lambda as a Lambda Extension. To learn more about how Lambda Ex

Jan 2, 2023
Hexa is the open-source, standards-based policy orchestration software for multi-cloud and hybrid businesses.

Hexa Policy Orchestrator Hexa is the open-source, standards-based policy orchestration software for multi-cloud and hybrid businesses. The Hexa projec

Dec 22, 2022
Template Repository For Bug Reporting
Template Repository For Bug Reporting

Template Repository For Bug Reporting Create a Repository From the Template First and foremost, create a repository from this template repository (the

Nov 26, 2021
Provider-template - Template for writing providers for crossplane

provider-template provider-template is a minimal Crossplane Provider that is mea

Feb 3, 2022
Quickly connect to your Kubernetes Cluster with Tailscale

tsk tsk is a quick and magical way to connect your Kubernetes cluster to your Tailscale Tailnet. Installation tsk requires you have Pulumi installed.

Dec 17, 2022
OC Wrapper to facilitate switch clusters quickly

Description OCS is a wrapper for openshift cli-client oc logins to facilitate switching between multiple clusters easily. Install CP binary from repo/

Jan 10, 2022
A simple webdev utility program that allows developers to quickly validate and format JSON code

Toolbox CLI A simple webdev utility program that allows developers to quickly validate and format JSON code, convert from UNIX epoch to timestamp and

Jan 4, 2022
Igo Agent is the agent of Igo, a command-line tool, through which you can quickly start Igo

igo agent θ‹±ζ–‡ | δΈ­ζ–‡ Igo Agent is the agent of Igo, a command-line tool, through which you can quickly start Igo, and other capabilities may be added lat

Dec 22, 2021
Use cli tool to troubleshoot external API service quickly.
Use cli tool to troubleshoot external API service quickly.

golang CLI Template golang project template for building CLI Setup Setup by Command git clone https://github.com/mpppk/cli-template your_awesome_tool

Jan 5, 2022
Quickly cross-compile your C code
Quickly cross-compile your C code

WORK IN PROGRESS: Viceroy is a very early work in progress and is subject to breaking changes. It's also subject to not really working all that well y

Aug 19, 2022
Becca - A simple dynamic language for exploring language design

Becca A simple dynamic language for exploring language design What is Becca Becc

Aug 15, 2022
A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

Mar 9, 2022
Gopherscript is a secure and minimal scripting language written in Go.
Gopherscript is a secure and minimal scripting language written in Go.

Gopherscript Gopherscript is a secure scripting/configuration language written in Go. It features a fined-grain permission system and enforces a stron

Oct 2, 2022