Skip to content

Commit d82d209

Browse files
author
Maxime Gréau
authored
[CI] non-root user to build artifacts inside the container (#1783)
[CI] non-root user to build artifacts inside the container This commit updates the Dockerfile to create a non-root specific user for building the ES Client artifacts inside the container. This specific user id can be passed at buildtime to make it work on both local dev and CI workflows.
1 parent a970dda commit d82d209

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

.ci/Dockerfile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
ARG RUBY_TEST_VERSION=2.7
22
FROM ruby:${RUBY_TEST_VERSION}
33

4+
# Default UID/GID to 1000
5+
# it can be overridden at build time
6+
ARG BUILDER_UID=1000
7+
ARG BUILDER_GID=1000
8+
ENV BUILDER_USER elastic
9+
ENV BUILDER_GROUP elastic
10+
411
ENV GEM_HOME="/usr/local/bundle"
5-
ENV PATH $GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH
12+
ENV PATH="$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"
613
ENV QUIET=true
714
ENV CI=true
815

16+
# Install required tools
17+
RUN apt-get -q update \
18+
&& apt-get -y install zip \
19+
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
20+
21+
# Create user
22+
RUN groupadd --system -g ${BUILDER_GID} ${BUILDER_GROUP} \
23+
&& useradd --system --shell /bin/bash -u ${BUILDER_UID} -g ${BUILDER_GROUP} -d /var/lib/elastic -m elastic 1>/dev/null 2>/dev/null \
24+
&& mkdir -p /usr/src/app && touch /Gemfile.lock \
25+
&& chown -R ${BUILDER_USER}:${BUILDER_GROUP} /usr/src/app /Gemfile.lock
26+
27+
COPY --chown=$BUILDER_USER:$BUILDER_GROUP . .
28+
929
WORKDIR /usr/src/app
1030

11-
COPY . .
31+
USER ${BUILDER_USER}:${BUILDER_GROUP}
1232

13-
RUN apt-get update
14-
RUN apt-get install zip
15-
RUN bundle install --quiet
16-
RUN bundle exec rake bundle:clean
17-
RUN rake bundle:install
33+
RUN bundle install \
34+
&& bundle exec rake bundle:clean \
35+
&& rake bundle:install

.ci/make.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ echo -e "\033[1m>>>>> Build [elastic/elasticsearch-ruby container] >>>>>>>>>>>>>
116116
# ------------------------------------------------------- #
117117

118118
echo -e "\033[34;1mINFO: building $product container\033[0m"
119-
docker build --file .ci/Dockerfile --tag ${product} .
119+
docker build --build-arg BUILDER_UID="$(id -u)" --file .ci/Dockerfile --tag ${product} .
120120

121121
# ------------------------------------------------------- #
122122
# Run the Container
@@ -131,6 +131,7 @@ args_string="${TASK_ARGS[*]}"
131131
args_string="${args_string// /,}"
132132

133133
docker run \
134+
-u "$(id -u)" \
134135
--env "RUBY_TEST_VERSION=${RUBY_TEST_VERSION}" \
135136
--env "WORKFLOW=${WORKFLOW}" \
136137
--name test-runner \

.ci/run-elasticsearch.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ END
115115
echo -e "\033[34;1mINFO:\033[0m Starting container $node_name \033[0m"
116116
set -x
117117
docker run \
118+
-u "$(id -u)" \
118119
--name "$node_name" \
119120
--network "$network_name" \
120121
--env "ES_JAVA_OPTS=-Xms1g -Xmx1g -da:org.elasticsearch.xpack.ccr.index.engine.FollowingEngineAssertions" \

.ci/run-repository.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ repo=`pwd`
4343
# run the client tests
4444
if [[ $TEST_SUITE != "platinum" ]]; then
4545
docker run \
46+
-u "$(id -u)" \
4647
--network="${network_name}" \
4748
--env "TEST_ES_SERVER=${elasticsearch_url}" \
4849
--env "TEST_SUITE=${TEST_SUITE}" \
@@ -53,6 +54,7 @@ if [[ $TEST_SUITE != "platinum" ]]; then
5354
bundle exec rake elasticsearch:download_artifacts test:rest_api
5455
else
5556
docker run \
57+
-u "$(id -u)" \
5658
--network="${network_name}" \
5759
--env "TEST_ES_SERVER=${elasticsearch_url}" \
5860
--env "ELASTIC_PASSWORD=${elastic_password}" \

rake_tasks/unified_release_tasks.rake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ namespace :unified_release do
3333

3434
build_gems(args[:output_dir])
3535
create_zip_file(args[:output_dir])
36-
sh "chmod 666 #{CURRENT_PATH.join(args[:output_dir])}/* && " \
37-
"chmod 666 #{CURRENT_PATH.join(args[:output_dir])}"
3836
end
3937

4038
def build_gems(output_dir)

0 commit comments

Comments
 (0)