Skip to content

Commit

Permalink
Merge pull request #125 from epasham/main
Browse files Browse the repository at this point in the history
added policy library and included sample policies
  • Loading branch information
FabianKramm authored Jun 7, 2024
2 parents 5b3b6d2 + 71eceee commit d524b26
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions library/general/allowedrepos/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: policy.jspolicy.com/v1beta1
kind: JsPolicy
metadata:
name: "allow-trusted-registries.infy.com"
spec:
type: Validating
operations: ["CREATE", "UPDATE"]
resources: ["pods", "deployments", "daemonsets", "statefulsets"]
javascript: |
const registries = ["registry.k8s.io", "gcr.io"]
// Use template.spec if defined (for Deployments and StatefulSets), or use spec otherwise (for Pods)
podSpec = request.object?.spec?.template?.spec || request.object?.spec
podSpec?.containers?.forEach(function(container, index) {
if (!registries.includes(container.image.split('/')[0])) {
deny("Field spec.containers[" + index + "].image must be pulled from " + registries.toString())
}
})
podSpec?.initContainers?.forEach(function(initContainer, index) {
let imageRegistry = initContainer.image.split('/')[0]
if (!registries.includes(initContainer.image.split('/')[0])) {
errors.push("Field spec.initContainers[" + index + "].image must match regex: " + registries.toString())
}
})
12 changes: 12 additions & 0 deletions library/general/block-loadbalancer-services/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: policy.jspolicy.com/v1beta1
kind: JsPolicy
metadata:
name: "deny-service-type-loadbalancer.example.com"
spec:
operations: ["CREATE", "UPDATE"]
resources: ["services"]
javascript: |
// This policy blocks Service of type LoadBalancer
if (request.object.spec.type === "LoadBalancer") {
deny(`${request.name} => service type ${request.object.spec.type} is not allowed!`);
}
12 changes: 12 additions & 0 deletions library/general/block-nodeport-services/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: policy.jspolicy.com/v1beta1
kind: JsPolicy
metadata:
name: "deny-service-type-nodeport.infy.com"
spec:
operations: ["CREATE", "UPDATE"]
resources: ["services"]
javascript: |
// This policy blocks Service of type NodePort
if (request.object.spec.type === "NodePort") {
deny(`${request.name} => service type ${request.object.spec.type} is not allowed!`);
}

0 comments on commit d524b26

Please sign in to comment.