|
| 1 | +/* |
| 2 | + * Copyright 2024 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 static com.google.common.base.Preconditions.checkNotNull; |
| 20 | +import static io.grpc.xds.client.XdsResourceType.ResourceInvalidException; |
| 21 | + |
| 22 | +import com.google.common.annotations.VisibleForTesting; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.protobuf.Any; |
| 25 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 26 | +import com.google.protobuf.Message; |
| 27 | +import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaBucketSettings; |
| 28 | +import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaFilterConfig; |
| 29 | +import io.envoyproxy.envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaOverride; |
| 30 | +import io.grpc.InternalLogId; |
| 31 | +import io.grpc.Metadata; |
| 32 | +import io.grpc.ServerCall; |
| 33 | +import io.grpc.ServerCall.Listener; |
| 34 | +import io.grpc.ServerCallHandler; |
| 35 | +import io.grpc.ServerInterceptor; |
| 36 | +import io.grpc.internal.GrpcUtil; |
| 37 | +import io.grpc.xds.Filter.ServerInterceptorBuilder; |
| 38 | +import io.grpc.xds.client.XdsLogger; |
| 39 | +import io.grpc.xds.client.XdsLogger.XdsLogLevel; |
| 40 | +import io.grpc.xds.internal.datatype.GrpcService; |
| 41 | +import io.grpc.xds.internal.matchers.HttpMatchInput; |
| 42 | +import io.grpc.xds.internal.matchers.Matcher; |
| 43 | +import io.grpc.xds.internal.matchers.MatcherList; |
| 44 | +import io.grpc.xds.internal.matchers.OnMatch; |
| 45 | +import io.grpc.xds.internal.rlqs.RlqsBucketSettings; |
| 46 | +import io.grpc.xds.internal.rlqs.RlqsCache; |
| 47 | +import io.grpc.xds.internal.rlqs.RlqsFilterState; |
| 48 | +import io.grpc.xds.internal.rlqs.RlqsRateLimitResult; |
| 49 | +import java.util.concurrent.ScheduledExecutorService; |
| 50 | +import java.util.concurrent.atomic.AtomicReference; |
| 51 | +import javax.annotation.Nullable; |
| 52 | + |
| 53 | +/** RBAC Http filter implementation. */ |
| 54 | +// TODO(sergiitk): introduce a layer between the filter and interceptor. |
| 55 | +// lds has filter names and the names are unique - even for server instances. |
| 56 | +final class RlqsFilter implements Filter, ServerInterceptorBuilder { |
| 57 | + private final XdsLogger logger; |
| 58 | + |
| 59 | + static final boolean enabled = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_ENABLE_RLQS", false); |
| 60 | + |
| 61 | + // TODO(sergiitk): [IMPL] remove |
| 62 | + // Do do not fail on parsing errors, only log requests. |
| 63 | + static final boolean dryRun = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_RLQS_DRY_RUN", false); |
| 64 | + |
| 65 | + static final RlqsFilter INSTANCE = new RlqsFilter(); |
| 66 | + |
| 67 | + static final String TYPE_URL = "type.googleapis.com/" |
| 68 | + + "envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaFilterConfig"; |
| 69 | + static final String TYPE_URL_OVERRIDE_CONFIG = "type.googleapis.com/" |
| 70 | + + "envoy.extensions.filters.http.rate_limit_quota.v3.RateLimitQuotaOverride"; |
| 71 | + |
| 72 | + private final AtomicReference<RlqsCache> rlqsCache = new AtomicReference<>(); |
| 73 | + |
| 74 | + public RlqsFilter() { |
| 75 | + // TODO(sergiitk): one per new instance when filters are refactored. |
| 76 | + logger = XdsLogger.withLogId(InternalLogId.allocate(this.getClass(), null)); |
| 77 | + logger.log(XdsLogLevel.DEBUG, |
| 78 | + "Created RLQS Filter with enabled=" + enabled + ", dryRun=" + dryRun); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String[] typeUrls() { |
| 83 | + return new String[]{TYPE_URL, TYPE_URL_OVERRIDE_CONFIG}; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean isEnabled() { |
| 88 | + return enabled; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public ConfigOrError<RlqsFilterConfig> parseFilterConfig(Message rawProtoMessage) { |
| 93 | + try { |
| 94 | + RlqsFilterConfig rlqsFilterConfig = |
| 95 | + parseRlqsFilter(unpackAny(rawProtoMessage, RateLimitQuotaFilterConfig.class)); |
| 96 | + return ConfigOrError.fromConfig(rlqsFilterConfig); |
| 97 | + } catch (InvalidProtocolBufferException e) { |
| 98 | + return ConfigOrError.fromError("Can't unpack RateLimitQuotaFilterConfig proto: " + e); |
| 99 | + } catch (ResourceInvalidException e) { |
| 100 | + return ConfigOrError.fromError(e.getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ConfigOrError<RlqsFilterConfig> parseFilterConfigOverride(Message rawProtoMessage) { |
| 106 | + try { |
| 107 | + RlqsFilterConfig rlqsFilterConfig = |
| 108 | + parseRlqsFilterOverride(unpackAny(rawProtoMessage, RateLimitQuotaOverride.class)); |
| 109 | + return ConfigOrError.fromConfig(rlqsFilterConfig); |
| 110 | + } catch (InvalidProtocolBufferException e) { |
| 111 | + return ConfigOrError.fromError("Can't unpack RateLimitQuotaOverride proto: " + e); |
| 112 | + } catch (ResourceInvalidException e) { |
| 113 | + return ConfigOrError.fromError(e.getMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Nullable |
| 118 | + @Override |
| 119 | + public ServerInterceptor buildServerInterceptor( |
| 120 | + FilterConfig config, @Nullable FilterConfig overrideConfig) { |
| 121 | + throw new UnsupportedOperationException("ScheduledExecutorService scheduler required"); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public ServerInterceptor buildServerInterceptor( |
| 126 | + FilterConfig config, |
| 127 | + @Nullable FilterConfig overrideConfig, |
| 128 | + ScheduledExecutorService scheduler) { |
| 129 | + // Called when we get an xds update - when the LRS or RLS changes. |
| 130 | + RlqsFilterConfig rlqsFilterConfig = (RlqsFilterConfig) checkNotNull(config, "config"); |
| 131 | + |
| 132 | + // Per-route and per-host configuration overrides. |
| 133 | + if (overrideConfig != null) { |
| 134 | + RlqsFilterConfig rlqsFilterOverride = (RlqsFilterConfig) overrideConfig; |
| 135 | + // All fields are inherited from the main config, unless overridden. |
| 136 | + RlqsFilterConfig.Builder overrideBuilder = rlqsFilterConfig.toBuilder(); |
| 137 | + if (!rlqsFilterOverride.domain().isEmpty()) { |
| 138 | + overrideBuilder.domain(rlqsFilterOverride.domain()); |
| 139 | + } |
| 140 | + if (rlqsFilterOverride.bucketMatchers() != null) { |
| 141 | + overrideBuilder.bucketMatchers(rlqsFilterOverride.bucketMatchers()); |
| 142 | + } |
| 143 | + // Override bucket matchers if not null. |
| 144 | + rlqsFilterConfig = overrideBuilder.build(); |
| 145 | + } |
| 146 | + |
| 147 | + rlqsCache.compareAndSet(null, RlqsCache.newInstance(scheduler)); |
| 148 | + return generateRlqsInterceptor(rlqsFilterConfig); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void shutdown() { |
| 153 | + // TODO(sergiitk): [DESIGN] besides shutting down everything, should there |
| 154 | + // be per-route interceptor destructors? |
| 155 | + RlqsCache oldCache = rlqsCache.getAndUpdate(unused -> null); |
| 156 | + if (oldCache != null) { |
| 157 | + oldCache.shutdown(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Nullable |
| 162 | + private ServerInterceptor generateRlqsInterceptor(RlqsFilterConfig config) { |
| 163 | + checkNotNull(config, "config"); |
| 164 | + checkNotNull(config.rlqsService(), "config.rlqsService"); |
| 165 | + RlqsCache rlqsCache = this.rlqsCache.get(); |
| 166 | + if (rlqsCache == null) { |
| 167 | + // Being shut down, return no interceptor. |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + final RlqsFilterState rlqsFilterState = rlqsCache.getOrCreateFilterState(config); |
| 172 | + |
| 173 | + return new ServerInterceptor() { |
| 174 | + @Override |
| 175 | + public <ReqT, RespT> Listener<ReqT> interceptCall( |
| 176 | + ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { |
| 177 | + HttpMatchInput httpMatchInput = HttpMatchInput.create(headers, call); |
| 178 | + |
| 179 | + // TODO(sergiitk): [IMPL] Remove |
| 180 | + if (dryRun) { |
| 181 | + // logger.log(XdsLogLevel.INFO, "RLQS DRY RUN: request <<" + httpMatchInput + ">>"); |
| 182 | + return next.startCall(call, headers); |
| 183 | + } |
| 184 | + |
| 185 | + RlqsRateLimitResult result = rlqsFilterState.rateLimit(httpMatchInput); |
| 186 | + if (result.isAllowed()) { |
| 187 | + return next.startCall(call, headers); |
| 188 | + } |
| 189 | + RlqsRateLimitResult.DenyResponse denyResponse = result.denyResponse().get(); |
| 190 | + call.close(denyResponse.status(), denyResponse.headersToAdd()); |
| 191 | + return new ServerCall.Listener<ReqT>(){}; |
| 192 | + } |
| 193 | + }; |
| 194 | + } |
| 195 | + |
| 196 | + @VisibleForTesting |
| 197 | + RlqsFilterConfig parseRlqsFilter(RateLimitQuotaFilterConfig rlqsFilterProto) |
| 198 | + throws ResourceInvalidException, InvalidProtocolBufferException { |
| 199 | + RlqsFilterConfig.Builder builder = RlqsFilterConfig.builder(); |
| 200 | + if (rlqsFilterProto.getDomain().isEmpty()) { |
| 201 | + throw new ResourceInvalidException("RateLimitQuotaFilterConfig domain is required"); |
| 202 | + } |
| 203 | + builder.domain(rlqsFilterProto.getDomain()) |
| 204 | + .rlqsService(GrpcService.fromEnvoyProto(rlqsFilterProto.getRlqsServer())); |
| 205 | + |
| 206 | + // TODO(sergiitk): [IMPL] Remove |
| 207 | + if (dryRun) { |
| 208 | + logger.log(XdsLogLevel.DEBUG, "Dry run: not parsing matchers in the filter filter"); |
| 209 | + return builder.build(); |
| 210 | + } |
| 211 | + |
| 212 | + // TODO(sergiitk): [IMPL] actually parse, move to RlqsBucketSettings.fromProto() |
| 213 | + RateLimitQuotaBucketSettings fallbackBucketSettingsProto = unpackAny( |
| 214 | + rlqsFilterProto.getBucketMatchers().getOnNoMatch().getAction().getTypedConfig(), |
| 215 | + RateLimitQuotaBucketSettings.class); |
| 216 | + RlqsBucketSettings fallbackBucket = RlqsBucketSettings.create( |
| 217 | + ImmutableMap.of("bucket_id", headers -> "hello"), |
| 218 | + fallbackBucketSettingsProto.getReportingInterval()); |
| 219 | + |
| 220 | + // TODO(sergiitk): [IMPL] actually parse, move to Matcher.fromProto() |
| 221 | + Matcher<HttpMatchInput, RlqsBucketSettings> bucketMatchers = new RlqsMatcher(fallbackBucket); |
| 222 | + |
| 223 | + return builder.bucketMatchers(bucketMatchers).build(); |
| 224 | + } |
| 225 | + |
| 226 | + static class RlqsMatcher extends Matcher<HttpMatchInput, RlqsBucketSettings> { |
| 227 | + private final RlqsBucketSettings fallbackBucket; |
| 228 | + |
| 229 | + RlqsMatcher(RlqsBucketSettings fallbackBucket) { |
| 230 | + this.fallbackBucket = fallbackBucket; |
| 231 | + } |
| 232 | + |
| 233 | + @Nullable |
| 234 | + @Override |
| 235 | + public MatcherList<HttpMatchInput, RlqsBucketSettings> matcherList() { |
| 236 | + return null; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public OnMatch<HttpMatchInput, RlqsBucketSettings> onNoMatch() { |
| 241 | + return OnMatch.ofAction(fallbackBucket); |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public RlqsBucketSettings match(HttpMatchInput input) { |
| 246 | + return null; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + @VisibleForTesting |
| 251 | + static RlqsFilterConfig parseRlqsFilterOverride(RateLimitQuotaOverride rlqsFilterProtoOverride) |
| 252 | + throws ResourceInvalidException { |
| 253 | + RlqsFilterConfig.Builder builder = RlqsFilterConfig.builder(); |
| 254 | + // TODO(sergiitk): [IMPL] bucket_matchers. |
| 255 | + |
| 256 | + return builder.domain(rlqsFilterProtoOverride.getDomain()).build(); |
| 257 | + } |
| 258 | + |
| 259 | + private static <T extends com.google.protobuf.Message> T unpackAny( |
| 260 | + Message message, Class<T> clazz) throws InvalidProtocolBufferException { |
| 261 | + if (!(message instanceof Any)) { |
| 262 | + throw new InvalidProtocolBufferException( |
| 263 | + "Invalid config type: " + message.getClass().getCanonicalName()); |
| 264 | + } |
| 265 | + return ((Any) message).unpack(clazz); |
| 266 | + } |
| 267 | +} |
0 commit comments