Java では、java.security.AccessController.doPrivileged() を呼び出すことで特権を上げることができます。
kotlin コルーチンで特権を上げるにはどうすればよいですか?
例: プログラムを呼び出すとき
import java.security.AccessControlContext
import java.security.AccessController
import java.security.PrivilegedAction
import java.security.ProtectionDomain
import kotlinx.coroutines.runBlocking
object Privileged {
private fun checkDirect(expectAllowed: Boolean) {
try {
System.getProperty("allowed")
if (expectAllowed) {
println("expected: allowed")
}
else {
println("UNEXPECTED: allowed")
}
} catch (e: SecurityException) {
if (expectAllowed) {
println("UNEXPECTED: forbidden")
}
else {
println("expected: forbidden")
}
}
}
private suspend fun checkSuspend(expectAllowed: Boolean) {
checkDirect(expectAllowed)
}
@JvmStatic
fun main(vararg argv: String) {
// drop privileges
AccessController.doPrivileged(
PrivilegedAction {
// privileges are all dropped here
// 1. direct functions:
// this check will fail
checkDirect(false)
// raise privilege
AccessController.doPrivileged(
PrivilegedAction {
// privileges are all raised here
// so this check will succeed
checkDirect(true)
}
)
// 2. suspend functions:
runBlocking {
// this call will fail
checkSuspend(false)
// FIXME: How to call checkSuspend(true) with raised privileges?
}
},
AccessControlContext(arrayOf(ProtectionDomain(null, null)))
)
}
}
java -Djava.security.manager -Djava.security.policy=java.policy Privileged
java.policyの場所
grant {
permission java.security.AllPermission;
};
私は得る
expected: forbidden
expected: allowed
expected: forbidden
昇格した特権で checkSuspend を呼び出すための AccessController.doPrivileged() に相当するものは何ですか (プログラム コードの FIXME を参照)。