Skip to content

Commit

Permalink
fix: use same regex code at ZTSClient (apache#11323)
Browse files Browse the repository at this point in the history
### Motivation
apache#9533 causes some degraded behaviors at [ZTSClient](https://github.com/apache/pulsar/pull/9533/files#diff-41a90a5ec7937b5e2151752843621d6acfe8963077dc04c285842a09a4e45ecc).

1. when using boost::regex
   - can't set `path` at file scheme
2. when using std::regex
   - can't set relative path like `file:./path/to/private.key`

I'd like to fix this issue.

### Modifications

* Use same regex code between boost and std like [this](https://github.com/apache/pulsar/pull/9533/files#diff-a67e917f5f42f3337507a69a4e0f13e286238dc759b4048cb5141290cf445b38)
* Fix regex to be able to capture relative path like `file:./`
  • Loading branch information
equanz authored Jul 19, 2021
1 parent 3adc475 commit 7cce8e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
22 changes: 6 additions & 16 deletions pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ namespace ptree = boost::property_tree;

#ifdef PULSAR_USE_BOOST_REGEX
#include <boost/regex.hpp>
#define PULSAR_REGEX_NAMESPACE boost
#else
#include <regex>
#define PULSAR_REGEX_NAMESPACE std
#endif

DECLARE_LOG_OBJECT()
Expand Down Expand Up @@ -365,27 +367,15 @@ const std::string ZTSClient::getHeader() const { return roleHeader_; }
PrivateKeyUri ZTSClient::parseUri(const char *uri) {
PrivateKeyUri uriSt;
// scheme mediatype[;base64] path file

#ifdef PULSAR_USE_BOOST_REGEX
static const boost::regex expression(
"^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)");
boost::cmatch groups;
if (boost::regex_match(uri, groups, expression)) {
uriSt.scheme = groups.str(1);
uriSt.mediaTypeAndEncodingType = groups.str(2);
uriSt.data = groups.str(4);
}
#else // !PULSAR_USE_BOOST_REGEX
static const std::regex expression(
R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?(\/[^?#]+)?)");
std::cmatch groups;
if (std::regex_match(uri, groups, expression)) {
static const PULSAR_REGEX_NAMESPACE::regex expression(
R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?([^?#]+)?)");
PULSAR_REGEX_NAMESPACE::cmatch groups;
if (PULSAR_REGEX_NAMESPACE::regex_match(uri, groups, expression)) {
uriSt.scheme = groups.str(1);
uriSt.mediaTypeAndEncodingType = groups.str(2);
uriSt.data = groups.str(3);
uriSt.path = groups.str(4);
}
#endif // PULSAR_USE_BOOST_REGEX
return uriSt;
}
} // namespace pulsar
12 changes: 12 additions & 0 deletions pulsar-client-cpp/tests/ZTSClientTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ TEST(ZTSClientTest, testZTSClient) {
ASSERT_EQ("/path/to/private.key", uri.path);
}

{
PrivateKeyUri uri = ZTSClientWrapper::parseUri("file:./path/to/private.key");
ASSERT_EQ("file", uri.scheme);
ASSERT_EQ("./path/to/private.key", uri.path);
}

{
PrivateKeyUri uri = ZTSClientWrapper::parseUri("file://./path/to/private.key");
ASSERT_EQ("file", uri.scheme);
ASSERT_EQ("./path/to/private.key", uri.path);
}

{
PrivateKeyUri uri = ZTSClientWrapper::parseUri("data:application/x-pem-file;base64,SGVsbG8gV29ybGQK");
ASSERT_EQ("data", uri.scheme);
Expand Down

0 comments on commit 7cce8e3

Please sign in to comment.