Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapter): Add nimbus-coco adapter #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/coco/1-kcp-origin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-runtimeclass
spec:
mutateExistingOnPolicyUpdate: true
rules:
- name: add-runtimeclass-to-deployment
match:
resources:
kinds:
- Deployment
mutate:
targets:
- apiVersion: apps/v1
kind: Deployment
patchStrategicMerge:
spec:
template:
spec:
runtimeClassName: kata-qemu
21 changes: 21 additions & 0 deletions examples/coco/2-kcp-snp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-runtimeclass
spec:
mutateExistingOnPolicyUpdate: true
rules:
- name: add-runtimeclass-to-deployment
match:
resources:
kinds:
- Deployment
mutate:
targets:
- apiVersion: apps/v1
kind: Deployment
patchStrategicMerge:
spec:
template:
spec:
runtimeClassName: kata-qemu-snp
24 changes: 24 additions & 0 deletions examples/coco/3-kcp-label.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-runtimeclass
spec:
mutateExistingOnPolicyUpdate: true
rules:
- name: add-runtimeclass-to-deployment
match:
resources:
kinds:
- Deployment
selector:
matchLabels:
app: nginx-test
mutate:
targets:
- apiVersion: apps/v1
kind: Deployment
patchStrategicMerge:
spec:
template:
spec:
runtimeClassName: kata-qemu-snp
25 changes: 25 additions & 0 deletions examples/coco/csib-si-coco.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: intent.security.nimbus.com/v1alpha1
kind: SecurityIntent
metadata:
name: coco-workload
spec:
intent:
id: cocoWorkload
description: "Ensure workload is encryted by running the specified workload in a Confidential VM"
action: Block
---
apiVersion: intent.security.nimbus.com/v1alpha1
kind: ClusterSecurityIntentBinding
metadata:
name: coco-workload-binding
spec:
intents:
- name: coco-workload
selector:
nsSelector:
matchNames:
- default
workloadSelector:
matchLabels:
app: nginx-test

19 changes: 19 additions & 0 deletions examples/coco/k8s-nginx-coco-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-test-coco
labels:
app: nginx-coco
spec:
replicas: 1
selector:
matchLabels:
app: nginx-coco
template:
metadata:
labels:
app: nginx-coco
spec:
containers:
- name: nginx-coco
image: 1hcoj/nginx
19 changes: 19 additions & 0 deletions examples/coco/k8s-nginx-test-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-test
labels:
app: nginx-test
spec:
replicas: 1
selector:
matchLabels:
app: nginx-test
template:
metadata:
labels:
app: nginx-test
spec:
containers:
- name: nginx-test
image: 1hcoj/nginx
7 changes: 7 additions & 0 deletions pkg/adapter/idpool/idpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DisallowChRoot = "disallowChRoot"
DisallowCapabilities = "disallowCapabilities"
ExploitPFA = "PreventExecutionFromTempOrLogsFolders"
CocoWorkload = "cocoWorkload"
)

// KaIds are IDs supported by KubeArmor.
Expand All @@ -46,6 +47,10 @@ var KyvIds = []string{
EscapeToHost,
}

var CocoIds = []string{
CocoWorkload,
}

// IsIdSupportedBy determines whether a given ID is supported by a security engine.
func IsIdSupportedBy(id, securityEngine string) bool {
switch strings.ToLower(securityEngine) {
Expand All @@ -55,6 +60,8 @@ func IsIdSupportedBy(id, securityEngine string) bool {
return in(id, NetPolIDs)
case "kyverno":
return in(id, KyvIds)
case "coco":
return in(id, CocoIds)
default:
return false
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/adapter/nimbus-coco/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2023 Authors of Nimbus

# Image URL to use all building/pushing image targets
IMG ?= 5gsec/nimbus-coco
# Image Tag to use all building/pushing image targets
TAG ?= v0.1

CONTAINER_TOOL ?= docker
BINARY ?= bin/nimbus-coco

build:
@go build -ldflags="-w" -o ${BINARY} main.go

run: build
@./${BINARY}

.PHONY: docker-build
docker-build:
$(CONTAINER_TOOL) build -t ${IMG}:${TAG} -t ${IMG}:latest --build-arg VERSION=${TAG} -f ./Dockerfile ../../../

.PHONY: docker-push
docker-push:
$(CONTAINER_TOOL) push ${IMG}:${TAG}
$(CONTAINER_TOOL) push ${IMG}:latest

PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx:
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg VERSION=${TAG} --tag ${IMG}:${TAG} -f Dockerfile.cross ../../../ || { $(CONTAINER_TOOL) buildx rm project-v3-builder; rm Dockerfile.cross; exit 1; }
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross
5 changes: 5 additions & 0 deletions pkg/adapter/nimbus-coco/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/5GSEC/nimbus/pkg/adapter/nimbus-coco

go 1.22.0

replace github.com/5GSEC/nimbus => ../../../../nimbus
35 changes: 35 additions & 0 deletions pkg/adapter/nimbus-coco/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Authors of Nimbus

package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/5GSEC/nimbus/pkg/adapter/nimbus-coco/manager"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func main() {
ctrl.SetLogger(zap.New())
logger := ctrl.Log

ctx, cancelFunc := context.WithCancel(context.Background())
ctrl.LoggerInto(ctx, logger)

go func() {
termChan := make(chan os.Signal)
signal.Notify(termChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-termChan
logger.Info("Shutdown signal received, waiting for all workers to finish")
cancelFunc()
logger.Info("All workers finished, shutting down")
}()

logger.Info("Coco adapter started")
manager.Run(ctx)
}
Loading
Loading