Showing
1 changed file
with
73 additions
and
0 deletions
... | ... | @@ -15,11 +15,21 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.system; |
17 | 17 | |
18 | +import net.bytebuddy.asm.AsmVisitorWrapper; | |
19 | +import net.bytebuddy.asm.MemberSubstitution; | |
20 | +import net.bytebuddy.matcher.ElementMatchers; | |
21 | +import org.apache.cassandra.cql3.functions.ThreadAwareSecurityManager; | |
22 | +import org.hibernate.HibernateException; | |
23 | +import org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState; | |
24 | +import org.hibernate.bytecode.internal.bytebuddy.HibernateMethodLookupDispatcher; | |
18 | 25 | import org.junit.ClassRule; |
19 | 26 | import org.junit.extensions.cpsuite.ClasspathSuite; |
20 | 27 | import org.junit.runner.RunWith; |
21 | 28 | import org.thingsboard.server.dao.CustomSqlUnit; |
22 | 29 | |
30 | +import java.lang.reflect.Method; | |
31 | +import java.security.AccessController; | |
32 | +import java.security.PrivilegedAction; | |
23 | 33 | import java.util.Arrays; |
24 | 34 | |
25 | 35 | /** |
... | ... | @@ -30,11 +40,74 @@ import java.util.Arrays; |
30 | 40 | public class SystemSqlTestSuite { |
31 | 41 | |
32 | 42 | static { |
43 | + //ThreadAwareSecurityManager.install(); | |
33 | 44 | SecurityManager appsm = System.getSecurityManager(); |
34 | 45 | System.out.println("SECURITY MANAGER = " + appsm); |
35 | 46 | if (appsm != null) { |
36 | 47 | System.out.println("SECURITY MANAGER CLASS = " + appsm.getClass()); |
37 | 48 | } |
49 | + | |
50 | + AsmVisitorWrapper.ForDeclaredMethods getDeclaredMethodMemberSubstitution; | |
51 | + AsmVisitorWrapper.ForDeclaredMethods getMethodMemberSubstitution; | |
52 | + | |
53 | + //if ( System.getSecurityManager() != null ) { | |
54 | + getDeclaredMethodMemberSubstitution = getDeclaredMethodMemberSubstitution(); | |
55 | + getMethodMemberSubstitution = getMethodMemberSubstitution(); | |
56 | + //} | |
57 | + //else { | |
58 | + // getDeclaredMethodMemberSubstitution = null; | |
59 | + // getMethodMemberSubstitution = null; | |
60 | + //} | |
61 | + | |
62 | + System.out.println("getDeclaredMethodMemberSubstitution = " + getDeclaredMethodMemberSubstitution); | |
63 | + System.out.println("getMethodMemberSubstitution = " + getMethodMemberSubstitution); | |
64 | + } | |
65 | + | |
66 | + private static class GetDeclaredMethodAction implements PrivilegedAction<Method> { | |
67 | + private final Class<?> clazz; | |
68 | + private final String methodName; | |
69 | + private final Class<?>[] parameterTypes; | |
70 | + | |
71 | + private GetDeclaredMethodAction(Class<?> clazz, String methodName, Class<?>... parameterTypes) { | |
72 | + this.clazz = clazz; | |
73 | + this.methodName = methodName; | |
74 | + this.parameterTypes = parameterTypes; | |
75 | + } | |
76 | + | |
77 | + @Override | |
78 | + public Method run() { | |
79 | + try { | |
80 | + Method method = clazz.getDeclaredMethod( methodName, parameterTypes ); | |
81 | + | |
82 | + return method; | |
83 | + } | |
84 | + catch (NoSuchMethodException e) { | |
85 | + throw new HibernateException( "Unable to prepare getDeclaredMethod()/getMethod() substitution", e ); | |
86 | + } | |
87 | + } | |
88 | + } | |
89 | + | |
90 | + | |
91 | + private static AsmVisitorWrapper.ForDeclaredMethods getDeclaredMethodMemberSubstitution() { | |
92 | + // this should only be called if the security manager is enabled, thus the privileged calls | |
93 | + return MemberSubstitution.relaxed() | |
94 | + .method( ElementMatchers.is( AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( Class.class, | |
95 | + "getDeclaredMethod", String.class, Class[].class ) ) ) ) | |
96 | + .replaceWith( | |
97 | + AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( HibernateMethodLookupDispatcher.class, | |
98 | + "getDeclaredMethod", Class.class, String.class, Class[].class ) ) ) | |
99 | + .on( ElementMatchers.isTypeInitializer() ); | |
100 | + } | |
101 | + | |
102 | + private static AsmVisitorWrapper.ForDeclaredMethods getMethodMemberSubstitution() { | |
103 | + // this should only be called if the security manager is enabled, thus the privileged calls | |
104 | + return MemberSubstitution.relaxed() | |
105 | + .method( ElementMatchers.is( AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( Class.class, | |
106 | + "getMethod", String.class, Class[].class ) ) ) ) | |
107 | + .replaceWith( | |
108 | + AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( HibernateMethodLookupDispatcher.class, | |
109 | + "getMethod", Class.class, String.class, Class[].class ) ) ) | |
110 | + .on( ElementMatchers.isTypeInitializer() ); | |
38 | 111 | } |
39 | 112 | |
40 | 113 | @ClassRule | ... | ... |