Skip to content

Commit 1eb6932

Browse files
Static authorization server interceptor implementation
1 parent 6559ef8 commit 1eb6932

File tree

9 files changed

+902
-219
lines changed

9 files changed

+902
-219
lines changed

authz/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ jar {
3333
classifier = 'original'
3434
}
3535

36-
// TODO(ashithasantosh): Remove javadoc exclusion on adding authorization
37-
// interceptor implementations.
38-
javadoc {
39-
exclude "io/grpc/authz/*"
40-
}
41-
4236
shadowJar {
4337
classifier = null
4438
dependencies {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.authz;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import io.envoyproxy.envoy.config.rbac.v3.RBAC;
22+
import io.grpc.Metadata;
23+
import io.grpc.ServerCall;
24+
import io.grpc.ServerCallHandler;
25+
import io.grpc.ServerInterceptor;
26+
import io.grpc.Status;
27+
import io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine;
28+
import io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine.AuthDecision;
29+
import io.grpc.xds.internal.rbac.engine.RbacParser;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
/**
35+
* Authorization server interceptor for static policy.
36+
*/
37+
public final class AuthorizationServerInterceptor implements ServerInterceptor {
38+
private final List<GrpcAuthorizationEngine> rbacEngines = new ArrayList<>();
39+
40+
List<GrpcAuthorizationEngine> getEngines() {
41+
return rbacEngines;
42+
}
43+
44+
private AuthorizationServerInterceptor(String authorizationPolicy)
45+
throws IllegalArgumentException, IOException {
46+
List<RBAC> rbacs = AuthorizationPolicyTranslator.translate(authorizationPolicy);
47+
if (rbacs == null || rbacs.isEmpty() || rbacs.size() > 2) {
48+
throw new IllegalArgumentException("Failed to create authorization engines");
49+
}
50+
for (RBAC rbac: rbacs) {
51+
rbacEngines.add(new GrpcAuthorizationEngine(RbacParser.parseRbac(rbac)));
52+
}
53+
}
54+
55+
@Override
56+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
57+
ServerCall<ReqT, RespT> call, Metadata headers,
58+
ServerCallHandler<ReqT, RespT> next) {
59+
for (GrpcAuthorizationEngine rbacEngine: rbacEngines) {
60+
AuthDecision authDecision = rbacEngine.evaluate(headers, call);
61+
if (GrpcAuthorizationEngine.Action.DENY.equals(authDecision.decision())) {
62+
Status status =
63+
Status.PERMISSION_DENIED.withDescription("Unauthorized RPC request rejected");
64+
call.close(status, new Metadata());
65+
return new ServerCall.Listener<ReqT>(){};
66+
}
67+
}
68+
return next.startCall(call, headers);
69+
}
70+
71+
// Static method that creates an AuthorizationServerInterceptor.
72+
public static AuthorizationServerInterceptor create(String authorizationPolicy)
73+
throws IllegalArgumentException, IOException {
74+
checkNotNull(authorizationPolicy, "authorizationPolicy");
75+
return new AuthorizationServerInterceptor(authorizationPolicy);
76+
}
77+
}

0 commit comments

Comments
 (0)