1919package org .apache .hadoop .util .concurrent ;
2020
2121import static org .junit .jupiter .api .Assertions .assertEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertNull ;
2223
2324import java .util .concurrent .Callable ;
2425
2526import javax .security .auth .Subject ;
2627
2728import org .apache .hadoop .security .authentication .util .SubjectUtil ;
2829import org .apache .hadoop .util .Daemon ;
30+ import org .apache .hadoop .util .Shell ;
2931import org .junit .jupiter .api .Test ;
3032
3133public class TestSubjectPropagation {
3234
3335 private Subject childSubject = null ;
3436
3537 @ Test
36- public void testWork () {
38+ public void testSubjectInheritingThreadOverride () {
3739 Subject parentSubject = new Subject ();
3840 childSubject = null ;
3941
4042 SubjectUtil .callAs (parentSubject , new Callable <Void >() {
4143 public Void call () throws InterruptedException {
4244 SubjectInheritingThread t = new SubjectInheritingThread () {
45+ @ Override
4346 public void work () {
4447 childSubject = SubjectUtil .current ();
4548 }
@@ -54,7 +57,7 @@ public void work() {
5457 }
5558
5659 @ Test
57- public void testRunnable () {
60+ public void testSubjectInheritingThreadRunnable () {
5861 Subject parentSubject = new Subject ();
5962 childSubject = null ;
6063
@@ -78,13 +81,14 @@ public void run() {
7881 }
7982
8083 @ Test
81- public void testDeamonWork () {
84+ public void testDaemonOverride () {
8285 Subject parentSubject = new Subject ();
8386 childSubject = null ;
8487
8588 SubjectUtil .callAs (parentSubject , new Callable <Void >() {
8689 public Void call () throws InterruptedException {
8790 Daemon t = new Daemon () {
91+ @ Override
8892 public void work () {
8993 childSubject = SubjectUtil .current ();
9094 }
@@ -122,4 +126,83 @@ public void run() {
122126 assertEquals (parentSubject , childSubject );
123127 }
124128
129+ @ Test
130+ public void testThreadOverride () {
131+ Subject parentSubject = new Subject ();
132+ childSubject = null ;
133+
134+ SubjectUtil .callAs (parentSubject , new Callable <Void >() {
135+ public Void call () throws InterruptedException {
136+
137+ Thread t = new Thread () {
138+ @ Override
139+ public void run () {
140+ childSubject = SubjectUtil .current ();
141+ }
142+ };
143+ t .start ();
144+ t .join (1000 );
145+ return (Void ) null ;
146+ }
147+ });
148+
149+ boolean securityManagerEnabled = true ;
150+ try {
151+ SecurityManager sm = System .getSecurityManager ();
152+ System .setSecurityManager (sm );
153+ } catch (UnsupportedOperationException e ) {
154+ // JDK24+ always throws this
155+ securityManagerEnabled = false ;
156+ } catch (Throwable t ) {
157+ // don't care
158+ }
159+
160+ if (Shell .isJavaVersionAtLeast (22 ) && !securityManagerEnabled ) {
161+ // This is the behaviour that breaks Hadoop authorization
162+ assertNull (childSubject );
163+ } else {
164+ assertEquals (parentSubject , childSubject );
165+ }
166+ }
167+
168+ @ Test
169+ public void testThreadRunnable () {
170+ Subject parentSubject = new Subject ();
171+ childSubject = null ;
172+
173+ SubjectUtil .callAs (parentSubject , new Callable <Void >() {
174+ public Void call () throws InterruptedException {
175+ Runnable r = new Runnable () {
176+ @ Override
177+ public void run () {
178+ childSubject = SubjectUtil .current ();
179+ }
180+ };
181+
182+ Thread t = new Thread (r );
183+ t .start ();
184+ t .join (1000 );
185+ return (Void ) null ;
186+ }
187+ });
188+
189+ boolean securityManagerEnabled = true ;
190+ try {
191+ SecurityManager sm = System .getSecurityManager ();
192+ System .setSecurityManager (sm );
193+ } catch (UnsupportedOperationException e ) {
194+ // JDK24+ always throws this
195+ securityManagerEnabled = false ;
196+ } catch (Throwable t ) {
197+ // don't care
198+ }
199+
200+ if (Shell .isJavaVersionAtLeast (22 ) && !securityManagerEnabled ) {
201+ // This is the behaviour that breaks Hadoop authorization
202+ assertNull (childSubject );
203+ } else {
204+ assertEquals (parentSubject , childSubject );
205+ }
206+ }
207+
125208}
0 commit comments