-
Notifications
You must be signed in to change notification settings - Fork 25
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
Minimise all root-owned files/directories in the running CKAN container #80
Changes from all commits
3ec895b
9fec5e3
ebf531d
b253bd9
21ec7c9
4a8efe3
6c8f240
475dbbf
6dea421
3b8b8fb
51e9cac
ac04d86
b9f6059
c4e5a3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,6 @@ RUN pip3 install -U pip && \ | |
cd ${SRC_DIR} && \ | ||
pip3 install -e git+${GIT_URL}@${CKAN_VERSION}#egg=ckan && \ | ||
cd ckan && \ | ||
cp who.ini ${APP_DIR} && \ | ||
pip3 install --no-binary markdown -r requirements.txt && \ | ||
# Install CKAN envvars to support loading config from environment variables | ||
pip3 install -e git+https://github.com/okfn/[email protected]#egg=ckanext-envvars && \ | ||
|
@@ -69,19 +68,31 @@ RUN pip3 install -U pip && \ | |
ckan config-tool ${CKAN_INI} "beaker.session.secret = " && \ | ||
ckan config-tool ${CKAN_INI} "ckan.plugins = ${CKAN__PLUGINS}" | ||
|
||
# Create a local user and group plus set up the storage path | ||
RUN groupadd -g 92 ckan && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan -u 92 ckan && \ | ||
mkdir -p ${CKAN_STORAGE_PATH} && \ | ||
chown -R ckan:ckan ${CKAN_STORAGE_PATH} | ||
|
||
# Create ckan and ckan-sys users and the ckan-sys group plus set up the storage path | ||
RUN groupadd -g 502 ckan-sys && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan-sys -u 502 ckan-sys && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan-sys -u 503 ckan | ||
|
||
COPY setup/prerun.py ${APP_DIR} | ||
COPY setup/start_ckan.sh ${APP_DIR} | ||
ADD https://raw.githubusercontent.com/ckan/ckan/${CKAN_VERSION}/wsgi.py ${APP_DIR} | ||
RUN chmod 644 ${APP_DIR}/wsgi.py | ||
|
||
# Create entrypoint directory for children image scripts | ||
ONBUILD RUN mkdir /docker-entrypoint.d | ||
RUN mkdir -p /docker-entrypoint.d && chmod 755 /docker-entrypoint.d | ||
|
||
# Set the ownership of the app directory, usr/local and the entrypoint directory to the ckan-sys user | ||
RUN chown -R ckan-sys:ckan-sys ${APP_DIR} && \ | ||
chown -R ckan-sys:ckan-sys /docker-entrypoint.d && \ | ||
chown -R ckan-sys:ckan-sys /usr/local | ||
|
||
# Set the ownership of the CKAN config file, src and the storage path to the ckan user | ||
wardi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RUN chown ckan:ckan-sys ${APP_DIR}/ckan.ini && \ | ||
chown -R ckan:ckan-sys ${APP_DIR}/src && \ | ||
mkdir -p ${CKAN_STORAGE_PATH} && \ | ||
chown -R ckan:ckan-sys ${CKAN_STORAGE_PATH} | ||
|
||
USER ckan | ||
|
||
EXPOSE 5000 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,7 +200,7 @@ def create_sysadmin(): | |
# We're running as root before pivoting to uwsgi and dropping privs | ||
data_dir = "%s/storage" % os.environ['CKAN_STORAGE_PATH'] | ||
|
||
command = ["chown", "-R", "ckan:ckan", data_dir] | ||
command = ["chown", "-R", "ckan:ckan-sys", data_dir] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On 2.10, the prerun.py script will still be used by both the alpine (Dockerfile) and Python (Dockerfile.py3.10) based images but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this in #85 (612d0b916ec917c05bb31a67b5d5dff5d5f2e3fc) |
||
subprocess.call(command) | ||
print("[prerun] Ensured storage directory is owned by ckan") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/bin/bash | ||
|
||
if [ $(id -u) -ne 0 ]; then | ||
echo "Please run as root" | ||
exit 1 | ||
fi | ||
|
||
# Install any local extensions in the src_extensions volume | ||
echo "Looking for local extensions to install..." | ||
echo "Extension dir contents:" | ||
ls -la $SRC_EXTENSIONS_DIR | ||
for i in $SRC_EXTENSIONS_DIR/* | ||
do | ||
if [ -d $i ]; | ||
then | ||
if [ -d $SRC_DIR/$(basename $i) ]; | ||
then | ||
pip uninstall -y "$(basename $i)" | ||
fi | ||
|
||
if [ -f $i/pip-requirements.txt ]; | ||
then | ||
pip install -r $i/pip-requirements.txt | ||
echo "Found requirements file in $i" | ||
fi | ||
if [ -f $i/requirements.txt ]; | ||
then | ||
pip install -r $i/requirements.txt | ||
echo "Found requirements file in $i" | ||
fi | ||
if [ -f $i/dev-requirements.txt ]; | ||
then | ||
pip install -r $i/dev-requirements.txt | ||
echo "Found dev-requirements file in $i" | ||
fi | ||
if [ -f $i/setup.py ]; | ||
then | ||
cd $i | ||
python3 $i/setup.py develop | ||
echo "Found setup.py file in $i" | ||
cd $APP_DIR | ||
fi | ||
if [ -f $i/pyproject.toml ]; | ||
then | ||
cd $i | ||
pip install -e . | ||
echo "Found pyproject.toml file in $i" | ||
cd $APP_DIR | ||
fi | ||
|
||
# Point `use` in test.ini to location of `test-core.ini` | ||
if [ -f $i/test.ini ]; | ||
then | ||
echo "Updating \`test.ini\` reference to \`test-core.ini\` for plugin $i" | ||
ckan config-tool $i/test.ini "use = config:../../src/ckan/test-core.ini" | ||
fi | ||
fi | ||
done |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ RUN update-locale LANG=${LC_ALL} | |
|
||
# Install system libraries | ||
RUN apt-get install --no-install-recommends -y \ | ||
apt-utils \ | ||
apt-utils \ | ||
git \ | ||
libpq-dev \ | ||
g++ \ | ||
|
@@ -61,20 +61,32 @@ RUN pip3 install -U pip && \ | |
ckan config-tool ${CKAN_INI} "SECRET_KEY = " && \ | ||
ckan config-tool ${CKAN_INI} "ckan.plugins = ${CKAN__PLUGINS}" | ||
|
||
# Create a local user and group plus set up the storage path | ||
RUN groupadd -g 92 ckan && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan -u 92 ckan && \ | ||
mkdir -p ${CKAN_STORAGE_PATH} && \ | ||
chown -R ckan:ckan ${CKAN_STORAGE_PATH} | ||
# Create ckan and ckan-sys users and the ckan-sys group plus set up the storage path | ||
RUN groupadd -g 502 ckan-sys && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan-sys -u 502 ckan-sys && \ | ||
useradd -rm -d /srv/app -s /bin/bash -g ckan-sys -u 503 ckan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment about home directories above |
||
|
||
COPY setup/prerun.py ${APP_DIR} | ||
COPY setup/start_ckan.sh ${APP_DIR} | ||
ADD https://raw.githubusercontent.com/ckan/ckan/${CKAN_VERSION}/wsgi.py ${APP_DIR} | ||
RUN chmod 644 ${APP_DIR}/wsgi.py | ||
|
||
# Create entrypoint directory for children image scripts | ||
ONBUILD RUN mkdir /docker-entrypoint.d | ||
RUN mkdir -p /docker-entrypoint.d && chmod 755 /docker-entrypoint.d | ||
|
||
# Set the ownership of the app directory, usr/local and the entrypoint directory to the ckan-sys user | ||
RUN chown -R ckan-sys:ckan-sys ${APP_DIR} && \ | ||
chown -R ckan-sys:ckan-sys /docker-entrypoint.d && \ | ||
chown -R ckan-sys:ckan-sys /usr/local | ||
|
||
# Set the ownership of the CKAN config file, src and the storage path to the ckan user | ||
RUN chown ckan:ckan-sys ${APP_DIR}/ckan.ini && \ | ||
chown -R ckan:ckan-sys ${APP_DIR}/src && \ | ||
mkdir -p ${CKAN_STORAGE_PATH} && \ | ||
chown -R ckan:ckan-sys ${CKAN_STORAGE_PATH} | ||
|
||
USER ckan | ||
|
||
EXPOSE 5000 | ||
|
||
CMD ["/srv/app/start_ckan.sh"] | ||
CMD ["/srv/app/start_ckan.sh"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having home directories owned by the user is nice for keeping track of things like
.bash_history
when logging in to the containerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the ckan user has the same primary group as ckan-sys we'll need to make sure the group permissions on all the ckan source files prevent writing. I'm not used to setting things up this way but there may be other issues as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is set up this way by default. The
ckan
user has write permissions only on the files/directories it needs to write into. Theckan-sys
user does not have write access. However I have made theckan-sys
group the primary group for bothckan
andckan-sys
users in case further down the track we need group write access for more granularity