smiklosovic commented on code in PR #4699: URL: https://github.com/apache/cassandra/pull/4699#discussion_r3026538610
########## src/java/org/apache/cassandra/db/guardrails/ClientDriverVersionGuardrail.java: ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.db.guardrails; + +import java.util.Map; +import java.util.function.Function; + +import javax.annotation.Nullable; + +import com.vdurmont.semver4j.Semver; +import com.vdurmont.semver4j.Semver.SemverType; + +import org.apache.cassandra.service.ClientState; + +/** + * A guardrail that warns or rejects client connections whose driver version + * is below a configured minimum per driver name. + * <p> + * The guardrail is configured with maps of driver name to minimum version string. + * Connections that do not report a driver name or version are considered valid + * and are not subject to this guardrail. + * <p> + * Version comparison uses semantic versioning via {@link Semver} with loose parsing + * to handle non-standard version strings reported by drivers. + */ +public class ClientDriverVersionGuardrail extends Predicates<String> +{ + /** + * Creates a new client driver version guardrail. + * + * @param warnVersions supplier of driver name to minimum warn version map + * @param disallowVersions supplier of driver name to minimum fail version map + */ + private final Function<ClientState, Map<String, String>> warnVersions; + private final Function<ClientState, Map<String, String>> disallowVersions; + + public ClientDriverVersionGuardrail(Function<ClientState, Map<String, String>> warnVersions, + Function<ClientState, Map<String, String>> disallowVersions) + { + super("minimum_client_driver_version", + null, + state -> driverId -> isBelowMinimum(driverId, warnVersions.apply(state)), + state -> driverId -> isBelowMinimum(driverId, disallowVersions.apply(state)), + // message provider is overridden below + (isWarning, driverId) -> ""); + this.warnVersions = warnVersions; + this.disallowVersions = disallowVersions; + } + + @Override + public void guard(String driverId, @Nullable ClientState state) + { + if (!enabled(state)) + return; + + Map<String, String> disallowed = disallowVersions.apply(state); + if (isBelowMinimum(driverId, disallowed)) + { + String minimumVersion = getMinimumVersion(driverId, disallowed); + fail(String.format("Client driver %s is below required minimum version %s, connection rejected", + driverId, minimumVersion), state); + return; + } + + Map<String, String> warned = warnVersions.apply(state); + if (isBelowMinimum(driverId, warned)) + { + String minimumVersion = getMinimumVersion(driverId, warned); + warn(String.format("Client driver %s is below recommended minimum version %s", + driverId, minimumVersion)); Review Comment: not applicable imho, overkill in our situation -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

