Copilot commented on code in PR #13807:
URL: https://github.com/apache/trafficserver/pull/13807#discussion_r4457557132


##########
plugins/experimental/jax_fingerprint/config.cc:
##########
@@ -0,0 +1,428 @@
+/** @file
+
+  @section license License
+
+  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.
+ */
+
+#include "config.h"
+
+#ifdef ENABLE_JAX_METHOD_JA4
+#include "ja4/method.h"
+#endif
+#ifdef ENABLE_JAX_METHOD_JA4H
+#include "ja4h/method.h"
+#endif
+#ifdef ENABLE_JAX_METHOD_JA3
+#include "ja3/method.h"
+#endif
+
+#include <yaml-cpp/yaml.h>
+
+#include <getopt.h>
+
+#include <algorithm>
+#include <initializer_list>
+#include <string>
+#include <string_view>
+#include <unordered_set>
+
+namespace
+{
+constexpr Method const *METHODS[] = {
+#ifdef ENABLE_JAX_METHOD_JA4
+  &ja4::method,
+#endif
+#ifdef ENABLE_JAX_METHOD_JA4H
+  &ja4h::method,
+#endif
+#ifdef ENABLE_JAX_METHOD_JA3
+  &ja3::method,
+#endif
+};
+
+constexpr std::string_view ROOT_KEY         = "jax_fingerprint";
+constexpr std::string_view FINGERPRINTS_KEY = "fingerprints";
+
+bool
+check_keys(const YAML::Node &node, std::initializer_list<std::string_view> 
allowed, std::string_view scope)
+{
+  std::unordered_set<std::string> seen;
+  for (auto const &entry : node) {
+    auto key = entry.first.as<std::string>();
+    if (std::find(allowed.begin(), allowed.end(), key) == allowed.end()) {
+      TSError("[%s] Unknown key '%s' in %.*s", PLUGIN_NAME, key.c_str(), 
static_cast<int>(scope.size()), scope.data());
+      return false;
+    }
+    if (!seen.insert(key).second) {
+      TSError("[%s] Duplicate key '%s' in %.*s", PLUGIN_NAME, key.c_str(), 
static_cast<int>(scope.size()), scope.data());
+      return false;
+    }
+  }
+  return true;
+}
+
+bool
+read_string(const YAML::Node &node, const char *key, std::string &value)
+{
+  if (YAML::Node field = node[key]; field) {
+    if (!field.IsScalar() || field.Scalar().empty()) {
+      TSError("[%s] '%s' must be a non-empty string", PLUGIN_NAME, key);
+      return false;
+    }
+    value = field.Scalar();
+  }
+  return true;
+}
+
+bool
+set_method(std::string_view name, PluginConfig &config)
+{
+  auto method = std::find_if(std::begin(METHODS), std::end(METHODS), 
[&](Method const *m) { return m->name == name; });
+  if (method == std::end(METHODS)) {
+    TSError("[%s] Unknown method: %.*s", PLUGIN_NAME, 
static_cast<int>(name.size()), name.data());
+    return false;
+  }
+  config.method = **method;
+  return true;
+}
+
+bool
+parse_mode(std::string_view name, Mode &mode)
+{
+  if (name.empty() || name == "overwrite") {
+    mode = Mode::OVERWRITE;
+  } else if (name == "keep") {
+    mode = Mode::KEEP;
+  } else if (name == "append") {
+    mode = Mode::APPEND;
+  } else {
+    TSError("[%s] Unknown mode: %.*s", PLUGIN_NAME, 
static_cast<int>(name.size()), name.data());
+    return false;
+  }
+  return true;
+}
+
+/** Validate a parsed fingerprint configuration and install its runtime 
settings. */
+bool
+finish_fingerprint(PluginType plugin_type, std::shared_ptr<RuntimeSettings> 
settings, PluginConfig &config)
+{
+  config.plugin_type = plugin_type;
+
+  if (!config.log_symbol.empty() && plugin_type == PluginType::REMAP) {
+    TSError("[%s] 'log_field' is only supported for the plugin loaded via 
plugin.config", PLUGIN_NAME);
+    return false;
+  }
+
+  if (!settings->servernames.empty() && config.method.type != 
Method::Type::CONNECTION_BASED) {
+    TSError("[%s] 'servernames' is not supported for %s, which does not 
fingerprint the TLS handshake", PLUGIN_NAME,
+            config.method.name.data());
+    return false;
+  }

Review Comment:
   A non-standalone remap JA3/JA4 entry still accepts `servernames`, but 
`TSRemapNewInstance` installs its ClientHello hook only for standalone 
instances, and `handle_read_request_hdr` never checks this allowlist. In a 
hybrid setup whose global collector allows all names, the remap rule therefore 
emits fingerprints for names excluded by its own YAML configuration. Reject 
nonempty `servernames` for non-standalone remap entries and require the filter 
on the global collector instead.



##########
tests/uranium_tests/pluginTest/jax_fingerprint/test_jax_fingerprint_reload.py:
##########
@@ -0,0 +1,134 @@
+#  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.
+
+import re
+
+import pytest
+import yaml
+
+from tools.uranium.services import ATS, ATSFactory, ServiceFactory, 
wait_for_file_lines
+
+
+def configure_proxy(ats_factory: ATSFactory, services: ServiceFactory, replay: 
str) -> ATS:
+    """Start dependencies and configure an unstarted TLS proxy.
+
+    :param ats_factory: Factory owning the proxy.
+    :param services: Factory owning DNS and verifier origins.
+    :param replay: Replay filename relative to this module.
+    """
+    ats = ats_factory.create("ts", enable_cache=False, enable_tls=True)
+    if not ats.plugin_exists("jax_fingerprint.so"):
+        pytest.skip("jax_fingerprint.so is required")
+    dns = services.dns("dns", default="127.0.0.1")
+    origin = services.verifier_server("origin", replay)
+    ats.add_default_ssl_files()
+    ats.records.update(
+        {
+            "proxy.config.ssl.client.verify.server.policy": "PERMISSIVE",
+            "proxy.config.dns.nameservers": f"127.0.0.1:{dns.port}",
+            "proxy.config.dns.resolv_conf": "NULL",
+            "proxy.config.proxy_name": "test.proxy.test",
+            "proxy.config.diags.debug.enabled": 1,
+            "proxy.config.diags.debug.tags": "jax_fingerprint",
+        })
+    for host in ("jax.server.test", "jax-filtered.server.test"):
+        ats.remap_config.add_line(f"map https://{host} 
https://jax.backend.test:{origin.https_port}";)
+    if replay == "jax_fingerprint_remap_reload.replay.yaml":
+        ats.remap_config.add_line(
+            f"map http://jax.server.test 
http://jax.backend.test:{origin.http_port} "
+            "@plugin=jax_fingerprint.so @pparam=jax_fingerprint_remap.yaml")
+        ats.write_config_file("jax_fingerprint_remap.yaml", 
config_text({"method": "JA4H", "header": "x-jax-initial"}))
+    dns.start()
+    origin.start()
+    return ats
+
+
+def config_text(fingerprint: dict[str, object]) -> str:
+    """Serialize one fingerprint configuration.
+
+    :param fingerprint: YAML fingerprint settings.
+    """
+    return yaml.safe_dump({"jax_fingerprint": {"fingerprints": [fingerprint]}})
+
+
+def run_client(ats: ATS, services: ServiceFactory, replay: str, name: str, 
keys: str) -> None:
+    """Send selected requests and verify all replay assertions.
+
+    :param ats: Running proxy to target.
+    :param services: Factory owning verifier clients.
+    :param replay: Replay filename.
+    :param name: Unique client process name.
+    :param keys: Space-separated replay transaction keys.
+    """
+    client = services.verifier_client(name, replay, 
http_ports=[ats.http_port], https_ports=[ats.https_port], keys=keys)
+    result = client.run()
+    assert result.returncode == 0, result.output
+
+
+def test_jax_fingerprint_reload(ats_factory: ATSFactory, services: 
ServiceFactory) -> None:
+    """Reload each plugin instance independently and preserve state on 
rejection.
+
+    :param ats_factory: Factory owning the proxy.
+    :param services: Factory owning DNS and verifier processes.
+    """
+    replay = "jax_fingerprint_reload.replay.yaml"
+    ats = configure_proxy(ats_factory, services, replay)
+    fingerprint = {
+        "method": "JA4",
+        "standalone": True,
+        "header": "x-jax",
+        "via_header": "x-jax-via",
+        "servernames": ["jax.server.test"]
+    }
+    ats.write_config_file("jax_fingerprint.yaml", config_text(fingerprint))
+    ats.write_config_file("jax_fingerprint_ja4h.yaml", config_text({"method": 
"JA4H", "standalone": True, "header": "x-ja4h"}))
+    ats.plugin_config.add_lines(["jax_fingerprint.so jax_fingerprint.yaml", 
"jax_fingerprint.so jax_fingerprint_ja4h.yaml"])
+    rejected = r"Configuration reload from jax_fingerprint\.yaml rejected: 
'method' of fingerprint entry 1 changed"
+    ats.allow_errors(rejected)
+    ats.start()
+    run_client(ats, services, replay, "initial", "initial-allowed 
initial-filtered")
+    fingerprint["servernames"] = ["jax-filtered.server.test"]
+    (ats.config_directory / 
"jax_fingerprint.yaml").write_text(config_text(fingerprint))
+    result = ats.traffic_ctl("plugin", "msg", "jax_fingerprint.reload")
+    assert result.returncode == 0, result.output
+    wait_for_file_lines(ats.diags_log, r"Configuration reloaded successfully 
from jax_fingerprint\.yaml", 1)
+    wait_for_file_lines(ats.diags_log, r"Configuration reloaded successfully 
from jax_fingerprint_ja4h\.yaml", 1)
+    run_client(ats, services, replay, "reloaded", "reloaded-allowed 
reloaded-filtered")
+    fingerprint.update(method="JA3", servernames=["jax.server.test"])
+    (ats.config_directory / 
"jax_fingerprint.yaml").write_text(config_text(fingerprint))
+    result = ats.traffic_ctl("plugin", "msg", "jax_fingerprint.reload")
+    assert result.returncode == 0, result.output
+    wait_for_file_lines(ats.diags_log, rejected, 1)
+    wait_for_file_lines(ats.diags_log, r"Configuration reloaded successfully 
from jax_fingerprint_ja4h\.yaml", 2)
+    run_client(ats, services, replay, "rejected", "reloaded-allowed 
reloaded-filtered")
+
+
+def test_jax_fingerprint_remap_reload(ats_factory: ATSFactory, services: 
ServiceFactory) -> None:
+    """Reload a remap fingerprint by editing only its referenced YAML file.
+
+    :param ats_factory: Factory owning the proxy.
+    :param services: Factory owning DNS and verifier processes.
+    """
+    replay = "jax_fingerprint_remap_reload.replay.yaml"
+    ats = configure_proxy(ats_factory, services, replay)
+    ats.start()
+    run_client(ats, services, replay, "initial", "remap-initial")
+    path = ats.config_directory / "jax_fingerprint_remap.yaml"
+    path.write_text(config_text({"method": "JA4H", "header": 
"x-jax-reloaded"}))
+    result = ats.traffic_ctl("config", "reload")
+    assert result.returncode == 0, result.output
+    wait_for_file_lines(ats.traffic_out, re.escape(f"Loading configuration 
from {path}"), 2, timeout=60)

Review Comment:
   This message is emitted before YAML parsing, not after the new remap table 
becomes active. Since `traffic_ctl config reload` without `--monitor` only 
schedules the reload, this wait can return while the old instance still serves 
requests, causing `remap-reloaded` to intermittently receive `x-jax-initial`. 
Wait for the second `remap.config finished loading` diagnostic, which is 
emitted after the table exchange, before running the client.



-- 
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]

Reply via email to