Skip to content

Commit 752c7bf

Browse files
author
Pedro Crespo
committed
WIP ITISFoundation#198: non-root user
- all volumes bound to /home/scu - All modules pip installed (in dev w/ edit mode). - All files produce in dev mode on bound volumes are not deletable - Production stage is further optimized by taking only venv from base - Fixes sidecar access to input/output/log volumes
1 parent ec755bc commit 752c7bf

9 files changed

+96
-44
lines changed

.env-devel

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# NOTE: write here host gid and docker gid.
2+
HOST_GID=1000
3+
DOCKER_GID=1001
4+
#--------
15
POSTGRES_ENDPOINT=postgres:5432
26
POSTGRES_USER=simcore
37
POSTGRES_PASSWORD=simcore

services/docker-compose.devel.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ services:
2929
sidecar:
3030
image: services_sidecar:dev
3131
build:
32+
args:
33+
- HOST_GID_ARG=${HOST_GID:?Undefined host gid}
3234
target: development
3335
volumes:
34-
- ./sidecar:/work/services/sidecar
35-
- ../packages:/work/packages
36+
- ./sidecar:/home/scu/services/sidecar
37+
- ../packages:/home/scu/packages

services/docker-compose.swarm.yml.template

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ services:
8787
- S3_SECRET_KEY=<enter secret key>
8888
- S3_BUCKET_NAME=simcore
8989
volumes:
90-
- input:/input
91-
- output:/output
92-
- log:/log
90+
- input:/home/scu/input
91+
- output:/home/scu/output
92+
- log:/home/scu/log
9393
- /var/run/docker.sock:/var/run/docker.sock
9494
ports:
9595
- "8000:8000"

services/docker-compose.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,17 @@ services:
9595
# the packages directory into any docker image
9696
context: ../
9797
dockerfile: services/sidecar/Dockerfile
98+
args:
99+
- DOCKER_GID_ARG=${DOCKER_GID:?Undefined docker gid in host}
98100
target: production
99101
volumes:
100-
- input:/input
101-
- output:/output
102-
- log:/log
102+
- input:/home/scu/input
103+
- output:/home/scu/output
104+
- log:/home/scu/log
103105
- /var/run/docker.sock:/var/run/docker.sock
104106
ports:
105107
- "8000:8000"
106108
environment:
107-
- PYTHONPATH=/work/packages/simcore-sdk/src:/work/packages/s3wrapper/src
108109
- RABBITMQ_USER=${RABBITMQ_USER}
109110
- RABBITMQ_PASSWORD=${RABBITMQ_PASSWORD}
110111
- POSTGRES_ENDPOINT=${POSTGRES_ENDPOINT}

services/sidecar/Dockerfile

+61-28
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,91 @@
1-
FROM python:3.6-alpine as common
1+
FROM python:3.6-alpine as base
22

3-
LABEL maintainer="Manuel Guidon <[email protected]"
3+
LABEL maintainer="Manuel Guidon guidon"
4+
5+
ARG DOCKER_GID_ARG=1001
6+
7+
# create user 'scu' and adds it to host's docker group
8+
RUN adduser -D -u 8004 scu &&\
9+
addgroup -g $DOCKER_GID_ARG docker &&\
10+
addgroup scu docker
11+
12+
ENV HOME /home/scu
13+
ENV PIP /home/scu/.venv/bin/pip3
14+
15+
EXPOSE 8000
16+
VOLUME /home/scu/input
17+
VOLUME /home/scu/output
18+
VOLUME /home/scu/log
19+
#VOLUME /var/run/docker.sock TODO: PC->MaG is this using docker??
20+
21+
# -------------------------- Build stage -------------------
22+
# Keeps same folder structure as in repo so we can reuse relative paths
23+
#
24+
# + /home/scu/ $HOME
25+
# + services/sidecar
26+
# ...
27+
# + packages
28+
# ...
29+
FROM base as build
430

531
RUN apk add --no-cache \
632
postgresql-dev \
733
gcc \
834
libc-dev
935

10-
RUN pip install --upgrade \
36+
RUN python3 -m venv $HOME/.venv &&\
37+
$PIP install --no-cache-dir --upgrade \
1138
pip \
1239
wheel \
1340
setuptools
1441

15-
WORKDIR /work
42+
WORKDIR /home/scu
43+
1644
# Buil context set at repo's root
17-
COPY services/sidecar/requirements requirements
45+
COPY --chown=scu:scu services/sidecar/requirements requirements
1846

19-
RUN pip install -r requirements/base.txt &&\
47+
RUN $PIP install --no-cache-dir -r requirements/base.txt &&\
2048
rm -rf requirements
2149

22-
# Keeps same folder structure as in repo so we can reuse relative paths
23-
RUN mkdir -p /work/packages &&\
24-
mkdir -p /work/services/sidecar
50+
# --------------------------Development stage -------------------
51+
FROM build as development
2552

26-
EXPOSE 8000
53+
ARG HOST_GID_ARG=1000
2754

55+
# in dev-mode we give access to `scu` to host's mapped volumes
56+
RUN addgroup -g $HOST_GID_ARG hgrp &&\
57+
addgroup scu hgrp && \
58+
chown -R scu:scu $HOME/.venv
2859

29-
# --------------------------Development stage -------------------
30-
FROM common as development
31-
32-
VOLUME /work/packages
33-
VOLUME /work/services/sidecar
60+
VOLUME /home/scu/packages
61+
VOLUME /home/scu/services/sidecar
3462

63+
USER scu
3564
ENV DEBUG 1
36-
WORKDIR /work/services/sidecar
65+
WORKDIR /home/scu/services/sidecar
3766
CMD ./boot.sh
38-
# FIXME: executing this as root will create folders (e.g. eggs) in the mapped
3967

4068

41-
# --------------------------Production stage -------------------
42-
FROM common as production
69+
# --------------------------Production mult-stage -------------------
70+
FROM build as build-production
4371

4472
# Buil context set at repo's root
45-
COPY packages /work/packages
46-
COPY services/sidecar /work/services/sidecar
73+
COPY --chown=scu:scu packages $HOME/packages
74+
COPY --chown=scu:scu services/sidecar $HOME/services/sidecar
75+
76+
WORKDIR /home/scu/services/sidecar
77+
RUN $PIP --no-cache-dir install -r requirements/prod.txt ;\
78+
$PIP list
79+
80+
#-------------------
81+
FROM base as production
4782

48-
WORKDIR /work/services/sidecar
83+
COPY --from=build-production --chown=scu:scu $HOME/services/sidecar/boot.sh $HOME
84+
COPY --from=build-production --chown=scu:scu $HOME/.venv $HOME/.venv
4985

50-
RUN pip install -r requirements/prod.txt ;\
51-
pip list &&\
52-
mv boot.sh /work &&\
53-
rm -rf /work/packages &&\
54-
rm -rf /work/services/sidecar
86+
RUN . $HOME/.venv/bin/activate; pip list
5587

88+
WORKDIR /home/scu
89+
USER scu
5690
ENV DEBUG 0
57-
WORKDIR /work
5891
ENTRYPOINT ./boot.sh

services/sidecar/boot.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/sh
2+
source $HOME/.venv/bin/activate
23

34
if [[ ${DEBUG} == "1" ]]
45
then
56
echo "Booting in development mode ..."
67
echo "Installing director service ..."
78

8-
pip install -r requirements/dev.txt
9+
pip3 install --no-cache-dir -r requirements/dev.txt
910
celery worker --app sidecar --concurrency 2 --loglevel=debug
1011
else
1112
echo "Booting in production mode ..."

services/sidecar/src/sidecar/_deprecated.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# TODO: PC->MaG, please check if something missing and delete
2+
13
import json
24
import logging
35
import os

services/sidecar/src/sidecar/celery.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@
1919
celery_obj= Celery(rabbit_config.name,
2020
broker=rabbit_config.broker,
2121
backend=rabbit_config.backend)
22+
23+
24+
__all__ = [
25+
"rabbit_config",
26+
"celery_obj"
27+
]

services/sidecar/src/sidecar/core.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
find_entry_point, is_node_ready)
2121

2222
_LOGGER = get_task_logger(__name__)
23-
_LOGGER.setLevel(logging.DEBUG)
23+
_LOGGER.setLevel(logging.DEBUG) # FIXME: set level via config
2424

2525

2626
class Sidecar:
@@ -271,11 +271,12 @@ def _process_task_log(self):
271271
def initialize(self, task):
272272
self._task = task
273273

274+
HOMEDIR = os.environ["HOME"]
274275
self._docker.image_name = self._docker.registry_name + "/" + task.image['name']
275276
self._docker.image_tag = task.image['tag']
276-
self._executor.in_dir = os.path.join("/", "input", task.job_id)
277-
self._executor.out_dir = os.path.join("/", "output", task.job_id)
278-
self._executor.log_dir = os.path.join("/", "log", task.job_id)
277+
self._executor.in_dir = os.path.join(HOMEDIR, "input", task.job_id)
278+
self._executor.out_dir = os.path.join(HOMEDIR, "output", task.job_id)
279+
self._executor.log_dir = os.path.join(HOMEDIR, "log", task.job_id)
279280

280281
self._docker.env = ["INPUT_FOLDER=" + self._executor.in_dir,
281282
"OUTPUT_FOLDER=" + self._executor.out_dir,
@@ -445,7 +446,9 @@ def inspect(self, celery_task, pipeline_id, node_id):
445446
return next_task_nodes
446447

447448

448-
449-
450449
# TODO: if a singleton, then use
451450
SIDECAR = Sidecar()
451+
452+
__all__ = [
453+
"SIDECAR"
454+
]

0 commit comments

Comments
 (0)