diff --git a/docker/runtime/README.md b/docker/runtime/README.md index cf28d0cb9e0e8a..96dfc3d162391e 100644 --- a/docker/runtime/README.md +++ b/docker/runtime/README.md @@ -141,9 +141,92 @@ Do not use `--pull` on a component build that consumes a locally built base. - The runtime does not contain gdb, perf, BCC, iperf3, patchelf, vim, development headers or Python 2. Use a separate diagnostic container when needed. - FE images omit Arthas and its bundled dependencies. -- Both operator tools remain at `/opt/apache-doris/`. They are compiled from - the pinned doris-operator source with the updated Go toolchain, x/net and edwards25519; - Operator debug mode and compute-group discovery retain their commands. +- Both operator tools remain at `/opt/apache-doris/`. The runtime base builds + them from the pinned doris-operator source. For `dorisctl preflight`, use the + standalone CLI from the Operator release that matches the target CRDs and + controller. + +## Running FE, BE and Meta Service without root + +The standalone FE, BE and MS images run as `doris` (`10001:10001`) by default. +The MS image also runs Recycler under this identity. Build arguments +`DORIS_UID` and `DORIS_GID` accept unused decimal IDs in the base image +from 1 through 4294967294. The build scripts validate them before downloading +the archive. An omitted argument uses 10001. Image-owned configuration, +logs, metadata and storage directories are writable by this user. The SQL +administrator account is independent of the operating-system user. The image +provides `/etc/krb5.conf` as a link into the writable Doris configuration +directory, so Operator-mounted Kerberos configuration does not require writes +to `/etc` at startup. + +Both `docker-build.sh` and `docker-build-ubi9.sh` forward `DORIS_UID` and `DORIS_GID` environment +variables to all three image builds. Use the same IDs in the image and the +Kubernetes configuration, and keep them stable across releases. + +For Kubernetes, configure each component's Pod security context, for example: + +```yaml +runAsNonRoot: true +runAsUser: 10001 +runAsGroup: 10001 +fsGroup: 10001 +seccompProfile: + type: RuntimeDefault +``` + +Use the container security context to set `privileged: false`, +`allowPrivilegeEscalation: false` and drop all capabilities. With Doris Operator, +set BE `enableWorkloadGroup` +to `false` and `skipDefaultSystemInit` to `true` to avoid privileged cgroup +setup and the default system init container. Check the generated Pod to +confirm these settings; custom init containers must also be omitted. + +With Operator 26.0.1, these settings are available without changing the +Operator. For a `DorisCluster`, set `feSpec.securityContext` and +`beSpec.securityContext`, and the corresponding `containerSecurityContext` +fields. For a `DorisDisaggregatedCluster`, configure `metaService`, `feSpec` +and every `computeGroups` entry. Set `skipDefaultSystemInit: true` and +`enableWorkloadGroup: false` on each BE/compute group when using Operator +26.0.1. With an Operator release that supports `systemInitMode`, prepare +the nodes and choose `NodePrepared` to retain the BE startup checks. The +Workload Group switch controls OS-level CPU setup. Query admission and +memory controls remain available. + +The disaggregated FE and BE entrypoints copy their projected primary +configuration before applying cloud settings. FE preserves an explicitly +configured FQDN mode and defaults to enabling it when the projected file +omits it. On restart, the entrypoints replace their cloud assignments in +the writable working copy. + +For MS, `doris_cloud.conf` from a ConfigMap is copied into a writable file +before applying `FDB_ENDPOINT`. Without that environment variable, the +configured `fdb_cluster` is preserved. The same entrypoint supports the +packaged default (Meta Service and Recycler) or explicit `--meta-service` +and `--recycler` arguments. FoundationDB and object storage are external +dependencies with their own container-user and storage requirements. + +New volumes still need to be writable: image ownership does not override a +mounted volume's permissions. Validate the actual StorageClass with a fresh +PVC. A successful deployment with local-path storage is not proof that every +CSI driver supports the same permissions. The non-root configuration can be +validated in a namespace enforcing the Kubernetes `restricted` Pod Security +Standard; a storage provisioner's infrastructure is managed separately. + +Skipping system initialization does not tune the host. Doris recommends +`vm.max_map_count >= 2000000`, disabled swap and a sufficient file-descriptor +limit. A non-root image cannot change these host settings. Operator versions +that set `SKIP_CHECK_ULIMIT=true` bypass startup checks, but this does not +remove the underlying kernel limits or their effects under load. Persistent +volumes must be writable by the configured UID/GID; `fsGroup` requires volume +driver support. Existing root-owned data needs its permissions prepared +separately. Custom log, cache, Kerberos and keytab paths must also be readable +or writable as required by Doris. A read-only container root filesystem is +not implied by running without root. + +Without a writable, delegated CPU cgroup, Workload Group CPU weights and CPU +hard limits are unavailable. Leave `doris_cgroup_cpu_path` unset in this mode. +Other Workload Group controls, including query admission and memory limits, +remain available; Kubernetes container CPU limits continue to apply. ## MySQL TLS certificates diff --git a/docker/runtime/base-image/install-runtime-ubi9.sh b/docker/runtime/base-image/install-runtime-ubi9.sh index 4e5841d5f86b37..c6457190cccc92 100644 --- a/docker/runtime/base-image/install-runtime-ubi9.sh +++ b/docker/runtime/base-image/install-runtime-ubi9.sh @@ -24,7 +24,7 @@ microdnf "${repositories[@]}" update -y microdnf "${repositories[@]}" install -y --setopt=install_weak_deps=0 \ bash ca-certificates curl-minimal findutils fontconfig gawk glibc-langpack-en \ grep gzip hostname iproute iputils jq libstdc++ mysql net-tools numactl \ - openssl openssl-libs procps-ng sed tar tzdata util-linux wget which + openssl openssl-libs procps-ng sed shadow-utils tar tzdata util-linux wget which # UBI Minimal keeps tzdata in the RPM database but removes its zoneinfo files. # Installing an already-current RPM does not restore them; BE needs the files. diff --git a/docker/runtime/base-image/install-runtime.sh b/docker/runtime/base-image/install-runtime.sh index 97225ef6fa9474..d6159a6fe700ba 100644 --- a/docker/runtime/base-image/install-runtime.sh +++ b/docker/runtime/base-image/install-runtime.sh @@ -52,7 +52,7 @@ apt-get "${apt_options[@]}" upgrade -y --with-new-pkgs # build-only packages so the same patch policy does not add unused tooling. if [[ $# -eq 0 ]]; then set -- curl fontconfig hostname iproute2 iputils-ping jq libstdc++6 \ - "${mysql_client}" net-tools numactl openssl procps tzdata util-linux wget + "${mysql_client}" net-tools numactl openssl passwd procps tzdata util-linux wget fi # Accept the core-client name from either supported Ubuntu release so test # images and explicit Jammy overrides use the same installer. diff --git a/docker/runtime/be/Dockerfile b/docker/runtime/be/Dockerfile index b099c6a1b26f5c..36faa059b3c1e7 100644 --- a/docker/runtime/be/Dockerfile +++ b/docker/runtime/be/Dockerfile @@ -33,13 +33,28 @@ FROM ${BASE_IMAGE} ARG TARGETARCH ARG DORIS_VERSION="x.x.x" +ARG DORIS_UID=10001 +ARG DORIS_GID=10001 + +RUN for id in "${DORIS_UID}" "${DORIS_GID}"; do \ + case "${id}" in ""|0*|*[!0-9]*) \ + echo "DORIS_UID and DORIS_GID must be positive decimal IDs" >&2; exit 1 ;; \ + esac; \ + if [ "${#id}" -gt 10 ] || [ "${id}" -gt 4294967294 ]; then \ + echo "DORIS_UID and DORIS_GID must not exceed 4294967294" >&2; exit 1; \ + fi; \ + done && \ + groupadd --gid "${DORIS_GID}" doris && \ + useradd --uid "${DORIS_UID}" --gid "${DORIS_GID}" --create-home --shell /bin/bash doris + +ENV HOME=/home/doris # set environment variables ENV JAVA_HOME=/usr/lib/jvm/java ENV PATH=$PATH:$JAVA_HOME/bin:/opt/apache-doris/be/bin -COPY resource/${TARGETARCH:-amd64}/apache-doris-${DORIS_VERSION}-bin-*/be /opt/apache-doris/be +COPY --chown=${DORIS_UID}:${DORIS_GID} resource/${TARGETARCH:-amd64}/apache-doris-${DORIS_VERSION}-bin-*/be /opt/apache-doris/be COPY resource/be_*.sh /opt/apache-doris/ @@ -49,6 +64,13 @@ COPY resource/init_be.sh /usr/local/bin/ #RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime +# Create writable runtime directories before dropping privileges. +RUN mkdir -p /opt/apache-doris/be/log /opt/apache-doris/be/storage && \ + chown "${DORIS_UID}:${DORIS_GID}" /opt/apache-doris /opt/apache-doris/be/log /opt/apache-doris/be/storage && \ + ln -sf /opt/apache-doris/be/conf/krb5.conf /etc/krb5.conf + +USER ${DORIS_UID}:${DORIS_GID} + WORKDIR /opt/apache-doris ENTRYPOINT ["bash","entry_point.sh"] diff --git a/docker/runtime/be/resource/be_disaggregated_entrypoint.sh b/docker/runtime/be/resource/be_disaggregated_entrypoint.sh index 78365b772edc30..cf8bd0c9a38811 100755 --- a/docker/runtime/be/resource/be_disaggregated_entrypoint.sh +++ b/docker/runtime/be/resource/be_disaggregated_entrypoint.sh @@ -94,38 +94,32 @@ function add_cpu_limit_config() fi } -# update config add `deploy_mode`. +# Install the projected config before setting the cloud deployment mode. update_conf_from_configmap() { - echo "" >> $DORIS_HOME/conf/be.conf - echo "########## doris-operator automatically adds ##########" >> $DORIS_HOME/conf/be.conf - echo "deploy_mode = cloud" >> $DORIS_HOME/conf/be.conf - if [[ "x$CONFIGMAP_MOUNT_PATH" == "x" ]] ; then - log_stderr '[info] Empty $CONFIGMAP_MOUNT_PATH env var, skip it!' - return 0 + if [[ -d "${CONFIGMAP_MOUNT_PATH:-}" ]]; then + local source target file + for source in "${CONFIGMAP_MOUNT_PATH}"/*; do + [[ -e "${source}" ]] || continue + file=${source##*/} + target="${DORIS_HOME}/conf/${file}" + if [[ -e "${target}" || -L "${target}" ]]; then + mv -fT -- "${target}" "${target}.bak" || return 1 + fi + if [[ "${file}" == be.conf ]]; then + cp -- "${source}" "${target}" && chmod u+w "${target}" || return 1 + else + ln -sfT -- "${source}" "${target}" || return 1 + fi + done fi - if ! test -d $CONFIGMAP_MOUNT_PATH ; then - log_stderr "[info] $CONFIGMAP_MOUNT_PATH not exist or not a directory, ignore ..." - return 0 + + local config="${DORIS_HOME}/conf/be.conf" + sed -i -E '/^[[:space:]]*deploy_mode[[:space:]]*=/d; /^########## doris-operator automatically adds ##########$/d' "${config}" || return 1 + if [[ -n "$(tail -c 1 -- "${config}")" ]]; then + printf '\n' >> "${config}" fi - local tgtconfdir=$DORIS_HOME/conf - for conffile in `ls $CONFIGMAP_MOUNT_PATH` - do - log_stderr "[info] Process conf file $conffile ..." - local tgt=$tgtconfdir/$conffile - if test -e $tgt ; then - # make a backup - mv -f $tgt ${tgt}.bak - fi - if [[ "$conffile" == "be.conf" ]]; then - cp $CONFIGMAP_MOUNT_PATH/$conffile $DORIS_HOME/conf/$conffile - echo "" >> $DORIS_HOME/conf/$conffile - echo "########## doris-operator automatically adds ##########" >> $DORIS_HOME/conf/$conffile - echo "deploy_mode = cloud" >> $DORIS_HOME/conf/$conffile - continue - fi - ln -sfT $CONFIGMAP_MOUNT_PATH/$conffile $tgt - done + printf '########## doris-operator automatically adds ##########\ndeploy_mode = cloud\n' >> "${config}" } # resolve password for root @@ -549,6 +543,16 @@ mount_kerberos_config() fi KRB5_CONFIG_DIR=$(dirname "$KRB5_CONFIG") + # Resolve only the first link: the conf file may already link to a read-only ConfigMap. + if [[ -L "$KRB5_CONFIG" ]]; then + local config_link + config_link=$(readlink "$KRB5_CONFIG") + if [[ "$config_link" == /* ]]; then + KRB5_CONFIG_DIR=$(dirname "$config_link") + else + KRB5_CONFIG_DIR="$KRB5_CONFIG_DIR/$(dirname "$config_link")" + fi + fi # If the krb5 directory does not exist, need to create it. if [[ ! -d "$KRB5_CONFIG_DIR" ]]; then log_stderr "[info] Creating krb5 directory: $KRB5_CONFIG_DIR" @@ -624,7 +628,7 @@ else log_stderr "[info] the host machine not support avx2 instruction set." fi -update_conf_from_configmap +update_conf_from_configmap || exit 1 add_workloadgroup_config add_cpu_limit_config mount_kerberos_config diff --git a/docker/runtime/be/resource/be_entrypoint.sh b/docker/runtime/be/resource/be_entrypoint.sh index 0e6bb9c7edacc7..219b65f7b11388 100755 --- a/docker/runtime/be/resource/be_entrypoint.sh +++ b/docker/runtime/be/resource/be_entrypoint.sh @@ -86,24 +86,23 @@ update_conf_from_configmap() log_stderr '[info] Empty $CONFIGMAP_MOUNT_PATH env var, skip it!' return 0 fi - if ! test -d $CONFIGMAP_MOUNT_PATH ; then + if [[ ! -d "${CONFIGMAP_MOUNT_PATH}" ]]; then log_stderr "[info] $CONFIGMAP_MOUNT_PATH not exist or not a directory, ignore ..." return 0 fi - local tgtconfdir=$DORIS_HOME/conf - for conffile in `ls $CONFIGMAP_MOUNT_PATH` - do - log_stderr "[info] Process conf file $conffile ..." - local tgt=$tgtconfdir/$conffile - if test -e $tgt ; then - # make a backup - mv -f $tgt ${tgt}.bak + local source target file + for source in "${CONFIGMAP_MOUNT_PATH}"/*; do + [[ -e "${source}" ]] || continue + file=${source##*/} + target="${DORIS_HOME}/conf/${file}" + if [[ -e "${target}" || -L "${target}" ]]; then + mv -fT -- "${target}" "${target}.bak" || return 1 fi - if [[ "$conffile" == "be.conf" ]]; then - cp $CONFIGMAP_MOUNT_PATH/$conffile $DORIS_HOME/conf/$file - continue + if [[ "${file}" == be.conf ]]; then + cp -- "${source}" "${target}" && chmod u+w "${target}" || return 1 + else + ln -sfT -- "${source}" "${target}" || return 1 fi - ln -sfT $CONFIGMAP_MOUNT_PATH/$conffile $tgt done } @@ -114,6 +113,16 @@ mount_kerberos_config() fi KRB5_CONFIG_DIR=$(dirname "$KRB5_CONFIG") + # Resolve only the first link: the conf file may already link to a read-only ConfigMap. + if [[ -L "$KRB5_CONFIG" ]]; then + local config_link + config_link=$(readlink "$KRB5_CONFIG") + if [[ "$config_link" == /* ]]; then + KRB5_CONFIG_DIR=$(dirname "$config_link") + else + KRB5_CONFIG_DIR="$KRB5_CONFIG_DIR/$(dirname "$config_link")" + fi + fi # If the krb5 directory does not exist, need to create it. if [[ ! -d "$KRB5_CONFIG_DIR" ]]; then log_stderr "[info] Creating krb5 directory: $KRB5_CONFIG_DIR" @@ -511,7 +520,7 @@ else log_stderr "[info] the host machine not support avx2 instruction set." fi -update_conf_from_configmap +update_conf_from_configmap || exit 1 add_workloadgroup_config add_cpu_limit_config mount_kerberos_config diff --git a/docker/runtime/build-runtime.sh b/docker/runtime/build-runtime.sh index 0aa410f9551309..6f5f4f01bcaec9 100644 --- a/docker/runtime/build-runtime.sh +++ b/docker/runtime/build-runtime.sh @@ -48,6 +48,16 @@ if [[ ! "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-._][a-zA-Z0-9._-]+)?$ ]]; then exit 1 fi +DORIS_UID=${DORIS_UID-10001} +DORIS_GID=${DORIS_GID-10001} +for arg in DORIS_UID DORIS_GID; do + id=${!arg} + if [[ ! "${id}" =~ ^[1-9][0-9]{0,9}$ ]] || (( 10#${id} > 4294967294 )); then + echo "Invalid ${arg}: expected a decimal ID between 1 and 4294967294" >&2 + exit 1 + fi +done + runtime_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) work_dir=${DORIS_IMAGE_WORK_DIR:-"${runtime_dir}/../../output/runtime-images/${runtime_variant}"} mkdir -p "${work_dir}" @@ -75,7 +85,7 @@ fi base_image=${DORIS_BASE_IMAGE:-"apache/doris:base-8.0-${runtime_variant}-jdk${java_version}"} package_name="apache-doris-${version}-bin-${url_arch}" archive_name="${package_name}.tar.gz" -download_base=${DORIS_DOWNLOAD_BASE_URL:-https://apache-doris-releases.oss-accelerate.aliyuncs.com} +download_base=${DORIS_DOWNLOAD_BASE_URL:-https://apache-doris-downloads.oss-accelerate.aliyuncs.com} expected_sha512=${DORIS_ARCHIVE_SHA512:-} # Read only the checksum, not paths or commands from the checksum document. @@ -138,6 +148,7 @@ for role in fe be ms; do image="${DORIS_IMAGE_PREFIX:-doris}.${role}:${version}${DORIS_IMAGE_TAG_SUFFIX-${runtime_tag_suffix}}" docker build --no-cache --platform "linux/${arch}" \ --build-arg "BASE_IMAGE=${base_image}" --build-arg "JAVA_VERSION=${java_version}" \ + --build-arg "DORIS_UID=${DORIS_UID}" --build-arg "DORIS_GID=${DORIS_GID}" \ --build-arg "DORIS_VERSION=${version}" -t "${image}" "${context}" docker image inspect "${image}" > "${work_dir}/${role}-${version}-${arch}.image.json" done diff --git a/docker/runtime/fe/Dockerfile b/docker/runtime/fe/Dockerfile index d6bbe9634d2f08..b3cfa040468435 100644 --- a/docker/runtime/fe/Dockerfile +++ b/docker/runtime/fe/Dockerfile @@ -40,11 +40,27 @@ RUN rm -rf /artifacts/fe/arthas /artifacts/fe/mysql_ssl_default_certificate FROM ${BASE_IMAGE} AS runtime +ARG DORIS_UID=10001 +ARG DORIS_GID=10001 + +RUN for id in "${DORIS_UID}" "${DORIS_GID}"; do \ + case "${id}" in ""|0*|*[!0-9]*) \ + echo "DORIS_UID and DORIS_GID must be positive decimal IDs" >&2; exit 1 ;; \ + esac; \ + if [ "${#id}" -gt 10 ] || [ "${id}" -gt 4294967294 ]; then \ + echo "DORIS_UID and DORIS_GID must not exceed 4294967294" >&2; exit 1; \ + fi; \ + done && \ + groupadd --gid "${DORIS_GID}" doris && \ + useradd --uid "${DORIS_UID}" --gid "${DORIS_GID}" --create-home --shell /bin/bash doris + +ENV HOME=/home/doris + # set environment variables ENV JAVA_HOME=/usr/lib/jvm/java ENV PATH=$PATH:$JAVA_HOME/bin:/opt/apache-doris/fe/bin -COPY --from=artifacts /artifacts/fe /opt/apache-doris/fe +COPY --from=artifacts --chown=${DORIS_UID}:${DORIS_GID} /artifacts/fe /opt/apache-doris/fe COPY resource/init_fe.sh /usr/local/bin/ @@ -53,6 +69,13 @@ COPY resource/fe_*.sh /opt/apache-doris/ # when you use beijing zone, please enable the set. # RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime +# Create writable runtime directories before dropping privileges. +RUN mkdir -p /opt/apache-doris/fe/log /opt/apache-doris/fe/doris-meta && \ + chown "${DORIS_UID}:${DORIS_GID}" /opt/apache-doris /opt/apache-doris/fe/log /opt/apache-doris/fe/doris-meta && \ + ln -sf /opt/apache-doris/fe/conf/krb5.conf /etc/krb5.conf + +USER ${DORIS_UID}:${DORIS_GID} + WORKDIR /opt/apache-doris ENTRYPOINT ["bash","init_fe.sh"] diff --git a/docker/runtime/fe/resource/fe_disaggregated_entrypoint.sh b/docker/runtime/fe/resource/fe_disaggregated_entrypoint.sh index 496c0ce22ab660..e332450e68a153 100755 --- a/docker/runtime/fe/resource/fe_disaggregated_entrypoint.sh +++ b/docker/runtime/fe/resource/fe_disaggregated_entrypoint.sh @@ -414,33 +414,32 @@ function check_and_modify_fqdn_config() function add_cluster_info_to_conf() { - echo "" >> ${DORIS_HOME}/conf/fe.conf - echo "########## doris-operator automatically adds ##########" >> ${DORIS_HOME}/conf/fe.conf - echo "meta_service_endpoint=$MS_ENDPOINT" >> ${DORIS_HOME}/conf/fe.conf - echo "cluster_id=$CLUSTER_ID" >> ${DORIS_HOME}/conf/fe.conf - echo "deploy_mode=cloud" >> ${DORIS_HOME}/conf/fe.conf + local config="${DORIS_HOME}/conf/fe.conf" + # Apply the cloud assignments to the effective file, including on restart. + sed -i -E '/^[[:space:]]*(meta_service_endpoint|cluster_id|deploy_mode)[[:space:]]*=/d; /^########## doris-operator automatically adds ##########$/d' "${config}" || return 1 + if [[ -n "$(tail -c 1 -- "${config}")" ]]; then + printf '\n' >> "${config}" + fi + printf '########## doris-operator automatically adds ##########\nmeta_service_endpoint=%s\ncluster_id=%s\ndeploy_mode=cloud\n' \ + "${MS_ENDPOINT:-}" "${CLUSTER_ID}" >> "${config}" } function link_config_files() { - if [[ -d $CONFIGMAP_MOUNT_PATH ]]; then - #backup files want to replace - for file in `ls $CONFIGMAP_MOUNT_PATH`; - do - if [[ -f $DORIS_HOME/conf/$file ]]; then - mv $DORIS_HOME/conf/$file $DORIS_HOME/conf/$file.bak + if [[ -d "${CONFIGMAP_MOUNT_PATH:-}" ]]; then + local source target file + for source in "${CONFIGMAP_MOUNT_PATH}"/*; do + [[ -e "${source}" ]] || continue + file=${source##*/} + target="${DORIS_HOME}/conf/${file}" + if [[ -e "${target}" || -L "${target}" ]]; then + mv -fT -- "${target}" "${target}.bak" || return 1 fi - done - - for file in `ls $CONFIGMAP_MOUNT_PATH`; - do - if [[ "$file" == "fe.conf" ]]; then - cp $CONFIGMAP_MOUNT_PATH/$file $DORIS_HOME/conf/$file - add_cluster_info_to_conf - continue + if [[ "${file}" == fe.conf ]]; then + cp -- "${source}" "${target}" && chmod u+w "${target}" || return 1 + else + ln -sfT -- "${source}" "${target}" || return 1 fi - - ln -sfT $CONFIGMAP_MOUNT_PATH/$file $DORIS_HOME/conf/$file done fi } @@ -538,9 +537,9 @@ if [[ "x$fe_addrs" == "x" ]]; then exit fi -add_cluster_info_to_conf -check_and_modify_fqdn_config -link_config_files +link_config_files || exit 1 +add_cluster_info_to_conf || exit 1 +check_and_modify_fqdn_config || exit 1 # resolve password for root to manage nodes in doris. resolve_password_from_secret #parse tls connection config diff --git a/docker/runtime/fe/resource/fe_entrypoint.sh b/docker/runtime/fe/resource/fe_entrypoint.sh index 7ea6866f1a0525..23e0ca37b1bea2 100755 --- a/docker/runtime/fe/resource/fe_entrypoint.sh +++ b/docker/runtime/fe/resource/fe_entrypoint.sh @@ -419,21 +419,24 @@ update_conf_from_configmap() add_fqdn_config return 0 fi - if ! test -d $CONFIGMAP_MOUNT_PATH ; then + if [[ ! -d "${CONFIGMAP_MOUNT_PATH}" ]]; then log_stderr "[info] $CONFIGMAP_MOUNT_PATH not exist or not a directory, ignore ..." add_fqdn_config return 0 fi - local tgtconfdir=$DORIS_HOME/conf - for conffile in `ls $CONFIGMAP_MOUNT_PATH` - do - log_stderr "[info] Process conf file $conffile ..." - local tgt=$tgtconfdir/$conffile - if test -e $tgt ; then - # make a backup - mv -f $tgt ${tgt}.bak + local source target file + for source in "${CONFIGMAP_MOUNT_PATH}"/*; do + [[ -e "${source}" ]] || continue + file=${source##*/} + target="${DORIS_HOME}/conf/${file}" + if [[ -e "${target}" || -L "${target}" ]]; then + mv -fT -- "${target}" "${target}.bak" || return 1 + fi + if [[ "${file}" == fe.conf ]]; then + cp -- "${source}" "${target}" && chmod u+w "${target}" || return 1 + else + ln -sfT -- "${source}" "${target}" || return 1 fi - ln -sfT $CONFIGMAP_MOUNT_PATH/$conffile $tgt done add_fqdn_config } @@ -445,6 +448,16 @@ mount_kerberos_config() fi KRB5_CONFIG_DIR=$(dirname "$KRB5_CONFIG") + # Resolve only the first link: the conf file may already link to a read-only ConfigMap. + if [[ -L "$KRB5_CONFIG" ]]; then + local config_link + config_link=$(readlink "$KRB5_CONFIG") + if [[ "$config_link" == /* ]]; then + KRB5_CONFIG_DIR=$(dirname "$config_link") + else + KRB5_CONFIG_DIR="$KRB5_CONFIG_DIR/$(dirname "$config_link")" + fi + fi # If the krb5 directory does not exist, need to create it. if [[ ! -d "$KRB5_CONFIG_DIR" ]]; then log_stderr "[info] Creating krb5 directory: $KRB5_CONFIG_DIR" @@ -604,7 +617,7 @@ if [[ "x$fe_addrs" == "x" ]]; then fi #first upate config -update_conf_from_configmap +update_conf_from_configmap || exit 1 collect_env_info wait_for_fqdn_ready || exit 1 mount_kerberos_config diff --git a/docker/runtime/ms/Dockerfile b/docker/runtime/ms/Dockerfile index a82636608ce4e8..d7103c109dc347 100644 --- a/docker/runtime/ms/Dockerfile +++ b/docker/runtime/ms/Dockerfile @@ -33,18 +33,38 @@ FROM ${BASE_IMAGE} ARG TARGETARCH ARG DORIS_VERSION="x.x.x" +ARG DORIS_UID=10001 +ARG DORIS_GID=10001 +RUN for id in "${DORIS_UID}" "${DORIS_GID}"; do \ + case "${id}" in ""|0*|*[!0-9]*) \ + echo "DORIS_UID and DORIS_GID must be positive decimal IDs" >&2; exit 1 ;; \ + esac; \ + if [ "${#id}" -gt 10 ] || [ "${id}" -gt 4294967294 ]; then \ + echo "DORIS_UID and DORIS_GID must not exceed 4294967294" >&2; exit 1; \ + fi; \ + done && \ + groupadd --gid "${DORIS_GID}" doris && \ + useradd --uid "${DORIS_UID}" --gid "${DORIS_GID}" --create-home --shell /bin/bash doris + +ENV HOME=/home/doris # set environment variables ENV JAVA_HOME=/usr/lib/jvm/java ENV PATH=$PATH:$JAVA_HOME/bin:/opt/apache-doris/ms/bin -COPY resource/${TARGETARCH:-amd64}/apache-doris-${DORIS_VERSION}-bin-*/ms /opt/apache-doris/ms +COPY --chown=${DORIS_UID}:${DORIS_GID} resource/${TARGETARCH:-amd64}/apache-doris-${DORIS_VERSION}-bin-*/ms /opt/apache-doris/ms COPY resource/ms_*.sh /opt/apache-doris/ #RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime +RUN mkdir -p /opt/apache-doris/ms/log && \ + chown "${DORIS_UID}:${DORIS_GID}" /opt/apache-doris /opt/apache-doris/ms/log && \ + ln -sf /opt/apache-doris/ms/conf/krb5.conf /etc/krb5.conf + +USER ${DORIS_UID}:${DORIS_GID} + WORKDIR /opt/apache-doris #ENTRYPOINT ["bash","entry_point.sh"] diff --git a/docker/runtime/ms/resource/ms_disaggregated_entrypoint.sh b/docker/runtime/ms/resource/ms_disaggregated_entrypoint.sh index bdb42cf6724b0e..a816325f4a824a 100755 --- a/docker/runtime/ms/resource/ms_disaggregated_entrypoint.sh +++ b/docker/runtime/ms/resource/ms_disaggregated_entrypoint.sh @@ -16,32 +16,40 @@ # specific language governing permissions and limitations # under the License. -# +set -eo pipefail -#get from env -FDB_ENDPOINT=${FDB_ENDPOINT} -CONFIGMAP_PATH=${CONFIGMAP_PATH:="/etc/doris"} -DORIS_HOME=${DORIS_HOME:="/opt/apache-doris"} +CONFIGMAP_PATH=${CONFIGMAP_PATH:-/etc/doris} +DORIS_HOME=${DORIS_HOME:-/opt/apache-doris} -echo "" >> $DORIS_HOME/ms/conf/doris_cloud.conf -echo "fdb_cluster=$FDB_ENDPOINT" >> $DORIS_HOME/ms/conf/doris_cloud.conf -if [[ -d $CONFIGMAP_PATH ]]; then - for file in `ls $CONFIGMAP_PATH` - do - if [[ "$file" == "doris_cloud.conf" ]] ; then - mv -f $DORIS_HOME/ms/conf/$file $DORIS_HOME/ms/conf/$file.bak - cp $CONFIGMAP_PATH/$file $DORIS_HOME/ms/conf/$file - echo "fdb_cluster=$FDB_ENDPOINT" >> $DORIS_HOME/ms/conf/doris_cloud.conf - continue +update_conf_from_configmap() +{ + local source target file + if [[ -d "${CONFIGMAP_PATH}" ]]; then + for source in "${CONFIGMAP_PATH}"/*; do + [[ -f "${source}" ]] || continue + file=$(basename "${source}") + target="${DORIS_HOME}/ms/conf/${file}" + if [[ -e "${target}" || -L "${target}" ]]; then + mv -f "${target}" "${target}.bak" fi - - if test -e $DORIS_HOME/ms/conf/$file ; then - mv -f $DORIS_HOME/ms/conf/$file $DORIS_HOME/ms/conf/$file.bak + if [[ "${file}" == doris_cloud.conf ]]; then + # Projected ConfigMaps are read-only; startup needs a writable copy. + cp "${source}" "${target}" + chmod u+w "${target}" + else + ln -sfT "${source}" "${target}" fi - ln -sfT $CONFIGMAP_PATH/$file $DORIS_HOME/ms/conf/$file - done -fi + done + fi -./doris-debug --component ms + if [[ -n "${FDB_ENDPOINT:-}" ]]; then + target="${DORIS_HOME}/ms/conf/doris_cloud.conf" + # Do not accumulate endpoint assignments when the entrypoint is rerun. + sed -i '/^[[:space:]]*fdb_cluster[[:space:]]*=/d' "${target}" + printf '\nfdb_cluster=%s\n' "${FDB_ENDPOINT}" >> "${target}" + fi +} -$DORIS_HOME/ms/bin/start.sh --console --conf=$DORIS_HOME/ms/conf/doris_cloud.conf +update_conf_from_configmap +"${DORIS_HOME}/doris-debug" --component ms +exec "${DORIS_HOME}/ms/bin/start.sh" --console "--conf=${DORIS_HOME}/ms/conf/doris_cloud.conf" "$@" diff --git a/docker/runtime/test/non-root-config-test.sh b/docker/runtime/test/non-root-config-test.sh new file mode 100755 index 00000000000000..dd27f241152998 --- /dev/null +++ b/docker/runtime/test/non-root-config-test.sh @@ -0,0 +1,170 @@ +#!/bin/bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +set -euo pipefail + +RUNTIME_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +TEST_DIR=$(mktemp -d) +trap 'chmod -R u+w "${TEST_DIR}"; rm -rf "${TEST_DIR}"' EXIT + +log_stderr() { :; } +add_fqdn_config() { echo 'enable_fqdn_mode = true' >> "${DORIS_HOME}/conf/fe.conf"; } + +add_cluster_info_to_conf() { echo 'deploy_mode = cloud' >> "${DORIS_HOME}/conf/fe.conf"; } + +for variant in fe be fe_disaggregated be_disaggregated; do + ( + component=${variant%%_*} + DORIS_HOME="${TEST_DIR}/${variant} home" + CONFIGMAP_MOUNT_PATH="${TEST_DIR}/${variant} configmap" + mkdir -p "${DORIS_HOME}/conf" "${CONFIGMAP_MOUNT_PATH}/..data" + printf '# projected config\n' > "${CONFIGMAP_MOUNT_PATH}/..data/${component}.conf" + chmod 444 "${CONFIGMAP_MOUNT_PATH}/..data/${component}.conf" + ln -s "..data/${component}.conf" "${CONFIGMAP_MOUNT_PATH}/${component}.conf" + printf '# original config\n' > "${DORIS_HOME}/conf/${component}.conf" + # Load only the configuration function, without executing the entrypoint. + if [[ "${variant}" == fe_disaggregated ]]; then + # shellcheck disable=SC1090 + source <(sed -n '/^function link_config_files()/,/^}/p' \ + "${RUNTIME_DIR}/fe/resource/fe_disaggregated_entrypoint.sh") + link_config_files + else + # shellcheck disable=SC1090 + source <(sed -n '/^update_conf_from_configmap()/,/^}/p' \ + "${RUNTIME_DIR}/${component}/resource/${variant}_entrypoint.sh") + update_conf_from_configmap + fi + test ! -L "${DORIS_HOME}/conf/${component}.conf" + grep -q '^# projected config$' "${DORIS_HOME}/conf/${component}.conf" + grep -q '^# original config$' "${DORIS_HOME}/conf/${component}.conf.bak" + echo 'test_setting = true' >> "${DORIS_HOME}/conf/${component}.conf" + test "$(wc -l < "${CONFIGMAP_MOUNT_PATH}/${component}.conf")" -eq 1 + if [[ "${variant}" == be_disaggregated ]]; then + CONFIGMAP_MOUNT_PATH="${TEST_DIR}/unmounted" + update_conf_from_configmap + update_conf_from_configmap + test "$(grep -c '^deploy_mode[[:space:]]*=' "${DORIS_HOME}/conf/be.conf")" -eq 1 + fi + ) +done + +# Exercise the actual call order, not just the copy helper. A projected file +# replaces the image's fe.conf, so defaults must be applied after the copy. +( + DORIS_HOME="${TEST_DIR}/cloud-fe-start" + FE_CONFFILE="${DORIS_HOME}/conf/fe.conf" + CONFIGMAP_MOUNT_PATH="${TEST_DIR}/cloud-fe-project" + export MS_ENDPOINT=ms:5000 + export CLUSTER_ID=42 + mkdir -p "${DORIS_HOME}/conf" "${CONFIGMAP_MOUNT_PATH}" + printf 'http_port = 8030\n' > "${FE_CONFFILE}" + printf 'http_port = 8040\n' > "${CONFIGMAP_MOUNT_PATH}/fe.conf" + chmod 444 "${CONFIGMAP_MOUNT_PATH}/fe.conf" + entrypoint="${RUNTIME_DIR}/fe/resource/fe_disaggregated_entrypoint.sh" + # shellcheck disable=SC1090 + source <(sed -n '/^parse_confval_from_fe_conf()/,/^}/p; /^function check_and_modify_fqdn_config()/,/^}/p; /^function add_cluster_info_to_conf()/,/^}/p; /^function link_config_files()/,/^}/p' "${entrypoint}") + set -- fe-service + # Match the literal positional argument in the entrypoint source. + # shellcheck disable=SC1090,SC2016 + source <(sed -n '/^fe_addrs=\$1$/,/^# resolve password/p' "${entrypoint}") + if ! grep -qx 'enable_fqdn_mode = true' "${FE_CONFFILE}"; then + echo 'Disaggregated FE lost the default FQDN mode after installing the projected config' >&2 + exit 1 + fi + grep -qx 'http_port = 8040' "${FE_CONFFILE}" + grep -qx 'meta_service_endpoint=ms:5000' "${FE_CONFFILE}" + CONFIGMAP_MOUNT_PATH="${TEST_DIR}/unmounted" + # Match the literal positional argument in the entrypoint source. + # shellcheck disable=SC1090,SC2016 + source <(sed -n '/^fe_addrs=\$1$/,/^# resolve password/p' "${entrypoint}") + for property in enable_fqdn_mode meta_service_endpoint cluster_id deploy_mode; do + test "$(grep -c "^${property}[[:space:]]*=" "${FE_CONFFILE}")" -eq 1 + done + CONFIGMAP_MOUNT_PATH="${TEST_DIR}/cloud-fe-project" + chmod u+w "${CONFIGMAP_MOUNT_PATH}/fe.conf" + printf 'enable_fqdn_mode = false\n' >> "${CONFIGMAP_MOUNT_PATH}/fe.conf" + chmod 444 "${CONFIGMAP_MOUNT_PATH}/fe.conf" + # Match the literal positional argument in the entrypoint source. + # shellcheck disable=SC1090,SC2016 + source <(sed -n '/^fe_addrs=\$1$/,/^# resolve password/p' "${entrypoint}") + grep -qx 'enable_fqdn_mode = false' "${FE_CONFFILE}" + test "$(grep -c '^enable_fqdn_mode[[:space:]]*=' "${FE_CONFFILE}")" -eq 1 +) + +echo 'Non-root projected configuration tests passed' + +for variant in fe be be_disaggregated; do + ( + component=${variant%%_*} + DORIS_HOME="${TEST_DIR}/${variant} home" + KRB5_MOUNT_PATH="${TEST_DIR}/${variant}-kerberos" + KRB5_CONFIG="${TEST_DIR}/${variant}-etc/krb5.conf" + export KEYTAB_MOUNT_PATH= + export KEYTAB_FINAL_USED_PATH= + mkdir -p "${KRB5_MOUNT_PATH}" "$(dirname "${KRB5_CONFIG}")" + printf '[libdefaults]\n default_realm = TEST.LOCAL\n' > "${KRB5_MOUNT_PATH}/krb5.conf" + ln -s "${DORIS_HOME}/conf/krb5.conf" "${KRB5_CONFIG}" + chmod 555 "$(dirname "${KRB5_CONFIG}")" + # shellcheck disable=SC1090 + source <(sed -n '/^mount_kerberos_config()/,/^}/p' \ + "${RUNTIME_DIR}/${component}/resource/${variant}_entrypoint.sh") + mount_kerberos_config + mount_kerberos_config + test "$(readlink -f "${KRB5_CONFIG}")" = "$(readlink -f "${KRB5_MOUNT_PATH}/krb5.conf")" + test -L "${DORIS_HOME}/conf/krb5.conf" + ) +done + +echo 'Non-root Kerberos configuration tests passed' + +( + DORIS_HOME="${TEST_DIR}/ms" + CONFIGMAP_PATH="${TEST_DIR}/ms-configmap" + FDB_ENDPOINT='test:test@fdb:4500' + mkdir -p "${DORIS_HOME}/ms/conf" "${CONFIGMAP_PATH}/..data" + printf '# original config\n' > "${DORIS_HOME}/ms/conf/doris_cloud.conf" + printf '# projected config\nfdb_cluster=old:old@old:4500\n' > "${CONFIGMAP_PATH}/..data/doris_cloud.conf" + chmod 444 "${CONFIGMAP_PATH}/..data/doris_cloud.conf" + ln -s '..data/doris_cloud.conf' "${CONFIGMAP_PATH}/doris_cloud.conf" + printf '\n' > "${CONFIGMAP_PATH}/core-site.xml" + # shellcheck disable=SC1090 + source <(sed -n '/^update_conf_from_configmap()/,/^}/p' \ + "${RUNTIME_DIR}/ms/resource/ms_disaggregated_entrypoint.sh") + update_conf_from_configmap + grep -q '^# original config$' "${DORIS_HOME}/ms/conf/doris_cloud.conf.bak" + update_conf_from_configmap + test ! -L "${DORIS_HOME}/ms/conf/doris_cloud.conf" + test -L "${DORIS_HOME}/ms/conf/core-site.xml" + grep -qx "fdb_cluster=${FDB_ENDPOINT}" "${DORIS_HOME}/ms/conf/doris_cloud.conf" + test "$(grep -c '^fdb_cluster=' "${DORIS_HOME}/ms/conf/doris_cloud.conf")" -eq 1 + grep -qx 'fdb_cluster=old:old@old:4500' "${CONFIGMAP_PATH}/doris_cloud.conf" + echo 'test_setting = true' >> "${DORIS_HOME}/ms/conf/doris_cloud.conf" + + # Without a ConfigMap, repeated starts replace the endpoint in the image config. + CONFIGMAP_PATH="${TEST_DIR}/no-configmap" + FDB_ENDPOINT='new:new@new-fdb:4500' + update_conf_from_configmap + update_conf_from_configmap + grep -qx "fdb_cluster=${FDB_ENDPOINT}" "${DORIS_HOME}/ms/conf/doris_cloud.conf" + test "$(grep -c '^fdb_cluster=' "${DORIS_HOME}/ms/conf/doris_cloud.conf")" -eq 1 + unset FDB_ENDPOINT + update_conf_from_configmap + grep -qx 'fdb_cluster=new:new@new-fdb:4500' "${DORIS_HOME}/ms/conf/doris_cloud.conf" +) + +echo 'Non-root MS configuration tests passed' diff --git a/docker/runtime/tests/test_docker_build.py b/docker/runtime/tests/test_docker_build.py index 7cef62a9de2404..282191e6b2a3ae 100644 --- a/docker/runtime/tests/test_docker_build.py +++ b/docker/runtime/tests/test_docker_build.py @@ -54,6 +54,7 @@ import sys args = sys.argv[1:] +Path(os.environ["CHECKSUM_URL"]).write_text(args[args.index("-o") - 1]) Path(args[args.index("-o") + 1]).write_text(os.environ["CHECKSUM_DOCUMENT"]) """ @@ -96,9 +97,11 @@ def archive(self, version="4.1.5", arch="arm64", include_ms=True): def build(self, archive, digest, version="4.1.5", script="docker-build.sh", **overrides): env = dict(os.environ) env.pop("DORIS_IMAGE_TAG_SUFFIX", None) + env.pop("DORIS_DOWNLOAD_BASE_URL", None) env.update( PATH=f"{self.bin}:{env['PATH']}", DOCKER_CALLS=str(self.calls), + CHECKSUM_URL=str(self.work / "checksum-url"), DORIS_ARCHIVE=str(archive), DORIS_ARCHIVE_SHA512=digest, DORIS_ARCH="arm64", @@ -148,13 +151,16 @@ def test_ubi9_build_selects_its_base_and_keeps_tags_and_metadata_separate(self): with self.subTest(script=script): self.calls.unlink(missing_ok=True) result = self.build(archive, digest, script=str(runtime / script), - DORIS_BASE_IMAGE="", DORIS_IMAGE_WORK_DIR="") + DORIS_BASE_IMAGE="", DORIS_IMAGE_WORK_DIR="", + DORIS_UID="20001", DORIS_GID="20002") self.assertEqual(result.returncode, 0, result.stderr) builds = [call["args"] for call in self.read_calls() if call["args"][0] == "build"] base = f"apache/doris:base-8.0-{variant}-jdk17" self.assertIn(base, builds[0]) self.assertIn(f"RUNTIME_DISTRO={distro}", builds[0]) for role, args in zip(["fe", "be", "ms"], builds[1:]): + self.assertIn("DORIS_UID=20001", args) + self.assertIn("DORIS_GID=20002", args) self.assertIn(f"BASE_IMAGE={base}", args) self.assertIn(f"runtime-test.{role}:4.1.5{suffix}", args) record = checkout / "output/runtime-images" / variant / f"{role}-4.1.5-arm64.image.json" @@ -203,6 +209,16 @@ def test_checksum_documents_accept_gnu_and_bsd_formats(self): result = self.build(archive, "", CHECKSUM_DOCUMENT=document) self.assertEqual(result.returncode, 0, result.stderr) + def test_default_checksum_uses_the_apache_download_mirror(self): + archive, digest = self.archive() + result = self.build(archive, "", CHECKSUM_DOCUMENT=f"{digest} archive.tar.gz\n") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + (self.work / "checksum-url").read_text(), + "https://apache-doris-downloads.oss-accelerate.aliyuncs.com/" + "apache-doris-4.1.5-bin-arm64.tar.gz.sha512", + ) + def test_ambiguous_or_malformed_checksum_document_is_rejected(self): archive, digest = self.archive() for document in ["not a checksum", f"{digest}\n{digest}\n"]: @@ -211,6 +227,17 @@ def test_ambiguous_or_malformed_checksum_document_is_rejected(self): self.assertNotEqual(result.returncode, 0) self.assertFalse(self.calls.exists()) + def test_invalid_runtime_identity_is_rejected_before_artifact_work(self): + archive, digest = self.archive() + for key in ("DORIS_UID", "DORIS_GID"): + for value in ("0", "-1", "1x", "010001", "4294967295", ""): + with self.subTest(key=key, value=value): + self.calls.unlink(missing_ok=True) + result = self.build(archive, digest, **{key: value}) + self.assertNotEqual(result.returncode, 0) + self.assertIn(f"Invalid {key}", result.stderr) + self.assertFalse(self.calls.exists()) + def test_invalid_version_is_rejected_before_building(self): archive, digest = self.archive() result = self.build(archive, digest, version="../4.1.5")