forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the AsyncSecurityPolicy class. (grpc#10622)
This is the async variant of SecurityPolicy, allowing callers to implement security checks based on slow calls that aren't meant to block the gRPC thread. BinderTransportSecurity.checkAuthorization **STILL** blocks while attempting to resolve the ListenableFuture<Status> it gets from the policy object. That will still be adressed in a follow-up. Relate issue: grpc#10566
- Loading branch information
1 parent
cd810c5
commit b6947de
Showing
4 changed files
with
265 additions
and
1 deletion.
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
71 changes: 71 additions & 0 deletions
71
binder/src/main/java/io/grpc/binder/AsyncSecurityPolicy.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,71 @@ | ||
/* | ||
* Copyright 2023 The gRPC Authors | ||
* | ||
* Licensed 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 io.grpc.binder; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.grpc.ExperimentalApi; | ||
import io.grpc.Status; | ||
|
||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.ExecutionException; | ||
import javax.annotation.CheckReturnValue; | ||
|
||
/** | ||
* Decides whether a given Android UID is authorized to access some resource. | ||
* | ||
* <p>This class provides the asynchronous version of {@link SecurityPolicy}, allowing | ||
* implementations of authorization logic that involves slow or asynchronous calls without | ||
* necessarily blocking the calling thread. | ||
* | ||
* @see SecurityPolicy | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/10566") | ||
@CheckReturnValue | ||
public abstract class AsyncSecurityPolicy extends SecurityPolicy { | ||
|
||
/** | ||
* @deprecated Prefer {@link #checkAuthorizationAsync(int)} for async or slow calls or subclass | ||
* {@link SecurityPolicy} directly for quick, synchronous implementations. | ||
*/ | ||
@Override | ||
@Deprecated | ||
public final Status checkAuthorization(int uid) { | ||
try { | ||
return checkAuthorizationAsync(uid).get(); | ||
} catch (ExecutionException e) { | ||
return Status.fromThrowable(e); | ||
} catch (CancellationException e) { | ||
return Status.CANCELLED.withCause(e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); // re-set the current thread's interruption state | ||
return Status.CANCELLED.withCause(e); | ||
} | ||
} | ||
|
||
/** | ||
* Decides whether the given Android UID is authorized. (Validity is implementation dependent). | ||
* | ||
* <p>As long as any given UID has active processes, this method should return the same value for | ||
* that UID. In order words, policy changes which occur while a transport instance is active, will | ||
* have no effect on that transport instance. | ||
* | ||
* @param uid The Android UID to authenticate. | ||
* @return A {@link ListenableFuture} for a gRPC {@link Status} object, with OK indicating | ||
* authorized. | ||
*/ | ||
abstract ListenableFuture<Status> checkAuthorizationAsync(int uid); | ||
} |
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