Copilot commented on code in PR #6145: URL: https://github.com/apache/shenyu/pull/6145#discussion_r2370857152
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/websocket/WebsocketCollector.java: ########## @@ -267,4 +267,18 @@ private void clearSession(final Session session) { } ThreadLocalUtils.clear(); } + + private static String maskSensitive(final String json) { + if (Objects.isNull(json)) { + return null; + } + try { + // simple regex-based masking for logging only + String masked = json.replaceAll("(\"apiKey\"\\s*:\\s*\")([^\"]+)(\")", "$1******$3"); + masked = masked.replaceAll("(\"realApiKey\"\\s*:\\s*\")([^\"]+)(\")", "$1******$3"); + return masked; Review Comment: The regex-based API key masking is brittle and may not catch all cases (escaped quotes, different JSON formatting). Consider using a proper JSON parser to safely mask sensitive fields. ```suggestion Map<String, Object> map = JsonUtils.fromJson(json, Map.class); if (map != null) { if (map.containsKey("apiKey")) { map.put("apiKey", "******"); } if (map.containsKey("realApiKey")) { map.put("realApiKey", "******"); } return JsonUtils.toJson(map); } return json; ``` ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/support/AiProxyRealKeyResolver.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.shenyu.admin.service.support; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.mapper.SelectorMapper; +import org.apache.shenyu.admin.model.entity.SelectorDO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; + +/** + * AiProxyRealKeyResolver resolves real api key from selector handle JSON. + * Event-driven invalidation, single-flight resolve, no time TTL by default. + */ +@Component +public class AiProxyRealKeyResolver { + + private static final Logger LOG = LoggerFactory.getLogger(AiProxyRealKeyResolver.class); + + private final SelectorMapper selectorMapper; + + private final ObjectMapper objectMapper; + + /** selectorId -> cached real key (nullable). */ + private final ConcurrentHashMap<String, AtomicReference<String>> cache = new ConcurrentHashMap<>(); + + /** selectorId -> in-flight resolving future. */ + private final Map<String, CompletableFuture<String>> inFlight = new ConcurrentHashMap<>(); + + public AiProxyRealKeyResolver(final SelectorMapper selectorMapper, final ObjectMapper objectMapper) { + this.selectorMapper = selectorMapper; + this.objectMapper = objectMapper; + } + + /** + * Resolve real api key by selector id with single-flight. + * @param selectorId selector id + * @return optional real api key + */ + public Optional<String> resolveRealKey(final String selectorId) { + if (StringUtils.isBlank(selectorId)) { + return Optional.empty(); + } + final AtomicReference<String> ref = cache.get(selectorId); + if (Objects.nonNull(ref)) { + final String v = ref.get(); + LOG.debug("[AiProxyRealKeyResolver] cache hit selectorId={}, masked={}...", selectorId, mask(v)); + return Optional.ofNullable(ref.get()); + } + // single-flight: only one resolving task per selectorId + final CompletableFuture<String> future = inFlight.computeIfAbsent(selectorId, id -> + CompletableFuture.supplyAsync(() -> doResolve(id)) + .whenComplete((val, ex) -> { + try { + cache.put(id, new AtomicReference<>(val)); + if (Objects.nonNull(ex)) { + LOG.warn("[AiProxyRealKeyResolver] resolve failed for selectorId={}: {}", id, ex.getMessage()); + } else { + LOG.info("[AiProxyRealKeyResolver] resolved selectorId={}, masked={}...", id, mask(val)); + } + } finally { + inFlight.remove(id); + } + }) + ); + try { + return Optional.ofNullable(future.join()); Review Comment: Using `future.join()` can block indefinitely and throw unchecked exceptions. The surrounding try-catch only catches Exception but join() can throw CompletionException. Consider using `future.get()` with timeout or handle CompletionException specifically. -- 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: notifications-unsubscr...@shenyu.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org