|
| 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