From 6c0ebaa1294b00e558cf2d52db9bddd0df22286c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 1 Nov 2023 22:10:30 +0100 Subject: [PATCH] fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- test/api/go/main/main.go | 51 +++++++++++++------------- website/docs/go-library/index.md | 61 +++++++++++++++----------------- 2 files changed, 55 insertions(+), 57 deletions(-) diff --git a/test/api/go/main/main.go b/test/api/go/main/main.go index a53153d5..77880054 100644 --- a/test/api/go/main/main.go +++ b/test/api/go/main/main.go @@ -5,33 +5,32 @@ import ( "encoding/json" "log" - "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1" jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine" - "gopkg.in/yaml.v2" + "github.com/kyverno/kyverno-json/pkg/policy" ) -func main() { - - // load policies - policyYAML := ` - apiVersion: json.kyverno.io/v1alpha1 - kind: ValidatingPolicy - metadata: - name: authz - spec: - rules: - - name: delete-checks - match: - all: - (input.method): "DELETE" - assert: - all: - - check: - role: "admin" - ` +const policyYAML = ` +apiVersion: json.kyverno.io/v1alpha1 +kind: ValidatingPolicy +metadata: + name: authz +spec: + rules: + - name: delete-checks + match: + all: + (input.method): "DELETE" + assert: + all: + - check: + role: "admin" +` - var policy v1alpha1.ValidatingPolicy - yaml.Unmarshal([]byte(policyYAML), &policy) +func main() { + policies, err := policy.Parse([]byte(policyYAML)) + if err != nil { + panic(err) + } // load payloads requestJSON := `{ @@ -44,12 +43,14 @@ func main() { }` var payload interface{} - json.Unmarshal([]byte(requestJSON), &payload) + if err := json.Unmarshal([]byte(requestJSON), &payload); err != nil { + panic(err) + } // create a JsonEngineRequest request := jsonengine.JsonEngineRequest{ Resources: []interface{}{payload}, - Policies: []*v1alpha1.ValidatingPolicy{&policy}, + Policies: policies, } // create a J diff --git a/website/docs/go-library/index.md b/website/docs/go-library/index.md index 9a8cdaa0..8d28142d 100644 --- a/website/docs/go-library/index.md +++ b/website/docs/go-library/index.md @@ -6,6 +6,8 @@ The Go API can be added to a program's dependencies as follows: ```sh go get github.com/kyverno/kyverno-json/pkg/jsonengine +go get github.com/kyverno/kyverno-json/pkg/policy + ``` Here is a sample program that shows the overall flow for programatically using the Kyverno JSON Engine: @@ -18,33 +20,32 @@ import ( "encoding/json" "log" - "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1" jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine" - "gopkg.in/yaml.v2" + "github.com/kyverno/kyverno-json/pkg/policy" ) -func main() { +const policyYAML = ` +apiVersion: json.kyverno.io/v1alpha1 +kind: ValidatingPolicy +metadata: + name: authz +spec: + rules: + - name: delete-checks + match: + all: + (input.method): "DELETE" + assert: + all: + - check: + role: "admin" +` - // load policies - policyYAML := ` - apiVersion: json.kyverno.io/v1alpha1 - kind: ValidatingPolicy - metadata: - name: authz - spec: - rules: - - name: delete-checks - match: - all: - (input.method): "DELETE" - assert: - all: - - check: - role: "admin" - ` - - var policy v1alpha1.ValidatingPolicy - yaml.Unmarshal([]byte(policyYAML), &policy) +func main() { + policies, err := policy.Parse([]byte(policyYAML)) + if err != nil { + panic(err) + } // load payloads requestJSON := `{ @@ -57,21 +58,21 @@ func main() { }` var payload interface{} - json.Unmarshal([]byte(requestJSON), &payload) + if err := json.Unmarshal([]byte(requestJSON), &payload); err != nil { + panic(err) + } // create a JsonEngineRequest request := jsonengine.JsonEngineRequest{ Resources: []interface{}{payload}, - Policies: []*v1alpha1.ValidatingPolicy{&policy}, + Policies: policies, } - // create a JSON Engine + // create a J engine := jsonengine.New() - // execute the request responses := engine.Run(context.Background(), request) - // process the response logger := log.Default() for _, resp := range responses { if resp.Error != nil { @@ -86,7 +87,3 @@ func main() { } } ``` - - - -