Skip to content

Use check permissions script and consolidation #1054

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

Merged
merged 7 commits into from
Apr 9, 2025
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.

### Added

- airflow: check for correct permissions and ownerships in /stackable folder via
`check-permissions-ownership.sh` provided in stackable-base image ([#1054]).
- hadoop: check for correct permissions and ownerships in /stackable folder via
`check-permissions-ownership.sh` provided in stackable-base image ([#1029]).
- hbase: check for correct permissions and ownerships in /stackable folder via
Expand Down Expand Up @@ -46,6 +48,7 @@ All notable changes to this project will be documented in this file.
[#1042]: https://github.com/stackabletech/docker-images/pull/1042
[#1044]: https://github.com/stackabletech/docker-images/pull/1044
[#1050]: https://github.com/stackabletech/docker-images/pull/1050
[#1054]: https://github.com/stackabletech/docker-images/pull/1054

## [25.3.0] - 2025-03-21

Expand Down
63 changes: 38 additions & 25 deletions airflow/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ARG PRODUCT
ARG STATSD_EXPORTER
ARG PYTHON
ARG TARGETARCH
ARG STACKABLE_USER_UID

COPY airflow/constraints-${PRODUCT}-python${PYTHON}.txt /tmp/constraints.txt
COPY --from=opa-auth-manager-builder /tmp/opa-auth-manager/dist/opa_auth_manager-0.1.0-py3-none-any.whl /tmp/
Expand Down Expand Up @@ -85,9 +86,17 @@ else
end)' /tmp/sbom.json > /stackable/app/airflow-${PRODUCT}.cdx.json
EOF

WORKDIR /stackable
COPY --from=statsd_exporter-builder /statsd_exporter/statsd_exporter /stackable/statsd_exporter
COPY --from=statsd_exporter-builder /statsd_exporter/statsd_exporter-${STATSD_EXPORTER}.cdx.json /stackable/statsd_exporter-${STATSD_EXPORTER}.cdx.json
COPY --from=gitsync-image --chown=${STACKABLE_USER_UID}:0 /git-sync /stackable/git-sync

RUN <<EOF
mkdir -pv /stackable/airflow
mkdir -pv /stackable/airflow/dags
mkdir -pv /stackable/airflow/logs
chmod --recursive g=u /stackable
EOF


FROM stackable/image/vector AS airflow-main-image

Expand All @@ -99,22 +108,26 @@ ARG TARGETARCH
ARG STACKABLE_USER_UID

LABEL name="Apache Airflow" \
maintainer="[email protected]" \
vendor="Stackable GmbH" \
version="${PRODUCT}" \
release="${RELEASE}" \
summary="The Stackable image for Apache Airflow." \
description="This image is deployed by the Stackable Operator for Apache Airflow."

COPY airflow/licenses /licenses
COPY --chown=${STACKABLE_USER_UID}:0 airflow/stackable/utils/entrypoint.sh /entrypoint.sh
COPY --chown=${STACKABLE_USER_UID}:0 airflow/stackable/utils/run-airflow.sh /run-airflow.sh
maintainer="[email protected]" \
vendor="Stackable GmbH" \
version="${PRODUCT}" \
release="${RELEASE}" \
summary="The Stackable image for Apache Airflow." \
description="This image is deployed by the Stackable Operator for Apache Airflow."

ENV HOME=/stackable
ENV AIRFLOW_USER_HOME_DIR=/stackable
ENV PATH=$PATH:/bin:$HOME/app/bin
ENV AIRFLOW_HOME=$HOME/airflow

COPY --from=airflow-build-image --chown=${STACKABLE_USER_UID}:0 /stackable/ ${HOME}/
COPY --from=airflow-build-image --chown=${STACKABLE_USER_UID}:0 /stackable/git-sync ${HOME}/git-sync

COPY --chown=${STACKABLE_USER_UID}:0 airflow/stackable/utils/entrypoint.sh /entrypoint.sh
COPY --chown=${STACKABLE_USER_UID}:0 airflow/stackable/utils/run-airflow.sh /run-airflow.sh

COPY airflow/licenses /licenses

# Update image and install needed packages
RUN <<EOF
microdnf update
Expand Down Expand Up @@ -142,33 +155,33 @@ rm -rf /var/cache/yum
# Get the correct `tini` binary for our architecture.
# It is used as an init alternative in the entrypoint
curl -o /usr/bin/tini "https://repo.stackable.tech/repository/packages/tini/tini-${TINI}-${TARGETARCH}"

# fix missing permissions
chmod a+x /entrypoint.sh
chmod a+x /run-airflow.sh
chmod +x /usr/bin/tini
EOF

mkdir -pv ${AIRFLOW_HOME}
mkdir -pv ${AIRFLOW_HOME}/dags
mkdir -pv ${AIRFLOW_HOME}/logs
# ----------------------------------------
# Checks
# This section is to run final checks to ensure the created final images
# adhere to several minimal requirements like:
# - check file permissions and ownerships
# ----------------------------------------

# All files and folders owned by root to support running as arbitrary users
# This is best practice as all container users will belong to the root group (0)
chown -R ${STACKABLE_USER_UID}:0 /stackable
chmod -R g=u /stackable
# Check that permissions and ownership in ${HOME} are set correctly
# This will fail and stop the build if any mismatches are found.
RUN <<EOF
/bin/check-permissions-ownership.sh ${HOME} ${STACKABLE_USER_UID} 0
EOF

# ----------------------------------------
# Attention: We are changing the group of all files in /stackable directly above
# If you do any file based actions (copying / creating etc.) below this comment you
# absolutely need to make sure that the correct permissions are applied!
# chown ${STACKABLE_USER_UID}:0
# Attention: Do not perform any file based actions (copying/creating etc.) below this comment because the permissions would not be checked.
# ----------------------------------------

USER ${STACKABLE_USER_UID}
WORKDIR /stackable

COPY --from=airflow-build-image --chown=${STACKABLE_USER_UID}:0 /stackable/ ${HOME}/
COPY --from=gitsync-image --chown=${STACKABLE_USER_UID}:0 /git-sync /stackable/git-sync

ENTRYPOINT ["/usr/bin/tini", "--", "/run-airflow.sh"]
CMD []

Expand Down
20 changes: 11 additions & 9 deletions shared/checks/check-permissions-ownership.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ EXPECTED_GID=$3
error_flag=0

# Check ownership
while IFS= read -r -d '' file; do
uid=$(stat -c "%u" "$file")
gid=$(stat -c "%g" "$file")
while IFS= read -r -d '' entry; do
uid=${entry%% *}
remainder=${entry#* }
gid=${remainder%% *}

if [[ "$uid" -ne "$EXPECTED_UID" || "$gid" -ne "$EXPECTED_GID" ]]; then
file=${remainder#* }
echo "Ownership mismatch: $file (Expected: $EXPECTED_UID:$EXPECTED_GID, Found: $uid:$gid)"
error_flag=1
fi
done < <(find "$DIRECTORY" -print0)
done < <(find "$DIRECTORY" -printf "%U %G %p\0")

# Check permissions
while IFS= read -r -d '' file; do
perms=$(stat -c "%A" "$file")
owner_perms="${perms:1:3}"
group_perms="${perms:4:3}"
while IFS= read -r -d '' entry; do
owner_perms="${entry:1:3}"
group_perms="${entry:4:3}"

if [[ "$owner_perms" != "$group_perms" ]]; then
file="${entry:11}"
echo "Permission mismatch: $file (Owner: $owner_perms, Group: $group_perms)"
error_flag=1
fi
done < <(find "$DIRECTORY" -print0)
done < <(find "$DIRECTORY" -printf "%M %p\0")

if [[ $error_flag -ne 0 ]]; then
echo "Permission and Ownership checks failed for $DIRECTORY!"
Expand Down