Skip to content

Commit 79b5fd9

Browse files
committed
Polish "Allow the user that runs the app to be specified via an env var"
See gh-16973
1 parent b57f358 commit 79b5fd9

File tree

6 files changed

+22
-25
lines changed

6 files changed

+22
-25
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/deployment.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,10 @@ For example, on Debian, you could use the following command:
491491
NOTE: The following is a set of guidelines on how to secure a Spring Boot application that runs as an init.d service.
492492
It is not intended to be an exhaustive list of everything that should be done to harden an application and the environment in which it runs.
493493

494-
When executed as root, as is the case when root is being used to start an init.d service, the default executable script runs the application as the user who owns the jar file.
495-
You should never run a Spring Boot application as `root`, so your application's jar file should never be owned by root.
496-
Instead, create a specific user to run your application and use `chown` to make it the owner of the jar file, as shown in the following example:
494+
When executed as root, as is the case when root is being used to start an init.d service, the default executable script runs the application as the user specified in the `RUN_AS_USER` environment variable.
495+
When the environment variable is not set, the user who owns the jar file is used instead.
496+
You should never run a Spring Boot application as `root`, so `RUN_AS_USER` should never be root and your application's jar file should never be owned by root.
497+
Instead, create a specific user to run your application and set the `RUN_AS_USER` environment variable or use `chown` to make it the owner of the jar file, as shown in the following example:
497498

498499
[indent=0,subs="verbatim,quotes,attributes"]
499500
----
@@ -709,9 +710,8 @@ The following environment properties are supported with the default script:
709710
You can explicitly set it to `service` so that the `stop\|start\|status\|restart` commands work or to `run` if you want to run the script in the foreground.
710711

711712
| `RUN_AS_USER`
712-
| If set, the application will be executed as the informed user.
713-
For security reasons, you should never run an user space application as `root`, therefore it's recommended to set this property.
714-
Defaults to the user who owns the jar file.
713+
| The user that will be used to run the application.
714+
When not set, the user that owns the jar file will be used.
715715

716716
| `USE_START_STOP_DAEMON`
717717
| Whether the `start-stop-daemon` command, when it's available, should be used to control the process.

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,18 @@ log_file="$LOG_FOLDER/$LOG_FILENAME"
128128
# shellcheck disable=SC2012
129129
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
130130

131-
# Force run as informed user (from environment variable)
131+
# Run as user specified in RUN_AS_USER
132132
if [[ -n "$RUN_AS_USER" ]]; then
133-
# checks performed for all actions except 'status' and 'run'
134133
if ! [[ "$action" =~ ^(status|run)$ ]]; then
135-
# Issue a error if informed user is not valid
136134
id -u "$RUN_AS_USER" || {
137135
echoRed "Cannot run as '$RUN_AS_USER': no such user"
138-
exit 5
136+
exit 2
139137
}
140-
141-
# Issue a error if we are not root
142138
[[ $(id -u) == 0 ]] || {
143-
echoRed "root required to run as '$RUN_AS_USER'"
144-
exit 6
139+
echoRed "Cannot run as '$RUN_AS_USER': current user is not root"
140+
exit 4
145141
}
146142
fi
147-
148143
run_user="$RUN_AS_USER"
149144
fi
150145

spring-boot-tests/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,32 +269,34 @@ public void launchWithRelativeLogFolder(String os, String version) throws Except
269269

270270
@ParameterizedTest(name = "{0} {1}")
271271
@MethodSource("parameters")
272-
public void launchWithRunAs(String os, String version) throws Exception {
273-
String output = doTest(os, version, "launch-with-run-as.sh");
272+
public void launchWithRunAsUser(String os, String version) throws Exception {
273+
String output = doTest(os, version, "launch-with-run-as-user.sh");
274274
assertThat(output).contains("wagner root");
275275
}
276276

277277
@ParameterizedTest(name = "{0} {1}")
278278
@MethodSource("parameters")
279-
public void launchWithRunAsInvalidUser(String os, String version) throws Exception {
279+
public void whenRunAsUserDoesNotExistLaunchFailsWithInvalidArgument(String os, String version) throws Exception {
280280
String output = doTest(os, version, "launch-with-run-as-invalid-user.sh");
281-
assertThat(output).contains("Status: 5");
281+
assertThat(output).contains("Status: 2");
282282
assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'johndoe': no such user"));
283283
}
284284

285285
@ParameterizedTest(name = "{0} {1}")
286286
@MethodSource("parameters")
287-
public void launchWithRunAsPreferUserInformed(String os, String version) throws Exception {
288-
String output = doTest(os, version, "launch-with-run-as-prefer-user-informed.sh");
287+
public void whenJarOwnerAndRunAsUserAreBothSpecifiedRunAsUserTakesPrecedence(String os, String version)
288+
throws Exception {
289+
String output = doTest(os, version, "launch-with-run-as-user-preferred-to-jar-owner.sh");
289290
assertThat(output).contains("wagner root");
290291
}
291292

292293
@ParameterizedTest(name = "{0} {1}")
293294
@MethodSource("parameters")
294-
public void launchWithRunAsRootRequired(String os, String version) throws Exception {
295-
String output = doTest(os, version, "launch-with-run-as-root-required.sh");
296-
assertThat(output).contains("Status: 6");
297-
assertThat(output).has(coloredString(AnsiColor.RED, "root required to run as 'wagner'"));
295+
public void whenLaunchedUsingNonRootUserWithRunAsUserSpecifiedLaunchFailsWithInsufficientPrivilege(String os,
296+
String version) throws Exception {
297+
String output = doTest(os, version, "launch-with-run-as-user-root-required.sh");
298+
assertThat(output).contains("Status: 4");
299+
assertThat(output).has(coloredString(AnsiColor.RED, "Cannot run as 'wagner': current user is not root"));
298300
}
299301

300302
static List<Object[]> parameters() {

0 commit comments

Comments
 (0)