Git Product home page Git Product logo

raspbernetes / k8s-security-policies Goto Github PK

View Code? Open in Web Editor NEW
173.0 15.0 23.0 63 KB

This repository offers a comprehensive library of security policies designed to enhance the security of Kubernetes cluster configurations. The policies are developed in accordance with the CIS Kubernetes benchmark.

Home Page: https://raspbernetes.github.io/

License: Apache License 2.0

Open Policy Agent 100.00%
raspbernetes security conftest open-policy-agent cis kubernetes rego-policy rego-files gatekeeper benchmark

k8s-security-policies's Introduction

Kubernetes Security Policies

A Comprehensive Library for Fortifying Cluster Configurations.

Introduction

This repository offers a comprehensive library of security policies designed to fortify Kubernetes cluster configurations. These security policies are derived from the CIS Kubernetes benchmark and rules specified by Kubesec.io. The policies are crafted in REGO, a high-level declarative language tailored for defining policies over intricate hierarchical data structures. For an in-depth understanding of REGO, refer to the Policy Language documentation.

Why use Kubernetes Security Policies?

Kubernetes Security Policies are crucial for:

  1. Enhancing Security: They protect clusters against threats, misconfigurations, and vulnerabilities by applying predefined rules and best practices.

  2. Ensuring Consistency: Policies enforce standardized configurations across clusters, simplifying management and reducing configuration drift.

  3. Maintaining Compliance: By implementing established benchmarks like CIS Kubernetes, policies help adhere to industry-specific regulatory requirements.

  4. Automating Enforcement: Policies automate the enforcement of security best practices, reducing human error and streamlining deployments.

  5. Simplifying Auditing: They provide a clear approach to defining and enforcing configurations, enabling easier auditing and monitoring of clusters.

Kubernetes Security Policies are essential for a robust security strategy and promoting a proactive security culture within organizations.

Repository Structure

The policies directory houses numerous folders, each representing a distinct policy. Folders are named according to the benchmark standard, followed by a number to ensure differentiation. For instance, the 1.2.1 Ensure that the --anonymous-auth argument is set to false policy from the CIS benchmark is verified by REGO files located in the CIS1.2.1 directory. To explore each policy further, consult the Policy Inventory.

Within each folder, you will find a .rego file and a unit test .rego file. Additionally, there is a separate lib folder containing two .rego files that feature general functions importable into other .rego files. The kubernetes.rego file encompasses functions and rules employed in security control .rego files, while the test.rego file houses functions that can be imported into unit test files.

Policy Implementation Process

Identifying Violations

Each REGO policy examines the manifests of resources deployable in a Kubernetes cluster. The violation block in each policy file typically includes:

  • Identifying the resource object being examined and returning it.
  • Verifying if the object breaches the controls defined in the benchmark.
  • Displaying an error message if a violation is detected.

Here's a simplified example of a violation:

violation[msg] {
    kubernetes.pods[pod]
    not kubernetes.contains_element(params.allowedDeploymentsOrPods, kubernetes.name)
    is_automount_serviceaccount_token_enabled(pod)
    msg := kubernetes.format(sprintf("%v: %v - Automount Service account token must be set to false", [kubernetes.kind, kubernetes.name]))
}

The kubernetes. prefix indicates a function call from kubernetes.rego, which contains general functions. Control-specific logic is written in the corresponding REGO file.

Configuring Parameters

Security policies use parameters to evaluate resources. These parameters can be external (e.g., defined in Gatekeeper constraints) or embedded as default values within the file. Parameters are standardized by combining them:

params = object.union(default_parameters, kubernetes.parameters)

Testing Policies

To validate policy accuracy, the OPA testing framework is used. Each unit test includes functions prefixed with test in the name, enabling opa test to recognize them. Positive cases start with test_no_violation and negative cases with test_violation. Tests use policy_input formatted like the manifests:

test_violation {
    test.violations(violation) with input as policy_input(true)
}

test_no_violation {
    test.no_violations(violation) with input as policy_input(false)
}

Unit test files share the same package as the REGO policy, ensuring the test verifies policy input against the violation block within the same package.

The opa test output displays whether all tests passed:

$ opa test policies -v
data.spec_volumes_hostpath_path_var_run_docker_sock.test_no_violation: PASS (318.786µs)
data.containers_image_tag.test_violation_1: PASS (512.689µs)
data.containers_image_tag.test_violation_2: PASS (515.964µs)
data.containers_image_tag.test_no_violation: PASS (389.822µs)
data.containers_securitycontext_allowprivilegedeescalation_true.test_violation: PASS (474.668µs)
data.containers_securitycontext_allowprivilegedeescalation_true.test_no_violation: PASS (361.12µs)
--------------------------------------------------------------------------------
PASS: 5/5

Integration with Policy Enforcement Tools

This policy library is designed to be compatible with various tools, such as Conftest and Gatekeeper.

Gatekeeper

Rego policies can serve as a schema in Gatekeeper ConstraintTemplate. Based on the template, constraints can be deployed onto Kubernetes clusters to monitor CREATE and UPDATE operations against the API server. Gatekeeper also offers AUDIT functionality to evaluate existing resources against the constraints. To learn more about creating constraints, visit the Gatekeeper repository.

Conftest

Conftest enables the evaluation of structured YAML manifest files intended for deployment to your clusters. To use Conftest, first install Conftest and then run conftest test on a YAML file against the policy library:

$ conftest test deployment.yaml -p policies/. --all-namespaces

The output indicates whether the tests have passed. Here's an example:

FAIL - deployment.yaml - Containers must not run as root
FAIL - deployment.yaml - Deployments are not allowed

2 tests, 0 passed, 0 warnings, 2 failure

k8s-security-policies's People

Contributors

fossabot avatar hsy3418 avatar renovate[bot] avatar saurabhpandit avatar xunholy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

k8s-security-policies's Issues

CIS Kubernetes Benchmark 1.5.1 # 5.2

5.2 Pod Security Policies

  • 5.2.1 Minimize the admission of privileged containers
  • 5.2.2 Minimize the admission of containers wishing to share the host process ID namespace
  • 5.2.3 Minimize the admission of containers wishing to share the host IPC namespace
  • 5.2.4 Minimize the admission of containers wishing to share the host network namespace
  • 5.2.5 Minimize the admission of containers with allowPrivilegeEscalation
  • 5.2.6 Minimize the admission of root containers
  • 5.2.7 Minimize the admission of containers with the NET_RAW capability
  • 5.2.8 Minimize the admission of containers with added capabilities
  • 5.2.9 Minimize the admission of containers with capabilities assigned

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v2
  • actions/github-script 0.9.0

  • Check this box to trigger a request for Renovate to run again on this repository

Implement K8s CIS benchmarks using kyverno

Details

As the policy engine eco-system continues to expand it's important to be able to have policies more suited in other engines available for consumption depending on the organizational decision on what tool suits their use case. Kyverno is new to the CNCF landscape and is a good candidate to present in this repository

https://github.com/kyverno/kyverno

CIS Kubernetes Benchmark 1.5.1 # 5.3

5.3 Network Policies and CNI

  • 5.3.1 Ensure that the CNI in use supports Network Policies
  • 5.3.2 Ensure that all Namespaces have Network Policies defined

CIS Kubernetes Benchmark 1.5.1 # 5.7

5.7 General Policies

  • 5.7.1 Create administrative boundaries between resources using namespaces
  • 5.7.2 Ensure that the seccomp profile is set to docker/default in your pod definitions
  • 5.7.3 Apply Security Context to Your Pods and Containers
  • 5.7.4 The default namespace should not be used

CIS Kubernetes Benchmark v1.5.1 # 1.2/1.3/1.4

Details

1 Control Plane Components

1.2 API Server

Checklist

  • 1.2.1 Ensure that the --anonymous-auth argument is set to false
  • 1.2.2 Ensure that the --basic-auth-file argument is not set
  • 1.2.3 Ensure that the --token-auth-file parameter is not set
  • 1.2.4 Ensure that the --kubelet-https argument is set to true
  • 1.2.5 Ensure that the --kubelet-client-certificate and --kubelet- client-key arguments are set as appropriate
  • 1.2.6 Ensure that the --kubelet-certificate-authority argument is set as appropriate
  • 1.2.7 Ensure that the --authorization-mode argument is not set to AlwaysAllow
  • 1.2.8 Ensure that the --authorization-mode argument includes Node
  • 1.2.9 Ensure that the --authorization-mode argument includes RBAC
  • 1.2.10 Ensure that the admission control plugin EventRateLimit is set
  • 1.2.11 Ensure that the admission control plugin AlwaysAdmit is not set
  • 1.2.12 Ensure that the admission control plugin AlwaysPullImages is set
  • 1.2.13 Ensure that the admission control plugin SecurityContextDeny is set if PodSecurityPolicy is not used
  • 1.2.14 Ensure that the admission control plugin ServiceAccount is set
  • 1.2.15 Ensure that the admission control plugin NamespaceLifecycle is set
  • 1.2.16 Ensure that the admission control plugin PodSecurityPolicy is set
  • 1.2.17 Ensure that the admission control plugin NodeRestriction is set
  • 1.2.18 Ensure that the --insecure-bind-address argument is not set
  • 1.2.19 Ensure that the --insecure-port argument is set to 0

1.3 Controller Manager

  • 1.3.1 Ensure that the --terminated-pod-gc-threshold argument is set as appropriate
  • 1.3.2 Ensure that the --profiling argument is set to false
  • 1.3.3 Ensure that the --use-service-account-credentials argument is set to true
  • 1.3.4 Ensure that the --service-account-private-key-file argument is set as appropriate
  • 1.3.5 Ensure that the --root-ca-file argument is set as appropriate
  • 1.3.6 Ensure that the RotateKubeletServerCertificate argument is set to true
  • 1.3.7 Ensure that the --bind-address argument is set to 127.0.0.1  

1.4 Scheduler

  • 1.4.1 Ensure that the --profiling argument is set to false
  • 1.4.2 Ensure that the --bind-address argument is set to 127.0.0.1

CIS Kubernetes Benchmark v1.5.1 # 1.2/1.3/1.4

Details

1 Control Plane Components

1.2 API Server

Checklist

  • 1.2.1 Ensure that the --anonymous-auth argument is set to false
  • 1.2.2 Ensure that the --basic-auth-file argument is not set
  • 1.2.3 Ensure that the --token-auth-file parameter is not set
  • 1.2.4 Ensure that the --kubelet-https argument is set to true
  • 1.2.5 Ensure that the --kubelet-client-certificate and --kubelet- client-key arguments are set as appropriate
  • 1.2.6 Ensure that the --kubelet-certificate-authority argument is set as appropriate
  • 1.2.7 Ensure that the --authorization-mode argument is not set to AlwaysAllow
  • 1.2.8 Ensure that the --authorization-mode argument includes Node
  • 1.2.9 Ensure that the --authorization-mode argument includes RBAC
  • 1.2.10 Ensure that the admission control plugin EventRateLimit is set
  • 1.2.11 Ensure that the admission control plugin AlwaysAdmit is not set
  • 1.2.12 Ensure that the admission control plugin AlwaysPullImages is set
  • 1.2.13 Ensure that the admission control plugin SecurityContextDeny is set if PodSecurityPolicy is not used
  • 1.2.14 Ensure that the admission control plugin ServiceAccount is set
  • 1.2.15 Ensure that the admission control plugin NamespaceLifecycle is set
  • 1.2.16 Ensure that the admission control plugin PodSecurityPolicy is set
  • 1.2.17 Ensure that the admission control plugin NodeRestriction is set
  • 1.2.18 Ensure that the --insecure-bind-address argument is not set
  • 1.2.19 Ensure that the --insecure-port argument is set to 0

1.3 Controller Manager

  • 1.3.1 Ensure that the --terminated-pod-gc-threshold argument is set as appropriate
  • 1.3.2 Ensure that the --profiling argument is set to false
  • 1.3.3 Ensure that the --use-service-account-credentials argument is set to true
  • 1.3.4 Ensure that the --service-account-private-key-file argument is set as appropriate
  • 1.3.5 Ensure that the --root-ca-file argument is set as appropriate
  • 1.3.6 Ensure that the RotateKubeletServerCertificate argument is set to true
  • 1.3.7 Ensure that the --bind-address argument is set to 127.0.0.1  

1.4 Scheduler

  • 1.4.1 Ensure that the --profiling argument is set to false
  • 1.4.2 Ensure that the --bind-address argument is set to 127.0.0.1

Question - Is there a reason why Kubec policies are not in folders like CIS policies

Describe the solution you'd like:
First, thank you for the great library of policies!!!

Is there a reason why the kubesec policies are not in their own folders similar to the CIS policies? From a consistency standpoint it makes it easier to just point to a folder if using something like konstraint to generate CRDs for gatekeeper. I am more than happy to create a PR to do the work and create the folders

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

CIS 5.1.3 policy produces a violation for the cluster-admin cluster role - when enforced by Gatekeeper this bricks the cluster

Details

What steps did you take and what happened:

We used the CIS 5.1.3 rego in an OPA gatekeeper policy to prevent creation of roles / cluster roles which utilize wildcards.

What did you expect to happen:

The cluster would remain functional.

Anything else you would like to add:

The K8s API periodically checks to ensure the bootstrap roles (admin, cluster-admin, etc.) are present after the control plane comes up. If they are not present, it attempts to reconcile them using a post start hook. If a post start hook cannot complete, the /healthz endpoint starts to return failures. In a managed K8s setting like EKS this causes the load balancer sitting in front of the API to stop routing traffic to it, rendering the cluster dead.

Additional Information:

  1. cluster-admin uses wildcards.
  2. Turns out to be tricky for AWS to correct this, we had to destroy and recreate our cluster to proceed with our Rego and Gatekeeper testing. Hopefully this issue helps folks avoid this in the future.

CIS Kubernetes Benchmark 1.5.1 # 5.1

5.1 RBAC and Service Accounts

  • 5.1.1 Ensure that the cluster-admin role is only used where required
  • 5.1.2 Minimize access to secrets
  • 5.1.3 Minimize wildcard use in Roles and ClusterRoles
  • 5.1.4 Minimize access to create pods
  • 5.1.5 Ensure that default service accounts are not actively used
  • 5.1.6 Ensure that Service Account Tokens are only mounted where necessary

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.