Repository: datreeio/datree
Pull request #957 (2023-07-11 12:33:57)

fix: update name rule logic to ignore incorrect kinds


Patch:
diff --git a/pkg/defaultRules/defaultRules.yaml b/pkg/defaultRules/defaultRules.yaml
index 3efb5be..63b9df8 100644
--- a/pkg/defaultRules/defaultRules.yaml
+++ b/pkg/defaultRules/defaultRules.yaml
@@ -1861,28 +1861,48 @@ rules:
     impact: Configurations with a missing or invalid name will pass k8s schema validation, but will fail when pushed into a cluster (i.e. when running kubectl apply/create)
     schema:
       definitions:
-        metadataNamePattern:
-          properties:
-            metadata:
-              type: object
-              properties:
-                name:
-                  type: string
-                  format: hostname
-              required:
-                - name
-          required:
-            - metadata
-        metadataGenerateNamePattern:
+        strictNamesPattern:
+          if:
+            properties:
+              kind:
+                not:
+                  enum:
+                    - ClusterRole
+                    - ClusterRoleBinding
+          then:
+            properties:
+              metadata:
+                type: object
+                anyOf:
+                  - properties:
+                      name:
+                        type: string
+                        format: hostname
+                    required:
+                      - name
+                  - properties:
+                      generateName:
+                        type: string
+                        format: hostname
+                    required:
+                      - generateName
+            required:
+              - metadata
+        nonStrictNamesPattern:
           properties:
             metadata:
               type: object
-              properties:
-                generateName:
-                  type: string
-                  format: hostname
-              required:
-                - generateName
+              anyOf:
+                - properties:
+                    name:
+                      type: string
+                  required:
+                    - name
+                - properties:
+                    generateName:
+                      type: string
+                  required:
+                    - generateName
           required:
             - metadata
       if:
@@ -1892,9 +1912,9 @@ rules:
               enum:
                 - Kustomization
       then:
-        anyOf:
-          - $ref: "#/definitions/metadataNamePattern"
-          - $ref: "#/definitions/metadataGenerateNamePattern"
+        allOf:
+          - $ref: "#/definitions/strictNamesPattern"
+          - $ref: "#/definitions/nonStrictNamesPattern"
   - id: 55
     name: Ensure each container probe has an initial delay configured
     uniqueName: CONTAINERS_INCORRECT_INITIALDELAYSECONDS_VALUE


Test patch:
diff --git a/pkg/policy/tests/54/fail/54-fail-2.yaml b/pkg/policy/tests/54/fail/54-fail-2.yaml
new file mode 100644
index 0000000..988f9d8
--- /dev/null
+++ b/pkg/policy/tests/54/fail/54-fail-2.yaml
@@ -0,0 +1,20 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: invalid:format
+spec:
+  selector:
+  matchLabels:
+    app: frontend-deployment
+  template:
+  metadata:
+    labels:
+      app: frontend-deployment
+  spec:
+    containers:
+      - name: app
+        image: foo/bar:latest
+        ports:
+          - name: http
+            containerPort: 8080
+            protocol: TCP
diff --git a/pkg/policy/tests/54/pass/54-pass-2.yaml b/pkg/policy/tests/54/pass/54-pass-2.yaml
new file mode 100644
index 0000000..c649cf3
--- /dev/null
+++ b/pkg/policy/tests/54/pass/54-pass-2.yaml
@@ -0,0 +1,12 @@
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+  # "namespace" omitted since ClusterRoles are not namespaced
+  name: system:secret-reader
+rules:
+- apiGroups: [""]
+  #
+  # at the HTTP level, the name of the resource for accessing Secret
+  # objects is "secrets"
+  resources: ["secrets"]
+  verbs: ["get", "watch", "list"]
diff --git a/pkg/policy/tests/54/pass/54-pass-3.yaml b/pkg/policy/tests/54/pass/54-pass-3.yaml
new file mode 100644
index 0000000..cdcb8f5
--- /dev/null
+++ b/pkg/policy/tests/54/pass/54-pass-3.yaml
@@ -0,0 +1,6 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+resources:
+  - service.yaml
+  - deployment.yaml
+  - hpa.yaml
diff --git a/pkg/policy/tests/54/pass/54-pass-4.yaml b/pkg/policy/tests/54/pass/54-pass-4.yaml
new file mode 100644
index 0000000..69aac75
--- /dev/null
+++ b/pkg/policy/tests/54/pass/54-pass-4.yaml
@@ -0,0 +1,12 @@
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+  # "namespace" omitted since ClusterRoles are not namespaced
+  name: valid:name
+rules:
+- apiGroups: [""]
+  #
+  # at the HTTP level, the name of the resource for accessing Secret
+  # objects is "secrets"
+  resources: ["secrets"]
+  verbs: ["get", "watch", "list"]
\ No newline at end of file