Skip to content

Commit 6d12360

Browse files
authored
Merge pull request #2829 from DFE-Digital/use-non-root-user
Use non-root user
2 parents 28338c3 + fade906 commit 6d12360

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ RUN apk add --update --no-cache tzdata && \
7979
cp /usr/share/zoneinfo/Europe/London /etc/localtime && \
8080
echo "Europe/London" > /etc/timezone
8181

82+
# Create non-root user and group
83+
RUN addgroup -S appgroup -g 20001 && adduser -S appuser -G appgroup -u 10001
84+
8285
# Upgrade ssl, crypto and curl libraries to latest version
8386
RUN apk upgrade --no-cache openssl libssl3 libcrypto3 curl expat
8487

@@ -97,5 +100,10 @@ RUN apk add font-terminus font-bitstream-100dpi font-bitstream-75dpi font-bitstr
97100
COPY --from=builder /app /app
98101
COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
99102

103+
RUN chown -R appuser:appgroup /app/tmp /app/log
104+
105+
# Use non-root user
106+
USER 10001
107+
100108
CMD bundle exec rails db:migrate:ignore_concurrent_migration_exceptions && \
101109
bundle exec rails server -b 0.0.0.0

terraform/application/application.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ module "web_application" {
5252
kubernetes_config_map_name = module.application_configuration.kubernetes_config_map_name
5353
kubernetes_secret_name = module.application_configuration.kubernetes_secret_name
5454

55-
docker_image = var.docker_image
56-
replicas = var.app_replicas
57-
enable_logit = var.enable_logit
55+
docker_image = var.docker_image
56+
replicas = var.app_replicas
57+
enable_logit = var.enable_logit
58+
run_as_non_root = var.run_as_non_root
5859
}
5960

6061
module "worker_application" {
@@ -76,6 +77,7 @@ module "worker_application" {
7677
command = ["bundle", "exec", "sidekiq", "-C", "./config/sidekiq.yml"]
7778
probe_command = ["pgrep", "-f", "sidekiq"]
7879

79-
enable_logit = var.enable_logit
80-
enable_gcp_wif = var.bigquery_federated_auth ? true : null
80+
enable_logit = var.enable_logit
81+
enable_gcp_wif = var.bigquery_federated_auth ? true : null
82+
run_as_non_root = var.run_as_non_root
8183
}

terraform/application/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ variable "bigquery_federated_auth" {
9191
description = "Configure environment variable to let dfe-analytics use federated authentication"
9292
}
9393

94+
variable "run_as_non_root" {
95+
type = bool
96+
default = true
97+
description = "Whether to enforce that containers must run as non-root user"
98+
}
99+
94100
locals {
95101
environment_variables = yamldecode(file("${path.module}/config/${var.app_environment}/variables.yml"))
96102
}

0 commit comments

Comments
 (0)