Copilot commented on code in PR #13600:
URL: https://github.com/apache/trafficserver/pull/13600#discussion_r3894155269
##########
plugins/experimental/rate_limit/sni_selector.cc:
##########
@@ -27,21 +27,30 @@ std::atomic<SniSelector *> SniSelector::_instance = nullptr;
///////////////////////////////////////////////////////////////////////////////
// YAML parser for the global YAML configuration (via plugin.config)
//
+// This is the exception boundary for the configuration parsing. The node
+// accessors and conversions in parseYamlFile() throw on malformed input, and
+// this runs on the management update continuation during a config reload, so
+// letting an exception escape here would terminate the server.
+//
bool
SniSelector::yamlParser(const std::string &yaml_file)
{
- YAML::Node config;
-
try {
- config = YAML::LoadFile(yaml_file);
+ return parseYamlFile(yaml_file);
} catch (YAML::BadFile const &e) {
TSError("[%s] Cannot load configuration file: %s.", PLUGIN_NAME, e.what());
Review Comment:
The `YAML::BadFile` error log doesn’t include the path being loaded, while
the generic parse error does. Including `yaml_file` in this message (e.g.,
\"Cannot load configuration file %s: %s\") will make reload failures easier to
diagnose, especially when multiple config paths are possible.
##########
plugins/experimental/rate_limit/sni_selector.cc:
##########
@@ -27,21 +27,30 @@ std::atomic<SniSelector *> SniSelector::_instance = nullptr;
///////////////////////////////////////////////////////////////////////////////
// YAML parser for the global YAML configuration (via plugin.config)
//
+// This is the exception boundary for the configuration parsing. The node
+// accessors and conversions in parseYamlFile() throw on malformed input, and
+// this runs on the management update continuation during a config reload, so
+// letting an exception escape here would terminate the server.
+//
bool
SniSelector::yamlParser(const std::string &yaml_file)
{
- YAML::Node config;
-
try {
- config = YAML::LoadFile(yaml_file);
+ return parseYamlFile(yaml_file);
} catch (YAML::BadFile const &e) {
TSError("[%s] Cannot load configuration file: %s.", PLUGIN_NAME, e.what());
- return false;
} catch (std::exception const &e) {
- TSError("[%s] Unknown error while loading configuration file: %s.",
PLUGIN_NAME, e.what());
- return false;
+ TSError("[%s] Failed to parse configuration file %s: %s.", PLUGIN_NAME,
yaml_file.c_str(), e.what());
}
+ return false;
+}
+
+bool
+SniSelector::parseYamlFile(const std::string &yaml_file)
+{
+ YAML::Node config = YAML::LoadFile(yaml_file);
+
_yaml_file = yaml_file;
Review Comment:
If `parseYamlFile()` throws after mutating member state (e.g., `_yaml_file`
and any subsequent parsing side effects), `yamlParser()` will return `false`
but the instance may already be partially updated. To ensure a failed reload
truly keeps the previous configuration, parse into temporary/local structures
and only commit (swap/assign to members like `_yaml_file` and any built tables)
after the full parse succeeds; at minimum, defer `_yaml_file = yaml_file` until
the end of a successful parse.
##########
tests/gold_tests/pluginTest/rate_limit/rate_limit_yaml_reload.test.py:
##########
@@ -0,0 +1,107 @@
+'''
+Test that a malformed rate_limit YAML file fails the reload instead of
+terminating traffic_server.
+'''
+# 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 os
+
+Test.Summary = '''
+rate_limit: a malformed YAML config fails the reload without killing ATS.
+'''
+
+Test.SkipUnless(Condition.PluginExists('rate_limit.so'))
+Test.ContinueOnFail = True
+
+server = Test.MakeOriginServer("server")
+server.addResponse(
+ "sessionlog.json", {
+ "headers": "GET /health HTTP/1.1\r\nHost: reload.example.com\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "body": ""
+ }, {
+ "headers": "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection:
close\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "body": "OK"
+ })
+
+ts = Test.MakeATSProcess("ts")
+
+rate_limit_yaml = os.path.join(ts.Variables.CONFIGDIR, 'rate_limit.yaml')
+ts.Disk.File(
+ rate_limit_yaml, typename="ats:config").AddLines([
+ 'selector:',
+ ' - sni: reload.example.com',
+ ' limit: 100',
+ '',
+ ])
+
+# A selector entry with no "sni" key. Reading sni["sni"] on the const node
+# throws YAML::InvalidNode before the "without a name" check can report it.
+missing_sni = os.path.join(Test.RunDirectory, 'missing_sni.yaml')
+with open(missing_sni, 'w') as f:
+ f.write('selector:\n - limit: 100\n')
+
+# "percentage" is read as a uint32_t, so the fractional value that the
+# documentation used to suggest throws YAML::TypedBadConversion.
+bad_percentage = os.path.join(Test.RunDirectory, 'bad_percentage.yaml')
+with open(bad_percentage, 'w') as f:
+ f.write('ip-rep:\n - name: test\n size: 15\n percentage: 0.9\n')
+
+ts.Disk.records_config.update({
+ 'proxy.config.diags.debug.enabled': 1,
+ 'proxy.config.diags.debug.tags': 'rate_limit',
+})
+
+ts.Disk.remap_config.AddLine(f'map /
http://127.0.0.1:{server.Variables.Port}/')
+ts.Disk.plugin_config.AddLine(f'rate_limit.so {rate_limit_yaml}')
+
+BASE_URL = f"http://127.0.0.1:{ts.Variables.port}/health"
+CURL = f"curl -s -o /dev/null -w '%{{http_code}}' -H 'Host:
reload.example.com' '{BASE_URL}'"
+
+tr = Test.AddTestRun("Start with a valid config")
+tr.Processes.Default.StartBefore(server,
ready=When.PortOpen(server.Variables.Port))
+tr.Processes.Default.StartBefore(ts)
+tr.Processes.Default.Command = CURL
+tr.Processes.Default.ReturnCode = 0
+tr.Processes.Default.Streams.stdout = Testers.ContainsExpression("200", "ATS
should serve the request")
+tr.StillRunningAfter = ts
+
+# The plugin callback only fires when the file mtime advances, so sleep before
+# each overwrite to make sure it does.
+for description, bad_config in [("selector entry without an sni key",
missing_sni), ("a fractional percentage", bad_percentage)]:
+ tr = Test.AddTestRun(f"Install {description}")
+ tr.Processes.Default.Command = f"sleep 2 && cp {bad_config}
{rate_limit_yaml}"
Review Comment:
The shell command interpolates file paths without quoting/escaping, which
can break if the run/config directory contains spaces or shell-special
characters. Consider quoting via `shlex.quote(...)` or avoid the shell copy
entirely by copying the file in Python (e.g., `shutil.copy2`) and using the
test framework to run a Python one-liner/script.
##########
plugins/experimental/rate_limit/sni_selector.cc:
##########
@@ -113,7 +122,7 @@ SniSelector::yamlParser(const std::string &yaml_file)
for (const auto &i : sel) {
const YAML::Node &sni = i;
- if (sni.IsMap() && !sni["sni"].IsSequence()) {
+ if (sni.IsMap() && sni["sni"] && !sni["sni"].IsSequence()) {
auto name = sni["sni"].as<std::string>();
Review Comment:
This indexes `sni[\"sni\"]` three times. Store it once in a local `const
YAML::Node sni_node = sni[\"sni\"];` (or similar) and use that for the
existence check, `IsSequence()`, and `as<std::string>()` to reduce repetition
and make the condition easier to read.
--
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]