Skip to content

Commit

Permalink
Match Rails 7.1 templates: placate k8s runAsNonRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Oct 31, 2023
1 parent ac5e921 commit ea38716
Show file tree
Hide file tree
Showing 25 changed files with 102 additions and 75 deletions.
13 changes: 8 additions & 5 deletions lib/generators/templates/Dockerfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,30 @@ RUN mkdir /data
<% else -%>
# Run and own only the runtime files as a non-root user for security
<% if options.compose? -%>
<% user = "rails:rails" -%>
ARG UID=1000 \
GID=1000
RUN groupadd -f -g $GID rails && \
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
<% else -%>
RUN useradd rails --create-home --shell /bin/bash && \
<% user = "1000:1000" -%>
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
<% end -%>
<% if options.nginx? -%>
chown rails:rails /var/lib/nginx /var/log/nginx/* && \
chown <%= user %> /var/lib/nginx /var/log/nginx/* && \
<% end -%>
<% if deploy_packages.include?("sudo") && options.sudo? -%>
sed -i 's/env_reset/env_keep="*"/' /etc/sudoers && \
<% end -%>
<% if deploy_database == 'sqlite3' -%>
mkdir /data<% if using_litefs? %> /litefs<% end %> && \
chown -R rails:rails <%= Dir[*%w(db log storage tmp)].join(" ") %> /data<% if using_litefs? %> /litefs<% end %>
chown -R <%= user %> <%= Dir[*%w(db log storage tmp)].join(" ") %> /data<% if using_litefs? %> /litefs<% end %>
<% else -%>
chown -R rails:rails <%= Dir[*%w(db log storage tmp)].join(" ") %>
chown -R <%= user %> <%= Dir[*%w(db log storage tmp)].join(" ") %>
<% end -%>
<% unless options.swap? or using_passenger? or using_litefs? -%>
USER rails:rails
USER <%= user %>
<% end -%>
<% end -%>
Expand Down
7 changes: 4 additions & 3 deletions test/results/bin_cd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/cache/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/config/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
Expand Down
7 changes: 4 additions & 3 deletions test/results/env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/execjs_importmap/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/execjs_node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/fullstaq/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/grover/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
Expand Down
5 changes: 3 additions & 2 deletions test/results/idle/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ COPY --from=build /rails /rails
COPY --from=build /root/.passenger/native_support /root/.passenger/native_support

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
chown -R 1000:1000 db log storage tmp /data

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/jemalloc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
Expand Down
7 changes: 4 additions & 3 deletions test/results/label/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
5 changes: 3 additions & 2 deletions test/results/litefs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data /litefs && \
chown -R rails:rails db log storage tmp /data /litefs
chown -R 1000:1000 db log storage tmp /data /litefs

# Authorize rails user to launch litefs
COPY <<-"EOF" /etc/sudoers.d/rails
Expand Down
7 changes: 4 additions & 3 deletions test/results/litestack/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/minimal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
9 changes: 5 additions & 4 deletions test/results/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown rails:rails /var/lib/nginx /var/log/nginx/* && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown 1000:1000 /var/lib/nginx /var/log/nginx/* && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
Expand Down
7 changes: 4 additions & 3 deletions test/results/no_prep/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/parallel/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/precompile_defer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ RUN apt-get update -qq && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/rmagick/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
5 changes: 3 additions & 2 deletions test/results/swap/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
chown -R 1000:1000 db log storage tmp /data

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/vite/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/windows/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"
Expand Down
7 changes: 4 additions & 3 deletions test/results/yjit/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
Expand Down

0 comments on commit ea38716

Please sign in to comment.