|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.k8s.common; |
| 19 | + |
| 20 | +import com.google.gson.JsonArray; |
| 21 | +import com.google.gson.JsonElement; |
| 22 | +import com.google.gson.JsonObject; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +/** |
| 30 | + * Strict, read-only Kubernetes label selector matching for Namespace labels. |
| 31 | + */ |
| 32 | +public final class NamespaceSelector { |
| 33 | + |
| 34 | + private static final Set<String> SELECTOR_FIELDS = Set.of("matchLabels", "matchExpressions"); |
| 35 | + |
| 36 | + private static final Set<String> EXPRESSION_FIELDS = Set.of("key", "operator", "values"); |
| 37 | + |
| 38 | + private static final Pattern LABEL_NAME = Pattern.compile("[A-Za-z0-9]([A-Za-z0-9_.-]*[A-Za-z0-9])?"); |
| 39 | + |
| 40 | + private static final Pattern DNS_PREFIX = Pattern.compile("[a-z0-9]([a-z0-9-]*[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*"); |
| 41 | + |
| 42 | + private NamespaceSelector() { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Match all requirements after validating the entire selector. |
| 47 | + * Omitted optional fields and empty collections impose no requirements, so an empty |
| 48 | + * selector matches any present Namespace, including one with no labels. Explicit JSON |
| 49 | + * nulls, unknown fields and malformed requirements fail closed without JSON coercion. |
| 50 | + * In and NotIn require nonempty values; Exists and DoesNotExist allow omitted or empty |
| 51 | + * values. NotIn also matches an absent key, as required by Kubernetes. |
| 52 | + * |
| 53 | + * @param selector raw label selector, null when missing |
| 54 | + * @param labels Namespace labels; use an empty map for a present, unlabeled Namespace and null for a missing Namespace |
| 55 | + * @return true only when the selector is valid, the Namespace is present and all requirements match |
| 56 | + */ |
| 57 | + public static boolean matches(final JsonObject selector, final Map<String, String> labels) { |
| 58 | + if (Objects.isNull(selector) || Objects.isNull(labels) || !isValidSelector(selector)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + if (selector.has("matchLabels")) { |
| 62 | + for (Map.Entry<String, JsonElement> entry : selector.getAsJsonObject("matchLabels").entrySet()) { |
| 63 | + if (!entry.getValue().getAsString().equals(labels.get(entry.getKey()))) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + if (selector.has("matchExpressions")) { |
| 69 | + for (JsonElement expression : selector.getAsJsonArray("matchExpressions")) { |
| 70 | + if (!matchesExpression(expression.getAsJsonObject(), labels)) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean isValidSelector(final JsonObject selector) { |
| 79 | + if (!SELECTOR_FIELDS.containsAll(selector.keySet())) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + if (selector.has("matchLabels")) { |
| 83 | + JsonElement matchLabels = selector.get("matchLabels"); |
| 84 | + if (!matchLabels.isJsonObject()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + for (Map.Entry<String, JsonElement> entry : matchLabels.getAsJsonObject().entrySet()) { |
| 88 | + if (!isValidKey(entry.getKey()) || !isValidValue(entry.getValue())) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + if (selector.has("matchExpressions")) { |
| 94 | + JsonElement expressions = selector.get("matchExpressions"); |
| 95 | + if (!expressions.isJsonArray()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + for (JsonElement expression : expressions.getAsJsonArray()) { |
| 99 | + if (!expression.isJsonObject() || !isValidExpression(expression.getAsJsonObject())) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private static boolean isValidExpression(final JsonObject expression) { |
| 108 | + if (!EXPRESSION_FIELDS.containsAll(expression.keySet()) |
| 109 | + || !isString(expression.get("key")) || !isValidKey(expression.get("key").getAsString()) |
| 110 | + || !isString(expression.get("operator"))) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + JsonElement values = expression.get("values"); |
| 114 | + if (expression.has("values") && !values.isJsonArray()) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + if (Objects.nonNull(values)) { |
| 118 | + for (JsonElement value : values.getAsJsonArray()) { |
| 119 | + if (!isValidValue(value)) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + String operator = expression.get("operator").getAsString(); |
| 125 | + switch (operator) { |
| 126 | + case "In": |
| 127 | + case "NotIn": |
| 128 | + return Objects.nonNull(values) && !values.getAsJsonArray().isEmpty(); |
| 129 | + case "Exists": |
| 130 | + case "DoesNotExist": |
| 131 | + return Objects.isNull(values) || values.getAsJsonArray().isEmpty(); |
| 132 | + default: |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static boolean matchesExpression(final JsonObject expression, final Map<String, String> labels) { |
| 138 | + String key = expression.get("key").getAsString(); |
| 139 | + String operator = expression.get("operator").getAsString(); |
| 140 | + switch (operator) { |
| 141 | + case "In": |
| 142 | + return labels.containsKey(key) && containsValue(expression.getAsJsonArray("values"), labels.get(key)); |
| 143 | + case "NotIn": |
| 144 | + return !labels.containsKey(key) || !containsValue(expression.getAsJsonArray("values"), labels.get(key)); |
| 145 | + case "Exists": |
| 146 | + return labels.containsKey(key); |
| 147 | + case "DoesNotExist": |
| 148 | + return !labels.containsKey(key); |
| 149 | + default: |
| 150 | + return false; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static boolean containsValue(final JsonArray values, final String label) { |
| 155 | + for (JsonElement value : values) { |
| 156 | + if (value.getAsString().equals(label)) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + } |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + private static boolean isString(final JsonElement value) { |
| 164 | + return Objects.nonNull(value) && value.isJsonPrimitive() && value.getAsJsonPrimitive().isString(); |
| 165 | + } |
| 166 | + |
| 167 | + private static boolean isValidValue(final JsonElement value) { |
| 168 | + if (!isString(value)) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + String text = value.getAsString(); |
| 172 | + return text.length() <= 63 && (text.isEmpty() || LABEL_NAME.matcher(text).matches()); |
| 173 | + } |
| 174 | + |
| 175 | + private static boolean isValidKey(final String key) { |
| 176 | + int slash = key.indexOf('/'); |
| 177 | + String name = slash < 0 ? key : key.substring(slash + 1); |
| 178 | + if (name.isEmpty() || name.length() > 63 || !LABEL_NAME.matcher(name).matches()) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + if (slash < 0) { |
| 182 | + return true; |
| 183 | + } |
| 184 | + String prefix = key.substring(0, slash); |
| 185 | + return !prefix.isEmpty() && prefix.length() <= 253 && DNS_PREFIX.matcher(prefix).matches(); |
| 186 | + } |
| 187 | +} |
0 commit comments