Skip to content

Commit

Permalink
Merge pull request #709 from Ankurk99/main
Browse files Browse the repository at this point in the history
KubeArmor support for un-orchestrated containers
  • Loading branch information
nyrahul authored Aug 13, 2022
2 parents b947160 + d51fcf9 commit ace1cc3
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 32 deletions.
9 changes: 7 additions & 2 deletions KubeArmor/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Authors of KubeArmor
# Copyright 2022 Authors of KubeArmor

CURDIR := $(shell pwd)
CRDDIR := $(realpath $(CURDIR)/../deployments/CRD)
Expand Down Expand Up @@ -43,7 +43,12 @@ run: build
cd $(CURDIR); sudo rm -f /tmp/kubearmor.log
cd $(CURDIR)/BPF; make clean
cd $(CURDIR)/BPF; make
cd $(CURDIR); sudo -E ./kubearmor -logPath=/tmp/kubearmor.log -enableKubeArmorPolicy=true -enableKubeArmorHostPolicy=true -hostVisibility=process,file,network,capabilities
cd $(CURDIR); sudo -E ./kubearmor -logPath=/tmp/kubearmor.log -enableKubeArmorPolicy -enableKubeArmorHostPolicy -hostVisibility=process,file,network,capabilities

.PHONY: run-container
run-container: build
cd $(CURDIR); sudo rm -f /tmp/kubearmor.log
cd $(CURDIR); sudo -E ./kubearmor -logPath=/tmp/kubearmor.log -enableKubeArmorHostPolicy -enableKubeArmorPolicy -k8s=false

.PHONY: run-host-only
run-host-only: build
Expand Down
14 changes: 10 additions & 4 deletions KubeArmor/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,15 @@ var ContainerRuntimeSocketMap = map[string][]string{

// GetCRISocket Function
func GetCRISocket(ContainerRuntime string) string {
for _, candidate := range ContainerRuntimeSocketMap[ContainerRuntime] {
if _, err := os.Stat(candidate); err == nil {
return candidate
for k := range ContainerRuntimeSocketMap {
if ContainerRuntime != "" && k != ContainerRuntime {
continue
}
criruntime := k
for _, candidate := range ContainerRuntimeSocketMap[criruntime] {
if _, err := os.Stat(candidate); err == nil {
return candidate
}
}
}
return ""
Expand Down Expand Up @@ -414,7 +420,7 @@ func MatchIdentities(identities []string, superIdentities []string) bool {
return false
}

// if super identities not include indentity, return false
// if super identities not include identity, return false
for _, identity := range identities {
if !ContainsElement(superIdentities, identity) {
matched = false
Expand Down
41 changes: 41 additions & 0 deletions KubeArmor/core/dockerHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ func (dh *DockerHandler) GetEventChannel() <-chan events.Message {
// == Docker Events == //
// =================== //

// Enable visibility flag arguments for un-orchestrated container
func (dm *KubeArmorDaemon) SetContainerVisibility(containerID string) {

// get container information from docker client
container, err := Docker.GetContainerInfo(containerID)
if err != nil {
return
}

if strings.Contains(cfg.GlobalCfg.Visibility, "process") {
container.ProcessVisibilityEnabled = true
}
if strings.Contains(cfg.GlobalCfg.Visibility, "file") {
container.FileVisibilityEnabled = true
}
if strings.Contains(cfg.GlobalCfg.Visibility, "network") {
container.NetworkVisibilityEnabled = true
}
if strings.Contains(cfg.GlobalCfg.Visibility, "capabilities") {
container.CapabilitiesVisibilityEnabled = true
}

dm.Containers[container.ContainerID] = container

container.EndPointName = container.ContainerName
container.NamespaceName = "container_namespace"
}

// GetAlreadyDeployedDockerContainers Function
func (dm *KubeArmorDaemon) GetAlreadyDeployedDockerContainers() {
// check if Docker exists else instantiate
Expand Down Expand Up @@ -237,6 +265,13 @@ func (dm *KubeArmorDaemon) GetAlreadyDeployedDockerContainers() {
continue
}

// check for unorchestrated docker containers
if !dm.K8sEnabled {
dm.ContainersLock.Lock()
dm.SetContainerVisibility(dcontainer.ID)
dm.ContainersLock.Unlock()
}

if dm.SystemMonitor != nil && cfg.GlobalCfg.Policy {
// update NsMap
dm.SystemMonitor.AddContainerIDToNsMap(container.ContainerID, container.PidNS, container.MntNS)
Expand Down Expand Up @@ -318,6 +353,12 @@ func (dm *KubeArmorDaemon) UpdateDockerContainer(containerID, action string) {
return
}

if !dm.K8sEnabled {
dm.ContainersLock.Lock()
dm.SetContainerVisibility(containerID)
dm.ContainersLock.Unlock()
}

if dm.SystemMonitor != nil && cfg.GlobalCfg.Policy {
// update NsMap
dm.SystemMonitor.AddContainerIDToNsMap(containerID, container.PidNS, container.MntNS)
Expand Down
59 changes: 52 additions & 7 deletions KubeArmor/core/kubeArmor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor
// Copyright 2022 Authors of KubeArmor

package core

Expand Down Expand Up @@ -311,6 +311,12 @@ func GetOSSigChannel() chan os.Signal {
return c
}

// Trim sock file location from the env variable
func trimmedSocketPath() string {
trimmedPath := strings.TrimPrefix(cfg.GlobalCfg.CRISocket, "unix://")
return trimmedPath
}

// ========== //
// == Main == //
// ========== //
Expand Down Expand Up @@ -412,6 +418,7 @@ func KubeArmor() {

// == //

// Containerized workloads with Host
if cfg.GlobalCfg.Policy || cfg.GlobalCfg.HostPolicy {
// initialize system monitor
if !dm.InitSystemMonitor() {
Expand All @@ -428,8 +435,6 @@ func KubeArmor() {
go dm.MonitorSystemEvents()
dm.Logger.Print("Started to monitor system events")

// == //

// initialize runtime enforcer
if !dm.InitRuntimeEnforcer() {
dm.Logger.Print("Disabled KubeArmor Enforcer since No LSM is enabled")
Expand All @@ -446,12 +451,44 @@ func KubeArmor() {
}
}

// == //
trimmedSocket := trimmedSocketPath()

// Un-orchestrated workloads
if !dm.K8sEnabled && cfg.GlobalCfg.Policy {

// Check if cri socket set, if not then auto detect
if cfg.GlobalCfg.CRISocket == "" {
if kl.GetCRISocket("") == "" {
dm.Logger.Warnf("Error while looking for CRI socket file")
} else {
cfg.GlobalCfg.CRISocket = "unix://" + kl.GetCRISocket("")
// update the value of trimmed socket path when the cfg.GlobalCfg.CRISocket is not set
trimmedSocket = trimmedSocketPath()
}
}

// monitor containers
if strings.Contains(cfg.GlobalCfg.CRISocket, "docker") {
// update already deployed containers
dm.GetAlreadyDeployedDockerContainers()
// monitor docker events
go dm.MonitorDockerEvents()
} else if strings.Contains(cfg.GlobalCfg.CRISocket, "containerd") {
// monitor containerd events
go dm.MonitorContainerdEvents()
} else if strings.Contains(cfg.GlobalCfg.CRISocket, "crio") {
// monitor crio events
go dm.MonitorCrioEvents()
} else {
dm.Logger.Warnf("Failed to monitor containers: %s is not a supported CRI socket.", cfg.GlobalCfg.CRISocket)
}

dm.Logger.Printf("Using %s for monitoring containers", cfg.GlobalCfg.CRISocket)
}

if dm.K8sEnabled && cfg.GlobalCfg.Policy {
// check if the CRI socket set while executing kubearmor exists
if cfg.GlobalCfg.CRISocket != "" {
trimmedSocket := strings.TrimPrefix(cfg.GlobalCfg.CRISocket, "unix://")
if _, err := os.Stat(trimmedSocket); err != nil {
dm.Logger.Warnf("Error while looking for CRI socket file: %s", err.Error())

Expand Down Expand Up @@ -479,7 +516,7 @@ func KubeArmor() {
return
}

dm.Logger.Printf("Using %s for monitoring containers.", cfg.GlobalCfg.CRISocket)
dm.Logger.Printf("Using %s for monitoring containers", cfg.GlobalCfg.CRISocket)

} else { // CRI socket not set, we'll have to auto detect
dm.Logger.Print("CRI socket not set. Trying to detect.")
Expand Down Expand Up @@ -577,8 +614,16 @@ func KubeArmor() {
dm.Logger.Print("Started to monitor host security policies")
}

policyService := &policy.ServiceServer{}

if !dm.K8sEnabled && cfg.GlobalCfg.Policy {
if _, err := os.Stat(trimmedSocket); err == nil {
policyService.UpdateContainerPolicy = dm.ParseAndUpdateContainerSecurityPolicy
dm.Logger.Print("Started to monitor container security policies on gRPC")
}
}

if !cfg.GlobalCfg.K8sEnv && cfg.GlobalCfg.HostPolicy {
policyService := &policy.ServiceServer{}
policyService.UpdateHostPolicy = dm.ParseAndUpdateHostSecurityPolicy
dm.Node.PolicyEnabled = tp.KubeArmorPolicyEnabled

Expand Down
Loading

0 comments on commit ace1cc3

Please sign in to comment.