Skip to content

Commit 8944d13

Browse files
committed
HADOOP-19744. Reduce WARN log messages in SubjectUtil.checkThreadInheritsSubject
1 parent a50fe99 commit 8944d13

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/util/SubjectUtil.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,17 @@ private static MethodHandle lookupCallAs() {
9090
*/
9191
private static boolean checkThreadInheritsSubject() {
9292

93-
boolean securityManagerEnabled = true;
94-
try {
95-
// TODO this needs SecurityManager to compile, use reflection to look it up instead
96-
SecurityManager sm = System.getSecurityManager();
97-
System.setSecurityManager(sm);
98-
} catch (UnsupportedOperationException e) {
99-
// JDK24+ unconditionally throws this, so we don't need to check for JDK24+
100-
// explicitly
101-
securityManagerEnabled = false;
102-
} catch (Throwable t) {
103-
// don't care
93+
if (JAVA_SPEC_VER <= 21) {
94+
return true;
95+
} else {
96+
// 24+ never inherits the Subject.
97+
// For 22 and 23 the behavior actually depends on whether the SecurityManager
98+
// is enabled, but this check is only used to enable a minor performance
99+
// optimization of bypassing a doAs/callAs call in SubjectInheritingThread.
100+
// We accept that possible minor performance cost to avoid extra complexity
101+
// and prevent the HVM from logging SecurityManager warnings to console.
102+
return false;
104103
}
105-
106-
return JAVA_SPEC_VER < 22 || securityManagerEnabled;
107104
}
108105

109106
/**

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/concurrent/TestSubjectPropagation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ public void run() {
147147
});
148148

149149
if (SubjectUtil.THREAD_INHERITS_SUBJECT) {
150+
150151
assertEquals(parentSubject, childSubject);
151152
} else {
152153
// This is the behaviour that breaks Hadoop authorization
154+
// This would break for Java 22-23 if the SecurityManager would be enabled,
155+
// but we don't run tests with the SecurityManager enabled.
153156
assertNull(childSubject);
154157
}
155158
}
@@ -179,6 +182,8 @@ public void run() {
179182
assertEquals(parentSubject, childSubject);
180183
} else {
181184
// This is the behaviour that breaks Hadoop authorization
185+
// This would break for Java 22-23 if the SecurityManager would be enabled,
186+
// but we don't run tests with the SecurityManager enabled.
182187
assertNull(childSubject);
183188
}
184189
}

0 commit comments

Comments
 (0)