|
| 1 | +# Makefile for building and pushing OPA policies to a registry |
| 2 | + |
| 3 | +# Variables |
| 4 | +REGISTRY_URL := ghcr.io |
| 5 | +NAMESPACE := chris-cmsoft |
| 6 | +POLICY_NAME := local-ssh-policies |
| 7 | +VERSION := latest |
| 8 | +POLICY_DIR := ./ssh # Directory containing your .rego files |
| 9 | + |
| 10 | +# Build and Push Commands |
| 11 | +.PHONY: all build bundle push clean |
| 12 | + |
| 13 | +# Default action |
| 14 | +all: test check build push clean |
| 15 | + |
| 16 | +# Check if OPA CLI is installed |
| 17 | +OPA := $(shell command -v opa 2> /dev/null) |
| 18 | +ifeq ($(OPA),) |
| 19 | +$(error "opa CLI not found. Please install it: https://www.openpolicyagent.org/docs/latest/cli/") |
| 20 | +endif |
| 21 | + |
| 22 | +# Check if Docker CLI is installed |
| 23 | + |
| 24 | +CONTAINER_CLI := "" |
| 25 | +DOCKER := $(shell command -v docker 2> /dev/null) |
| 26 | +PODMAN := $(shell command -v podman 2> /dev/null) |
| 27 | +ifeq ($(DOCKER),) |
| 28 | + PODMAN := := $(shell command -v podman 2> /dev/null) |
| 29 | + ifeq ($(PODMAN),) |
| 30 | + $(error "either docker or podman CLI is required.") |
| 31 | + else |
| 32 | + CONTAINER_CLI = PODMAN |
| 33 | + endif |
| 34 | +else |
| 35 | + CONTAINER_CLI = DOCKER |
| 36 | +endif |
| 37 | + |
| 38 | +test: |
| 39 | + @echo "Testing policies..." |
| 40 | + @OPA test policies |
| 41 | + |
| 42 | +# Build the policies |
| 43 | +check: |
| 44 | + @echo "Checking policies..." |
| 45 | + @opa check policies |
| 46 | + |
| 47 | +# Bundle the policies into a tarball for OCI registry |
| 48 | +build: clean |
| 49 | + @echo "Bundling policies..." |
| 50 | + @mkdir -p dist/ |
| 51 | + @opa build -b policies -o dist/bundle.tar.gz |
| 52 | + |
| 53 | +# Push the bundled policies to an OCI-compliant registry |
| 54 | +push: build |
| 55 | + @echo "Pushing bundle to registry..." |
| 56 | + @# Log in to the registry if necessary |
| 57 | + @$(CONTAINER_CLI) login $(REGISTRY_URL) |
| 58 | + @# Push the bundle as an OCI artifact |
| 59 | + @$(CONTAINER_CLI) cp dist/bundle.tar.gz $(REGISTRY_URL)/$(NAMESPACE)/$(POLICY_NAME):$(VERSION) |
| 60 | + @echo "Bundle pushed successfully to $(REGISTRY_URL)/$(NAMESPACE)/$(POLICY_NAME):$(VERSION)" |
| 61 | + |
| 62 | +# Clean up build artifacts |
| 63 | +clean: |
| 64 | + @echo "Cleaning up..." |
| 65 | + @rm -f dist/bundle.tar.gz |
0 commit comments