Skip to content

Commit 7062d36

Browse files
Make image rootless, and run it rootless (#1975)
Create megalinter user and group in Docker image, both with ID 1000, and activate this user after dependencies have been installed. Run Docker container as current user via mega-linter-runner. The change to mega-linter-runner only affects POSIX platforms, because process.getuid and process.getgid are only available there. Previously, mega-linter-runner ran the MegaLinter Docker image as root. Users whose files became owned by root as a consequence of this behavior will need to chown them to be owned by the appropriate user when upgrading MegaLinter.
1 parent 4f4b2e8 commit 7062d36

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
1616

1717
- Core
1818
- Fix failure of AzureCommentReporter when there is no pull request found in ENV vars
19+
- Make Docker image rootless, and run it as current user rather than root on
20+
POSIX by @Kurt-von-Laven in [#1975](https://github.com/oxsecurity/megalinter/issues/1975).
1921

2022
- Documentation
2123

Dockerfile

+27-20
Original file line numberDiff line numberDiff line change
@@ -634,39 +634,46 @@ RUN dotnet tool install --global Microsoft.CST.DevSkim.CLI --version 0.7.104 \
634634
&& find ${ML_THIRD_PARTY_DIR} -type f -not -name 'LICENSE*' -delete -o -type d -empty -delete \
635635
&& find /tmp -path '/tmp/tmp.*' -type f -name 'misspell*' -delete -o -type d -empty -delete \
636636

637-
# tsqllint installation
638-
# Next line commented because already managed by another linter
639-
# RUN wget --tries=5 -q -O dotnet-install.sh https://dot.net/v1/dotnet-install.sh \
640-
# && chmod +x dotnet-install.sh \
641-
# && ./dotnet-install.sh --install-dir /usr/share/dotnet -channel 6.0 -version latest
642-
# Next line commented because already managed by another linter
643-
# ENV PATH="${PATH}:/root/.dotnet/tools:/usr/share/dotnet"
637+
# tsqllint installation
638+
# Next line commented because already managed by another linter
639+
# RUN wget --tries=5 -q -O dotnet-install.sh https://dot.net/v1/dotnet-install.sh \
640+
# && chmod +x dotnet-install.sh \
641+
# && ./dotnet-install.sh --install-dir /usr/share/dotnet -channel 6.0 -version latest
642+
# Next line commented because already managed by another linter
643+
# ENV PATH="${PATH}:/root/.dotnet/tools:/usr/share/dotnet"
644644
&& dotnet tool install --global TSQLLint \
645645

646-
# tflint installation
647-
# Managed with COPY --link --from=tflint /usr/local/bin/tflint /usr/bin/
646+
# tflint installation
647+
# Managed with COPY --link --from=tflint /usr/local/bin/tflint /usr/bin/
648648

649-
# terrascan installation
650-
# Managed with COPY --link --from=terrascan /go/bin/terrascan /usr/bin/
649+
# terrascan installation
650+
# Managed with COPY --link --from=terrascan /go/bin/terrascan /usr/bin/
651651

652-
# terragrunt installation
653-
# Managed with COPY --link --from=terragrunt /usr/local/bin/terragrunt /usr/bin/
652+
# terragrunt installation
653+
# Managed with COPY --link --from=terragrunt /usr/local/bin/terragrunt /usr/bin/
654654

655-
# terraform-fmt installation
656-
# Managed with COPY --link --from=terragrunt /bin/terraform /usr/bin/
655+
# terraform-fmt installation
656+
# Managed with COPY --link --from=terragrunt /bin/terraform /usr/bin/
657657

658-
# kics installation
659-
# Managed with COPY --link --from=kics /app/bin/kics /usr/bin/
658+
# kics installation
659+
# Managed with COPY --link --from=kics /app/bin/kics /usr/bin/
660660
&& mkdir -p /opt/kics/assets
661661
ENV KICS_QUERIES_PATH=/opt/kics/assets/queries KICS_LIBRARIES_PATH=/opt/kics/assets/libraries
662662
# Managed with COPY --from=kics /app/bin/assets /opt/kics/assets/
663663

664664
#OTHER__END
665665

666+
#######################################
667+
# Create and activate megalinter user #
668+
#######################################
669+
RUN groupadd --gid 1000 megalinter \
670+
&& adduser --uid 1000 megalinter
671+
USER megalinter
672+
666673
################################
667674
# Installs python dependencies #
668675
################################
669-
COPY megalinter /megalinter
676+
COPY --chown=megalinter:megalinter megalinter /megalinter
670677
RUN PYTHONDONTWRITEBYTECODE=1 python /megalinter/setup.py install \
671678
&& PYTHONDONTWRITEBYTECODE=1 python /megalinter/setup.py clean --all \
672679
&& rm -rf /var/cache/apk/* \
@@ -675,8 +682,8 @@ RUN PYTHONDONTWRITEBYTECODE=1 python /megalinter/setup.py install \
675682
#######################################
676683
# Copy scripts and rules to container #
677684
#######################################
678-
COPY megalinter/descriptors /megalinter-descriptors
679-
COPY TEMPLATES /action/lib/.automation
685+
COPY --chown=megalinter:megalinter megalinter/descriptors /megalinter-descriptors
686+
COPY --chown=megalinter:megalinter TEMPLATES /action/lib/.automation
680687

681688
###########################
682689
# Get the build arguments #

mega-linter-runner/lib/runner.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const optionsDefinition = require("./options");
44
const { spawnSync } = require("child_process");
55
const c = require("chalk");
66
const path = require("path");
7+
const { getgid, getuid } = require("process");
78
const which = require("which");
89
const fs = require("fs-extra");
910
const { MegaLinterUpgrader } = require("./upgrade");
@@ -132,6 +133,9 @@ ERROR: Docker engine has not been found on your system.
132133
if (options["containerName"]) {
133134
commandArgs.push(...["--name", options["containerName"]]);
134135
}
136+
if (getuid && getgid) {
137+
commandArgs.push(...["--user", `${getuid()}:${getgid()}`]);
138+
}
135139
commandArgs.push(...["-v", "/var/run/docker.sock:/var/run/docker.sock:rw"]);
136140
commandArgs.push(...["-v", `${lintPath}:/tmp/lint:rw`]);
137141
if (options.fix === true) {

0 commit comments

Comments
 (0)