Skip to content

Commit 49a8b72

Browse files
author
Tom McCormick
committed
Pass auth header to rpc call and fix unit tests to be run
1 parent 90cc57d commit 49a8b72

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ public static class Call implements Schedulable,
10051005
final byte[] clientId;
10061006
private final Span span; // the trace span on the server side
10071007
private final CallerContext callerContext; // the call context
1008+
private final byte[] authHeader; // the auth header
10081009
private boolean deferredResponse = false;
10091010
private int priorityLevel;
10101011
// the priority level assigned by scheduler, 0 by default
@@ -1036,6 +1037,11 @@ public Call(int id, int retryCount, Void ignore1, Void ignore2,
10361037

10371038
Call(int id, int retryCount, RPC.RpcKind kind, byte[] clientId,
10381039
Span span, CallerContext callerContext) {
1040+
this(id, retryCount, kind, clientId, span, callerContext, null);
1041+
}
1042+
1043+
Call(int id, int retryCount, RPC.RpcKind kind, byte[] clientId,
1044+
Span span, CallerContext callerContext, byte[] authHeader) {
10391045
this.callId = id;
10401046
this.retryCount = retryCount;
10411047
this.timestampNanos = Time.monotonicNowNanos();
@@ -1044,6 +1050,7 @@ public Call(int id, int retryCount, Void ignore1, Void ignore2,
10441050
this.clientId = clientId;
10451051
this.span = span;
10461052
this.callerContext = callerContext;
1053+
this.authHeader = authHeader;
10471054
this.clientStateId = Long.MIN_VALUE;
10481055
this.isCallCoordinated = false;
10491056
}
@@ -1244,7 +1251,14 @@ private class RpcCall extends Call {
12441251
RpcCall(Connection connection, int id, int retryCount,
12451252
Writable param, RPC.RpcKind kind, byte[] clientId,
12461253
Span span, CallerContext context) {
1247-
super(id, retryCount, kind, clientId, span, context);
1254+
this(connection, id, retryCount, param, kind, clientId,
1255+
span, context, new byte[0]);
1256+
}
1257+
1258+
RpcCall(Connection connection, int id, int retryCount,
1259+
Writable param, RPC.RpcKind kind, byte[] clientId,
1260+
Span span, CallerContext context, byte[] authHeader) {
1261+
super(id, retryCount, kind, clientId, span, context, authHeader);
12481262
this.connection = connection;
12491263
this.rpcRequest = param;
12501264
}
@@ -2977,17 +2991,18 @@ private void processRpcRequest(RpcRequestHeaderProto header,
29772991
}
29782992

29792993
// Set AuthorizationContext for this thread if present
2994+
byte[] authHeader = null;
29802995
boolean authzSet = false;
29812996
try {
29822997
if (header.hasAuthorizationHeader()) {
2983-
AuthorizationContext.setCurrentAuthorizationHeader(header.getAuthorizationHeader().toByteArray());
2998+
authHeader = header.getAuthorizationHeader().toByteArray();
29842999
authzSet = true;
29853000
}
29863001

29873002
RpcCall call = new RpcCall(this, header.getCallId(),
29883003
header.getRetryCount(), rpcRequest,
29893004
ProtoUtil.convert(header.getRpcKind()),
2990-
header.getClientId().toByteArray(), span, callerContext);
3005+
header.getClientId().toByteArray(), span, callerContext, authHeader);
29913006

29923007
// Save the priority level assignment by the scheduler
29933008
call.setPriorityLevel(callQueue.getPriorityLevel(call));
@@ -3259,6 +3274,7 @@ public void run() {
32593274
}
32603275
// always update the current call context
32613276
CallerContext.setCurrent(call.callerContext);
3277+
AuthorizationContext.setCurrentAuthorizationHeader(call.authHeader);
32623278
UserGroupInformation remoteUser = call.getRemoteUser();
32633279
connDropped = !call.isOpen();
32643280
if (remoteUser != null) {

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestAuthorizationContext.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
*/
1818
package org.apache.hadoop.security;
1919

20-
import org.junit.Assert;
21-
import org.junit.Test;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
2222

2323
public class TestAuthorizationContext {
2424

2525
@Test
2626
public void testSetAndGetAuthorizationHeader() {
2727
byte[] header = "my-auth-header".getBytes();
2828
AuthorizationContext.setCurrentAuthorizationHeader(header);
29-
Assert.assertArrayEquals(header, AuthorizationContext.getCurrentAuthorizationHeader());
29+
Assertions.assertArrayEquals(header, AuthorizationContext.getCurrentAuthorizationHeader());
3030
AuthorizationContext.clear();
3131
}
3232

@@ -35,35 +35,35 @@ public void testClearAuthorizationHeader() {
3535
byte[] header = "clear-me".getBytes();
3636
AuthorizationContext.setCurrentAuthorizationHeader(header);
3737
AuthorizationContext.clear();
38-
Assert.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
38+
Assertions.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
3939
}
4040

4141
@Test
4242
public void testThreadLocalIsolation() throws Exception {
4343
byte[] mainHeader = "main-thread".getBytes();
4444
AuthorizationContext.setCurrentAuthorizationHeader(mainHeader);
4545
Thread t = new Thread(() -> {
46-
Assert.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
46+
Assertions.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
4747
byte[] threadHeader = "other-thread".getBytes();
4848
AuthorizationContext.setCurrentAuthorizationHeader(threadHeader);
49-
Assert.assertArrayEquals(threadHeader, AuthorizationContext.getCurrentAuthorizationHeader());
49+
Assertions.assertArrayEquals(threadHeader, AuthorizationContext.getCurrentAuthorizationHeader());
5050
AuthorizationContext.clear();
51-
Assert.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
51+
Assertions.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
5252
});
5353
t.start();
5454
t.join();
5555
// Main thread should still have its header
56-
Assert.assertArrayEquals(mainHeader, AuthorizationContext.getCurrentAuthorizationHeader());
56+
Assertions.assertArrayEquals(mainHeader, AuthorizationContext.getCurrentAuthorizationHeader());
5757
AuthorizationContext.clear();
5858
}
5959

6060
@Test
6161
public void testNullAndEmptyHeader() {
6262
AuthorizationContext.setCurrentAuthorizationHeader(null);
63-
Assert.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
63+
Assertions.assertNull(AuthorizationContext.getCurrentAuthorizationHeader());
6464
byte[] empty = new byte[0];
6565
AuthorizationContext.setCurrentAuthorizationHeader(empty);
66-
Assert.assertArrayEquals(empty, AuthorizationContext.getCurrentAuthorizationHeader());
66+
Assertions.assertArrayEquals(empty, AuthorizationContext.getCurrentAuthorizationHeader());
6767
AuthorizationContext.clear();
6868
}
6969
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestAuthorizationHeaderPropagation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
import org.apache.hadoop.hdfs.HdfsConfiguration;
2525
import org.apache.hadoop.hdfs.MiniDFSCluster;
2626
import org.apache.hadoop.security.AuthorizationContext;
27-
import org.junit.Test;
27+
import org.junit.jupiter.api.Test;
2828

2929
import java.net.InetAddress;
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
3232
import java.util.List;
3333

3434
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_AUDIT_LOGGERS_KEY;
35-
import static org.junit.Assert.assertArrayEquals;
36-
import static org.junit.Assert.assertNull;
35+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
36+
import static org.junit.jupiter.api.Assertions.assertNull;
3737

3838
public class TestAuthorizationHeaderPropagation {
3939

0 commit comments

Comments
 (0)