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

feat: add miscellaneous policies in CEL expressions - Part 1 #981

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2f5de0f
add argo-cel folder to CI tests
Chandan-DK Apr 30, 2024
77c6b80
copy application-field-validation
Chandan-DK Apr 30, 2024
81511a1
add kyverno tests for application-field-validation
Chandan-DK Apr 30, 2024
addb858
convert application-field-validation
Chandan-DK Apr 30, 2024
74b3763
copy application-prevent-default-project
Chandan-DK Apr 30, 2024
ceac40a
add kyverno tests for application-prevent-default-project
Chandan-DK Apr 30, 2024
93f5ba0
convert application-prevent-default-project
Chandan-DK Apr 30, 2024
92b1a67
copy applicationset-name-matches-project
Chandan-DK May 1, 2024
b6b9e45
convert applicationset-name-matches-project
Chandan-DK May 1, 2024
d36eb82
add kyverno tests for applicationset-name-matches-project
Chandan-DK May 1, 2024
70388fb
copy appproject-clusterresourceblacklist
Chandan-DK May 1, 2024
1e62b60
convert appproject-clusterresourceblacklist
Chandan-DK May 1, 2024
ee63075
copy application-prevent-updates-project
Chandan-DK May 1, 2024
b08976c
convert application-prevent-updates-project
Chandan-DK May 1, 2024
2876f4e
enforce-min-tls-version
Chandan-DK May 2, 2024
fbb5a7c
convert enforce-min-tls-version
Chandan-DK May 2, 2024
840e1b0
add CI test for consul directory
Chandan-DK May 2, 2024
faec413
copy verify-flux-sources
Chandan-DK May 11, 2024
e3119a5
rename chainsaw resources for clarity
Chandan-DK May 11, 2024
c4a8aaa
add kyverno tests for verify-flux-sources
Chandan-DK May 11, 2024
304b7e5
convert verify-flux-sources
Chandan-DK May 11, 2024
05cdded
copy verify-git-repositories
Chandan-DK May 11, 2024
c2a38fc
rename chainsaw resources and add kyverno tests for verify-git-reposi…
Chandan-DK May 11, 2024
832f3ac
convert verify-git-repositories
Chandan-DK May 11, 2024
c693458
add CI test for flux-cel
Chandan-DK May 11, 2024
e4f44d8
rename files for clarity
Chandan-DK May 11, 2024
b5056a8
rename more files for clarity
Chandan-DK May 11, 2024
6a61b3d
copy require-encryption-aws-loadbalancers
Chandan-DK May 11, 2024
47923e5
rename files for clarity
Chandan-DK May 11, 2024
7336149
add kyverno tests for require-encryption-aws-loadbalancers
Chandan-DK May 11, 2024
7c3fab7
convert require-encryption-aws-loadbalancer
Chandan-DK May 11, 2024
a550d59
add CI tests for aws-cel
Chandan-DK May 11, 2024
4e2b7a9
Merge branch 'main' into miscellaneous-policies-cel-part-1
Chandan-DK May 11, 2024
0dbaaed
remove unused file
Chandan-DK May 12, 2024
0dbec06
update policies to specify CREATE and UPDATE operations
Chandan-DK Jul 9, 2024
a0721f6
Merge branch 'main' into miscellaneous-policies-cel-part-1
Chandan-DK Jul 9, 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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ jobs:
version: v1.29.2
tests:
- ^argo$
- ^argo-cel$
- ^aws$
- ^aws-cel$
- ^best-practices$
- ^best-practices-cel$
- ^castai$
- ^cert-manager$
- ^cleanup$
- ^consul$
- ^consul-cel$
- ^external-secret-operator$
- ^flux$
- ^flux-cel$
- ^istio$
- ^karpenter$
- ^kasten$
Expand Down
15 changes: 15 additions & 0 deletions argo-cel/application-field-validation/.chainsaw-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Description

This is an automated test of the sample policy in this directory.

## Expected Behavior

A policy report should be generated in which the following results are observed:

* `badapp01` fails for the rule `source-path-chart` and passes for the rule `destination-server-name`
* `badapp02` fails for the rule `destination-server-name` and passes for the rule `source-path-chart`
* `goodapp01` passes for both rules

## Reference Issue(s)

N/A
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: badapp01
namespace: default
spec:
project: foo
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
chart: foo
destination:
server: https://kubernetes.default.svc
namespace: guestbook
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: badapp02
namespace: default
spec:
project: bar
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
name: foobar
namespace: guestbook
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
creationTimestamp: null
name: application-field-validation
spec:
steps:
- name: step-01
try:
- assert:
file: crd-assert.yaml
- name: step-02
try:
- apply:
file: ../application-field-validation.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: application-field-validation
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-03
try:
- apply:
file: good-application.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: applications.argoproj.io
spec: {}
status:
acceptedNames:
kind: Application
listKind: ApplicationList
plural: applications
singular: application
storedVersions:
- v1alpha1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: goodapp01
namespace: default
spec:
project: biz
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: guestbook
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: application-field-validation
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: application-field-validation
policies:
- ../application-field-validation.yaml
resources:
- ../.chainsaw-test/bad-application.yaml
- ../.chainsaw-test/good-application.yaml
results:
- policy: application-field-validation
rule: source-path-chart
kind: Application
resources:
- badapp01
result: fail
- policy: application-field-validation
rule: destination-server-name
kind: Application
resources:
- badapp02
result: fail
- policy: application-field-validation
rule: source-path-chart
kind: Application
resources:
- goodapp01
result: pass
- policy: application-field-validation
rule: destination-server-name
kind: Application
resources:
- goodapp01
result: pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: application-field-validation
annotations:
policies.kyverno.io/title: Application Field Validation in CEL expressions
policies.kyverno.io/category: Argo in CEL
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Application
kyverno.io/kyverno-version: 1.11.0
policies.kyverno.io/minversion: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/description: >-
This policy performs some best practices validation on Application fields.
Path or chart must be specified but never both. And destination.name or
destination.server must be specified but never both.
spec:
validationFailureAction: Audit
background: true
rules:
- name: source-path-chart
match:
any:
- resources:
kinds:
- Application
operations:
- CREATE
- UPDATE
validate:
cel:
expressions:
- expression: >-
has(object.spec.source) &&
(
(has(object.spec.source.path) && !has(object.spec.source.chart)) ||
(!has(object.spec.source.path) && has(object.spec.source.chart))
)
message: >-
`spec.source.path` OR `spec.source.chart` should be specified but never both.
- name: destination-server-name
match:
any:
- resources:
kinds:
- Application
operations:
- CREATE
- UPDATE
validate:
cel:
expressions:
- expression: >-
has(object.spec.destination) &&
(
(has(object.spec.destination.server) && !has(object.spec.destination.name)) ||
(!has(object.spec.destination.server) && has(object.spec.destination.name))
)
message: >-
`spec.destination.server` OR `spec.destination.name` should be specified but never both.

23 changes: 23 additions & 0 deletions argo-cel/application-field-validation/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: application-field-validation-cel
version: 1.0.0
displayName: Application Field Validation in CEL expressions
description: >-
This policy performs some best practices validation on Application fields. Path or chart must be specified but never both. And destination.name or destination.server must be specified but never both.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/argo-cel/application-field-validation/application-field-validation.yaml
```
keywords:
- kyverno
- Argo
- CEL Expressions
readme: |
This policy performs some best practices validation on Application fields. Path or chart must be specified but never both. And destination.name or destination.server must be specified but never both.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Argo in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Application"
digest: e3256994e09731ea081230c85e0d6384a187d53377562b1c82ea9385cec0c4a4
createdAt: "2024-04-30T15:34:03Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: badapp
namespace: default
spec:
project: default
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: guestbook
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
creationTimestamp: null
name: application-prevent-default-project
spec:
steps:
- name: step-01
try:
- assert:
file: crd-assert.yaml
- name: step-02
try:
- apply:
file: ../application-prevent-default-project.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: application-prevent-default-project
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-03
try:
- apply:
file: good-application.yaml
- apply:
expect:
- check:
($error != null): true
file: bad-application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: applications.argoproj.io
spec: {}
status:
acceptedNames:
kind: Application
listKind: ApplicationList
plural: applications
singular: application
storedVersions:
- v1alpha1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: goodapp
namespace: default
spec:
project: biz
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: guestbook
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: application-prevent-default-project
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: application-prevent-default-project
policies:
- ../application-prevent-default-project.yaml
resources:
- ../.chainsaw-test/bad-application.yaml
- ../.chainsaw-test/good-application.yaml
results:
- policy: application-prevent-default-project
rule: default-project
kind: Application
resources:
- badapp
result: fail
- policy: application-prevent-default-project
rule: default-project
kind: Application
resources:
- goodapp
result: pass

Loading
Loading