19
19
package org .apache .hadoop .util .concurrent ;
20
20
21
21
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
+ import static org .junit .jupiter .api .Assertions .assertNull ;
22
23
23
24
import java .util .concurrent .Callable ;
24
25
25
26
import javax .security .auth .Subject ;
26
27
27
28
import org .apache .hadoop .security .authentication .util .SubjectUtil ;
28
29
import org .apache .hadoop .util .Daemon ;
30
+ import org .apache .hadoop .util .Shell ;
29
31
import org .junit .jupiter .api .Test ;
30
32
31
33
public class TestSubjectPropagation {
32
34
33
35
private Subject childSubject = null ;
34
36
35
37
@ Test
36
- public void testWork () {
38
+ public void testSubjectInheritingThreadOverride () {
37
39
Subject parentSubject = new Subject ();
38
40
childSubject = null ;
39
41
40
42
SubjectUtil .callAs (parentSubject , new Callable <Void >() {
41
43
public Void call () throws InterruptedException {
42
44
SubjectInheritingThread t = new SubjectInheritingThread () {
45
+ @ Override
43
46
public void work () {
44
47
childSubject = SubjectUtil .current ();
45
48
}
@@ -54,7 +57,7 @@ public void work() {
54
57
}
55
58
56
59
@ Test
57
- public void testRunnable () {
60
+ public void testSubjectInheritingThreadRunnable () {
58
61
Subject parentSubject = new Subject ();
59
62
childSubject = null ;
60
63
@@ -78,13 +81,14 @@ public void run() {
78
81
}
79
82
80
83
@ Test
81
- public void testDeamonWork () {
84
+ public void testDaemonOverride () {
82
85
Subject parentSubject = new Subject ();
83
86
childSubject = null ;
84
87
85
88
SubjectUtil .callAs (parentSubject , new Callable <Void >() {
86
89
public Void call () throws InterruptedException {
87
90
Daemon t = new Daemon () {
91
+ @ Override
88
92
public void work () {
89
93
childSubject = SubjectUtil .current ();
90
94
}
@@ -122,4 +126,83 @@ public void run() {
122
126
assertEquals (parentSubject , childSubject );
123
127
}
124
128
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
+
125
208
}
0 commit comments