Skip to content

Commit 609aa85

Browse files
committed
Add Dockerfile and main.go for OPA with NATS plugin integration
Introduce a new Dockerfile for building the OPA binary with the NATS store plugin. The Dockerfile includes multi-stage builds for dependency management and a non-root user setup. Additionally, add main.go to implement the OPA runtime with the NATS plugin, enhancing the project's capabilities. Update docker-compose.yaml to reference the new Dockerfile and adjust the command for running the OPA server.
1 parent 8c2cc0d commit 609aa85

4 files changed

Lines changed: 54 additions & 35 deletions

File tree

Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS builder
2+
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /app
7+
8+
# Copy go.mod and go.sum for dependency caching
9+
COPY go.mod go.sum ./
10+
11+
# Download dependencies
12+
RUN go mod download
13+
14+
# Copy the entire source code
15+
COPY . .
16+
17+
# Build the custom OPA binary with natsstore plugin
18+
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o opa ./cmd/opa-nats
19+
20+
FROM alpine:latest
21+
22+
# Install ca-certificates for TLS connections
23+
RUN apk --no-cache add ca-certificates
24+
25+
# Create non-root user
26+
RUN adduser -D -s /bin/sh opa
27+
28+
# Copy the built binary from builder stage
29+
COPY --from=builder /app/opa /usr/local/bin/opa
30+
31+
# Switch to non-root user
32+
USER opa
33+
34+
ENTRYPOINT ["opa"]

cmd/opa-nats/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/open-policy-agent/opa/cmd"
7+
"github.com/open-policy-agent/opa/v1/runtime"
8+
natsstore "github.com/permitio/opa-nats/pkg/natsstore"
9+
)
10+
11+
func main() {
12+
pluginFactory := natsstore.NewPluginFactory()
13+
runtime.RegisterPlugin(natsstore.PluginName, pluginFactory)
14+
15+
if err := cmd.RootCommand.Execute(); err != nil {
16+
log.Fatalf("Failed to execute OPA runtime: %v", err)
17+
}
18+
}

examples/handledpaths/Dockerfile

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/handledpaths/docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ services:
131131
opa:
132132
build:
133133
context: ../../
134-
dockerfile: examples/handledpaths/Dockerfile
135-
container_name: opa-server
134+
dockerfile: Dockerfile
136135
depends_on:
137136
nats:
138137
condition: service_healthy
139138
nats-setup:
140139
condition: service_completed_successfully
140+
command: ["run", "--server", "--config-file=/config/config.yaml", "/policies", "--addr", "0.0.0.0:8181"]
141141
ports:
142142
- "8181:8181"
143143
volumes:

0 commit comments

Comments
 (0)