Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell): add a util to validate the input ip string with list_nodes #611

Merged
merged 7 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/shell/command_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

#include "command_utils.h"
Smityz marked this conversation as resolved.
Show resolved Hide resolved

#include "command_executor.h"

bool validate_ip(shell_context *sc,
const std::string &ip_str,
dsn::rpc_address &target_address,
std::string &err_info)
{
if (!target_address.from_string_ipv4(ip_str.c_str())) {
err_info = fmt::format("invalid ip:port={}, can't transform it into rpc_address", ip_str);
return false;
}

std::map<dsn::rpc_address, dsn::replication::node_status::type> nodes;
auto error = sc->ddl_client->list_nodes(dsn::replication::node_status::NS_INVALID, nodes);
if (error != dsn::ERR_OK) {
err_info = fmt::format("list nodes failed, error={}", error.to_string());
return false;
}

for (const auto &node : nodes) {
if (target_address == node.first) {
return true;
}
}

err_info = fmt::format("invalid ip:port={}, can't find it in metas", ip_str);
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
10 changes: 10 additions & 0 deletions src/shell/command_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#include "shell/argh.h"
#include <dsn/dist/fmt_logging.h>

namespace dsn {
class rpc_address;
}
class shell_context;

inline bool validate_cmd(const argh::parser &cmd,
const std::set<std::string> &params,
const std::set<std::string> &flags)
Expand Down Expand Up @@ -42,6 +47,11 @@ inline bool validate_cmd(const argh::parser &cmd,
return true;
}

bool validate_ip(shell_context *sc,
const std::string &ip_str,
/*out*/ dsn::rpc_address &target_address,
/*out*/ std::string &err_info);

#define verify_logged(exp, ...) \
do { \
if (!(exp)) { \
Expand Down
6 changes: 3 additions & 3 deletions src/shell/commands/detect_hotkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ bool detect_hotkey(command_executor *e, shell_context *sc, arguments args)
}

dsn::rpc_address target_address;
std::string err_info;
std::string ip_str = cmd({"-d", "--address"}).str();
if (!target_address.from_string_ipv4(ip_str.c_str())) {
fmt::print("invalid ip, error={}\n", ip_str);
if (!validate_ip(sc, ip_str, target_address, err_info)) {
fmt::print(stderr, "{}\n", err_info);
return false;
}

std::string err_info;
std::string hotkey_action = cmd({"-c", "--hotkey_action"}).str();
std::string hotkey_type = cmd({"-t", "--hotkey_type"}).str();
dsn::replication::detect_hotkey_request req;
Expand Down