|
| 1 | +/* |
| 2 | + * Copyright 2025 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.xds; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import io.envoyproxy.envoy.extensions.filters.http.ext_authz.v3.ExtAuthz; |
| 21 | +import io.grpc.internal.GrpcUtil; |
| 22 | +import io.grpc.xds.client.Bootstrapper.BootstrapInfo; |
| 23 | +import io.grpc.xds.client.Bootstrapper.ServerInfo; |
| 24 | +import io.grpc.xds.internal.MatcherParser; |
| 25 | +import io.grpc.xds.internal.extauthz.ExtAuthzConfig; |
| 26 | +import io.grpc.xds.internal.extauthz.ExtAuthzParseException; |
| 27 | +import io.grpc.xds.internal.grpcservice.GrpcServiceConfig; |
| 28 | +import io.grpc.xds.internal.grpcservice.GrpcServiceParseException; |
| 29 | +import io.grpc.xds.internal.headermutations.HeaderMutationRulesParseException; |
| 30 | +import io.grpc.xds.internal.headermutations.HeaderMutationRulesParser; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Parser for {@link io.envoyproxy.envoy.extensions.filters.http.ext_authz.v3.ExtAuthz}. |
| 35 | + */ |
| 36 | +final class ExtAuthzConfigParser { |
| 37 | + |
| 38 | + private ExtAuthzConfigParser() {} |
| 39 | + |
| 40 | + /** |
| 41 | + * Parses the {@link io.envoyproxy.envoy.extensions.filters.http.ext_authz.v3.ExtAuthz} proto to |
| 42 | + * create an {@link ExtAuthzConfig} instance. |
| 43 | + * |
| 44 | + * @param extAuthzProto The ext_authz proto to parse. |
| 45 | + * @return An {@link ExtAuthzConfig} instance. |
| 46 | + * @throws ExtAuthzParseException if the proto is invalid or contains unsupported features. |
| 47 | + */ |
| 48 | + public static ExtAuthzConfig parse( |
| 49 | + ExtAuthz extAuthzProto, BootstrapInfo bootstrapInfo, ServerInfo serverInfo) |
| 50 | + throws ExtAuthzParseException { |
| 51 | + if (!extAuthzProto.hasGrpcService()) { |
| 52 | + throw new ExtAuthzParseException( |
| 53 | + "unsupported ExtAuthz service type: only grpc_service is supported"); |
| 54 | + } |
| 55 | + GrpcServiceConfig grpcServiceConfig; |
| 56 | + try { |
| 57 | + grpcServiceConfig = |
| 58 | + GrpcServiceConfigParser.parse(extAuthzProto.getGrpcService(), bootstrapInfo, serverInfo); |
| 59 | + } catch (GrpcServiceParseException e) { |
| 60 | + throw new ExtAuthzParseException("Failed to parse GrpcService config: " + e.getMessage(), e); |
| 61 | + } |
| 62 | + ExtAuthzConfig.Builder builder = ExtAuthzConfig.builder().grpcService(grpcServiceConfig) |
| 63 | + .failureModeAllow(extAuthzProto.getFailureModeAllow()) |
| 64 | + .failureModeAllowHeaderAdd(extAuthzProto.getFailureModeAllowHeaderAdd()) |
| 65 | + .includePeerCertificate(extAuthzProto.getIncludePeerCertificate()) |
| 66 | + .denyAtDisable(extAuthzProto.getDenyAtDisable().getDefaultValue().getValue()); |
| 67 | + |
| 68 | + if (extAuthzProto.hasFilterEnabled()) { |
| 69 | + try { |
| 70 | + builder.filterEnabled( |
| 71 | + MatcherParser.parseFractionMatcher(extAuthzProto.getFilterEnabled().getDefaultValue())); |
| 72 | + } catch (IllegalArgumentException e) { |
| 73 | + throw new ExtAuthzParseException(e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (extAuthzProto.hasStatusOnError()) { |
| 78 | + builder.statusOnError( |
| 79 | + GrpcUtil.httpStatusToGrpcStatus(extAuthzProto.getStatusOnError().getCodeValue())); |
| 80 | + } |
| 81 | + |
| 82 | + if (extAuthzProto.hasAllowedHeaders()) { |
| 83 | + builder.allowedHeaders(extAuthzProto.getAllowedHeaders().getPatternsList().stream() |
| 84 | + .map(MatcherParser::parseStringMatcher).collect(ImmutableList.toImmutableList())); |
| 85 | + } |
| 86 | + |
| 87 | + if (extAuthzProto.hasDisallowedHeaders()) { |
| 88 | + builder.disallowedHeaders(extAuthzProto.getDisallowedHeaders().getPatternsList().stream() |
| 89 | + .map(MatcherParser::parseStringMatcher).collect(ImmutableList.toImmutableList())); |
| 90 | + } |
| 91 | + |
| 92 | + if (extAuthzProto.hasDecoderHeaderMutationRules()) { |
| 93 | + try { |
| 94 | + builder.decoderHeaderMutationRules( |
| 95 | + HeaderMutationRulesParser.parse(extAuthzProto.getDecoderHeaderMutationRules())); |
| 96 | + } catch (HeaderMutationRulesParseException e) { |
| 97 | + throw new ExtAuthzParseException(e.getMessage(), e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return builder.build(); |
| 102 | + } |
| 103 | +} |
0 commit comments