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

fix: update chainsaw test for validate-ecs-container-insights-enabled #154

Merged

Conversation

Chandan-DK
Copy link
Contributor

Description:
kyverno-json earlier used to report a FieldValueInvalid error when it could not find a field in the json payload. Now it reports FieldValueRequired error which is more comprehensible.

Example:

check-awsvpc-network-mode.yaml

apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
  name: check-awsvpc-network-mode
  labels:
    ecs.aws.network.kyverno.io: awsvpc
  annotations:
    policies.kyverno.io/title: Check awsvpc network mode
    policies.kyverno.io/category: ECS Best Practices
    policies.kyverno.io/severity: medium
    policies.kyverno.io/description: >-
      The awsvpc network mode restricts the flow of traffic between different 
      tasks or between your tasks and other services that run within your Amazon VPC.
      The awsvpc network mode provides task-level network isolation for tasks 
      that run on Amazon EC2.
spec:
  rules:
    - name: check-awsvpc-network-mode
      match:
        all:
        # - ($analyzer.resource.type): terraform-config
        - (resource.aws_ecs_task_definition != null): true
      assert:
        all:
        - message: ECS services and tasks are required to use awsvpc network mode.
          check:
            ~.(resource.aws_ecs_task_definition.values(@)[]):
                network_mode: awsvpc

payload.yaml

Here, the field that we are looking for network_mode is absent.

{
    "provider": {
        "aws": [
            {
                "region": "us-west-1"
            }
        ],
        "docker": [
            {}
        ]
    },
    "resource": {
        "aws_ecs_task_definition": {
            "task": [
                {
                    "container_definitions": "  [\n    {\n      \"name\"      : \"foo-task\",\n      \"image\"     : \"nginx:1.23.1\",\n      \"cpu\"       : 512,\n      \"memory\"    : 2048,\n      \"essential\" : true,\n      \"portMappings\" : [\n        {\n          \"containerPort\" : 80,\n          \"hostPort\"      : 80\n        }\n      ]\n    }\n  ]\n",
                    "cpu": 512,
                    "family": "service",
                    "memory": 2048,
                    "requires_compatibilities": [
                        "EC2"
                    ]
                }
            ]
        }
    },
    "terraform": [
        {
            "required_providers": [
                {
                    "aws": {
                        "source": "hashicorp/aws",
                        "version": "~\u003e 4.0"
                    },
                    "docker": {
                        "source": "kreuzwerker/docker",
                        "version": "~\u003e2.20.0"
                    }
                }
            ]
        }
    ]
}

Run: kyverno-json scan --payload payload.yaml --policy check-awsvpc-network-mode.yaml --output json

In the output's errors, we get this:

"violations": [
              {
                "message": "ECS services and tasks are required to use awsvpc network mode.",
                "errors": [
                  {
                    "type": "FieldValueRequired",
                    "field": "all[0].check.~.(resource.aws_ecs_task_definition.values(@)[])[0].network_mode",
                    "value": "",
                    "detail": "field not found in the input object"
                  }
                ]
              }
            ]

Before the changes made to kyverno-json it used to give this output:

"violations": [
              {
                "message": "ECS services and tasks are required to use awsvpc network mode.",
                "errors": [
                  {
                    "type": "FieldValueInvalid",
                    "field": "all[0].check.~.(resource.aws_ecs_task_definition.values(@)[])[0].network_mode",
                    "detail": "Expected value: \"awsvpc\""
                  }
                ]
              }
            ]

Related Issues:

Checklist:

  • This PR requires a bump in kyverno-policies chart version .
  • I have created a PR to bump the enterprise-kyverno-operator chart version.

@anusha94 anusha94 merged commit ca6ddae into main Jul 31, 2024
19 checks passed
@Chandan-DK Chandan-DK deleted the fix-validate-ecs-container-insights-enabled-chainsaw-test branch July 31, 2024 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants