Skip to content

Commit 71d0709

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

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-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
/**

0 commit comments

Comments
 (0)