traeak commented on code in PR #13853:
URL: https://github.com/apache/trafficserver/pull/13853#discussion_r4567974299
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -23,15 +23,65 @@
#include <yaml-cpp/yaml.h>
+#include <sys/stat.h>
+
+#include <cerrno>
+#include <filesystem>
#include <fstream>
#include <cstring>
+#include <stdexcept>
+#include <string>
#include <string_view>
+#include <system_error>
+#include <utility>
+#include "records/RecCore.h"
+#include "tscore/ink_platform.h"
+#include "tscore/Layout.h"
#include "proxy/http/remap/NextHopStrategyFactory.h"
#include "proxy/http/remap/NextHopConsistentHash.h"
#include "proxy/http/remap/NextHopRoundRobin.h"
#include <tsutil/YamlCfg.h>
+namespace
+{
+// Returns the include target resolved against the configuration directory.
Layout::relative_to() calls ink_fatal() for a
+// path too long to merge, which would take down a running traffic_server on a
reload, so a target that would exceed the
+// ink_filepath_merge() length bound is rejected as a configuration error
instead.
+std::string
+config_dir_include_path(std::string const &include_target, std::string const
&fileName)
+{
+ if (RecConfigReadConfigDir().size() + include_target.size() + 4 >
PATH_NAME_MAX) {
Review Comment:
Absolute targets are now opened unchanged and the length guard is removed;
an include too long to open fails the load. Added a test with an absolute path
through a symlink followed by `..`.
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -23,15 +23,65 @@
#include <yaml-cpp/yaml.h>
+#include <sys/stat.h>
+
+#include <cerrno>
+#include <filesystem>
#include <fstream>
#include <cstring>
+#include <stdexcept>
+#include <string>
#include <string_view>
+#include <system_error>
+#include <utility>
+#include "records/RecCore.h"
+#include "tscore/ink_platform.h"
+#include "tscore/Layout.h"
#include "proxy/http/remap/NextHopStrategyFactory.h"
#include "proxy/http/remap/NextHopConsistentHash.h"
#include "proxy/http/remap/NextHopRoundRobin.h"
#include <tsutil/YamlCfg.h>
+namespace
+{
+// Returns the include target resolved against the configuration directory.
Layout::relative_to() calls ink_fatal() for a
+// path too long to merge, which would take down a running traffic_server on a
reload, so a target that would exceed the
+// ink_filepath_merge() length bound is rejected as a configuration error
instead.
+std::string
+config_dir_include_path(std::string const &include_target, std::string const
&fileName)
+{
+ if (RecConfigReadConfigDir().size() + include_target.size() + 4 >
PATH_NAME_MAX) {
+ throw std::invalid_argument("'#include' path in '" + fileName + "' exceeds
the " + std::to_string(PATH_NAME_MAX) +
+ " character path limit");
+ }
+ return RecConfigReadConfigPath(nullptr, include_target.c_str());
+}
+
+// Earlier releases opened a relative include from the traffic_server working
directory, which is the install prefix.
+// ToDo: Deprecated in 11.0.0, remove in 12.0.0
+std::string
+legacy_include_path(std::string_view include_path)
+{
+ std::filesystem::path const target{include_path};
+ if (target.is_absolute()) {
+ return {};
+ }
+ return (std::filesystem::path{Layout::get()->prefix} /
target).lexically_normal().string();
Review Comment:
The fallback path is now joined to the install prefix without normalization,
and the recommended spelling keeps `..` elements. Added a test with a symlink
under the prefix followed by `..`.
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -261,7 +307,22 @@ NextHopStrategyFactory::loadConfigFile(const std::string
&fileName, std::strings
}
const auto include_line = line_view.substr(include_start);
const auto include_end =
include_line.find_first_of(separators);
- const std::string f{include_line.substr(0, include_end)};
+ const std::string include_target{include_line.substr(0,
include_end)};
+ std::string f =
RecConfigReadConfigPath(nullptr, include_target.c_str());
+ bool used_legacy_path = false;
+
+ // Retry a missing include when it starts with the configuration
directory's old
+ // install-prefix-relative spelling, e.g.
etc/trafficserver/hosts.yaml.
+ struct stat include_stat;
+ if (stat(f.c_str(), &include_stat) == -1 && errno == ENOENT) {
+ std::string legacy_path =
legacy_config_dir_include_path(include_target);
+ struct stat legacy_stat;
+ if (!legacy_path.empty() && stat(legacy_path.c_str(),
&legacy_stat) == 0) {
Review Comment:
`<sys/stat.h>` and `<cerrno>` are included.
##########
src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc:
##########
@@ -27,16 +27,61 @@
*/
#include <catch2/catch_test_macros.hpp> /* catch unit-test framework */
-#include <fstream> /* ofstream */
+#include <catch2/reporters/catch_reporter_event_listener.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+#include <chrono>
+#include <cstdlib>
+#include <filesystem>
+#include <fstream> /* ofstream */
#include <memory>
#include <utime.h>
#include <yaml-cpp/yaml.h>
-#include "nexthop_test_stubs.h"
+#include "tscore/Layout.h"
#include "proxy/http/remap/NextHopSelectionStrategy.h"
#include "proxy/http/remap/NextHopStrategyFactory.h"
#include "proxy/http/remap/NextHopConsistentHash.h"
-#include "proxy/http/remap/NextHopRoundRobin.h"
+
+// '#include' directives in strategy files resolve against the configuration
+// directory, so point it at a temporary configuration directory containing the
+// test fixtures.
+static std::filesystem::path config_root;
+
+struct ConfigDirListener : Catch::EventListenerBase {
+ using EventListenerBase::EventListenerBase;
+
+ void
+ testRunStarting(Catch::TestRunInfo const & /* testRunInfo ATS_UNUSED */)
override
+ {
+ const auto temp_dir = std::filesystem::temp_directory_path();
+ const auto prefix = "nh_strategy_config_test-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+
+ for (unsigned int attempt = 0; attempt < 100; ++attempt) {
+ config_root = temp_dir / (prefix + "-" + std::to_string(attempt));
+ if (std::filesystem::create_directory(config_root)) {
+ break;
+ }
+ config_root.clear();
+ }
+ if (config_root.empty()) {
+ std::abort();
+ }
Review Comment:
Replaced with a thrown `std::runtime_error` that carries a message.
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -271,6 +332,10 @@ NextHopStrategyFactory::loadConfigFile(const std::string
&fileName, std::strings
throw std::invalid_argument("Unable to load included file '" +
f + "' from '" + fileName + "': " + ex.what());
}
}
+ if (used_legacy_path) {
+ const auto recommended_include =
std::filesystem::path(f).lexically_relative(RecConfigReadConfigDir()).string();
+ NH_Warn("Deprecated '#include %s'; use '#include %s' instead.",
include_target.c_str(), recommended_include.c_str());
Review Comment:
`recommended_include()` returns the full resolved path when the file is not
under the configuration directory, so it is never empty.
##########
tests/gold_tests/next_hop/strategies_include/strategies_include.test.py:
##########
@@ -0,0 +1,103 @@
+'''
+Verify '#include' handling in strategies.yaml.
+'''
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
Review Comment:
This file no longer exists; the test moved to
`tests/uranium_tests/next_hop/strategies_include/test_strategies_include.py`.
--
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]