Skip to content

Commit 169cb44

Browse files
authored
Add getSubject -> current method replacement patcher (elastic#119779)
1 parent 16d8aaf commit 169cb44

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

plugins/repository-hdfs/hadoop-client-api/src/patcher/java/org/elasticsearch/hdfs/patch/HdfsClassPatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public class HdfsClassPatcher {
2727
"org/apache/hadoop/util/ShutdownHookManager.class",
2828
ShutdownHookManagerPatcher::new,
2929
"org/apache/hadoop/util/Shell.class",
30-
ShellPatcher::new
30+
ShellPatcher::new,
31+
"org/apache/hadoop/security/UserGroupInformation.class",
32+
SubjectGetSubjectPatcher::new,
33+
"org/apache/hadoop/security/authentication/client/KerberosAuthenticator.class",
34+
SubjectGetSubjectPatcher::new
3135
);
3236

3337
public static void main(String[] args) throws Exception {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hdfs.patch;
11+
12+
import org.objectweb.asm.ClassVisitor;
13+
import org.objectweb.asm.ClassWriter;
14+
import org.objectweb.asm.MethodVisitor;
15+
import org.objectweb.asm.Type;
16+
17+
import static org.objectweb.asm.Opcodes.ASM9;
18+
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
19+
import static org.objectweb.asm.Opcodes.POP;
20+
21+
class SubjectGetSubjectPatcher extends ClassVisitor {
22+
SubjectGetSubjectPatcher(ClassWriter classWriter) {
23+
super(ASM9, classWriter);
24+
}
25+
26+
@Override
27+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
28+
return new ReplaceCallMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions), name, access, descriptor);
29+
}
30+
31+
/**
32+
* Replaces calls to Subject.getSubject(context); with calls to Subject.current();
33+
*/
34+
private static class ReplaceCallMethodVisitor extends MethodVisitor {
35+
private static final String SUBJECT_CLASS_INTERNAL_NAME = "javax/security/auth/Subject";
36+
private static final String METHOD_NAME = "getSubject";
37+
38+
ReplaceCallMethodVisitor(MethodVisitor methodVisitor, String name, int access, String descriptor) {
39+
super(ASM9, methodVisitor);
40+
}
41+
42+
@Override
43+
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
44+
if (opcode == INVOKESTATIC && SUBJECT_CLASS_INTERNAL_NAME.equals(owner) && METHOD_NAME.equals(name)) {
45+
// Get rid of the extra arg on the stack
46+
mv.visitInsn(POP);
47+
// Call Subject.current()
48+
mv.visitMethodInsn(
49+
INVOKESTATIC,
50+
SUBJECT_CLASS_INTERNAL_NAME,
51+
"current",
52+
Type.getMethodDescriptor(Type.getObjectType(SUBJECT_CLASS_INTERNAL_NAME)),
53+
false
54+
);
55+
} else {
56+
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)