Skip to content

Commit

Permalink
feat: restricted security context
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Jan 8, 2023
1 parent 171ee6d commit 5faae35
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
10 changes: 10 additions & 0 deletions bento-downloader/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ RUN curl https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud
&& tar -xf google-cloud-cli-410.tar.gz \
&& ./google-cloud-sdk/install.sh \
&& rm google-cloud-cli-410.tar.gz

ARG USERNAME=yetone
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME

USER $USER_UID
2 changes: 1 addition & 1 deletion bento-downloader/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
IMAGE := quay.io/bentoml/bento-downloader:0.0.1
IMAGE := quay.io/bentoml/bento-downloader:0.0.3

build:
docker build -t ${IMAGE} .
Expand Down
33 changes: 27 additions & 6 deletions controllers/resources/bentorequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -1170,6 +1171,17 @@ echo "Done"
})
}

restrictedSecurityContext := &corev1.SecurityContext{
AllowPrivilegeEscalation: pointer.BoolPtr(false),
RunAsNonRoot: pointer.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
}

initContainers := []corev1.Container{
{
Name: "bento-downloader",
Expand All @@ -1179,9 +1191,10 @@ echo "Done"
"-c",
bentoDownloadCommand,
},
VolumeMounts: volumeMounts,
Resources: downloaderContainerResources,
EnvFrom: downloaderContainerEnvFrom,
VolumeMounts: volumeMounts,
Resources: downloaderContainerResources,
EnvFrom: downloaderContainerEnvFrom,
SecurityContext: restrictedSecurityContext,
},
}

Expand Down Expand Up @@ -1294,9 +1307,10 @@ echo "Done"
"-c",
modelDownloadCommand,
},
VolumeMounts: volumeMounts,
Resources: downloaderContainerResources,
EnvFrom: downloaderContainerEnvFrom,
VolumeMounts: volumeMounts,
Resources: downloaderContainerResources,
EnvFrom: downloaderContainerEnvFrom,
SecurityContext: restrictedSecurityContext,
})
}

Expand Down Expand Up @@ -1498,6 +1512,7 @@ echo "Done"
Env: envs,
TTY: true,
Stdin: true,
SecurityContext: restrictedSecurityContext,
}

if opt.BentoRequest.Spec.ImageBuilderContainerResources != nil {
Expand All @@ -1517,6 +1532,12 @@ echo "Done"
Containers: []corev1.Container{
container,
},
SecurityContext: &corev1.PodSecurityContext{
RunAsNonRoot: pointer.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
},
}

Expand Down
20 changes: 10 additions & 10 deletions helm/yatai-image-builder/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ serviceAccount:

podAnnotations: {}

podSecurityContext: {}
# fsGroup: 2000
podSecurityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault

securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL

service:
type: ClusterIP
Expand Down Expand Up @@ -96,5 +96,5 @@ aws:
secretAccessKeyExistingSecretKey: ''

internalImages:
bentoDownloader: quay.io/bentoml/bento-downloader:0.0.1
bentoDownloader: quay.io/bentoml/bento-downloader:0.0.3
kaniko: quay.io/bentoml/kaniko:1.9.1

0 comments on commit 5faae35

Please sign in to comment.