You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by GitBox <gi...@apache.org> on 2022/09/20 16:21:54 UTC
[GitHub] [trafficserver] bneradt commented on a diff in pull request #9090: s3_auth: accept longer config lines
bneradt commented on code in PR #9090:
URL: https://github.com/apache/trafficserver/pull/9090#discussion_r974759262
##########
plugins/s3_auth/s3_auth.cc:
##########
@@ -523,41 +525,29 @@ S3Config::parse_config(const std::string &config_fname)
TSError("[%s] called without a config file, this is broken", PLUGIN_NAME);
return false;
} else {
- char line[512]; // These are long lines ...
- FILE *file = fopen(config_fname.c_str(), "r");
+ std::ifstream file;
+ file.open(config_fname, std::ios_base::in);
- if (nullptr == file) {
+ if (!file.is_open()) {
TSError("[%s] unable to open %s", PLUGIN_NAME, config_fname.c_str());
return false;
}
- while (fgets(line, sizeof(line), file) != nullptr) {
- char *pos1, *pos2;
+ for (std::string buf; std::getline(file, buf);) {
+ std::string_view line{buf};
- // Skip leading white spaces
- pos1 = line;
- while (*pos1 && isspace(*pos1)) {
- ++pos1;
- }
- if (!*pos1 || ('#' == *pos1)) {
- continue;
- }
+ // Skip leading/trailing white spaces
+ const auto key_val = trimWhiteSpaces(line);
- // Skip trailing white spaces
- pos2 = pos1;
- pos1 = pos2 + strlen(pos2) - 1;
- while ((pos1 > pos2) && isspace(*pos1)) {
- *(pos1--) = '\0';
- }
- if (pos1 == pos2) {
+ // Skip empty or comment lines
+ if (key_val.length() == 0 || ('#' == key_val[0])) {
Review Comment:
Usually `.empty()` instead of `.length() == 0`.
##########
plugins/s3_auth/aws_auth_v4.cc:
##########
@@ -22,11 +22,12 @@
* @see aws_auth_v4.h
*/
-#include <cstring> /* strlen() */
-#include <string> /* stoi() */
-#include <ctime> /* strftime(), time(), gmtime_r() */
-#include <iomanip> /* std::setw */
-#include <sstream> /* std::stringstream */
+#include <cstring> /* strlen() */
+#include <string> /* stoi() */
+#include <ctime> /* strftime(), time(), gmtime_r() */
+#include <iomanip> /* std::setw */
+#include <sstream> /* std::stringstream */
+#include <string_view>
#include <openssl/sha.h> /* SHA(), sha256_Update(), SHA256_Final, etc. */
Review Comment:
It doesn't look like this change is needed since this file does not contain a std::string_view.
##########
plugins/s3_auth/aws_auth_v4.h:
##########
@@ -67,7 +67,23 @@ static const String X_AMZ = "x-amz-";
static const String CONTENT_TYPE = "content-type";
static const String HOST = "host";
-String trimWhiteSpaces(const String &s);
+/**
+ * @brief Trim white spaces from beginning and end.
+ * @returns trimmed string
+ */
+template <class StringLike>
+StringLike
+trimWhiteSpaces(const StringLike &s)
+{
+ /* @todo do this better? */
+ static const auto whiteSpace = " \t\n\v\f\r";
+ size_t start = s.find_first_not_of(whiteSpace);
+ if (StringLike::npos == start) {
+ return StringLike();
+ }
+ size_t stop = s.find_last_not_of(whiteSpace);
+ return s.substr(start, stop - start + 1);
+}
Review Comment:
Since we're making this change, we might as well replace the uses of this with ts::TextView:
http://docs.solidwallofcode.com/libswoc/code/TextView.en.html
In that case, this function is removed and replaced with:
```
ts::TextView tv{some_string};
tv.trim_if(&isspace);
```
That is, we should be able to entirely remove this function and replace all calls with ts::TextView.
##########
tests/gold_tests/pluginTest/s3_auth/s3_auth_config.test.py:
##########
@@ -0,0 +1,77 @@
+'''
+Test s3_auth config parsing
+'''
+# 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
+
+ts = Test.MakeATSProcess("ts")
+server = Test.MakeOriginServer("server")
+
+Test.testName = "s3_auth: config parsing"
+
+# define the request header and the desired response header
+request_header = {
+ "headers": "GET /s3-bucket HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "x-amz-security-token": "hkMsi6/bfHyBKrSeM/H0hoXeyx8z1yZ/mJ0c+B/TqYx=tTJDjnQWtul38Z9iVJjeH1HB4VT2c=2o3yE3o=I9kmFs/lJDR85qWjB8e5asY/WbjyRpbAzmDipQpboIcYnUYg55bxrQFidV/q8gZa5A9MpR3n=op1C0lWjeBqcEJxpevNZxteSQTQfeGsi98Cdf+On=/SINVlKrNhMnmMsDOLMGx1YYt9d4UsRg1jtVrwxL4Vd/F7aHCZySAXKv+1rkhACR023wpa3dhp+xirGJxSO9LWwvcrTdM4xJo4RS8B40tGENOJ1NKixUJxwN/6og58Oft/u==uleR89Ja=7zszK2H7tX3DqmEYNvNDYQh/7VBRe5otghQtPwJzWpXAGk+Vme4hPPM5K6axH2LxipXzRiIV=oxNs0upKNu1FvuzbCQmkQdKQVmXl0344vngngrgN7wkEfrYtmKwICmpAS0cbW9jdSClgziVo4NaFc/hsIfok=4UA3hVtxIdw74lFNXD0RR7HKXkFPLIn85M7peOZsqMUCfO4gxr7KCfabszQQf0YcP/mt79XK50=WrSJG7oUyn+clUySPhlegqHAfT9a50uSK5WiQmOnGNGLF4wDO10sqKN1xRgQbYHPtwL+Ye0EMisvmYA3==kScorTSGaQWyibSWXAvxq9+IVGBYShVJ6S7DmTT=u/2d/fGEge+Xmbxlftza=cxJ=Md=k1Q71Lp6Boa56d7wtYRpK6tXHJ9I/2r7rN1E4OtwkFqb7SfWV3UXwyUrXyaaNPTIbqnAHnbgUGtuU6pgICpfREiIxVqvKBf6ErbxHRmMmAuYKxk5E9Mn6nnbxR4WTniweKYeDv2w39zge/tss+36Moeuio9d2eoyRFqXhq=rUGtDwX3fzXV0wV+dUojxOYQ57GQDl7+68PwHPcX794OIXuGOxBk83lNIYIcYz3Vc7qnGy6tFTz7f6S9
+EZuSGN7TY5VKkT2eWye46DebrDF9Nwzs/FVpTzbPD/KGDIBtFIbazglhKoWe9txqb1QW8vFNNVOEhYa+cViO3g8ZmY1wG960US2zsnX5Eg8Q5a4h3+sxaJSJ4ONiXZWJuAgKRQzcrszu+M5C0ZVoCOv1goEgfNJeSm/yFc/3rx8wmeWLIJFtq65B7zF72HRKq1nthHAguaxXr20nguHpKkDpNBDVa=WwuJsbeGI",
+ "body": ""
+}
+
+# desired response form the origin server
+response_header = {
+ "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "body": "success!"
+}
+
+# add request/response
+server.addResponse("sessionlog.log", request_header, response_header)
+
+ts.Disk.records_config.update({
+ 'proxy.config.diags.debug.enabled': 1,
+ 'proxy.config.diags.debug.tags': 's3_auth',
+})
+
+ts.Setup.CopyAs('rules/v4-parse-test.test_input', Test.RunDirectory)
+
+ts.Disk.remap_config.AddLine(
+ 'map http://www.example.com http://127.0.0.1:{0} \
+ @plugin=s3_auth.so \
+ @pparam=--config @pparam={1}/v4-parse-test.test_input'
+ .format(server.Variables.Port, Test.RunDirectory)
+)
+
+# Commands to get the following response headers
+# 1. make a request
+# 2. modify the config
+# 3. make another request
+curlRequest = (
+ 'curl -s -v -H "Host: www.example.com" http://127.0.0.1:{0}/s3-bucket;'
+)
Review Comment:
This would be clearer simply as a f-string:
```
curlRequest = f''curl -s -v -H "Host: www.example.com" http://127.0.0.1:{ts.Variables.port}/s3-bucket;'
```
##########
plugins/s3_auth/s3_auth.cc:
##########
@@ -523,41 +525,29 @@ S3Config::parse_config(const std::string &config_fname)
TSError("[%s] called without a config file, this is broken", PLUGIN_NAME);
return false;
} else {
- char line[512]; // These are long lines ...
- FILE *file = fopen(config_fname.c_str(), "r");
+ std::ifstream file;
+ file.open(config_fname, std::ios_base::in);
- if (nullptr == file) {
+ if (!file.is_open()) {
TSError("[%s] unable to open %s", PLUGIN_NAME, config_fname.c_str());
return false;
}
- while (fgets(line, sizeof(line), file) != nullptr) {
- char *pos1, *pos2;
+ for (std::string buf; std::getline(file, buf);) {
+ std::string_view line{buf};
- // Skip leading white spaces
- pos1 = line;
- while (*pos1 && isspace(*pos1)) {
- ++pos1;
- }
- if (!*pos1 || ('#' == *pos1)) {
- continue;
- }
+ // Skip leading/trailing white spaces
+ const auto key_val = trimWhiteSpaces(line);
- // Skip trailing white spaces
- pos2 = pos1;
- pos1 = pos2 + strlen(pos2) - 1;
- while ((pos1 > pos2) && isspace(*pos1)) {
- *(pos1--) = '\0';
- }
- if (pos1 == pos2) {
+ // Skip empty or comment lines
+ if (key_val.length() == 0 || ('#' == key_val[0])) {
continue;
}
// Identify the keys (and values if appropriate)
- std::string key_val(pos2, pos1 - pos2 + 1);
- size_t eq_pos = key_val.find_first_of("=");
- std::string key_str = trimWhiteSpaces(key_val.substr(0, eq_pos == String::npos ? key_val.size() : eq_pos));
- std::string val_str = eq_pos == String::npos ? "" : trimWhiteSpaces(key_val.substr(eq_pos + 1, key_val.size()));
+ size_t eq_pos = key_val.find_first_of('=');
+ std::string key_str{trimWhiteSpaces(key_val.substr(0, eq_pos == std::string_view::npos ? key_val.size() : eq_pos))};
+ std::string val_str{eq_pos == std::string_view::npos ? "" : trimWhiteSpaces(key_val.substr(eq_pos + 1, key_val.size()))};
Review Comment:
This also becomes easier with TextView via take_prefix_at.
##########
tests/gold_tests/pluginTest/s3_auth/s3_auth_config.test.py:
##########
@@ -0,0 +1,77 @@
+'''
+Test s3_auth config parsing
+'''
+# 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
+
+ts = Test.MakeATSProcess("ts")
+server = Test.MakeOriginServer("server")
+
+Test.testName = "s3_auth: config parsing"
+
+# define the request header and the desired response header
+request_header = {
+ "headers": "GET /s3-bucket HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "x-amz-security-token": "hkMsi6/bfHyBKrSeM/H0hoXeyx8z1yZ/mJ0c+B/TqYx=tTJDjnQWtul38Z9iVJjeH1HB4VT2c=2o3yE3o=I9kmFs/lJDR85qWjB8e5asY/WbjyRpbAzmDipQpboIcYnUYg55bxrQFidV/q8gZa5A9MpR3n=op1C0lWjeBqcEJxpevNZxteSQTQfeGsi98Cdf+On=/SINVlKrNhMnmMsDOLMGx1YYt9d4UsRg1jtVrwxL4Vd/F7aHCZySAXKv+1rkhACR023wpa3dhp+xirGJxSO9LWwvcrTdM4xJo4RS8B40tGENOJ1NKixUJxwN/6og58Oft/u==uleR89Ja=7zszK2H7tX3DqmEYNvNDYQh/7VBRe5otghQtPwJzWpXAGk+Vme4hPPM5K6axH2LxipXzRiIV=oxNs0upKNu1FvuzbCQmkQdKQVmXl0344vngngrgN7wkEfrYtmKwICmpAS0cbW9jdSClgziVo4NaFc/hsIfok=4UA3hVtxIdw74lFNXD0RR7HKXkFPLIn85M7peOZsqMUCfO4gxr7KCfabszQQf0YcP/mt79XK50=WrSJG7oUyn+clUySPhlegqHAfT9a50uSK5WiQmOnGNGLF4wDO10sqKN1xRgQbYHPtwL+Ye0EMisvmYA3==kScorTSGaQWyibSWXAvxq9+IVGBYShVJ6S7DmTT=u/2d/fGEge+Xmbxlftza=cxJ=Md=k1Q71Lp6Boa56d7wtYRpK6tXHJ9I/2r7rN1E4OtwkFqb7SfWV3UXwyUrXyaaNPTIbqnAHnbgUGtuU6pgICpfREiIxVqvKBf6ErbxHRmMmAuYKxk5E9Mn6nnbxR4WTniweKYeDv2w39zge/tss+36Moeuio9d2eoyRFqXhq=rUGtDwX3fzXV0wV+dUojxOYQ57GQDl7+68PwHPcX794OIXuGOxBk83lNIYIcYz3Vc7qnGy6tFTz7f6S9
+EZuSGN7TY5VKkT2eWye46DebrDF9Nwzs/FVpTzbPD/KGDIBtFIbazglhKoWe9txqb1QW8vFNNVOEhYa+cViO3g8ZmY1wG960US2zsnX5Eg8Q5a4h3+sxaJSJ4ONiXZWJuAgKRQzcrszu+M5C0ZVoCOv1goEgfNJeSm/yFc/3rx8wmeWLIJFtq65B7zF72HRKq1nthHAguaxXr20nguHpKkDpNBDVa=WwuJsbeGI",
+ "body": ""
+}
+
+# desired response form the origin server
+response_header = {
+ "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "body": "success!"
+}
+
+# add request/response
+server.addResponse("sessionlog.log", request_header, response_header)
+
+ts.Disk.records_config.update({
+ 'proxy.config.diags.debug.enabled': 1,
+ 'proxy.config.diags.debug.tags': 's3_auth',
+})
+
+ts.Setup.CopyAs('rules/v4-parse-test.test_input', Test.RunDirectory)
+
+ts.Disk.remap_config.AddLine(
+ 'map http://www.example.com http://127.0.0.1:{0} \
+ @plugin=s3_auth.so \
+ @pparam=--config @pparam={1}/v4-parse-test.test_input'
+ .format(server.Variables.Port, Test.RunDirectory)
Review Comment:
f-string
##########
tests/gold_tests/pluginTest/s3_auth/s3_auth_config.test.py:
##########
@@ -0,0 +1,77 @@
+'''
+Test s3_auth config parsing
+'''
+# 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
+
+ts = Test.MakeATSProcess("ts")
+server = Test.MakeOriginServer("server")
+
+Test.testName = "s3_auth: config parsing"
+
+# define the request header and the desired response header
+request_header = {
+ "headers": "GET /s3-bucket HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "x-amz-security-token": "hkMsi6/bfHyBKrSeM/H0hoXeyx8z1yZ/mJ0c+B/TqYx=tTJDjnQWtul38Z9iVJjeH1HB4VT2c=2o3yE3o=I9kmFs/lJDR85qWjB8e5asY/WbjyRpbAzmDipQpboIcYnUYg55bxrQFidV/q8gZa5A9MpR3n=op1C0lWjeBqcEJxpevNZxteSQTQfeGsi98Cdf+On=/SINVlKrNhMnmMsDOLMGx1YYt9d4UsRg1jtVrwxL4Vd/F7aHCZySAXKv+1rkhACR023wpa3dhp+xirGJxSO9LWwvcrTdM4xJo4RS8B40tGENOJ1NKixUJxwN/6og58Oft/u==uleR89Ja=7zszK2H7tX3DqmEYNvNDYQh/7VBRe5otghQtPwJzWpXAGk+Vme4hPPM5K6axH2LxipXzRiIV=oxNs0upKNu1FvuzbCQmkQdKQVmXl0344vngngrgN7wkEfrYtmKwICmpAS0cbW9jdSClgziVo4NaFc/hsIfok=4UA3hVtxIdw74lFNXD0RR7HKXkFPLIn85M7peOZsqMUCfO4gxr7KCfabszQQf0YcP/mt79XK50=WrSJG7oUyn+clUySPhlegqHAfT9a50uSK5WiQmOnGNGLF4wDO10sqKN1xRgQbYHPtwL+Ye0EMisvmYA3==kScorTSGaQWyibSWXAvxq9+IVGBYShVJ6S7DmTT=u/2d/fGEge+Xmbxlftza=cxJ=Md=k1Q71Lp6Boa56d7wtYRpK6tXHJ9I/2r7rN1E4OtwkFqb7SfWV3UXwyUrXyaaNPTIbqnAHnbgUGtuU6pgICpfREiIxVqvKBf6ErbxHRmMmAuYKxk5E9Mn6nnbxR4WTniweKYeDv2w39zge/tss+36Moeuio9d2eoyRFqXhq=rUGtDwX3fzXV0wV+dUojxOYQ57GQDl7+68PwHPcX794OIXuGOxBk83lNIYIcYz3Vc7qnGy6tFTz7f6S9
+EZuSGN7TY5VKkT2eWye46DebrDF9Nwzs/FVpTzbPD/KGDIBtFIbazglhKoWe9txqb1QW8vFNNVOEhYa+cViO3g8ZmY1wG960US2zsnX5Eg8Q5a4h3+sxaJSJ4ONiXZWJuAgKRQzcrszu+M5C0ZVoCOv1goEgfNJeSm/yFc/3rx8wmeWLIJFtq65B7zF72HRKq1nthHAguaxXr20nguHpKkDpNBDVa=WwuJsbeGI",
+ "body": ""
+}
+
+# desired response form the origin server
+response_header = {
+ "headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n",
+ "timestamp": "1469733493.993",
+ "body": "success!"
+}
+
+# add request/response
+server.addResponse("sessionlog.log", request_header, response_header)
+
+ts.Disk.records_config.update({
+ 'proxy.config.diags.debug.enabled': 1,
+ 'proxy.config.diags.debug.tags': 's3_auth',
+})
+
+ts.Setup.CopyAs('rules/v4-parse-test.test_input', Test.RunDirectory)
+
+ts.Disk.remap_config.AddLine(
+ 'map http://www.example.com http://127.0.0.1:{0} \
+ @plugin=s3_auth.so \
+ @pparam=--config @pparam={1}/v4-parse-test.test_input'
+ .format(server.Variables.Port, Test.RunDirectory)
+)
+
+# Commands to get the following response headers
+# 1. make a request
+# 2. modify the config
+# 3. make another request
+curlRequest = (
+ 'curl -s -v -H "Host: www.example.com" http://127.0.0.1:{0}/s3-bucket;'
+)
+
+# Test Case
+tr = Test.AddTestRun()
+tr.Processes.Default.Command = curlRequest.format(ts.Variables.port, Test.RunDirectory)
Review Comment:
Test.RunDirectory is not used, I think?
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org