forked from kubearmor/KubeArmor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from navin772/test-actions
test(tests): LFX term 2 Pre-task submission
- Loading branch information
Showing
20 changed files
with
469 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
# Edit the daemonset to add the -enableKubeArmorHostPolicy=true flag | ||
# kubectl edit daemonset -n kubearmor <<EOF | ||
# /args:/a \ | ||
# - -enableKubeArmorHostPolicy=true | ||
# EOF | ||
|
||
kubectl get daemonset -n kubearmor -o yaml > daemonset.yaml | ||
sed -i '/args:/a \ - -enableKubeArmorHostPolicy=true' daemonset.yaml | ||
kubectl apply -f daemonset.yaml | ||
|
||
sleep 1m | ||
|
||
# Apply annotations to the node | ||
NODE_NAME=$(kubectl get nodes -o=jsonpath='{.items[0].metadata.name}') | ||
kubectl annotate node $NODE_NAME "kubearmorvisibility=process,file,network,capabilities" | ||
kubectl get no -o wide |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2022 Authors of KubeArmor | ||
|
||
package hsp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHsp(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Hsp Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package hsp | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/kubearmor/KubeArmor/tests/util" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
|
||
// delete all HSPs | ||
DeleteAllHsp() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
|
||
// delete all HSPs | ||
DeleteAllHsp() | ||
}) | ||
|
||
var _ = Describe("HSP", func() { | ||
|
||
BeforeEach(func() { | ||
time.Sleep(1 * time.Second) | ||
}) | ||
|
||
AfterEach(func() { | ||
KarmorLogStop() | ||
err := DeleteAllHsp() | ||
Expect(err).To(BeNil()) | ||
// wait for policy deletion | ||
time.Sleep(2 * time.Second) | ||
}) | ||
|
||
Describe("Policy Apply", func() { | ||
It("can block access to date command", func() { | ||
// Apply the Host Security Policy | ||
err := K8sApplyFile("manifests/hsp-kubearmor-dev-proc-path-block.yaml") | ||
Expect(err).To(BeNil()) | ||
|
||
// Start Kubearmor Logs | ||
err = KarmorLogStart("policy", "", "Process", "") | ||
Expect(err).To(BeNil()) | ||
|
||
// Execute the date command | ||
out, err := ExecCommandHost([]string{"bash", "-c", "date"}) | ||
Expect(err).NotTo(BeNil()) | ||
fmt.Printf("---START---\n%s---END---\n", out) | ||
Expect(out).To(MatchRegexp(".*Permission denied")) | ||
|
||
// check policy violation alert | ||
_, alerts, err := KarmorGetLogs(5*time.Second, 1) | ||
Expect(err).To(BeNil()) | ||
Expect(len(alerts)).To(BeNumerically(">=", 1)) | ||
Expect(alerts[0].PolicyName).To(Equal("hsp-kubearmor-dev-proc-path-block")) | ||
Expect(alerts[0].Action).To(Equal("Block")) | ||
|
||
// Execute a command that should not be blocked | ||
out, err = ExecCommandHost([]string{"bash", "-c", "ls"}) | ||
Expect(err).To(BeNil()) | ||
Expect(out).NotTo(MatchRegexp(".*Permission denied")) | ||
}) | ||
|
||
It("can block access to /etc/hostname file", func() { | ||
// Apply the Host Security Policy | ||
err := K8sApplyFile("manifests/hsp-kubearmor-dev-file-path-block.yaml") | ||
Expect(err).To(BeNil()) | ||
|
||
// Start Kubearmor Logs | ||
err = KarmorLogStart("policy", "", "File", "") | ||
Expect(err).To(BeNil()) | ||
|
||
// Try to access the /etc/hostname file | ||
out, err := ExecCommandHost([]string{"bash", "-c", "cat /etc/hostname"}) | ||
Expect(err).NotTo(BeNil()) | ||
fmt.Printf("---START---\n%s---END---\n", out) | ||
Expect(out).To(MatchRegexp(".*Permission denied")) | ||
|
||
// check policy violation alert | ||
_, alerts, err := KarmorGetLogs(5*time.Second, 1) | ||
Expect(err).To(BeNil()) | ||
Expect(len(alerts)).To(BeNumerically(">=", 1)) | ||
Expect(alerts[0].PolicyName).To(Equal("hsp-kubearmor-dev-file-path-block")) | ||
Expect(alerts[0].Action).To(Equal("Block")) | ||
}) | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
tests/k8s_env/hsp/manifests/hsp-kubearmor-dev-file-path-block.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: security.kubearmor.com/v1 | ||
kind: KubeArmorHostPolicy | ||
metadata: | ||
name: hsp-kubearmor-dev-file-path-block | ||
spec: | ||
nodeSelector: | ||
matchLabels: | ||
kubernetes.io/os: linux | ||
severity: 5 | ||
file: | ||
matchPaths: | ||
- path: /etc/hostname | ||
action: | ||
Block | ||
|
||
|
||
# test | ||
# $ cat /etc/hostname | ||
# cat: /etc/hostname: Permission denied |
24 changes: 24 additions & 0 deletions
24
tests/k8s_env/hsp/manifests/hsp-kubearmor-dev-proc-path-block.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: security.kubearmor.com/v1 | ||
kind: KubeArmorHostPolicy | ||
metadata: | ||
name: hsp-kubearmor-dev-proc-path-block | ||
spec: | ||
nodeSelector: | ||
matchLabels: | ||
kubernetes.io/os: linux | ||
severity: 5 | ||
process: | ||
matchPaths: | ||
- path: /bin/date | ||
- path: /usr/bin/date | ||
|
||
action: | ||
Block | ||
|
||
# kubearmor-dev_test_04 | ||
|
||
# test | ||
# $ bash -c date | ||
# bash: 1: date: Permission denied | ||
# $ bash -c ls | ||
# ls ... |
17 changes: 17 additions & 0 deletions
17
tests/k8s_env/multicontainer/manifests/non-existent-container-block-ls.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apiVersion: security.kubearmor.com/v1 | ||
kind: KubeArmorPolicy | ||
metadata: | ||
name: non-existent-container-block-ls | ||
namespace: multicontainer | ||
spec: | ||
severity: 5 | ||
selector: | ||
matchLabels: | ||
container: multicontainer | ||
kubearmor.io/container.name: "[container-1, non-existent-container ]" | ||
process: | ||
matchPaths: | ||
- path: /bin/ls | ||
# ls | ||
action: | ||
Block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2022 Authors of KubeArmor | ||
|
||
package hsp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHsp(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Hsp Suite") | ||
} |
Oops, something went wrong.