Skip to content
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

feat: Do not change permissions on existing data directory #49

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions snap/hooks/install
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
#!/bin/bash
#!/usr/bin/env bash
# Installation hook for charmed-mongodb snap

set -eux

export CONF="${SNAP_DATA}"/etc/mongod
export PBM_CONF="${SNAP_DATA}"/etc/pbm
export DATA="${SNAP_COMMON}"/var/lib/mongodb
export LOGS="${SNAP_COMMON}"/var/log/mongodb
export MONGO_CONFIG_FILE="${CONF}"/mongod.conf

# Create the necessary parent directories
mkdir -p "${SNAP_DATA}/etc/pbm/"
mkdir -p "${SNAP_DATA}/etc/mongod/"
mkdir -p "${SNAP_COMMON}/var/lib/mongodb/"
mkdir -p "${SNAP_COMMON}/var/log/mongodb/"
mkdir -p $DATA
mkdir -p $LOGS
mkdir -p $PBM_CONF
mkdir -p $CONF

# If we just created the directory, we set up permissions
if [ stat -c '%u' "$DATA" == 0 ]; then
chmod -R 770 "${SNAP_COMMON}"
chmod 750 "${DATA}"
fi

# If we just created the directory, we set up permissions
if [ stat -c '%u' "$LOGS" == 0 ]; then
chmod -R 770 "${SNAP_COMMON}"
chmod g+s "$LOGS"/*
fi

chown -R 584788:root "${SNAP_COMMON}"/*

# Copy over the mongod.conf and create needed directories/files
MONGO_CONFIG_FILE="${SNAP_DATA}/etc/mongod/mongod.conf"
echo "configuration file does not exist."
echo "copying default config to ${MONGO_CONFIG_FILE}"
cp -r ${SNAP}/etc/mongod.conf ${MONGO_CONFIG_FILE}


# mongod.conf default values are not consistent with the snap directory system.
sed -i "s/fork: true/fork: false/g" $MONGO_CONFIG_FILE
sed -i "s:/var/log/mongodb:$SNAP_COMMON/var/log/mongodb:g" $MONGO_CONFIG_FILE
sed -i "s:/var/lib/mongodb:$SNAP_COMMON/var/lib/mongodb:g" $MONGO_CONFIG_FILE
sed -i "s:/var/log/mongodb:$LOGS:g" $MONGO_CONFIG_FILE
sed -i "s:/var/lib/mongodb:$DATA:g" $MONGO_CONFIG_FILE
sed -i "s:/var/run:/tmp:g" $MONGO_CONFIG_FILE

# Change ownership of snap directories to allow snap_daemon to read/write
chmod g+s "${SNAP_COMMON}/var/log/"*
chown -R 584788:root "${SNAP_DATA}"/*
chown -R 584788:root "${SNAP_COMMON}"/*
chgrp root "${SNAP_COMMON}/var/log/"*
13 changes: 7 additions & 6 deletions snap/local/drop_priv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
export PBM_MONGODB_URI="$(snapctl get pbm-uri)"

if [[ $(id -u) == "0" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?


exec "${SNAP}"/usr/bin/setpriv \
exec bash -c "cd ${SNAP} && \
${SNAP}/usr/bin/setpriv \
--clear-groups \
--reuid snap_daemon \
--regid snap_daemon -- \
"$SNAP/usr/bin/$@"
--regid snap_daemon \
-- \
${SNAP}/usr/bin/$*"
else

exec "$SNAP/usr/bin/$@"
exec bash -c "cd ${SNAP} && \
${SNAP}/usr/bin/$*"
fi
Loading