From d59000ceb153b533a839b9e2f9b07eef75b21609 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Feb 2018 14:18:03 +0000 Subject: [PATCH] Change ownership when log file is created by launch script Closes gh-11951 --- .../boot/launchscript/SysVinitLaunchScriptIT.java | 13 ++++++++++++- .../log-file-ownership-is-changed-when-created.sh | 9 +++++++++ .../log-file-ownership-is-unchanged-when-exists.sh | 11 +++++++++++ .../springframework/boot/loader/tools/launch.script | 10 +++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100755 spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-changed-when-created.sh create mode 100755 spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-unchanged-when-exists.sh diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java index d8309130868..6bd7d6bbcd8 100644 --- a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java @@ -223,7 +223,6 @@ public class SysVinitLaunchScriptIT { @Test public void pidFolderOwnership() throws Exception { String output = doTest("pid-folder-ownership.sh"); - System.err.println(output); assertThat(output).contains("phil root"); } @@ -239,6 +238,18 @@ public class SysVinitLaunchScriptIT { assertThat(output).contains("phil root"); } + @Test + public void logFileOwnershipIsChangedWhenCreated() throws Exception { + String output = doTest("log-file-ownership-is-changed-when-created.sh"); + assertThat(output).contains("andy root"); + } + + @Test + public void logFileOwnershipIsUnchangedWhenExists() throws Exception { + String output = doTest("log-file-ownership-is-unchanged-when-exists.sh"); + assertThat(output).contains("root root"); + } + @Test public void launchWithRelativeLogFolder() throws Exception { String output = doTest("launch-with-relative-log-folder.sh"); diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-changed-when-created.sh b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-changed-when-created.sh new file mode 100755 index 00000000000..c007f35cadc --- /dev/null +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-changed-when-created.sh @@ -0,0 +1,9 @@ +source ./test-functions.sh +install_service +echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf +mkdir -p /test-service/log +useradd andy +chown andy /test-service/spring-boot-app.jar +start_service +await_app +ls -al /test-service/log/spring-boot-app.log \ No newline at end of file diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-unchanged-when-exists.sh b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-unchanged-when-exists.sh new file mode 100755 index 00000000000..ec28e35ac2f --- /dev/null +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/log-file-ownership-is-unchanged-when-exists.sh @@ -0,0 +1,11 @@ +source ./test-functions.sh +install_service +echo 'LOG_FOLDER=log' > /test-service/spring-boot-app.conf +mkdir -p /test-service/log +touch /test-service/log/spring-boot-app.log +chmod a+w /test-service/log/spring-boot-app.log +useradd andy +chown andy /test-service/spring-boot-app.jar +start_service +await_app +ls -al /test-service/log/spring-boot-app.log \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script index aac9114eb3a..9105ee3051a 100755 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script @@ -148,7 +148,15 @@ do_start() { pushd "$working_dir" > /dev/null if [[ ! -e "$PID_FOLDER" ]]; then mkdir -p "$PID_FOLDER" &> /dev/null - chown "$run_user" "$PID_FOLDER" + if [[ -n "$run_user" ]]; then + chown "$run_user" "$PID_FOLDER" + fi + fi + if [[ ! -e "$log_file" ]]; then + touch "$log_file" &> /dev/null + if [[ -n "$run_user" ]]; then + chown "$run_user" "$log_file" + fi fi if [[ -n "$run_user" ]]; then checkPermissions || return $?