1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

feat: support wildcard in subjects statements (#8068)

* feat: support wildcard in subjects statements

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* kuttl tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* sa tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-08-21 02:46:42 +02:00 committed by GitHub
parent d7771cb835
commit ce4beb0e92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
74 changed files with 765 additions and 3 deletions

View file

@ -60,6 +60,7 @@ jobs:
- deferred - deferred
- events - events
- exceptions - exceptions
- filter
- generate/clusterpolicy - generate/clusterpolicy
- generate/policy - generate/policy
- generate/validation - generate/validation

View file

@ -1,6 +1,7 @@
package match package match
import ( import (
"github.com/kyverno/kyverno/pkg/utils/wildcard"
authenticationv1 "k8s.io/api/authentication/v1" authenticationv1 "k8s.io/api/authentication/v1"
rbacv1 "k8s.io/api/rbac/v1" rbacv1 "k8s.io/api/rbac/v1"
) )
@ -14,17 +15,17 @@ func CheckSubjects(
switch subject.Kind { switch subject.Kind {
case rbacv1.ServiceAccountKind: case rbacv1.ServiceAccountKind:
username := "system:serviceaccount:" + subject.Namespace + ":" + subject.Name username := "system:serviceaccount:" + subject.Namespace + ":" + subject.Name
if userInfo.Username == username { if wildcard.Match(username, userInfo.Username) {
return true return true
} }
case rbacv1.GroupKind: case rbacv1.GroupKind:
for _, group := range userInfo.Groups { for _, group := range userInfo.Groups {
if group == subject.Name { if wildcard.Match(subject.Name, group) {
return true return true
} }
} }
case rbacv1.UserKind: case rbacv1.UserKind:
if userInfo.Username == subject.Name { if wildcard.Match(subject.Name, userInfo.Username) {
return true return true
} }
} }

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding service account `system:serviceaccount:kyverno:kyverno`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,22 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: ServiceAccount
name: kyverno
namespace: kyverno
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding service account `system:serviceaccount:?*:?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,22 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: ServiceAccount
name: '?*'
namespace: '?*'
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding users `not-kubernetes-admin`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: User
name: not-kubernetes-admin
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding users `kubernetes-admin`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: User
name: kubernetes-admin
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding users with wildcard `not-?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: User
name: not-?*
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, excluding users with wildcard `?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- subjects:
- kind: User
name: '?*'
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching service account `system:serviceaccount:kyverno:kyverno`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,20 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: ServiceAccount
name: kyverno
namespace: kyverno
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching service account `system:serviceaccount:?*:?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,20 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: ServiceAccount
name: '?*'
namespace: '?*'
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching users `kubernetes-admin`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: User
name: kubernetes-admin
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching users `not-kubernetes-admin`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: User
name: not-kubernetes-admin
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: true

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching users with wildcard `?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be denied (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: User
name: '?*'
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,5 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: resource.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
## Description
This test creates a policy, matching users with wildcard `not-?*`.
This policy denies pod creation.
## Expected Behavior
The pod should be accepted (user is `kubernetes-admin`).
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7938

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec: {}
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: block-pod
spec:
validationFailureAction: Enforce
background: false
rules:
- name: block-pod
match:
any:
- resources:
kinds:
- Pod
subjects:
- kind: User
name: not-?*
validate:
deny: {}

View file

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: container
image: nginx:latest
ports:
- containerPort: 80