|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.security.authentication.util; |
| 20 | + |
| 21 | +import java.lang.invoke.MethodHandle; |
| 22 | +import java.lang.invoke.MethodHandles; |
| 23 | +import java.lang.invoke.MethodType; |
| 24 | +import java.security.PrivilegedAction; |
| 25 | +import java.security.PrivilegedActionException; |
| 26 | +import java.security.PrivilegedExceptionAction; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.concurrent.Callable; |
| 29 | +import java.util.concurrent.CompletionException; |
| 30 | + |
| 31 | +import javax.security.auth.Subject; |
| 32 | + |
| 33 | +import org.apache.hadoop.classification.InterfaceAudience.Private; |
| 34 | + |
| 35 | +/** |
| 36 | + * An utility class that adapts the Security Manager and APIs related to it for |
| 37 | + * JDK 8 and above. |
| 38 | + * <p> |
| 39 | + * In JDK 17, the Security Manager and APIs related to it have been deprecated |
| 40 | + * and are subject to removal in a future release. There is no replacement for |
| 41 | + * the Security Manager. See <a href="https://openjdk.org/jeps/411">JEP 411</a> |
| 42 | + * for discussion and alternatives. |
| 43 | + * <p> |
| 44 | + * In JDK 24, the Security Manager has been permanently disabled. See |
| 45 | + * <a href="https://openjdk.org/jeps/486">JEP 486</a> for more information. |
| 46 | + * <p> |
| 47 | + * This is derived from Apache Calcite Avatica, which is derived from the Jetty |
| 48 | + * implementation. |
| 49 | + */ |
| 50 | +@Private |
| 51 | +public final class SubjectUtil { |
| 52 | + private static final MethodHandle CALL_AS = lookupCallAs(); |
| 53 | + static final boolean HAS_CALL_AS = CALL_AS != null; |
| 54 | + private static final MethodHandle DO_AS = HAS_CALL_AS ? null : lookupDoAs(); |
| 55 | + private static final MethodHandle DO_AS_THROW_EXCEPTION = |
| 56 | + HAS_CALL_AS ? null : lookupDoAsThrowException(); |
| 57 | + private static final MethodHandle CURRENT = lookupCurrent(); |
| 58 | + |
| 59 | + private static MethodHandle lookupCallAs() { |
| 60 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 61 | + try { |
| 62 | + try { |
| 63 | + // Subject.callAs() is available since Java 18. |
| 64 | + return lookup.findStatic(Subject.class, "callAs", |
| 65 | + MethodType.methodType(Object.class, Subject.class, Callable.class)); |
| 66 | + } catch (NoSuchMethodException x) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + } catch (IllegalAccessException e) { |
| 70 | + throw new ExceptionInInitializerError(e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static MethodHandle lookupDoAs() { |
| 75 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 76 | + try { |
| 77 | + MethodType signature = MethodType.methodType( |
| 78 | + Object.class, Subject.class, PrivilegedAction.class); |
| 79 | + return lookup.findStatic(Subject.class, "doAs", signature); |
| 80 | + } catch (IllegalAccessException | NoSuchMethodException e) { |
| 81 | + throw new ExceptionInInitializerError(e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static MethodHandle lookupDoAsThrowException() { |
| 86 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 87 | + try { |
| 88 | + MethodType signature = MethodType.methodType( |
| 89 | + Object.class, Subject.class, PrivilegedExceptionAction.class); |
| 90 | + return lookup.findStatic(Subject.class, "doAs", signature); |
| 91 | + } catch (IllegalAccessException | NoSuchMethodException e) { |
| 92 | + throw new ExceptionInInitializerError(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static MethodHandle lookupCurrent() { |
| 97 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 98 | + try { |
| 99 | + // Subject.getSubject(AccessControlContext) is deprecated for removal and |
| 100 | + // replaced by Subject.current(). |
| 101 | + // Lookup first the new API, since for Java versions where both exists, the |
| 102 | + // new API delegates to the old API (e.g. Java 18, 19 and 20). |
| 103 | + // Otherwise (e.g. Java 17), lookup the old API. |
| 104 | + return lookup.findStatic( |
| 105 | + Subject.class, "current", MethodType.methodType(Subject.class)); |
| 106 | + } catch (NoSuchMethodException e) { |
| 107 | + MethodHandle getContext = lookupGetContext(); |
| 108 | + MethodHandle getSubject = lookupGetSubject(); |
| 109 | + return MethodHandles.filterReturnValue(getContext, getSubject); |
| 110 | + } catch (IllegalAccessException e) { |
| 111 | + throw new ExceptionInInitializerError(e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static MethodHandle lookupGetSubject() { |
| 116 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 117 | + try { |
| 118 | + Class<?> contextKlass = ClassLoader.getSystemClassLoader() |
| 119 | + .loadClass("java.security.AccessControlContext"); |
| 120 | + return lookup.findStatic(Subject.class, |
| 121 | + "getSubject", MethodType.methodType(Subject.class, contextKlass)); |
| 122 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) { |
| 123 | + throw new ExceptionInInitializerError(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private static MethodHandle lookupGetContext() { |
| 128 | + try { |
| 129 | + // Use reflection to work with Java versions that have and don't have |
| 130 | + // AccessController. |
| 131 | + Class<?> controllerKlass = ClassLoader.getSystemClassLoader() |
| 132 | + .loadClass("java.security.AccessController"); |
| 133 | + Class<?> contextKlass = ClassLoader.getSystemClassLoader() |
| 134 | + .loadClass("java.security.AccessControlContext"); |
| 135 | + |
| 136 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 137 | + return lookup.findStatic( |
| 138 | + controllerKlass, "getContext", MethodType.methodType(contextKlass)); |
| 139 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) { |
| 140 | + throw new ExceptionInInitializerError(e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Map to Subject.callAs() if available, otherwise maps to Subject.doAs(). |
| 146 | + * |
| 147 | + * @param subject the subject this action runs as |
| 148 | + * @param action the action to run |
| 149 | + * @return the result of the action |
| 150 | + * @param <T> the type of the result |
| 151 | + * @throws NullPointerException if action is null |
| 152 | + * @throws CompletionException if {@code action.call()} throws an exception. |
| 153 | + * The cause of the {@code CompletionException} is set to the exception |
| 154 | + * thrown by {@code action.call()}. |
| 155 | + */ |
| 156 | + @SuppressWarnings("unchecked") |
| 157 | + public static <T> T callAs(Subject subject, Callable<T> action) throws CompletionException { |
| 158 | + Objects.requireNonNull(action); |
| 159 | + if (HAS_CALL_AS) { |
| 160 | + try { |
| 161 | + return (T) CALL_AS.invoke(subject, action); |
| 162 | + } catch (Throwable t) { |
| 163 | + throw sneakyThrow(t); |
| 164 | + } |
| 165 | + } else { |
| 166 | + try { |
| 167 | + return doAs(subject, callableToPrivilegedAction(action)); |
| 168 | + } catch (Exception e) { |
| 169 | + throw new CompletionException(e); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Map action to a Callable on Java 18 onwards, and delegates to callAs(). |
| 176 | + * Call Subject.doAs directly on older JVM. |
| 177 | + * <p> |
| 178 | + * Note: Exception propagation behavior is different since Java 12, it always |
| 179 | + * throw the original exception thrown by action; for lower Java versions, |
| 180 | + * throw a PrivilegedActionException that wraps the original exception when |
| 181 | + * action throw a checked exception. |
| 182 | + * |
| 183 | + * @param subject the subject this action runs as |
| 184 | + * @param action the action to run |
| 185 | + * @return the result of the action |
| 186 | + * @param <T> the type of the result |
| 187 | + * @throws NullPointerException if action is null |
| 188 | + */ |
| 189 | + @SuppressWarnings("unchecked") |
| 190 | + public static <T> T doAs(Subject subject, PrivilegedAction<T> action) { |
| 191 | + Objects.requireNonNull(action); |
| 192 | + if (HAS_CALL_AS) { |
| 193 | + try { |
| 194 | + return callAs(subject, privilegedActionToCallable(action)); |
| 195 | + } catch (CompletionException ce) { |
| 196 | + Throwable cause = ce.getCause(); |
| 197 | + if (cause != null) { |
| 198 | + throw sneakyThrow(cause); |
| 199 | + } else { |
| 200 | + // This should never happen, CompletionException thrown by Subject.callAs |
| 201 | + // should always wrap an exception |
| 202 | + throw ce; |
| 203 | + } |
| 204 | + } |
| 205 | + } else { |
| 206 | + try { |
| 207 | + return (T) DO_AS.invoke(subject, action); |
| 208 | + } catch (Throwable t) { |
| 209 | + throw sneakyThrow(t); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Maps action to a Callable on Java 18 onwards, and delegates to callAs(). |
| 216 | + * Call Subject.doAs directly on older JVM. |
| 217 | + * |
| 218 | + * @param subject the subject this action runs as |
| 219 | + * @param action the action to run |
| 220 | + * @return the result of the action |
| 221 | + * @param <T> the type of the result |
| 222 | + * @throws NullPointerException if action is null |
| 223 | + * @throws PrivilegedActionException if {@code action.run()} throws an checked exception. |
| 224 | + * The cause of the {@code PrivilegedActionException} is set to the exception thrown |
| 225 | + * by {@code action.run()}. |
| 226 | + */ |
| 227 | + @SuppressWarnings("unchecked") |
| 228 | + public static <T> T doAs( |
| 229 | + Subject subject, PrivilegedExceptionAction<T> action) throws PrivilegedActionException { |
| 230 | + Objects.requireNonNull(action); |
| 231 | + if (HAS_CALL_AS) { |
| 232 | + try { |
| 233 | + return callAs(subject, privilegedExceptionActionToCallable(action)); |
| 234 | + } catch (CompletionException ce) { |
| 235 | + Throwable cause = ce.getCause(); |
| 236 | + if (cause instanceof RuntimeException) { |
| 237 | + throw (RuntimeException) cause; |
| 238 | + } else if (cause instanceof Exception) { |
| 239 | + throw new PrivilegedActionException((Exception) cause); |
| 240 | + } else { |
| 241 | + // This should never happen, CompletionException should only wraps an exception |
| 242 | + throw sneakyThrow(cause); |
| 243 | + } |
| 244 | + } |
| 245 | + } else { |
| 246 | + try { |
| 247 | + return (T) DO_AS_THROW_EXCEPTION.invoke(subject, action); |
| 248 | + } catch (Throwable t) { |
| 249 | + throw sneakyThrow(t); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Maps to Subject.current() if available, otherwise maps to Subject.getSubject(). |
| 256 | + * |
| 257 | + * @return the current subject |
| 258 | + */ |
| 259 | + public static Subject current() { |
| 260 | + try { |
| 261 | + return (Subject) CURRENT.invoke(); |
| 262 | + } catch (Throwable t) { |
| 263 | + throw sneakyThrow(t); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + private static <T> PrivilegedAction<T> callableToPrivilegedAction( |
| 268 | + Callable<T> callable) { |
| 269 | + return () -> { |
| 270 | + try { |
| 271 | + return callable.call(); |
| 272 | + } catch (Exception e) { |
| 273 | + throw sneakyThrow(e); |
| 274 | + } |
| 275 | + }; |
| 276 | + } |
| 277 | + |
| 278 | + private static <T> Callable<T> privilegedExceptionActionToCallable( |
| 279 | + PrivilegedExceptionAction<T> action) { |
| 280 | + return action::run; |
| 281 | + } |
| 282 | + |
| 283 | + private static <T> Callable<T> privilegedActionToCallable( |
| 284 | + PrivilegedAction<T> action) { |
| 285 | + return action::run; |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * The sneaky throw concept allows the caller to throw any checked exception without |
| 290 | + * defining it explicitly in the method signature. |
| 291 | + * <p> |
| 292 | + * See <a href="https://www.baeldung.com/java-sneaky-throws">"Sneaky Throws" in Java</a> |
| 293 | + * for more details. |
| 294 | + * |
| 295 | + * @param e the exception that will be thrown. |
| 296 | + * @return unreachable, the method always throws an exception before returning |
| 297 | + * @param <E> the thrown exception type, trick the compiler into inferring it as |
| 298 | + * a {@code RuntimeException} type. |
| 299 | + * @throws E the original exception passes by caller |
| 300 | + */ |
| 301 | + @SuppressWarnings("unchecked") |
| 302 | + static <E extends Throwable> RuntimeException sneakyThrow(Throwable e) throws E { |
| 303 | + throw (E) e; |
| 304 | + } |
| 305 | + |
| 306 | + private SubjectUtil() { |
| 307 | + } |
| 308 | +} |
0 commit comments