Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Playground modes and UI changes. #69

Merged
merged 109 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
8fb3e18
adds toggle mode and modal to choose mode
joaovictor3g Apr 8, 2024
787a6e4
saves mode in localStorage and uses the value to start the mode
joaovictor3g Apr 8, 2024
f55dc7c
adds interactions
joaovictor3g Apr 8, 2024
14702ad
Merge branch 'feat/vap' into UD-1369
joaovictor3g Apr 8, 2024
b85aa09
refactors structure of tooltips and modals
joaovictor3g Apr 9, 2024
9d37991
adds tabs when in VAP mode
joaovictor3g Apr 9, 2024
c838c59
adds accordion components
joaovictor3g Apr 9, 2024
6bb9384
shows accordion only in VAP mode
joaovictor3g Apr 10, 2024
6174bdb
updates import
joaovictor3g Apr 10, 2024
10fe25d
adds tooltips css
joaovictor3g Apr 10, 2024
1728a12
updates tooltip js logic
joaovictor3g Apr 10, 2024
3c6587d
componentizes tooltip
joaovictor3g Apr 10, 2024
418ec4e
wip: schema proposal for dynamic modes, inputs and examples
matheusfm Apr 10, 2024
8229ef0
Merge branch 'wip/dynamic-modes' of github.com:undistro/cel-playgroun…
joaovictor3g Apr 10, 2024
455e981
uses modes.json to fill modes in modal
joaovictor3g Apr 10, 2024
9de99b2
loads examples by mode
joaovictor3g Apr 10, 2024
e8eb649
refactor
joaovictor3g Apr 10, 2024
930be53
shows tabs when the inputs is bigger than 1
joaovictor3g Apr 10, 2024
9c5004f
renders tab dinamically
joaovictor3g Apr 10, 2024
c7ff232
adds min-width to nice-select
joaovictor3g Apr 11, 2024
12256df
adds min-width
joaovictor3g Apr 11, 2024
11af0d4
fixes tab content behavior
joaovictor3g Apr 11, 2024
0b1452d
adds syntax highlighting
joaovictor3g Apr 11, 2024
5ee7022
makes inputs empty when changing mode
joaovictor3g Apr 11, 2024
d8028f9
adds accessible attrs in modal
joaovictor3g Apr 11, 2024
5affbe4
adds animation to tabs
joaovictor3g Apr 11, 2024
e3c7783
refactors structure of project
joaovictor3g Apr 11, 2024
a1480bd
removes MODES and rename localStorage key
joaovictor3g Apr 11, 2024
5056946
improves modal accessibility
joaovictor3g Apr 12, 2024
bee40d5
calls hide function
joaovictor3g Apr 12, 2024
3f4e764
adds license
joaovictor3g Apr 12, 2024
860c754
fixes tab bottom position
joaovictor3g Apr 12, 2024
979dd19
loads json files
joaovictor3g Apr 12, 2024
bb247ef
adds inputs object into the examples
joaovictor3g Apr 12, 2024
593524d
updates json files format
joaovictor3g Apr 15, 2024
3683b3f
adds categories
joaovictor3g Apr 15, 2024
51c26c7
refactors code to new json format
joaovictor3g Apr 15, 2024
9a7b0f3
adds blank function
joaovictor3g Apr 15, 2024
9f88b4f
removes unused import
joaovictor3g Apr 15, 2024
cb91424
mocks results to render accordion
joaovictor3g Apr 16, 2024
c93f926
refactors accordion styles
joaovictor3g Apr 16, 2024
7ca8827
handles accordions overflow
joaovictor3g Apr 16, 2024
0486586
adds license
joaovictor3g Apr 16, 2024
10d7cc3
changes json format
joaovictor3g Apr 17, 2024
76c3a56
handles new json format and apply changes
joaovictor3g Apr 18, 2024
88e5f45
fixes tabs and run function
joaovictor3g Apr 18, 2024
6f7d0fc
fixes tab behavior
joaovictor3g Apr 18, 2024
b8cf0ed
adds blank functionality
joaovictor3g Apr 18, 2024
1b43521
refactors folder structure and adds share function
joaovictor3g Apr 18, 2024
b4d1dc9
fixes share button
joaovictor3g Apr 18, 2024
18e463d
Integrate ValidatingAdmissionPolicy and Webhooks backends with new UI
knrc Oct 3, 2023
d26774e
fixes theme of editors when run button is clicked
joaovictor3g Apr 22, 2024
23041ef
fixes theme in the editors when running
joaovictor3g Apr 22, 2024
1f0e478
fixes copy of each editor
joaovictor3g Apr 22, 2024
23df193
renders accordion properly
joaovictor3g Apr 22, 2024
2be6077
Add cost to k8s and reenable in the UI
knrc Apr 22, 2024
1ef3830
fixes error in the first app load
joaovictor3g Apr 22, 2024
830a61e
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 22, 2024
37beb16
fixes syntax of expression and content of tabs
joaovictor3g Apr 22, 2024
d13689e
adds border when select is open
joaovictor3g Apr 22, 2024
2229de4
Added tests for webhooks
knrc Apr 22, 2024
539ebb0
removes unused classes
joaovictor3g Apr 23, 2024
15fdbd3
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 23, 2024
946e607
changes logic to render accordions
joaovictor3g Apr 23, 2024
c54aa66
fixes error when changing examples
joaovictor3g Apr 23, 2024
6184dde
adds license
joaovictor3g Apr 23, 2024
6bb370a
fixes tabs to hd resolutions
joaovictor3g Apr 24, 2024
4b6a40d
remove unused script
joaovictor3g Apr 24, 2024
72d97fe
adds title in tab button
joaovictor3g Apr 24, 2024
c7086bb
adds text changes when mode changes
joaovictor3g Apr 24, 2024
1acd7ee
changes timeout to hiding modal to 350ms
joaovictor3g Apr 24, 2024
1f4a10e
changes color of copy button and font weight
joaovictor3g Apr 24, 2024
9d64423
hides accordions when an error is given
joaovictor3g Apr 24, 2024
6fa3bab
adds tooltip in tabs
joaovictor3g Apr 24, 2024
40c4c9c
handles exceptions in eval function
joaovictor3g Apr 25, 2024
fc5e56c
Add guard for details
knrc Apr 25, 2024
338273a
update wasm
knrc Apr 25, 2024
d89c419
fixes min width of modes container holder
joaovictor3g Apr 26, 2024
3c53da9
Update error handling to propagate cleanly the nested exception
knrc Apr 26, 2024
69588db
Add isError flag to response values
knrc Apr 26, 2024
680f49d
Update wasm
knrc Apr 26, 2024
573687c
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 26, 2024
1ebd073
omit empty value and isError
knrc Apr 26, 2024
2667428
adds tooltip in error icon
joaovictor3g Apr 26, 2024
4e989c6
fixes tab behavior when changing example
joaovictor3g Apr 26, 2024
c035bc5
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 26, 2024
6fe6ce3
refactors code separating responsibilities
joaovictor3g Apr 29, 2024
74f5ba7
imports mode key from constants
joaovictor3g Apr 29, 2024
0b0b808
uses variable name when it exists
joaovictor3g Apr 29, 2024
f3fc51a
adds 'variables.'
joaovictor3g Apr 29, 2024
170fcba
fixes names in accordion
joaovictor3g Apr 29, 2024
535f0a0
removes console log
joaovictor3g Apr 29, 2024
1ecd6c7
updates json files to keep old url shares working
joaovictor3g Apr 29, 2024
ca2bb66
uses cel-input instead of cel and data-input instead dataInput in cel…
joaovictor3g Apr 29, 2024
56578cf
fixes share functionallity
joaovictor3g Apr 29, 2024
7744cb3
Switch original/updated to old object/object
knrc Apr 29, 2024
2fac7c0
fixes share button
joaovictor3g Apr 29, 2024
8660bb9
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 29, 2024
a349087
fixes index
joaovictor3g Apr 29, 2024
e5469ef
adds only id of mode in localStorage
joaovictor3g Apr 30, 2024
4094bd5
scrolls output to top when running, changing example or changing mode
joaovictor3g Apr 30, 2024
f3a0b62
changes order Object <-> Old Object
joaovictor3g Apr 30, 2024
3a98516
Add isError to expected output
knrc Apr 30, 2024
e5288b0
fixes shared content first load in dark mode
joaovictor3g Apr 30, 2024
408917f
adds license
joaovictor3g Apr 30, 2024
bd80c04
Merge branch 'UD-1369' of github.com:undistro/cel-playground into UD-…
joaovictor3g Apr 30, 2024
7c7a74a
adds line height 0 to close btn in modal
joaovictor3g Apr 30, 2024
4324d95
Updated to k8s 0.30.0 and change vap audit annotation example to show…
knrc May 7, 2024
43531d9
Display annotation message
knrc May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ serve: ## Serve static files.
#go run cmd/server/main.go --dir web/

.PHONY: update-data
update-data: ## Update the web/assets/data.json file.
yq -ojson '.' examples.yaml > web/assets/data.json
yq -ojson -i '.versions.cel-go = "$(CEL_GO_VERSION)"' web/assets/data.json
update-data: ## Update the example files
yq -ojson '.' examples.yaml > web/assets/examples/cel.json
yq -ojson -i '.versions.cel-go = "$(CEL_GO_VERSION)"' web/assets/examples/cel.json
yq -ojson '.' validating_examples.yaml > web/assets/examples/vap.json
yq -ojson -i '.versions.cel-go = "$(CEL_GO_VERSION)"' web/assets/examples/vap.json
yq -ojson '.' webhooks_examples.yaml > web/assets/examples/webhooks.json
yq -ojson -i '.versions.cel-go = "$(CEL_GO_VERSION)"' web/assets/examples/webhooks.json

.PHONY: addlicense
addlicense: ## Add copyright license headers in source code files.
@test -s $(LOCALBIN)/addlicense || GOBIN=$(LOCALBIN) go install github.com/google/addlicense@latest
$(LOCALBIN)/addlicense -c "Undistro Authors" -l "apache" -ignore ".github/**" -ignore ".idea/**" -ignore "web/dist/**" .
$(LOCALBIN)/addlicense -c "Undistro Authors" -l "apache" -ignore ".github/**" -ignore ".idea/**" -ignore "web/dist/**" -ignore "k8s/testdata/**" .

.PHONY: checklicense
checklicense: ## Check copyright license headers in source code files.
@test -s $(LOCALBIN)/addlicense || GOBIN=$(LOCALBIN) go install github.com/google/addlicense@latest
$(LOCALBIN)/addlicense -c "Undistro Authors" -l "apache" -ignore ".github/**" -ignore ".idea/**" -ignore "web/dist/**" -check .
$(LOCALBIN)/addlicense -c "Undistro Authors" -l "apache" -ignore ".github/**" -ignore ".idea/**" -ignore "web/dist/**" -ignore "k8s/testdata/**" -check .

##@ Build

Expand Down
70 changes: 56 additions & 14 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,73 @@ import (
"fmt"
"syscall/js"

"gopkg.in/yaml.v3"

"github.com/undistro/cel-playground/eval"
"github.com/undistro/cel-playground/k8s"
)

type execFunction func(mode string, argMap js.Value) (string, error)

func getArg(value js.Value, name string) []byte {
arg := value.Get(name)
if arg.Type() == js.TypeString {
return []byte(arg.String())
}
return []byte{}
}

var modeExecFns = map[string]execFunction{
"cel": func(mode string, argMap js.Value) (string, error) {
return eval.CelEval(
getArg(argMap, "cel"),
getArg(argMap, "dataInput"),
)
},
"vap": func(mode string, argMap js.Value) (string, error) {
return k8s.EvalValidatingAdmissionPolicy(
getArg(argMap, "vap"),
getArg(argMap, "dataOldObject"),
getArg(argMap, "dataObject"),
getArg(argMap, "dataNamespace"),
getArg(argMap, "dataRequest"),
getArg(argMap, "dataAuthorizer"),
)
},
"webhooks": func(mode string, argMap js.Value) (string, error) {
return k8s.EvalWebhook(
getArg(argMap, "webhooks"),
getArg(argMap, "dataOldObject"),
getArg(argMap, "dataObject"),
getArg(argMap, "dataRequest"),
getArg(argMap, "dataAuthorizer"),
)
},
}

func main() {
evalFunc := js.FuncOf(evalWrapper)
js.Global().Set("eval", evalFunc)
defer evalFunc.Release()
defer addFunction("eval", dynamicEvalWrapper).Release()
<-make(chan bool)
}

// evalWrapper wraps the eval function with `syscall/js` parameters
func evalWrapper(_ js.Value, args []js.Value) any {
func addFunction(name string, fn func(js.Value, []js.Value) any) js.Func {
function := js.FuncOf(fn)
js.Global().Set(name, function)
return function
}

func dynamicEvalWrapper(_ js.Value, args []js.Value) any {
if len(args) < 2 {
return response("", errors.New("invalid arguments"))
}
exp := args[0].String()
is := args[1].String()

var input map[string]any
if err := yaml.Unmarshal([]byte(is), &input); err != nil {
return response("", fmt.Errorf("failed to decode input: %w", err))
if args[0].Type() != js.TypeString || args[1].Type() != js.TypeObject {
return response("", errors.New("invalid argument types, expecting string and object"))
}
mode := args[0].String()
fn, ok := modeExecFns[mode]
if !ok {
return response("", fmt.Errorf("unknown mode %s", mode))
}
output, err := eval.Eval(exp, input)

output, err := fn(mode, args[1])
if err != nil {
return response("", err)
}
Expand Down
9 changes: 9 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/google/cel-go/ext"
"github.com/google/cel-go/interpreter"
"google.golang.org/protobuf/types/known/structpb"
"gopkg.in/yaml.v2"
k8s "k8s.io/apiserver/pkg/cel/library"
)

Expand Down Expand Up @@ -76,6 +77,14 @@ var celProgramOptions = []cel.ProgramOption{
cel.CostTrackerOptions(interpreter.PresenceTestHasCost(false)),
}

func CelEval(exp []byte, input []byte) (string, error) {
var inputMap map[string]any
if err := yaml.Unmarshal(input, &inputMap); err != nil {
return "", fmt.Errorf("failed to decode input: %w", err)
}
return Eval(string(exp), inputMap)
}

// Eval evaluates the cel expression against the given input
func Eval(exp string, input map[string]any) (string, error) {
inputVars := make([]cel.EnvOption, 0, len(input))
Expand Down
28 changes: 14 additions & 14 deletions examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ examples:
|| (account.overdraftProtection
&& account.overdraftLimit >= transaction.withdrawal - account.balance)

data: |
dataInput: |
# Here is the input data in YAML or JSON format.

account:
Expand All @@ -46,7 +46,7 @@ examples:
container.image.startsWith(registry)
)
)
data: |
dataInput: |
params:
allowedRegistries:
- myregistry.com
Expand Down Expand Up @@ -83,7 +83,7 @@ examples:
port.hostPort == 0
)
)
data: |
dataInput: |
object:
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -129,7 +129,7 @@ examples:
!has(container.securityContext) || !has(container.securityContext.runAsNonRoot) || container.securityContext.runAsNonRoot != false
)
)
data: |
dataInput: |
object:
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -175,7 +175,7 @@ examples:
!has(container.securityContext.capabilities.add) ||
container.securityContext.capabilities.add.all(cap, cap in params.allowedCapabilities)
)
data: |
dataInput: |
params:
allowedCapabilities: [NET_BIND_SERVICE]
object:
Expand Down Expand Up @@ -214,7 +214,7 @@ examples:
// the regex above is suggested by semver.org: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
// allowing the "v" prefix
)
data: |
dataInput: |
object:
apiVersion: v1
kind: Pod
Expand Down Expand Up @@ -244,7 +244,7 @@ examples:
&& url(object.href).getPort() == '80'
&& url(object.href).getEscapedPath() == '/path'
&& url(object.href).getQuery().size() == 1
data: |
dataInput: |
{
"object": {
"href": "https://user:[email protected]:80/path?query=val#fragment"
Expand All @@ -266,7 +266,7 @@ examples:
.filter(c, c.startsWith('group'))
.all(c, jwt.extra_claims[c]
.all(g, g.endsWith('@acme.co')))
data: |
dataInput: |
jwt: {
"iss": "auth.acme.com:12350",
"sub": "serviceAccount:[email protected]",
Expand All @@ -286,15 +286,15 @@ examples:

- name: "Optional"
cel: 'object.?foo.orValue("fallback")'
data: "object: {}"
dataInput: "object: {}"
category: "General"

- name: "Duration and timestamp"
cel: |
// Validate that 'expired' date is after a 'created' date plus a 'ttl' duration
has(object.expired) &&
timestamp(object.created) + duration(object.ttl) < timestamp(object.expired)
data: |
dataInput: |
object:
created: "2023-06-14T02:00:14+00:00"
ttl: "5m"
Expand All @@ -310,7 +310,7 @@ examples:
.add(quantity("700M"))
.sub(1) // test without this subtraction
.isLessThan(quantity(object.limit))
data: |
dataInput: |
object:
memory: 1.3G
limit: 2G
Expand All @@ -334,7 +334,7 @@ examples:
// expression: "response.code >= 400 || xds.cluster_name == 'BlackHoleCluster' || xds.cluster_name == 'PassthroughCluster' "

response.code >= 400 || (xds.cluster_name == 'BlackHoleCluster' || xds.cluster_name == 'PassthroughCluster')
data: |
dataInput: |
# The following configuration is true access logs only when the response code is greater or equal to 400
# or the request went to the BlackHoleCluster or the PassthroughCluster
request:
Expand Down Expand Up @@ -404,7 +404,7 @@ examples:
// value: "request.host" # <--- CEL

has(request.host) ? request.host : "unknown"
data: |
dataInput: |
request:
duration: "4.144461ms"
headers:
Expand Down Expand Up @@ -474,5 +474,5 @@ examples:

- name: "Blank"
cel: ""
data: ""
dataInput: ""
category: "Blank"
34 changes: 16 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
module github.com/undistro/cel-playground

go 1.21
go 1.22.0

require (
github.com/google/cel-go v0.17.8
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apiserver v0.28.4
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/apiserver v0.30.0
k8s.io/client-go v0.30.0
)

require (
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -29,27 +33,21 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/apimachinery v0.28.4 // indirect
k8s.io/client-go v0.28.4 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace k8s.io/apiserver v0.28.4 => github.com/kubernetes/kubernetes/staging/src/k8s.io/apiserver v0.0.0-20231128103858-022d50fe3a1b
Loading