|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.db.guardrails; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +import com.vdurmont.semver4j.Semver; |
| 27 | +import com.vdurmont.semver4j.Semver.SemverType; |
| 28 | + |
| 29 | +import org.apache.cassandra.config.GuardrailsOptions; |
| 30 | +import org.apache.cassandra.service.ClientState; |
| 31 | + |
| 32 | +/** |
| 33 | + * A guardrail that warns or rejects client connections whose driver version |
| 34 | + * is below a configured minimum per driver name. |
| 35 | + * <p> |
| 36 | + * The guardrail is configured with maps of driver name to minimum version string. |
| 37 | + * Connections that do not report a driver name or version are considered valid |
| 38 | + * and are not subject to this guardrail. |
| 39 | + * <p> |
| 40 | + * Version comparison uses semantic versioning via {@link Semver} with loose parsing |
| 41 | + * to handle non-standard version strings reported by drivers. |
| 42 | + */ |
| 43 | +public class ClientDriverVersionGuardrail extends Predicates<String> |
| 44 | +{ |
| 45 | + private final Function<ClientState, Map<String, String>> warnVersions; |
| 46 | + private final Function<ClientState, Map<String, String>> disallowVersions; |
| 47 | + |
| 48 | + public ClientDriverVersionGuardrail(Function<ClientState, Map<String, String>> warnVersions, |
| 49 | + Function<ClientState, Map<String, String>> disallowVersions) |
| 50 | + { |
| 51 | + super("minimum_client_driver_versions", null, null, null, null); |
| 52 | + this.warnVersions = warnVersions; |
| 53 | + this.disallowVersions = disallowVersions; |
| 54 | + } |
| 55 | + |
| 56 | + public void guard(@Nullable String driverName, @Nullable String driverVersion, @Nullable ClientState state) |
| 57 | + { |
| 58 | + if (!enabled(state)) |
| 59 | + return; |
| 60 | + |
| 61 | + String sanitizedDriverId = driverName == null ? null : driverName.trim(); |
| 62 | + if (sanitizedDriverId == null || sanitizedDriverId.isBlank()) |
| 63 | + { |
| 64 | + logger.debug("minimum_client_driver_versions guardrail identified empty driver " + |
| 65 | + "id to check the minimum version of, such connections will be allowed but " + |
| 66 | + "an operator should check what kind of clients are connecting to the cluster."); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + String sanitizedDriverVersion = GuardrailsOptions.sanitizeVersion(driverVersion); |
| 71 | + if (sanitizedDriverVersion == null) |
| 72 | + { |
| 73 | + logger.debug("minimum_client_driver_versions guardrail identified empty driver " + |
| 74 | + "version to check the minimum version of, such connections will be allowed but " + |
| 75 | + "an operator should check what kind of clients are connecting to the cluster."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (!GuardrailsOptions.isValidVersion(sanitizedDriverVersion)) |
| 80 | + { |
| 81 | + logger.debug("minimum_client_driver_versions guardrail identified driver " + |
| 82 | + "version which is not compliant semver version, such connections will be allowed but " + |
| 83 | + "an operator should check what kind of clients are connecting to the cluster."); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + Map<String, String> disallowed = disallowVersions.apply(state); |
| 88 | + if (disallowed != null && !disallowed.isEmpty()) |
| 89 | + { |
| 90 | + String minimumVersionFail = disallowed.get(sanitizedDriverId); |
| 91 | + if (minimumVersionFail != null && isBelowMinimum(sanitizedDriverVersion, minimumVersionFail)) |
| 92 | + { |
| 93 | + fail(String.format("Client driver %s is below required minimum version %s, connection rejected", |
| 94 | + sanitizedDriverId, minimumVersionFail), state); |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Map<String, String> warned = warnVersions.apply(state); |
| 100 | + if (warned != null && !warned.isEmpty()) |
| 101 | + { |
| 102 | + String minimumVersionWarn = warned.get(sanitizedDriverId); |
| 103 | + if (minimumVersionWarn != null && isBelowMinimum(sanitizedDriverVersion, minimumVersionWarn)) |
| 104 | + { |
| 105 | + warn(String.format("Client driver %s is below recommended minimum version %s", |
| 106 | + sanitizedDriverId, minimumVersionWarn)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Driver id should be in the format "driver:version". It will be parsed and |
| 113 | + * {@link #guard(String, String, ClientState)} called. |
| 114 | + * <p> |
| 115 | + * This method is not expected to be called in normal circumstances, it is just that we |
| 116 | + * would need to pass it with colon separator from StartupMessage as guard method |
| 117 | + * expects one string as a value to check, just so that we would break it apart in turn to get |
| 118 | + * driver id and version again. |
| 119 | + * |
| 120 | + * @param rawDriverId the value to check, driver name and version delimited by a colon. |
| 121 | + * @param state client state |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public void guard(String rawDriverId, @Nullable ClientState state) |
| 125 | + { |
| 126 | + if (rawDriverId == null) |
| 127 | + return; |
| 128 | + |
| 129 | + String[] pair = rawDriverId.trim().split(":"); |
| 130 | + if (pair.length != 2) |
| 131 | + return; |
| 132 | + |
| 133 | + String sanitizedDriverName = pair[0].trim(); |
| 134 | + if (sanitizedDriverName.isBlank()) |
| 135 | + return; |
| 136 | + |
| 137 | + String sanitizedDriverVersion = pair[1].trim(); |
| 138 | + if (sanitizedDriverVersion.isBlank()) |
| 139 | + return; |
| 140 | + |
| 141 | + guard(sanitizedDriverName, sanitizedDriverVersion, state); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Checks if the driver version is below the minimum version |
| 146 | + * specified in the config map. If driver name is not in minimum versions, |
| 147 | + * such connection is considered to be allowed. |
| 148 | + * <p> |
| 149 | + * |
| 150 | + * @param driverVersion version of a driver |
| 151 | + * @param minimumVersion minimum allowed version |
| 152 | + * @return true if the driver version is lower than the configured minimum |
| 153 | + */ |
| 154 | + static boolean isBelowMinimum(String driverVersion, String minimumVersion) |
| 155 | + { |
| 156 | + Semver versionToCheck = new Semver(driverVersion, SemverType.LOOSE); |
| 157 | + Semver minimumVersionAllowed = new Semver(minimumVersion, SemverType.LOOSE); |
| 158 | + return versionToCheck.isLowerThan(minimumVersionAllowed); |
| 159 | + } |
| 160 | +} |
0 commit comments