Skip to content

Commit f220312

Browse files
author
Leonid Podolinskiy
authored
init container RunAsNonRoot + securityContext (#4)
* init container RunAsNonRoot + securityContext * add k3s ver due to install script issue k3s-io/k3s#5528 * update init container in test pipeline * replace busybox in test deployment (root user)
1 parent 73a6a82 commit f220312

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

.github/workflows/e2e.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
./get_helm.sh
3939
4040
#Install k3s
41-
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -s - --docker
41+
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_VERSION="v1.24.4+k3s1" sh -s - --docker
4242
4343
mkdir ~/.kube || echo "~/.kube already existed"
4444
sudo chmod 777 /etc/rancher/k3s/k3s.yaml

.github/workflows/init_container.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ jobs:
4141
file: ./lightrun-init-agent/Dockerfile
4242
push: true
4343
tags: "lightruncom/k8s-operator-init-java-agent-linux:${{steps.set_tag.outputs.TAG_NAME}}"
44-
build-args: |
44+
secrets: |
4545
GITHUB_TOKEN=${{ secrets.PRETTY_GITHUB_READ_TOKEN }}
46+
build-args: |
4647
VERSION=${{ inputs.release_tag }}
4748
FILE=agent.zip
4849
@@ -53,8 +54,9 @@ jobs:
5354
file: ./lightrun-init-agent/Dockerfile
5455
push: true
5556
tags: "lightruncom/k8s-operator-init-java-agent-alpine:${{steps.set_tag.outputs.TAG_NAME}}"
56-
build-args: |
57+
secrets: |
5758
GITHUB_TOKEN=${{ secrets.PRETTY_GITHUB_READ_TOKEN }}
59+
build-args: |
5860
VERSION=${{ inputs.release_tag }}
5961
FILE=agent-alpine.zip
6062

.github/workflows/tests_data/lightrunjavaagent.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: sample
55
spec:
66
initContainer:
7-
image: "lightruncom/k8s-operator-init-java-agent-linux:1.7.0-init.0"
7+
image: "lightruncom/k8s-operator-init-java-agent-linux:1.8.5-init.1"
88
sharedVolumeName: lightrun-agent-init
99
sharedVolumeMountPath: "/lightrun"
1010
deploymentName: app

controllers/patch_funcs.go

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ func (r *LightrunJavaAgentReconciler) addInitContainer(deploymentApplyConfig *ap
129129
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)),
130130
},
131131
),
132+
).
133+
WithSecurityContext(
134+
corev1ac.SecurityContext().
135+
WithCapabilities(
136+
corev1ac.Capabilities().WithDrop(corev1.Capability("ALL")),
137+
).
138+
WithAllowPrivilegeEscalation(false).
139+
WithRunAsNonRoot(true).
140+
WithSeccompProfile(
141+
corev1ac.SeccompProfile().
142+
WithType(corev1.SeccompProfileTypeRuntimeDefault),
143+
),
132144
),
133145
)
134146
}

examples/deployment.yaml

+16-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ spec:
2020
- name: JAVA_TOOL_OPTIONS
2121
value: -Djava.net.preferIPv4Stack=true
2222
image: lightruncom/operator-demo-app
23+
securityContext:
24+
allowPrivilegeEscalation: false
25+
capabilities:
26+
drop: ["ALL"]
27+
runAsNonRoot: true
28+
seccompProfile:
29+
type: RuntimeDefault
2330
# Second container will be not patched, as not mentioned in the custom resource
24-
- name: busybox
25-
image: busybox
26-
command: ["/bin/sh", "-c", "--"]
27-
args: ["while true; do sleep 30; done;"]
31+
- name: non-patched-app
32+
image: lightruncom/operator-demo-app
33+
securityContext:
34+
allowPrivilegeEscalation: false
35+
capabilities:
36+
drop: ["ALL"]
37+
runAsNonRoot: true
38+
seccompProfile:
39+
type: RuntimeDefault

lightrun-init-agent/Dockerfile

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
FROM --platform=linux/amd64 alpine:latest
22

3-
ARG GITHUB_TOKEN
43
ARG VERSION
54
ARG FILE
65

7-
86
RUN apk --no-cache add jq \
97
wget
10-
RUN export REPO="lightrun-platform/athena"; \
8+
RUN --mount=type=secret,id=GITHUB_TOKEN \
9+
GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) \
10+
export REPO="lightrun-platform/athena"; \
1111
# curl inside alpine can't properly forward cookies when downloading asset, hence wget
1212
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
1313
https://$GITHUB_TOKEN:@api.github.com/repos/$REPO/releases/assets/`wget -q -O- --auth-no-challenge --header "Accept: application/vnd.github+json" https://$GITHUB_TOKEN:@api.github.com/repos/$REPO/releases | jq ". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"` \
@@ -17,7 +17,11 @@ RUN export REPO="lightrun-platform/athena"; \
1717
rm -rf /tmp/agent.zip && \
1818
# Erase default values
1919
sed -i.bak "s|com.lightrun.secret=.*|com.lightrun.secret=|" /agent/agent.config && rm /agent/agent.config.bak && \
20-
sed -i.bak "s|pinned_certs=.*|pinned_certs=|" /agent/agent.config && rm /agent/agent.config.bak
20+
sed -i.bak "s|pinned_certs=.*|pinned_certs=|" /agent/agent.config && rm /agent/agent.config.bak && \
21+
# In openshift UID will be dynamic per project, hence chmo and not chown
22+
chmod -R 777 /agent
23+
24+
USER 1000
2125

2226
COPY lightrun-init-agent/update_config.sh /update_config.sh
2327

lightrun-init-agent/update_config.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ echo "Set server and secrets"
2424
sed -i.bak "s|com.lightrun.server=.*|com.lightrun.server=https://$LIGHTRUN_SERVER|" /agent/agent.config && rm /agent/agent.config.bak
2525
sed -i.bak "s|com.lightrun.secret=.*|com.lightrun.secret=$LIGHTRUN_KEY|" /agent/agent.config && rm /agent/agent.config.bak
2626
sed -i.bak "s|pinned_certs=.*|pinned_certs=$PINNED_CERT|" /agent/agent.config && rm /agent/agent.config.bak
27-
mv /agent /tmp/agent
27+
cp -R /agent /tmp/agent
2828
echo "Finished"

0 commit comments

Comments
 (0)