Skip to content

Commit

Permalink
run containers securely (except kaniko - GoogleContainerTools/kaniko#105
Browse files Browse the repository at this point in the history
)
  • Loading branch information
srfrnk committed Dec 11, 2021
1 parent 49f7d34 commit 8cd8d3f
Show file tree
Hide file tree
Showing 37 changed files with 171 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ jobs:
--tla-str 'imagePrefix=${{env.REGISTRY}}/${{env.IMAGE_PREFIX}}' \
--tla-str 'buildNumber=${{env.VERSION}}.${{env.BUILD_NUMBER}}' \
--tla-str 'namespace=${{env.NAMESPACE}}' \
--tla-str 'debug=false' \
--tla-str 'isProduction=true' \
> ./build/jabos-manifests.yaml
cp ./build/jabos-manifests.yaml ./build/input/jabos-manifests.yaml
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ manifests: FORCE build_number
--tla-str 'imagePrefix=' \
--tla-str 'buildNumber=${BUILD_NUMBER}' \
--tla-str 'namespace=jabos' \
--tla-str 'debug=true' \
--tla-str 'isProduction=false' \
> build/manifests.yaml

build: FORCE images manifests
Expand All @@ -77,9 +77,9 @@ deploy-examples: FORCE
kubectl apply -f ../jabos-examples-gitlab/simple-build.yaml

un-deploy-examples: FORCE
kubectl delete -f ../jabos-examples/simple-build.yaml
kubectl delete -f ../jabos-examples-private/simple-build.yaml
kubectl delete -f ../jabos-examples-gitlab/simple-build.yaml
- kubectl delete -f ../jabos-examples/simple-build.yaml
- kubectl delete -f ../jabos-examples-private/simple-build.yaml
- kubectl delete -f ../jabos-examples-gitlab/simple-build.yaml

service-port-forward: FORCE
parallel --linebuffer -j0 eval kubectl port-forward -n {} ::: "efk svc/efk-kibana 5601" "monitoring svc/kube-prometheus-stack-grafana 3000:80"
Expand Down
4 changes: 2 additions & 2 deletions base-manifest-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine

RUN apk add --update curl wget bash coreutils

RUN adduser -s /bin/bash -S user
RUN adduser -s /bin/bash -S user -u 1000

RUN mkdir /build && chown user /build

Expand All @@ -11,6 +11,6 @@ RUN wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64

COPY ./build.sh /

USER user
USER 1000

ENTRYPOINT ["/build.sh"]
4 changes: 2 additions & 2 deletions docker-image-builder-init/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ RUN apk add --update curl wget git bash
RUN wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq &&\
chmod +x /usr/bin/yq

RUN adduser -s /bin/bash -S user
RUN adduser -s /bin/bash -S user -u 1000

COPY ./init.sh /

USER user
USER 1000

ENTRYPOINT ["/init.sh"]
4 changes: 1 addition & 3 deletions git-repository-updater/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ USER root

RUN apk add --update bc git openssh-client

RUN mkdir /gitTemp && chown user /gitTemp

RUN mkdir /home/user/.ssh &&\
printf "Host *\n\tStrictHostKeyChecking no\n" >> /home/user/.ssh/config &&\
touch /home/user/.ssh/known_hosts &&\
chown user /home/user/.ssh/known_hosts

COPY ./get_latest_commit.sh /

USER user
USER 1000

ENTRYPOINT ["/get_latest_commit.sh"]
5 changes: 2 additions & 3 deletions git-repository-updater/get_latest_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ curl -s -X POST "${JABOS_OPERATOR_URL}addMetric/gitRepositoryUpdaterStart" \
source /kubectl-setup.sh

if [ -n "${SSH_KEY}" ]; then
eval "$(ssh-agent -s)" >&2
echo "${SSH_PASSPHRASE}" | setsid ssh-add <(printf -- "${SSH_KEY}") >&2
eval "$(ssh-agent -s)"
echo "${SSH_PASSPHRASE}" | setsid ssh-add <(printf -- "${SSH_KEY}")
fi

git clone --bare --single-branch --depth 1 --branch ${BRANCH} ${URL} /gitTemp

cd /gitTemp
LATEST_COMMIT=$(git log -n 1 --pretty=format:"%H" | head -n 1)

Expand Down
2 changes: 1 addition & 1 deletion helm-template-manifest-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ RUN wget https://get.helm.sh/helm-v3.7.2-linux-amd64.tar.gz -O - | tar -xz && \

COPY ./build.sh /

USER user
USER 1000
2 changes: 1 addition & 1 deletion jsonnet-manifest-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN wget https://github.com/google/go-jsonnet/releases/download/v0.17.0/go-jsonn

COPY ./build.sh /

USER user
USER 1000
4 changes: 2 additions & 2 deletions kubectl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ RUN \
RUN wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq &&\
chmod +x /usr/bin/yq

RUN adduser -s /bin/bash -S user
RUN adduser -s /bin/bash -S user -u 1000

COPY ./kubectl-setup.sh /
COPY ./kube-config.yaml /home/user/.kube/config

RUN chown -R user /home/user

USER user
USER 1000

ENTRYPOINT ["/setup.sh"]
2 changes: 1 addition & 1 deletion kustomize-manifest-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ COPY --from=kustomize /app/kustomize /usr/bin/kustomize

COPY ./build.sh /

USER user
USER 1000
2 changes: 1 addition & 1 deletion manifests/DockerImage.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
kube.CRD(kind='DockerImage',
singular='docker-image',
Expand Down
2 changes: 1 addition & 1 deletion manifests/DockerImageController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
local metacontroller = import './metacontroller.libsonnet';
metacontroller.DecoratorController(namespace=namespace,
Expand Down
2 changes: 1 addition & 1 deletion manifests/GitRepository.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
kube.CRD(kind='GitRepository',
singular='git-repository',
Expand Down
2 changes: 1 addition & 1 deletion manifests/GitRepositoryController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
local metacontroller = import './metacontroller.libsonnet';
metacontroller.DecoratorController(namespace=namespace,
Expand Down
2 changes: 1 addition & 1 deletion manifests/GrafanaDashboards.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local grafanaDashboard = import './grafana-dashboard.libsonnet';
local globals = import './globals.libsonnet';
grafanaDashboard.GrafanaDashboard(name='grafana-dashboards', namespace=namespace, grafonnet={
Expand Down
2 changes: 1 addition & 1 deletion manifests/HelmTemplateManifests.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.CRD(
description='`HelmTemplateManifest` objects define a folder with a [helm](https://helm.sh/) chart to deploy with `helm template`.',
Expand Down
2 changes: 1 addition & 1 deletion manifests/HelmTemplateManifestsController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.Controller(namespace=namespace, name='helm-template')
)
2 changes: 1 addition & 1 deletion manifests/JsonnetManifests.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.CRD(
description='`JsonnetManifest` objects define a folder with [jsonnet](https://jsonnet.org/) based manifests to deploy.',
Expand Down
2 changes: 1 addition & 1 deletion manifests/JsonnetManifestsController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.Controller(namespace=namespace, name='jsonnet')
)
2 changes: 1 addition & 1 deletion manifests/KustomizeManifests.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.CRD(
description='`KustomizeManifest` objects define a folder with [Kustomize](https://kustomize.io/) based manifests to deploy.',
Expand Down
2 changes: 1 addition & 1 deletion manifests/KustomizeManifestsController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.Controller(namespace=namespace, name='kustomize')
)
8 changes: 4 additions & 4 deletions manifests/OperatorDeployment.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
local globals = import './globals.libsonnet';
kube.Deployment(
Expand All @@ -7,7 +7,7 @@ function(imagePrefix, buildNumber, namespace, debug) (
replicas=1,
serviceAccountName='operator',
containers=[
kube.Container(name='operator', image=imagePrefix + 'operator:' + buildNumber) +
kube.Container(name='operator', image=imagePrefix + 'operator:' + buildNumber, imagePullPolicy=(if isProduction == 'true' then 'Always' else 'IfNotPresent')) +
{
env+: [
{
Expand All @@ -23,8 +23,8 @@ function(imagePrefix, buildNumber, namespace, debug) (
value: buildNumber,
},
{
name: 'DEBUG',
value: debug,
name: 'IS_PRODUCTION',
value: isProduction,
},
{
name: 'PROMETHEUS_METRIC_PREFIX',
Expand Down
2 changes: 1 addition & 1 deletion manifests/OperatorService.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
local monitoring = import './monitoring.libsonnet';
local globals = import './globals.libsonnet';
Expand Down
2 changes: 1 addition & 1 deletion manifests/OperatorServiceAccount.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local kube = import './kube.libsonnet';
local globals = import './globals.libsonnet';
kube.ServiceAccount(namespace=namespace, name='operator')
Expand Down
2 changes: 1 addition & 1 deletion manifests/PlainManifests.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.CRD(
description='`PlainManifest` objects define a folder with plain manifests to deploy (YAML/YML/JSON).',
Expand Down
2 changes: 1 addition & 1 deletion manifests/PlainManifestsController.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(imagePrefix, buildNumber, namespace, debug) (
function(imagePrefix, buildNumber, namespace, isProduction) (
local manifests = (import './manifests.libsonnet');
manifests.Controller(namespace=namespace, name='plain')
)
2 changes: 1 addition & 1 deletion operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ RUN npm install

COPY . /app

USER node
USER 1000

ENTRYPOINT ["./start.sh"]
65 changes: 63 additions & 2 deletions operator/builderJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ export default function (options: {
metricLabels: {},
labels: {}
}) {
options.containers.forEach(container => {
container.imagePullPolicy = settings.imagePullPolicy();

container.securityContext = {
...container.securityContext,
"readOnlyRootFilesystem": true,
"allowPrivilegeEscalation": false,
"runAsNonRoot": true,
"capabilities": {
"drop": ['ALL'],
},
};

container.volumeMounts = [
...container.volumeMounts,
{
"name": "temp",
"mountPath": "/tmp",
},
{
"name": "build",
"mountPath": "/build",
}
];
});

return {
"apiVersion": "batch/v1",
"kind": "Job",
Expand All @@ -43,6 +69,9 @@ export default function (options: {
"spec": {
"serviceAccountName": options.serviceAccountName,
"restartPolicy": "OnFailure",
"securityContext": {
"runAsNonRoot": true,
},
"initContainers": [
{
"image": `${settings.imagePrefix()}pre-builder:${settings.buildNumber()}`,
Expand All @@ -67,7 +96,15 @@ export default function (options: {
}
}
])],
"imagePullPolicy": "IfNotPresent",
"imagePullPolicy": settings.imagePullPolicy(),
"securityContext": {
"readOnlyRootFilesystem": true,
"allowPrivilegeEscalation": false,
"runAsNonRoot": true,
"capabilities": {
"drop": ['ALL'],
},
},
"name": "pre-builder",
"resources": {
"limits": {
Expand All @@ -84,6 +121,10 @@ export default function (options: {
"name": "git-temp",
"mountPath": "/gitTemp",
},
{
"name": "temp",
"mountPath": "/tmp",
},
{
"name": "timer",
"mountPath": "/timer",
Expand All @@ -102,9 +143,21 @@ export default function (options: {
"name": "timer",
"mountPath": "/timer",
"readOnly": true
},
{
"name": "temp",
"mountPath": "/tmp"
}
],
"imagePullPolicy": "IfNotPresent",
"imagePullPolicy": settings.imagePullPolicy(),
"securityContext": {
"readOnlyRootFilesystem": true,
"allowPrivilegeEscalation": false,
"runAsNonRoot": true,
"capabilities": {
"drop": ['ALL'],
},
},
"name": "post-builder",
"resources": {
"limits": {
Expand All @@ -124,6 +177,14 @@ export default function (options: {
"name": "git-temp",
"emptyDir": {}
},
{
"name": "temp",
"emptyDir": {}
},
{
"name": "build",
"emptyDir": {}
},
{
"name": "timer",
"emptyDir": {}
Expand Down
Loading

0 comments on commit 8cd8d3f

Please sign in to comment.