-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAK-11199: Java 23: getSubject is supported only if a security manage…
…r is allowed (#1891) Added new class to replace the deprecated API
- Loading branch information
Showing
31 changed files
with
272 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jdkcompat/Java23Subject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.jackrabbit.oak.commons.jdkcompat; | ||
|
||
import javax.security.auth.Subject; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.security.AccessControlContext; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* This class contains methods replacing the deprecated | ||
* {@link Subject#getSubject(AccessControlContext)} | ||
* and associated methods, which changed their behavior | ||
* with Java 23 (@see https://inside.java/2024/07/08/quality-heads-up). | ||
*/ | ||
public class Java23Subject { | ||
|
||
static Method current, callAs; | ||
|
||
static { | ||
try { | ||
current = Subject.class.getMethod("current"); | ||
callAs = Subject.class.getMethod("callAs", Subject.class, Callable.class); | ||
} catch (NoSuchMethodException ignored) {} | ||
} | ||
|
||
public static Subject getSubject() { | ||
Subject result; | ||
if (current != null) { | ||
try { | ||
result = (Subject) current.invoke(null); | ||
} catch (InvocationTargetException | IllegalAccessException e) { | ||
throw new SecurityException(e); | ||
} | ||
} else { | ||
result = Subject.getSubject(AccessController.getContext()); | ||
} | ||
return result; | ||
} | ||
|
||
public static <T> T doAs(Subject subject, PrivilegedAction<T> action) { | ||
T result; | ||
if (callAs != null) { | ||
try { | ||
result = (T) callAs.invoke(null, subject, (Callable<T>) () -> action.run()); | ||
} catch (InvocationTargetException | IllegalAccessException e) { | ||
throw new SecurityException(e); | ||
} | ||
} else { | ||
result = Subject.doAs(subject, action); | ||
} | ||
return result; | ||
} | ||
|
||
public static <T> T doAsPrivileged(Subject subject, PrivilegedAction<T> action, AccessControlContext acc) { | ||
T result; | ||
if (callAs != null) { | ||
try { | ||
result = (T) callAs.invoke(null, subject, (Callable<T>) () -> action.run()); | ||
} catch (InvocationTargetException | IllegalAccessException e) { | ||
throw new SecurityException(e); | ||
} | ||
} else { | ||
result = Subject.doAsPrivileged(subject, action, acc); | ||
} | ||
return result; | ||
} | ||
|
||
public static <T> T doAs(Subject subject, PrivilegedExceptionAction<T> action) throws PrivilegedActionException { | ||
T result; | ||
if (callAs != null) { | ||
try { | ||
result = (T) callAs.invoke(null, subject, (Callable<T>) () -> action.run()); | ||
} catch (InvocationTargetException | IllegalAccessException e) { | ||
throw new SecurityException(e); | ||
} | ||
} else { | ||
result = Subject.doAs(subject, action); | ||
} | ||
return result; | ||
} | ||
|
||
public static <T> T doAsPrivileged(Subject subject, PrivilegedExceptionAction<T> action, AccessControlContext acc) throws PrivilegedActionException { | ||
T result; | ||
if (callAs != null) { | ||
try { | ||
result = (T) callAs.invoke(null, subject, (Callable<T>) () -> action.run()); | ||
} catch (InvocationTargetException | IllegalAccessException e) { | ||
throw new SecurityException(e); | ||
} | ||
} else { | ||
result = Subject.doAsPrivileged(subject, action, acc); | ||
} | ||
return result; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jdkcompat/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Package containing utilities to handle incompatible JDK changes. | ||
*/ | ||
@Internal | ||
@Version("1.0.0") | ||
package org.apache.jackrabbit.oak.commons.jdkcompat; | ||
|
||
import org.apache.jackrabbit.oak.commons.annotations.Internal; | ||
import org.osgi.annotation.versioning.Version; |
Oops, something went wrong.