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


##########
plugins/experimental/jax_fingerprint/plugin.cc:
##########
@@ -384,69 +357,49 @@ TSPluginInit(int argc, char const **argv)
     return;
   }
 
-  auto owned_config         = std::make_unique<PluginConfig>();
-  owned_config->plugin_type = PluginType::GLOBAL;
-
-  if (!read_config_option(argc, argv, *owned_config)) {
-    TSError("[%s] Failed to parse options.", PLUGIN_NAME);
+  if (argc != 2) {
+    TSError("[%s] Usage: %s <config.yaml>", PLUGIN_NAME, argv[0]);
     return;
   }
 
-  if (!owned_config->log_filename.empty()) {
-    if (!create_log_file(owned_config->log_filename, 
owned_config->log_handle)) {
-      TSError("[%s] Failed to create log.", PLUGIN_NAME);
-      return;
-    } else {
-      Dbg(dbg_ctl, "Created log file.");
-    }
+  PluginConfigs configs;
+  if (!load_config_file(argv[1], PluginType::GLOBAL, configs)) {
+    TSError("[%s] Failed to load configuration from %s.", PLUGIN_NAME, 
argv[1]);
+    return;
   }
 
-  // Reserve the index before registering the log field, so that every failure 
exit happens while the
-  // configuration is still owned here and nothing has taken a reference to it 
yet.
-  if (reserve_user_arg(*owned_config) == TS_ERROR) {
-    TSError("[%s] Failed to reserve user arg index.", PLUGIN_NAME);
+  if (!prepare_configs(configs)) {
     return;
   }
 
-  // A global plugin's configuration lives for the life of the process: the 
log field callback and the
-  // continuation below both keep a reference to it, so release it from the 
unique_ptr here.
-  PluginConfig *config = owned_config.release();
-
-  if (!config->log_symbol.empty()) {
-    std::string name  = "jax_fingerprint-";
-    name             += config->method.name;
-    TSLogFieldRegister(
-      name.c_str(), config->log_symbol, TS_LOG_TYPE_STRING,
-      [config](TSHttpTxn txnp, char *buf) -> int {
-        void *container;
-        if (config->method.type == Method::Type::CONNECTION_BASED) {
-          container = TSHttpSsnClientVConnGet(TSHttpTxnSsnGet(txnp));
-        } else {
-          container = txnp;
-        }
-        JAxContext *ctx = get_user_arg(container, *config);
-        if (ctx) {
-          return TSLogStringMarshal(buf, ctx->get_fingerprint());
-        } else {
-          return TSLogStringMarshal(buf, "-");
-        }
-      },
-      TSLogIntUnmarshal);
-  }
+  g_global_state.config_filename = argv[1];

Review Comment:
   Multiple global plugin entries share `g_global_state`: this assignment keeps 
only the last filename, while the loop below appends every entry's 
configurations. Two files containing one fingerprint each therefore leave two 
active configurations, but reload reads only one file and always fails the 
count check. ATS permits repeated loading with a warning. Give each lifecycle 
continuation its own filename/configuration state, or explicitly reject a 
second initialization before preparing resources or installing hooks.



##########
plugins/experimental/jax_fingerprint/config.cc:
##########
@@ -0,0 +1,261 @@
+/** @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 <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
+parse_fingerprint(const YAML::Node &node, PluginType plugin_type, PluginConfig 
&config)
+{
+  if (!node.IsMap()) {
+    TSError("[%s] Each '%.*s' entry must be a map", PLUGIN_NAME, 
static_cast<int>(FINGERPRINTS_KEY.size()),
+            FINGERPRINTS_KEY.data());
+    return false;
+  }
+
+  if (!check_keys(node,
+                  {"method", "standalone", "mode", "header", "via_header", 
"log_filename", "log_field", "servernames", "export"},
+                  "fingerprint entry")) {
+    return false;
+  }
+
+  config.plugin_type = plugin_type;
+
+  std::string method_name;
+  if (!read_string(node, "method", method_name) || method_name.empty()) {
+    TSError("[%s] Each fingerprint entry requires a 'method'", PLUGIN_NAME);
+    return false;
+  }
+  auto method = std::find_if(std::begin(METHODS), std::end(METHODS), 
[&](Method const *m) { return m->name == method_name; });
+  if (method == std::end(METHODS)) {
+    TSError("[%s] Unknown method: %s", PLUGIN_NAME, method_name.c_str());
+    return false;
+  }
+  config.method = **method;
+
+  if (YAML::Node standalone = node["standalone"]; standalone) {
+    config.standalone = standalone.as<bool>();
+  }
+
+  auto settings = std::make_shared<RuntimeSettings>();
+
+  std::string mode;
+  if (!read_string(node, "mode", mode)) {
+    return false;
+  }
+  if (mode.empty() || mode == "overwrite") {
+    settings->mode = Mode::OVERWRITE;
+  } else if (mode == "keep") {
+    settings->mode = Mode::KEEP;
+  } else if (mode == "append") {
+    settings->mode = Mode::APPEND;
+  } else {
+    TSError("[%s] Unknown mode: %s", PLUGIN_NAME, mode.c_str());
+    return false;
+  }
+
+  if (!read_string(node, "header", settings->header_name) || 
!read_string(node, "via_header", settings->via_header_name) ||
+      !read_string(node, "log_filename", config.log_filename) || 
!read_string(node, "log_field", config.log_symbol) ||
+      !read_string(node, "export", config.export_name)) {
+    return false;
+  }
+
+  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 (YAML::Node servernames = node["servernames"]; servernames) {
+    if (!servernames.IsSequence()) {
+      TSError("[%s] 'servernames' must be a list of server names", 
PLUGIN_NAME);
+      return false;
+    }

Review Comment:
   A JA4H entry with a non-empty `servernames` list is accepted even though its 
filter is never applied. JA4H has no ClientHello callback, and its request 
handler does not check this list, so it still fingerprints requests outside the 
configured allowlist. Reject non-empty allowlists for request-based methods 
rather than silently accepting an ineffective restriction.



##########
plugins/experimental/jax_fingerprint/plugin.cc:
##########
@@ -459,88 +412,77 @@ TSRemapInit(TSRemapInterface *api_info, char *errbuf, int 
errbuf_size)
 }
 
 TSReturnCode
-TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf 
ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */)
+TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int 
errbuf_size)
 {
-  Dbg(dbg_ctl, "New instance for client matching %s to %s", argv[0], argv[1]);
-  auto owned_config         = std::make_unique<PluginConfig>();
-  owned_config->plugin_type = PluginType::REMAP;
-
-  // Parse parameters
-  if (!read_config_option(argc - 1, const_cast<const char **>(argv + 1), 
*owned_config)) {
-    Dbg(dbg_ctl, "Bad arguments");
+  if (argc != 3) {
+    snprintf(errbuf, errbuf_size, "[%s] Usage: @plugin=%s.so 
@pparam=<config.yaml>", PLUGIN_NAME, PLUGIN_NAME);
     return TS_ERROR;
   }
+  Dbg(dbg_ctl, "New instance for client matching %s to %s", argv[0], argv[1]);
 
-  if (!owned_config->log_symbol.empty()) {
-    TSError("[%s] --log-field is not supported in remap.config. Use it in 
plugin.config instead.", PLUGIN_NAME);
+  auto configs = std::make_unique<PluginConfigs>();
+  if (!load_config_file(argv[2], PluginType::REMAP, *configs)) {
+    snprintf(errbuf, errbuf_size, "[%s] Failed to load configuration from %s", 
PLUGIN_NAME, argv[2]);
     return TS_ERROR;
   }

Review Comment:
   Editing only this YAML file and running `traffic_ctl config reload` leaves 
the remap instance unchanged. Reload checks files registered with FileManager, 
but this plugin never registers its YAML dependency, so instance creation is 
not triggered by a YAML-only edit. Register the resolved YAML path with 
`TSMgmtConfigFileAdd` under the remap configuration that owns the instance, and 
cover a reload where the remap file itself is unchanged.



##########
plugins/experimental/jax_fingerprint/config.cc:
##########
@@ -0,0 +1,261 @@
+/** @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 <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
+parse_fingerprint(const YAML::Node &node, PluginType plugin_type, PluginConfig 
&config)
+{
+  if (!node.IsMap()) {
+    TSError("[%s] Each '%.*s' entry must be a map", PLUGIN_NAME, 
static_cast<int>(FINGERPRINTS_KEY.size()),
+            FINGERPRINTS_KEY.data());
+    return false;
+  }
+
+  if (!check_keys(node,
+                  {"method", "standalone", "mode", "header", "via_header", 
"log_filename", "log_field", "servernames", "export"},
+                  "fingerprint entry")) {
+    return false;
+  }
+
+  config.plugin_type = plugin_type;
+
+  std::string method_name;
+  if (!read_string(node, "method", method_name) || method_name.empty()) {
+    TSError("[%s] Each fingerprint entry requires a 'method'", PLUGIN_NAME);
+    return false;
+  }
+  auto method = std::find_if(std::begin(METHODS), std::end(METHODS), 
[&](Method const *m) { return m->name == method_name; });
+  if (method == std::end(METHODS)) {
+    TSError("[%s] Unknown method: %s", PLUGIN_NAME, method_name.c_str());
+    return false;
+  }
+  config.method = **method;
+
+  if (YAML::Node standalone = node["standalone"]; standalone) {
+    config.standalone = standalone.as<bool>();
+  }
+
+  auto settings = std::make_shared<RuntimeSettings>();
+
+  std::string mode;
+  if (!read_string(node, "mode", mode)) {
+    return false;
+  }
+  if (mode.empty() || mode == "overwrite") {
+    settings->mode = Mode::OVERWRITE;
+  } else if (mode == "keep") {
+    settings->mode = Mode::KEEP;
+  } else if (mode == "append") {
+    settings->mode = Mode::APPEND;
+  } else {
+    TSError("[%s] Unknown mode: %s", PLUGIN_NAME, mode.c_str());
+    return false;
+  }
+
+  if (!read_string(node, "header", settings->header_name) || 
!read_string(node, "via_header", settings->via_header_name) ||
+      !read_string(node, "log_filename", config.log_filename) || 
!read_string(node, "log_field", config.log_symbol) ||
+      !read_string(node, "export", config.export_name)) {
+    return false;
+  }
+
+  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 (YAML::Node servernames = node["servernames"]; servernames) {
+    if (!servernames.IsSequence()) {
+      TSError("[%s] 'servernames' must be a list of server names", 
PLUGIN_NAME);
+      return false;
+    }
+    for (auto const &servername : servernames) {
+      if (!servername.IsScalar() || servername.Scalar().empty()) {
+        TSError("[%s] Each 'servernames' entry must be a non-empty server 
name", PLUGIN_NAME);
+        return false;
+      }
+      settings->servernames.emplace(servername.Scalar());
+    }
+  }
+
+  Dbg(dbg_ctl, "JAx method is %s", config.method.name.data());
+  Dbg(dbg_ctl, "JAx mode is %d", static_cast<int>(settings->mode));
+  Dbg(dbg_ctl, "JAx header is %s", !settings->header_name.empty() ? 
settings->header_name.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx via-header is %s", !settings->via_header_name.empty() ? 
settings->via_header_name.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx log file is %s", !config.log_filename.empty() ? 
config.log_filename.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx export registry is %s", !config.export_name.empty() ? 
config.export_name.c_str() : PLUGIN_NAME);
+  Dbg(dbg_ctl, "JAx standalone mode is %s", config.standalone ? "ENABLED" : 
"DISABLED");
+  for (auto const &servername : settings->servernames) {
+    Dbg(dbg_ctl, "JAx servername: %s", servername.c_str());
+  }
+
+  config.set_settings(std::move(settings));
+  return true;
+}
+} // namespace
+
+bool
+is_reload_compatible(std::vector<PluginConfig *> const &current, PluginConfigs 
const &updated, std::string &reason)
+{
+  if (current.size() != updated.size()) {
+    reason =
+      "the number of fingerprint entries changed from " + 
std::to_string(current.size()) + " to " + std::to_string(updated.size());
+    return false;
+  }
+
+  for (size_t i = 0; i < current.size(); ++i) {
+    PluginConfig const &before = *current[i];
+    PluginConfig const &after  = *updated[i];
+    std::string_view    changed;
+    if (before.method.name != after.method.name) {
+      changed = "method";
+    } else if (before.standalone != after.standalone) {
+      changed = "standalone";
+    } else if (before.export_name != after.export_name) {
+      changed = "export";
+    } else if (before.log_filename != after.log_filename) {
+      changed = "log_filename";
+    } else if (before.log_symbol != after.log_symbol) {
+      changed = "log_field";
+    }
+    if (!changed.empty()) {
+      reason  = "'";
+      reason += changed;
+      reason += "' of fingerprint entry " + std::to_string(i + 1) + " changed, 
but it can only be set at startup";
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool
+load_config_file(std::string_view filename, PluginType plugin_type, 
PluginConfigs &configs)
+{
+  std::string path{filename};
+  if (!path.empty() && path.front() != '/') {
+    path = std::string{TSConfigDirGet()} + "/" + path;
+  }
+  Dbg(dbg_ctl, "Loading configuration from %s", path.c_str());
+
+  try {
+    YAML::Node root = YAML::LoadFile(path);
+    if (!root.IsMap() || !check_keys(root, {ROOT_KEY}, "the top level")) {
+      TSError("[%s] %s: the top level must be a map with a single '%.*s' key", 
PLUGIN_NAME, path.c_str(),
+              static_cast<int>(ROOT_KEY.size()), ROOT_KEY.data());
+      return false;
+    }
+
+    YAML::Node plugin_node = root[std::string{ROOT_KEY}];
+    if (!plugin_node.IsMap() || !check_keys(plugin_node, {FINGERPRINTS_KEY}, 
ROOT_KEY)) {
+      TSError("[%s] %s: '%.*s' must be a map containing '%.*s'", PLUGIN_NAME, 
path.c_str(), static_cast<int>(ROOT_KEY.size()),
+              ROOT_KEY.data(), static_cast<int>(FINGERPRINTS_KEY.size()), 
FINGERPRINTS_KEY.data());
+      return false;
+    }
+
+    YAML::Node fingerprints = plugin_node[std::string{FINGERPRINTS_KEY}];
+    if (!fingerprints.IsSequence() || fingerprints.size() == 0) {
+      TSError("[%s] %s: '%.*s' must be a non-empty list", PLUGIN_NAME, 
path.c_str(), static_cast<int>(FINGERPRINTS_KEY.size()),
+              FINGERPRINTS_KEY.data());
+      return false;
+    }
+
+    for (auto const &fingerprint : fingerprints) {
+      auto config = std::make_unique<PluginConfig>();
+      if (!parse_fingerprint(fingerprint, plugin_type, *config)) {
+        TSError("[%s] %s: invalid fingerprint entry at line %d", PLUGIN_NAME, 
path.c_str(), fingerprint.Mark().line + 1);
+        return false;
+      }
+      configs.push_back(std::move(config));

Review Comment:
   Two standalone JA4 entries with the same export name and different 
`servernames` lists share one method-keyed context. On a connection allowed 
only by the first entry, the second skips ClientHello processing but still 
writes its header because the request handler finds the first entry's context. 
Reject duplicate `(method, effective export name)` pairs within this file 
before installing hooks. Treat an omitted export and an explicit 
`jax_fingerprint` export as the same registry.



##########
plugins/experimental/jax_fingerprint/config.cc:
##########
@@ -0,0 +1,261 @@
+/** @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 <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
+parse_fingerprint(const YAML::Node &node, PluginType plugin_type, PluginConfig 
&config)
+{
+  if (!node.IsMap()) {
+    TSError("[%s] Each '%.*s' entry must be a map", PLUGIN_NAME, 
static_cast<int>(FINGERPRINTS_KEY.size()),
+            FINGERPRINTS_KEY.data());
+    return false;
+  }
+
+  if (!check_keys(node,
+                  {"method", "standalone", "mode", "header", "via_header", 
"log_filename", "log_field", "servernames", "export"},
+                  "fingerprint entry")) {
+    return false;
+  }
+
+  config.plugin_type = plugin_type;
+
+  std::string method_name;
+  if (!read_string(node, "method", method_name) || method_name.empty()) {
+    TSError("[%s] Each fingerprint entry requires a 'method'", PLUGIN_NAME);
+    return false;
+  }
+  auto method = std::find_if(std::begin(METHODS), std::end(METHODS), 
[&](Method const *m) { return m->name == method_name; });
+  if (method == std::end(METHODS)) {
+    TSError("[%s] Unknown method: %s", PLUGIN_NAME, method_name.c_str());
+    return false;
+  }
+  config.method = **method;
+
+  if (YAML::Node standalone = node["standalone"]; standalone) {
+    config.standalone = standalone.as<bool>();
+  }
+
+  auto settings = std::make_shared<RuntimeSettings>();
+
+  std::string mode;
+  if (!read_string(node, "mode", mode)) {
+    return false;
+  }
+  if (mode.empty() || mode == "overwrite") {
+    settings->mode = Mode::OVERWRITE;
+  } else if (mode == "keep") {
+    settings->mode = Mode::KEEP;
+  } else if (mode == "append") {
+    settings->mode = Mode::APPEND;
+  } else {
+    TSError("[%s] Unknown mode: %s", PLUGIN_NAME, mode.c_str());
+    return false;
+  }
+
+  if (!read_string(node, "header", settings->header_name) || 
!read_string(node, "via_header", settings->via_header_name) ||
+      !read_string(node, "log_filename", config.log_filename) || 
!read_string(node, "log_field", config.log_symbol) ||
+      !read_string(node, "export", config.export_name)) {
+    return false;
+  }
+
+  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 (YAML::Node servernames = node["servernames"]; servernames) {
+    if (!servernames.IsSequence()) {
+      TSError("[%s] 'servernames' must be a list of server names", 
PLUGIN_NAME);
+      return false;
+    }
+    for (auto const &servername : servernames) {
+      if (!servername.IsScalar() || servername.Scalar().empty()) {
+        TSError("[%s] Each 'servernames' entry must be a non-empty server 
name", PLUGIN_NAME);
+        return false;
+      }
+      settings->servernames.emplace(servername.Scalar());
+    }
+  }
+
+  Dbg(dbg_ctl, "JAx method is %s", config.method.name.data());
+  Dbg(dbg_ctl, "JAx mode is %d", static_cast<int>(settings->mode));
+  Dbg(dbg_ctl, "JAx header is %s", !settings->header_name.empty() ? 
settings->header_name.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx via-header is %s", !settings->via_header_name.empty() ? 
settings->via_header_name.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx log file is %s", !config.log_filename.empty() ? 
config.log_filename.c_str() : "DISABLED");
+  Dbg(dbg_ctl, "JAx export registry is %s", !config.export_name.empty() ? 
config.export_name.c_str() : PLUGIN_NAME);
+  Dbg(dbg_ctl, "JAx standalone mode is %s", config.standalone ? "ENABLED" : 
"DISABLED");
+  for (auto const &servername : settings->servernames) {
+    Dbg(dbg_ctl, "JAx servername: %s", servername.c_str());
+  }
+
+  config.set_settings(std::move(settings));
+  return true;
+}
+} // namespace
+
+bool
+is_reload_compatible(std::vector<PluginConfig *> const &current, PluginConfigs 
const &updated, std::string &reason)
+{
+  if (current.size() != updated.size()) {
+    reason =
+      "the number of fingerprint entries changed from " + 
std::to_string(current.size()) + " to " + std::to_string(updated.size());
+    return false;
+  }
+
+  for (size_t i = 0; i < current.size(); ++i) {
+    PluginConfig const &before = *current[i];
+    PluginConfig const &after  = *updated[i];
+    std::string_view    changed;
+    if (before.method.name != after.method.name) {
+      changed = "method";
+    } else if (before.standalone != after.standalone) {
+      changed = "standalone";
+    } else if (before.export_name != after.export_name) {
+      changed = "export";
+    } else if (before.log_filename != after.log_filename) {
+      changed = "log_filename";
+    } else if (before.log_symbol != after.log_symbol) {
+      changed = "log_field";
+    }
+    if (!changed.empty()) {
+      reason  = "'";
+      reason += changed;
+      reason += "' of fingerprint entry " + std::to_string(i + 1) + " changed, 
but it can only be set at startup";
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool
+load_config_file(std::string_view filename, PluginType plugin_type, 
PluginConfigs &configs)
+{
+  std::string path{filename};
+  if (!path.empty() && path.front() != '/') {
+    path = std::string{TSConfigDirGet()} + "/" + path;
+  }
+  Dbg(dbg_ctl, "Loading configuration from %s", path.c_str());
+
+  try {
+    YAML::Node root = YAML::LoadFile(path);
+    if (!root.IsMap() || !check_keys(root, {ROOT_KEY}, "the top level")) {
+      TSError("[%s] %s: the top level must be a map with a single '%.*s' key", 
PLUGIN_NAME, path.c_str(),
+              static_cast<int>(ROOT_KEY.size()), ROOT_KEY.data());
+      return false;
+    }
+
+    YAML::Node plugin_node = root[std::string{ROOT_KEY}];
+    if (!plugin_node.IsMap() || !check_keys(plugin_node, {FINGERPRINTS_KEY}, 
ROOT_KEY)) {
+      TSError("[%s] %s: '%.*s' must be a map containing '%.*s'", PLUGIN_NAME, 
path.c_str(), static_cast<int>(ROOT_KEY.size()),
+              ROOT_KEY.data(), static_cast<int>(FINGERPRINTS_KEY.size()), 
FINGERPRINTS_KEY.data());
+      return false;
+    }
+
+    YAML::Node fingerprints = plugin_node[std::string{FINGERPRINTS_KEY}];
+    if (!fingerprints.IsSequence() || fingerprints.size() == 0) {
+      TSError("[%s] %s: '%.*s' must be a non-empty list", PLUGIN_NAME, 
path.c_str(), static_cast<int>(FINGERPRINTS_KEY.size()),
+              FINGERPRINTS_KEY.data());
+      return false;
+    }
+
+    for (auto const &fingerprint : fingerprints) {
+      auto config = std::make_unique<PluginConfig>();
+      if (!parse_fingerprint(fingerprint, plugin_type, *config)) {
+        TSError("[%s] %s: invalid fingerprint entry at line %d", PLUGIN_NAME, 
path.c_str(), fingerprint.Mark().line + 1);
+        return false;
+      }

Review Comment:
   The list accepts multiple entries with the same non-empty `log_field` 
symbol. The later `TSLogFieldRegister` returns `TS_ERROR`, which 
`register_log_field` ignores, so the symbol continues to output the first 
method's fingerprint without reporting the conflict. Validate that log-field 
symbols are unique before preparing or registering any entries, so this 
configuration is rejected rather than silently producing the wrong log value.



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