Skip to content

Commit efa0ff3

Browse files
Merge pull request #1245 from daemon1024/fix-restore-unorchestrated
2 parents 5e010da + 69e0a8a commit efa0ff3

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

KubeArmor/core/kubeArmor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ func KubeArmor() {
662662

663663
if cfg.GlobalCfg.KVMAgent || !dm.K8sEnabled {
664664
// Restore and apply all kubearmor host security policies
665-
dm.restoreKubeArmorHostPolicies()
665+
dm.restoreKubeArmorPolicies()
666666
}
667667

668668
// == //

KubeArmor/core/unorchestratedUpdates.go

+32-5
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func (dm *KubeArmorDaemon) backupKubeArmorContainerPolicy(policy tp.SecurityPoli
597597
}
598598
}
599599

600-
func (dm *KubeArmorDaemon) restoreKubeArmorHostPolicies() {
600+
func (dm *KubeArmorDaemon) restoreKubeArmorPolicies() {
601601
if _, err := os.Stat(cfg.PolicyDir); err != nil {
602602
kg.Warn("Policies dir not found for restoration")
603603
return
@@ -607,15 +607,42 @@ func (dm *KubeArmorDaemon) restoreKubeArmorHostPolicies() {
607607
if policyFiles, err := os.ReadDir(cfg.PolicyDir); err == nil {
608608
for _, file := range policyFiles {
609609
if data, err := os.ReadFile(cfg.PolicyDir + file.Name()); err == nil {
610-
var hostPolicy tp.HostSecurityPolicy
611-
if err := json.Unmarshal(data, &hostPolicy); err == nil {
612-
dm.HostSecurityPolicies = append(dm.HostSecurityPolicies, hostPolicy)
610+
611+
var k struct {
612+
Metadata map[string]string `json:"metadata"`
613+
}
614+
615+
err := json.Unmarshal(data, &k)
616+
if err != nil {
617+
kg.Errf("Failed to unmarshal policy: %v", err)
618+
continue
619+
}
620+
621+
if _, ok := k.Metadata["namespaceName"]; ok { // ContainerPolicy contains namespaceName
622+
var containerPolicy tp.K8sKubeArmorPolicy
623+
if err := json.Unmarshal(data, &containerPolicy); err == nil {
624+
containerPolicy.Metadata.Name = k.Metadata["policyName"]
625+
dm.ParseAndUpdateContainerSecurityPolicy(tp.K8sKubeArmorPolicyEvent{
626+
Type: "ADDED",
627+
Object: containerPolicy,
628+
})
629+
}
630+
631+
} else { // HostSecurityPolicy
632+
var hostPolicy tp.HostSecurityPolicy
633+
if err := json.Unmarshal(data, &hostPolicy); err == nil {
634+
dm.HostSecurityPolicies = append(dm.HostSecurityPolicies, hostPolicy)
635+
} else {
636+
kg.Errf("Failed to unmarshal host policy: %v", err)
637+
}
613638
}
614639
}
615640
}
616641

617642
if len(policyFiles) != 0 {
618-
dm.UpdateHostSecurityPolicies()
643+
if len(dm.HostSecurityPolicies) != 0 {
644+
dm.UpdateHostSecurityPolicies()
645+
}
619646
} else {
620647
kg.Warn("No policies found for restoration")
621648
}

0 commit comments

Comments
 (0)