From b77e0a5b4e026d547c83c6ac44b6d54b09ff1bde Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Sun, 5 Apr 2020 22:18:30 +0300 Subject: [PATCH] Avoid writing "HOST ANY" if the host is any by default. --- src/Access/AllowedClientHosts.h | 6 ++-- src/Parsers/ParserCreateUserQuery.cpp | 14 ++++---- src/Parsers/parseUserName.cpp | 51 +++++++++++---------------- src/Parsers/parseUserName.h | 2 +- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/Access/AllowedClientHosts.h b/src/Access/AllowedClientHosts.h index 9e89c2b92a1..2baafb2e04a 100644 --- a/src/Access/AllowedClientHosts.h +++ b/src/Access/AllowedClientHosts.h @@ -91,7 +91,7 @@ public: /// Allows IP addresses or host names using LIKE pattern. /// This pattern can contain % and _ wildcard characters. - /// For example, addLikePattern("@") will allow all addresses. + /// For example, addLikePattern("%") will allow all addresses. void addLikePattern(const String & pattern); void removeLikePattern(const String & like_pattern); const std::vector & getLikePatterns() const { return like_patterns; } @@ -298,7 +298,7 @@ inline void AllowedClientHosts::addLikePattern(const String & pattern) { if (boost::iequals(pattern, "localhost") || (pattern == "127.0.0.1") || (pattern == "::1")) local_host = true; - else if ((pattern == "@") || (pattern == "0.0.0.0/0") || (pattern == "::/0")) + else if ((pattern == "%") || (pattern == "0.0.0.0/0") || (pattern == "::/0")) any_host = true; else if (boost::range::find(like_patterns, pattern) == name_regexps.end()) like_patterns.push_back(pattern); @@ -308,7 +308,7 @@ inline void AllowedClientHosts::removeLikePattern(const String & pattern) { if (boost::iequals(pattern, "localhost") || (pattern == "127.0.0.1") || (pattern == "::1")) local_host = false; - else if ((pattern == "@") || (pattern == "0.0.0.0/0") || (pattern == "::/0")) + else if ((pattern == "%") || (pattern == "0.0.0.0/0") || (pattern == "::/0")) any_host = false; else boost::range::remove_erase(like_patterns, pattern); diff --git a/src/Parsers/ParserCreateUserQuery.cpp b/src/Parsers/ParserCreateUserQuery.cpp index acc8586fc84..7564c02da45 100644 --- a/src/Parsers/ParserCreateUserQuery.cpp +++ b/src/Parsers/ParserCreateUserQuery.cpp @@ -23,7 +23,7 @@ namespace ErrorCodes namespace { - bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name, String & new_host_pattern) + bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name, std::optional & new_host_pattern) { return IParserBase::wrapParseImpl(pos, [&] { @@ -286,12 +286,12 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec } String name; - String host_pattern; + std::optional host_pattern; if (!parseUserName(pos, expected, name, host_pattern)) return false; String new_name; - String new_host_pattern; + std::optional new_host_pattern; std::optional authentication; std::optional hosts; std::optional add_hosts; @@ -327,10 +327,10 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec if (!hosts) { - if (!alter) - hosts.emplace().addLikePattern(host_pattern); - else if (alter && !new_name.empty()) - hosts.emplace().addLikePattern(new_host_pattern); + if (!alter && host_pattern) + hosts.emplace().addLikePattern(*host_pattern); + else if (alter && new_host_pattern) + hosts.emplace().addLikePattern(*new_host_pattern); } auto query = std::make_shared(); diff --git a/src/Parsers/parseUserName.cpp b/src/Parsers/parseUserName.cpp index 9558f26cfc4..e6b91ba4af3 100644 --- a/src/Parsers/parseUserName.cpp +++ b/src/Parsers/parseUserName.cpp @@ -3,48 +3,39 @@ #include #include + namespace DB { -namespace +bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, std::optional & host_like_pattern) { - bool parseUserNameImpl(IParser::Pos & pos, Expected & expected, String & user_name, String * host_like_pattern) + String name; + if (!parseIdentifierOrStringLiteral(pos, expected, name)) + return false; + + boost::algorithm::trim(name); + + std::optional pattern; + if (ParserToken{TokenType::At}.ignore(pos, expected)) { - String name; - if (!parseIdentifierOrStringLiteral(pos, expected, name)) + if (!parseIdentifierOrStringLiteral(pos, expected, pattern.emplace())) return false; - boost::algorithm::trim(name); - - String pattern = "%"; - - if (ParserToken{TokenType::At}.ignore(pos, expected)) - { - if (!parseIdentifierOrStringLiteral(pos, expected, pattern)) - return false; - - boost::algorithm::trim(pattern); - } - - if (pattern != "%") - name += '@' + pattern; - - user_name = std::move(name); - if (host_like_pattern) - *host_like_pattern = std::move(pattern); - return true; + boost::algorithm::trim(*pattern); } + + if (pattern && (pattern != "%")) + name += '@' + *pattern; + + user_name = std::move(name); + host_like_pattern = std::move(pattern); + return true; } bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name) { - return parseUserNameImpl(pos, expected, user_name, nullptr); -} - - -bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, String & host_like_pattern) -{ - return parseUserNameImpl(pos, expected, user_name, &host_like_pattern); + std::optional unused_pattern; + return parseUserName(pos, expected, user_name, unused_pattern); } diff --git a/src/Parsers/parseUserName.h b/src/Parsers/parseUserName.h index c3556f4dc59..641aa09d1f3 100644 --- a/src/Parsers/parseUserName.h +++ b/src/Parsers/parseUserName.h @@ -10,7 +10,7 @@ namespace DB /// The `host` can be an ip address, ip subnet, or a host name. /// The % and _ wildcard characters are permitted in `host`. /// These have the same meaning as for pattern-matching operations performed with the LIKE operator. -bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, String & host_like_pattern); +bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, std::optional & host_like_pattern); bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name); /// Parses either a user name or the 'CURRENT_USER' keyword (or some of the aliases).