Giter VIP home page Giter VIP logo

weave-policy-validator's Introduction

codecov

Weaveworks Infrastructure as Code Validator

Validates infrastucture as code against weave policies

Supported Resources

  • Helm
  • Kustomize

Supported CI/CD

Usage

USAGE:
   app [global options] command [command options] [arguments...]

VERSION:
   0.0.1

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --path value                       path to scan resources from
   --helm-values-file value           path to resources helm values file
   --policies-path value              path to policies source directory
   --policies-helm-values-file value  path to policies helm values file
   --git-repo-provider value          git repository provider [$WEAVE_REPO_PROVIDER]
   --git-repo-host value              git repository host [$WEAVE_REPO_HOST]
   --git-repo-url value               git repository url [$WEAVE_REPO_URL]
   --git-repo-branch value            git repository branch [$WEAVE_REPO_BRANCH]
   --git-repo-sha value               git repository commit sha [$WEAVE_REPO_SHA]
   --git-repo-token value             git repository token [$WEAVE_REPO_TOKEN]
   --azure-project value              azure project name [$AZURE_PROJECT]
   --sast value                       save result as gitlab sast format
   --sarif value                      save result as sarif format
   --json value                       save result as json format
   --generate-git-report              generate git report if supported (default: false) [$WEAVE_GENERATE_GIT_PROVIDER_REPORT]
   --remediate                        auto remediate resources if possible (default: false)
   --no-exit-error                    exit with no error (default: false)
   --help, -h                         show help (default: false)
   --version, -v                      print the version (default: false)

Examples

Github

See how to setup the Github Action

Gitlab

weave:
  image:
    name: weaveworks/weave-policy-validator:v1.4
  script:
  - weave-validator --path <path to resources> --policies-path <path to policies>

Enable Auto Remediation

  script:
  - weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token $GITLAB_TOKEN --remediate

Enable Static Application Security Testing

stages:
  - weave
  - sast

weave:
  stage: weave
  image:
    name: weaveworks/weave-policy-validator:v1.4
  script:
  - weave-validator <path to resources> --policies-path <path to policies> --sast sast.json
  artifacts:
    when: on_failure
    paths:
    - sast.json

upload_sast:
  stage: sast
  when: always
  script:
  - echo "creating sast report"
  artifacts:
    reports:
      sast: sast.json

Bitbucket

pipelines:
  default:
    - step:
        name: 'Weaveworks'
        image: weaveworks/weave-policy-validator:v1.4
        script:
          - weave-validator --path <path to resources> --policies-path <path to policies>

Enable Auto Remediation

  script:
    - weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token $TOKEN --remediate

Create Pipeline Report

  script:
    - weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token $TOKEN -generate-git-report

Circle CI

jobs:
  weave:
    docker:
    - image: weaveworks/weave-policy-validator:v1.4
    steps:
    - checkout
    - run:
        command: weave-validator --path <path to resources> --policies-path <path to policies>

Enable Auto Remediation

    - run:
        command: weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token ${GITHUB_TOKEN} --remediate

Azure DevOps

trigger:
- <list of branches to trigger the pipeline on>

pool:
  vmImage: ubuntu-latest

container:
  image: weaveworks/weave-policy-validator:v1.4-azure

steps:
- script: weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token $(TOKEN)

Enable Auto Remediation

steps:
- script: weave-validator --path <path to resources> --policies-path <path to policies> --git-repo-token $(TOKEN) --remediate

Contribution

Need help or want to contribute? Please see the links below.

weave-policy-validator's People

Contributors

waleedhammam avatar ahsayde avatar nalum avatar serboctor avatar heba0 avatar

Stargazers

Koichi Shiraishi avatar

Watchers

Ahmed Faris avatar Mostafa Megahid avatar Ahmad Samir avatar Ahmed Magdy  avatar  avatar

weave-policy-validator's Issues

using absolute entity path could lead github action to fail in auto-remediate

Describe the bug

In auto-remediate a PR will be created with the remediated files, and creating commit would require create a git tree

The problem tree.path mustn't start with / hence it will fail to create a tree nor a commit neither the PR

Environment

  • Validator v1.3, failure occurred after this fix

To Reproduce

  • create a weave action in your repository and try to validate any resources with auto remediate on

Expected behavior

  • A PR should be created after the checks for violations are complete with the remediated files

Actual Behavior

  • The checks for violations are complete but PR fails to create

Additional Context (screenshots, logs, etc)

Log of fail

2023/05/31 20:12:24 failed to create tree, error: POST https://api.github.com/repos/waleedhammam/test-validator/git/trees: 422 tree.path cannot start with a slash []

Riot Games | Transform OPA GateKeeper policy to Weave Policy CR

Background

RiotGames has OPA Gatekeeper installed on their clusters with roughly 15 policies enforced. They are trying to enforce the same set of policies in their CI. Initially, the plan was to use checkov to replicate the policies in the CI, but since the policies were already written in Rego, so we are looking for using our own weave-iac-validator to enforce the policies.

The current plan is to have an end-to-end flow for one policy before we transform all policies to weave policy.

Note: RiotGames is using GitHub Enterprise, so we will have to test things out in the customer environment to make sure the flow works there. Luke Mallon is our person there and will be helping us out through this experiment.

Objective

For this ticket, we are looking to transform the following policy: allow-privilege-escalation-container. to our weave policy CR. Below is template.yaml and constraint.yaml for their policy.

Template.yaml

apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8spspallowprivilegeescalationcontainer
  annotations:
    description: >-
      Controls restricting escalation to root privileges. Corresponds to the
      `allowPrivilegeEscalation` field in a PodSecurityPolicy. For more
      information, see
      https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privilege-escalation
spec:
  crd:
    spec:
      names:
        kind: K8sPSPAllowPrivilegeEscalationContainer
      validation:
        openAPIV3Schema:
          type: object
          description: >-
            Controls restricting escalation to root privileges. Corresponds to the
            `allowPrivilegeEscalation` field in a PodSecurityPolicy. For more
            information, see
            https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privilege-escalation
          properties:
            exemptImages:
              description: >-
                Any container that uses an image that matches an entry in this list will be excluded
                from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`.

                It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name)
                in order to avoid unexpectedly exempting images from an untrusted repository.
              type: array
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8spspallowprivilegeescalationcontainer

        import data.lib.exempt_container.is_exempt

        violation[{"msg": msg, "details": {}}] {
            c := input_containers[_]
            not is_exempt(c)
            input_allow_privilege_escalation(c)
            msg := sprintf("Privilege escalation container is not allowed: %v", [c.name])
        }

        input_allow_privilege_escalation(c) {
            not has_field(c, "securityContext")
        }
        input_allow_privilege_escalation(c) {
            not c.securityContext.allowPrivilegeEscalation == false
        }
        input_containers[c] {
            c := input.review.object.spec.containers[_]
        }
        input_containers[c] {
            c := input.review.object.spec.initContainers[_]
        }
        # has_field returns whether an object has a field
        has_field(object, field) = true {
            object[field]
        }
      libs:
        - |
          package lib.exempt_container

          is_exempt(container) {
              exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", [])
              img := container.image
              exemption := exempt_images[_]
              _matches_exemption(img, exemption)
          }

          _matches_exemption(img, exemption) {
              not endswith(exemption, "*")
              exemption == img
          }

          _matches_exemption(img, exemption) {
              endswith(exemption, "*")
              prefix := trim_suffix(exemption, "*")
              startswith(img, prefix)
          }

Constraint.yaml

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowPrivilegeEscalationContainer
metadata:
  name: psp-allow-privilege-escalation-container
spec:
  enforcementAction: warn
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
    excludedNamespaces: ["kube-system"]

Note: This is probably the same stock policy found in OPA gatekeeper policy library here: https://open-policy-agent.github.io/gatekeeper-library/website/allow-privilege-escalation

Enable Auto-remediation on Azure DevOps

We need to enable auto-remediation functionality on Azure DevOps.

Note: We need to ask corporate-it for the best way to acquire Azure DevOps account to be able to test it out.

Support RiotGames end-to-end policy scenario

In our efforts to support RiotGames end-to-end policy scenario, we might need to change how the validator work. This is ticket to track changes needed to get in place for it:

  • Handle the recursive paths in helm / kustomize / resources directories.
  • Fix handling helm charts by ignoring non-yaml files.
  • Ignore hidden files.

use file based configuration to parameterise testing

Problem
I need to be able to configure policysets to be applied to numerous different building within a git repository. Each cluster has multiple varying components/applications and it would be ideal to apply on the specific policies to those clusters and applications in order to not fail them for policies which do not apply to their specifics.

Solution

In a git repository today I can only specify a single path to an to the iac-validator, however this means that If I need to target more than one directory I need to devise a loop or I need to consume more than I would like by referencing the parent directory, returning me the results of data I didn't want to test.

Ideally I would have the ability to both specify:

  • ClI commands for --included-paths and --excluded paths so I can refine what I target in testing

  • A .weaveignore or a validator.yaml that would automatically be read form the root of the directory or via --config flags so that I can predefine what tests each cluster build runs in a manifest.

Additional context

This is being used both locally and in CI, though it is wrapped in some logic so that I can get both the results of each application that I test and also the the clusters total violation count (all applications together). I might follow up with some more details if they are needed

Create end-to-end Policy demo docs

Create docs to be used to demo Policy capabilities end-to-end. We want to showcase how it will be used in commit-time validations and how the user can benefit from auto-remediation. And how the agent can block the same violating deployment in runtime.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.