Skip to content

Commit 0da3422

Browse files
robcmannlimingyaoo
authored andcommitted
feat: assume the privileged user is the user that is running nucleus
1 parent b0b767b commit 0da3422

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/main/java/com/aws/greengrass/util/platforms/unix/UnixPlatform.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public class UnixPlatform extends Platform {
6060

6161
public static final String LOADER_LOGS_FILE_NAME = "loader.log";
6262
public static final Pattern PS_PID_PATTERN = Pattern.compile("(\\d+)\\s+(\\d+)");
63-
public static final String PRIVILEGED_USER = "root";
6463
public static final String STDOUT = "stdout";
6564
public static final String STDERR = "stderr";
6665
protected static final int SIGTERM = 15;
@@ -80,6 +79,9 @@ public class UnixPlatform extends Platform {
8079
private static final int MAX_IPC_SOCKET_CREATION_WAIT_TIME_SECONDS = 30;
8180
public static final int SOCKET_CREATE_POLL_INTERVAL_MS = 200;
8281

82+
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
83+
justification = "Writing to static field in constructor is safe because Platform is a singleton.")
84+
private static String PRIVILEGED_USER;
8385
private static UnixUserAttributes CURRENT_USER;
8486
private static UnixGroupAttributes CURRENT_USER_PRIMARY_GROUP;
8587
private static final Lock lock = LockFactory.newReentrantLock(UnixPlatform.class.getSimpleName());
@@ -93,11 +95,18 @@ public class UnixPlatform extends Platform {
9395
/**
9496
* Construct a new instance.
9597
*/
98+
@SuppressWarnings("PMD.AssignmentToNonFinalStatic")
9699
public UnixPlatform() {
97100
super();
98101
// avoid spamming DEBUG-level oshi logs when reading process stats
99102
LogManager.getLogger(oshi.util.FileUtil.class.getName()).setLevel("INFO");
100103
runWithGenerator = new UnixRunWithGenerator(this);
104+
try {
105+
PRIVILEGED_USER = loadCurrentUser().getPrincipalName();
106+
} catch (IOException e) {
107+
// This should not happen, but set the PRIVILEGED_USER to root if it fails
108+
PRIVILEGED_USER = "root";
109+
}
101110
}
102111

103112
/**

src/test/java/com/aws/greengrass/util/platforms/unix/UnixPlatformTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import static org.hamcrest.Matchers.contains;
2121
import static org.hamcrest.Matchers.containsInAnyOrder;
2222
import static org.hamcrest.Matchers.empty;
23+
import static org.hamcrest.Matchers.equalTo;
2324
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.not;
26+
import static org.hamcrest.Matchers.notNullValue;
2427

2528
@ExtendWith({GGExtension.class})
2629
@DisabledOnOs(OS.WINDOWS)
@@ -63,6 +66,16 @@ void GIVEN_user_WHEN_decorate_THEN_generate_sudo_without_group() {
6366
is(arrayContaining("sudo", "-n", "-E", "-H", "-u", "foo", "--", "echo", "hello", "world")));
6467
}
6568

69+
@Test
70+
void GIVEN_unix_platform_WHEN_get_privileged_user_THEN_returns_current_user() {
71+
UnixPlatform platform = new UnixPlatform();
72+
String privilegedUser = platform.getPrivilegedUser();
73+
74+
assertThat(privilegedUser, is(notNullValue()));
75+
assertThat(privilegedUser, is(not(equalTo(""))));
76+
assertThat(privilegedUser, is(equalTo(System.getProperty("user.name"))));
77+
}
78+
6679
@Test
6780
void GIVEN_file_system_permission_WHEN_convert_to_posix_THEN_succeed() {
6881
// Nothing

0 commit comments

Comments
 (0)