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

Add k8s event generator #997

Merged
merged 2 commits into from
Jan 15, 2020
Merged
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
7 changes: 5 additions & 2 deletions docker/event-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM alpine:latest
LABEL maintainer="opensource@sysdig.com"
RUN apk add --no-cache bash g++
RUN apk add --no-cache bash g++ curl
COPY ./event_generator.cpp /usr/local/bin
COPY ./docker-entrypoint.sh ./k8s_event_generator.sh /
COPY ./yaml /yaml
RUN mkdir -p /var/lib/rpm
RUN g++ --std=c++0x /usr/local/bin/event_generator.cpp -o /usr/local/bin/event_generator
CMD ["/usr/local/bin/event_generator"]
RUN curl -o /usr/local/bin/kubectl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod +x /usr/local/bin/kubectl
ENTRYPOINT ["/docker-entrypoint.sh"]
21 changes: 21 additions & 0 deletions docker/event-generator/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

CMD=${1:-syscall}

shift

set -euo pipefail

if [[ "$CMD" == "syscall" ]]; then
/usr/local/bin/event_generator
elif [[ "$CMD" == "k8s_audit" ]]; then
. k8s_event_generator.sh
elif [[ "$CMD" == "bash" ]]; then
bash
else
echo "Unknown command. Can be one of"
echo " \"syscall\": generate falco syscall-related activity"
echo " \"k8s_audit\": generate falco k8s audit-related activity"
echo " \"bash\": spawn a shell"
exit 1
fi
4 changes: 2 additions & 2 deletions docker/event-generator/event_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
#include <cstdlib>
#include <unistd.h>
#include <getopt.h>
#include <sys/errno.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -146,7 +146,7 @@ void read(const char *filename)
open_file(filename, "r");
}

uid_t become_user(const char *user)
void become_user(const char *user)
{
struct passwd *pw;
pw = getpwnam(user);
Expand Down
57 changes: 57 additions & 0 deletions docker/event-generator/k8s_event_generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

set -euo pipefail

# You can pass a specific falco rule name and only yaml files matching
# that rule will be considered. The default is "all", meaning all yaml
# files will be applied.

RULE=${1:-all}

# Replace any '/' in RULES with a '.' and any space with a dash. (K8s
# label values can not contain slashes/spaces)
RULE=$(echo "$RULE" | tr '/ ' '.-')

echo "***Testing kubectl configuration..."
kubectl version --short

while true; do

RET=$(kubectl get namespaces --output=name | grep falco-event-generator || true)

if [[ "$RET" == *falco-event-generator* ]]; then
echo "***Deleting existing falco-event-generator namespace..."
kubectl delete namespace falco-event-generator
fi

echo "***Creating falco-event-generator namespace..."
kubectl create namespace falco-event-generator

for file in yaml/*.yaml; do

MATCH=0
if [[ "${RULE}" == "all" ]]; then
MATCH=1
else
RET=$(grep -E "falco.rules:.*${RULE}" $file || true)
if [[ "$RET" != "" ]]; then
MATCH=1
fi
fi

if [[ $MATCH == 1 ]]; then
MESSAGES=$(grep -E 'message' $file | cut -d: -f2 | tr '\n' ',')
RULES=$(grep -E 'falco.rules' $file | cut -d: -f2 | tr '\n' ',')

# The message uses dashes in place of spaces, convert them back to spaces
MESSAGES=$(echo "$MESSAGES" | tr '-' ' ' | sed -e 's/ *//' | sed -e 's/,$//')
RULES=$(echo "$RULES" | tr '-' ' '| tr '.' '/' | sed -e 's/ *//' | sed -e 's/,$//')

echo "***$MESSAGES (Rule(s) $RULES)..."
kubectl apply -f $file
sleep 2
fi
done

sleep 10
done
16 changes: 16 additions & 0 deletions docker/event-generator/yaml/configmap-private-creds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: private-creds-configmap
namespace: falco-event-generator
labels:
app.kubernetes.io/name: private-creds-configmap
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create.Modify-Configmap-With-Private-Credentials
message: Creating-configmap-with-private-credentials
data:
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
password=some_secret_password
25 changes: 25 additions & 0 deletions docker/event-generator/yaml/disallowed-pod-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: disallowed-pod-deployment
namespace: falco-event-generator
labels:
app.kubernetes.io/name: disallowed-pod-deployment
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create-Disallowed-Pod
message: Creating-pod-with-image-outside-of-allowed-images
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: disallowed-pod-busybox
template:
metadata:
labels:
app.kubernetes.io/name: disallowed-pod-busybox
app.kubernetes.io/part-of: falco-event-generator
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "while true; do echo sleeping; sleep 3600; done"]
26 changes: 26 additions & 0 deletions docker/event-generator/yaml/hostnetwork-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: hostnetwork-deployment
namespace: falco-event-generator
labels:
app.kubernetes.io/name: hostnetwork-deployment
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create-HostNetwork-Pod
message: Creating-deployment-with-hostNetwork-true-pod
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: hostnetwork-busybox
template:
metadata:
labels:
app.kubernetes.io/name: hostnetwork-busybox
app.kubernetes.io/part-of: falco-event-generator
spec:
hostNetwork: true
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "while true; do echo sleeping; sleep 3600; done"]
16 changes: 16 additions & 0 deletions docker/event-generator/yaml/nodeport-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: nodeport-service
namespace: falco-event-generator
labels:
app.kubernetes.io/name: nodeport-service
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create-NodePort-Service
message: Creating-service-of-type-NodePort
spec:
type: NodePort
ports:
- port: 80
selector:
app: busybox
27 changes: 27 additions & 0 deletions docker/event-generator/yaml/privileged-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: privileged-deployment
namespace: falco-event-generator
labels:
app.kubernetes.io/name: privileged-deployment
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create-Privileged-Pod
message: Creating-deployment-with-privileged-true-pod
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: privileged-busybox
template:
metadata:
labels:
app.kubernetes.io/name: privileged-busybox
app.kubernetes.io/part-of: falco-event-generator
spec:
containers:
- securityContext:
privileged: true
name: busybox
image: busybox
command: ["/bin/sh", "-c", "while true; do echo sleeping; sleep 3600; done"]
17 changes: 17 additions & 0 deletions docker/event-generator/yaml/role-pod-exec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-exec-role
namespace: falco-event-generator
labels:
app.kubernetes.io/name: pod-exec-role
app.kubernetes.io/part-of: falco-event-generator
falco.rules: ClusterRole-With-Pod-Exec-Created
message: Creating-role-that-can-exec-to-pods
rules:
- apiGroups:
- ""
resources:
- "pods/exec"
verbs:
- get
17 changes: 17 additions & 0 deletions docker/event-generator/yaml/role-wildcard-resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: wildcard-resources-role
namespace: falco-event-generator
labels:
app.kubernetes.io/name: wildcard-resources-role
app.kubernetes.io/part-of: falco-event-generator
falco.rules: ClusterRole-With-Write-Privileges-Created
message: Creating-role-with-wildcard-resources
rules:
- apiGroups:
- ""
resources:
- "*"
verbs:
- get
17 changes: 17 additions & 0 deletions docker/event-generator/yaml/role-write-privileges.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: write-privileges-role
namespace: falco-event-generator
labels:
app.kubernetes.io/name: write-privileges-role
app.kubernetes.io/part-of: falco-event-generator
falco.rules: ClusterRole-With-Write-Privileges-Created
message: Creating-role-with-write-privileges
rules:
- apiGroups:
- ""
resources:
- "pods"
verbs:
- create
32 changes: 32 additions & 0 deletions docker/event-generator/yaml/sensitive-mount-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: sensitive-mount-deployment
namespace: falco-event-generator
labels:
app.kubernetes.io/name: sensitive-mount-deployment
app.kubernetes.io/part-of: falco-event-generator
falco.rules: Create-Sensitive-Mount-Pod
message: Creating-deployment-with-pod-mounting-sensitive-path-from-host
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: sensitive-mount-busybox
template:
metadata:
labels:
app.kubernetes.io/name: sensitive-mount-busybox
app.kubernetes.io/part-of: falco-event-generator
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "while true; do echo sleeping; sleep 3600; done"]
volumeMounts:
- mountPath: /host/etc
name: etc
volumes:
- name: etc
hostPath:
path: /etc
15 changes: 15 additions & 0 deletions docker/event-generator/yaml/vanilla-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: vanilla-configmap
namespace: falco-event-generator
labels:
app.kubernetes.io/name: vanilla-configmap
app.kubernetes.io/part-of: falco-event-generator
falco.rules: K8s-ConfigMap-Created
message: Creating-configmap
data:
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
25 changes: 25 additions & 0 deletions docker/event-generator/yaml/vanilla-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: vanilla-deployment
namespace: falco-event-generator
labels:
app.kubernetes.io/name: vanilla-deployment
app.kubernetes.io/part-of: falco-event-generator
falco.rules: K8s-Deployment-Created
message: Creating-deployment
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: vanilla-busybox
template:
metadata:
labels:
app.kubernetes.io/name: vanilla-busybox
app.kubernetes.io/part-of: falco-event-generator
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "while true; do echo sleeping; sleep 3600; done"]
Loading