• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sidecar for managing OPA instances in Kubernetes.

logo kube-mgmt

Policy-based control for Kubernetes deployments.

About

kube-mgmt manages policies / data of Open Policy Agent instances in Kubernetes.

Use kube-mgmt to:

Deployment Guide

Both OPA and kube-mgmt can be installed using Helm chart.

Follow README to install it into K8s cluster.

Policies and Data loading

kube-mgmt automatically discovers policies and JSON data stored in ConfigMaps in Kubernetes and loads them into OPA.

Policies or data can be disabled using --enable-policy=false or --enable-data=false flags respectively.

kube-mgmt assumes a ConfigMap contains policy or JSON data if the ConfigMap is:

  • Created in a namespace listed in the --namespaces option. If you specify --namespaces=* then kube-mgmt will look for policies in ALL namespaces.
  • Labelled with openpolicyagent.org/policy=rego for policies
  • Labelled with openpolicyagent.org/data=op for JSON data

Label names and their values can be configured using --policy-label, --policy-value, --data-label, --data-value CLI options.

When a ConfigMap has been successfully loaded into OPA, the openpolicyagent.org/kube-mgmt-status annotation is set to {"status": "ok"}.

If loading fails for some reason (e.g., because of a parse error), the openpolicyagent.org/kube-mgmt-status annotation is set to {"status": "error", "error": ...} where the error field contains details about the failure.

Data loaded out of ConfigMaps is laid out as follows:

<namespace>/<name>/<key>

For example, if the following ConfigMap was created:

kind: ConfigMap
apiVersion: v1
metadata:
  name: hello-data
  namespace: opa
  labels:
    openpolicyagent.org/data: opa
data:
  x.json: |
    {"a": [1,2,3,4]}

Note: "x.json" may be any key.

You could refer to the data inside your policies as follows:

data.opa["hello-data"]["x.json"].a[0]  # evaluates to 1

Note: "opa" is the namespace for the configMap. You may mock this in a test like other objects: with data.opa as my_mocked_object.

Caching

kube-mgmt can be configured to replicate Kubernetes resources into OPA so that you can express policies over an eventually consistent cache of Kubernetes state.

Replication is enabled with the following options:

# Replicate namespace-level resources. May be specified multiple times.
--replicate=<[group/]version/resource>

# Replicate cluster-level resources. May be specified multiple times.
--replicate-cluster=<[group/]version/resource>

Kubernetes resources replicated into OPA are laid out as follows:

<replicate-path>/<resource>/<namespace>/<name> # namespace scoped
<replicate-path>/<resource>/<name>             # cluster scoped
  • <replicate-path> is configurable (via --replicate-path) and defaults to kubernetes.
  • <resource> is the Kubernetes resource plural, e.g., nodes, pods, services, etc.
  • <namespace> is the namespace of the Kubernetes resource.
  • <name> is the name of the Kubernetes resource.

For example, to search for services with the label "foo" you could write:

some namespace, name
service := data.kubernetes.services[namespace][name]
service.metadata.labels["foo"]

An alternative way to visualize the layout is as single JSON document:

{
  "kubernetes": {
    "services": {
      "default": {
        "example-service": {...},
          "another-service": {...},
        }
      }
    }
  }
}

The example below would replicate Deployments, Services, and Nodes into OPA:

--replicate=apps/v1beta/deployments
--replicate=v1/services
--replicate-cluster=v1/nodes

Custom Resource Definitions (CRDs)

kube-mgmt can also be configured to replicate Kubernetes Custom Resources using the --replicate and --replicate-cluster options. For an example of how OPA can be used to enforce admission control polices on Kubernetes custom resources see Admission Control For Custom Resources

Admission Control

To get started with admission control policy enforcement in Kubernetes 1.9 or later see the Kubernetes Admission Control tutorial. For older versions of Kubernetes, see Admission Control (1.7).

In the Kubernetes Admission Control tutorial, OPA is NOT running with an authorization policy configured and hence clients can read and write policies in OPA. When deploying OPA in an insecure environment, it is recommended to configure authentication and authorization on the OPA daemon. For an example of how OPA can be securely deployed as an admission controller see Admission Control Secure.

OPA API Endpoints and Least-privilege Configuration

kube-mgmt is a privileged component that can load policy and data into OPA. Other clients connecting to the OPA API only need to query for policy decisions.

To load policy and data into OPA, kube-mgmt uses the following OPA API endpoints:

  • PUT v1/policy/<path> - upserting policies
  • DELETE v1/policy/<path> - deleting policies
  • PUT v1/data/<path> - upserting data
  • PATCH v1/data/<path> - updating and removing data

Many users configure OPA with a simple API authorization policy that restricts access to the OPA APIs:

package system.authz

# Deny access by default.
default allow = false

# Allow anonymous access to decision `data.example.response`
#
# NOTE: the specific decision differs depending on your policies.
# NOTE: depending on how callers are configured, they may only require this or the default decision below.
allow {
  input.path == ["v0", "data", "example", "response"]
  input.method == "POST"
}

# Allow anonymous access to default decision.
allow {
  input.path == [""]
  input.method == "POST"
}

# This is only used for health check in liveness and readiness probe
allow {
  input.path == ["health"]
  input.method == "GET"
}

# This is only used for prometheus metrics
allow {
  input.path == ["metrics"]
  input.method == "GET"
}

# This is used by kube-mgmt to PUT/PATCH against /v1/data and PUT/DELETE against /v1/policies.
#
# NOTE: The $TOKEN value is replaced at deploy-time with the actual value that kube-mgmt will use. This is typically done by an initContainer.
allow {
  input.identity == "$TOKEN"
}

Development Guide

This project uses excellent tool Just for buiding. It is configured by justfile file in root directory. All available targets can be inspected by running just in command line.

Release procedure

To release a new version - just create GitHub release with corresponding tag, following semantic version converntion.

As soon as tag will be pushed - CI pipeline will build and publish all artifacts.

More Repositories

1

opa

Open Policy Agent (OPA) is an open source, general-purpose policy engine.
Go
9,480
star
2

gatekeeper

🐊 Gatekeeper - Policy Controller for Kubernetes
Go
3,602
star
3

conftest

Write tests against structured configuration data using the Open Policy Agent Rego query language
Go
2,857
star
4

gatekeeper-library

📚 The OPA Gatekeeper policy library
Open Policy Agent
628
star
5

contrib

Integrations, examples, and proof-of-concepts that are not part of OPA proper.
Go
321
star
6

opa-envoy-plugin

A plugin to enforce OPA policies with Envoy
Go
316
star
7

npm-opa-wasm

Open Policy Agent WebAssembly NPM module (opa-wasm)
JavaScript
130
star
8

frameworks

Go
120
star
9

vscode-opa

An extension for VS Code which provides support for OPA and the Rego policy language
TypeScript
109
star
10

library

The Open Policy Agent project standard library.
Open Policy Agent
93
star
11

example-api-authz-go

Example Go service that uses OPA for API authorization.
Go
93
star
12

cert-controller

Go
89
star
13

opa-docker-authz

A policy-enabled authorization plugin for Docker.
Go
81
star
14

opa-idea-plugin

Open Policy Agent plugin for IntelliJ
Kotlin
56
star
15

rego-python

Python library for interacting with Rego ASTs.
Python
48
star
16

opa-envoy-spire-ext-authz

OPA-Envoy-SPIRE External Authorization Example.
Go
47
star
17

setup-opa

Sets up Open Policy Agent CLI in your GitHub Actions workflow.
TypeScript
44
star
18

example-api-authz-python

Example Python service that uses OPA for API authorization.
Python
32
star
19

community

The Community repository is the place to go for support with OPA and OPA Sub-Projects, like Conftest and Gatekeeper.
31
star
20

golang-opa-wasm

Open Policy Agent WebAssembly Go SDK
Go
20
star
21

gatekeeper-external-data-provider

A template repository for building external data providers for Gatekeeper.
Shell
10
star