Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat(security): handle SASL_LIST_MECHANISMS by server_negotiation #588

Merged
merged 34 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0078e42
refactor: handle list mechanism
levy5307 Aug 11, 2020
299fdc4
fix
levy5307 Aug 11, 2020
43c248f
refactor
levy5307 Aug 11, 2020
e706de9
fix
levy5307 Aug 11, 2020
42c31ab
fix
levy5307 Aug 11, 2020
98ea35b
refactor
levy5307 Aug 11, 2020
6c7550c
fix
levy5307 Aug 11, 2020
e0a9e8f
refactor
levy5307 Aug 11, 2020
27e075b
refactor
levy5307 Aug 11, 2020
7cc5613
fix by review
levy5307 Aug 12, 2020
6d72531
refactor
levy5307 Aug 12, 2020
a35f7e1
Merge branch 'master' into handle-list-mechanism
levy5307 Aug 13, 2020
125e6ac
fix
levy5307 Aug 13, 2020
0b18f47
Merge branch 'handle-list-mechanism' of github.com:levy5307/rdsn into…
levy5307 Aug 13, 2020
9780183
fix
levy5307 Aug 13, 2020
b333d68
fix by review
levy5307 Aug 13, 2020
e1168f8
fix by review
levy5307 Aug 13, 2020
db64bc5
fix
levy5307 Aug 13, 2020
8f61406
fix
levy5307 Aug 14, 2020
c354138
Merge branch 'handle-list-mechanism' of github.com:levy5307/rdsn into…
levy5307 Aug 14, 2020
f6c8854
fix
levy5307 Aug 14, 2020
9640e9d
fix
levy5307 Aug 14, 2020
6c27335
fix
levy5307 Aug 14, 2020
b4d515e
Merge branch 'master' into handle-list-mechanism
levy5307 Aug 14, 2020
b460bcb
fix
levy5307 Aug 14, 2020
d3ac6b0
Merge branch 'handle-list-mechanism' of github.com:levy5307/rdsn into…
levy5307 Aug 14, 2020
b524c30
fix
levy5307 Aug 14, 2020
7a6fc8f
fix
levy5307 Aug 18, 2020
b38b02e
Merge branch 'master' into handle-list-mechanism
Aug 18, 2020
e9c329d
fix
levy5307 Aug 18, 2020
555f1d9
fix
levy5307 Aug 18, 2020
885c2f5
fix
levy5307 Aug 18, 2020
31c96bc
fix
levy5307 Aug 18, 2020
a0f994c
fix
levy5307 Aug 18, 2020
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
3 changes: 3 additions & 0 deletions include/dsn/tool-api/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class rpc_session : public ref_counter

/// for negotiation
void start_negotiation();
security::negotiation *get_negotiation() const;

public:
///
Expand Down Expand Up @@ -300,6 +301,8 @@ class rpc_session : public ref_counter

void clear_send_queue(bool resend_msgs);
bool on_disconnected(bool is_write);
void on_failure(bool is_write = false);
void on_success();

protected:
// constant info
Expand Down
4 changes: 2 additions & 2 deletions include/dsn/utility/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ char *trim_string(char *s);

// calculate the md5 checksum of buffer
std::string string_md5(const char *buffer, unsigned int length);
}
}
} // namespace utils
} // namespace dsn
10 changes: 0 additions & 10 deletions src/runtime/rpc/asio_rpc_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,6 @@ asio_rpc_session::asio_rpc_session(asio_network_provider &net,
set_options();
}

void asio_rpc_session::on_failure(bool is_write)
{
if (on_disconnected(is_write)) {
close();
}
}

void asio_rpc_session::close()
{
utils::auto_write_lock socket_guard(_socket_lock);
Expand Down Expand Up @@ -202,9 +195,6 @@ void asio_rpc_session::connect()

// start auth negotiation when client is connecting to server
start_negotiation();

set_connected();
on_send_completed();
start_read_next();
} else {
derror("client session connect to %s failed, error = %s",
Expand Down
1 change: 0 additions & 1 deletion src/runtime/rpc/asio_rpc_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class asio_rpc_session : public rpc_session

private:
void do_read(int read_next) override;
void on_failure(bool is_write = false);
void set_options();
void on_message_read(message_ex *msg)
{
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/rpc/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,21 @@ bool rpc_session::on_disconnected(bool is_write)
return ret;
}

void rpc_session::on_failure(bool is_write)
{
if (on_disconnected(is_write)) {
close();
}
}

void rpc_session::on_success()
{
if (is_client()) {
set_connected();
on_send_completed();
}
}

bool rpc_session::on_recv_message(message_ex *msg, int delay_ms)
{
if (msg->header->from_address.is_invalid())
Expand Down Expand Up @@ -442,6 +457,9 @@ void rpc_session::start_negotiation()
}

auth_negotiation();
} else {
// set negotiation success if auth is disabled
on_success();
}
}

Expand All @@ -451,6 +469,8 @@ void rpc_session::auth_negotiation()
_negotiation->start();
}

security::negotiation *rpc_session::get_negotiation() const { return _negotiation.get(); }

////////////////////////////////////////////////////////////////////////////////////////////////
network::network(rpc_engine *srv, network *inner_provider)
: _engine(srv), _client_hdr_format(NET_HDR_DSN), _unknown_msg_header_format(NET_HDR_INVALID)
Expand Down
22 changes: 15 additions & 7 deletions src/runtime/security/client_negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "negotiation_utils.h"

#include <dsn/dist/fmt_logging.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/utility/smart_pointers.h>

namespace dsn {
namespace security {
Expand All @@ -34,18 +36,24 @@ void client_negotiation::start()
list_mechanisms();
}

void client_negotiation::handle_response(error_code err, const negotiation_response &&response)
{
// TBD(zlw)
}

void client_negotiation::list_mechanisms()
{
negotiation_request request;
_status = request.status = negotiation_status::type::SASL_LIST_MECHANISMS;
send(request);
auto request = dsn::make_unique<negotiation_request>();
_status = request->status = negotiation_status::type::SASL_LIST_MECHANISMS;
send(std::move(request));
}

void client_negotiation::send(const negotiation_request &request)
void client_negotiation::send(std::unique_ptr<negotiation_request> request)
{
message_ptr req = message_ex::create_request(RPC_NEGOTIATION);
dsn::marshall(req, request);
_session->send_message(req);
negotiation_rpc rpc(std::move(request), RPC_NEGOTIATION);
rpc.call(_session->remote_address(), nullptr, [this, rpc](error_code err) mutable {
handle_response(err, std::move(rpc.response()));
});
}

} // namespace security
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/security/client_negotiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class client_negotiation : public negotiation
void start();

private:
void handle_response(error_code err, const negotiation_response &&response);
void list_mechanisms();
void send(const negotiation_request &request);
void send(std::unique_ptr<negotiation_request> request);
};

} // namespace security
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/security/negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

namespace dsn {
namespace security {

/// TODO(zlw):we can't get string list from cflags now,
/// so we should get supported mechanisms from config in the later
const std::set<std::string> supported_mechanisms{"GSSAPI"};
DSN_DEFINE_bool("security", enable_auth, false, "whether open auth or not");

negotiation::~negotiation() {}
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/security/negotiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
#pragma once

#include "security_types.h"

#include <memory>
#include <dsn/cpp/rpc_holder.h>

namespace dsn {
class rpc_session;

namespace security {
typedef rpc_holder<negotiation_request, negotiation_response> negotiation_rpc;

levy5307 marked this conversation as resolved.
Show resolved Hide resolved
class negotiation
{
Expand Down
51 changes: 51 additions & 0 deletions src/runtime/security/negotiation_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 "negotiation_service.h"
#include "negotiation_utils.h"
#include "server_negotiation.h"

namespace dsn {
namespace security {
extern bool FLAGS_enable_auth;

negotiation_service::negotiation_service() : serverlet("negotiation_service") {}

void negotiation_service::open_service()
{
register_rpc_handler_with_rpc_holder(
RPC_NEGOTIATION, "Negotiation", &negotiation_service::on_negotiation_request);
}

void negotiation_service::on_negotiation_request(negotiation_rpc rpc)
{
dassert(!rpc.dsn_request()->io_session->is_client(),
"only server session receive negotiation request");

// reply SASL_AUTH_DISABLE if auth is not enable
if (!security::FLAGS_enable_auth) {
acelyc111 marked this conversation as resolved.
Show resolved Hide resolved
rpc.response().status = negotiation_status::type::SASL_AUTH_DISABLE;
return;
}

server_negotiation *srv_negotiation =
static_cast<server_negotiation *>(rpc.dsn_request()->io_session->get_negotiation());
srv_negotiation->handle_request(rpc);
}

} // namespace security
} // namespace dsn
40 changes: 40 additions & 0 deletions src/runtime/security/negotiation_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.

#pragma once

#include "server_negotiation.h"

#include <dsn/cpp/serverlet.h>

namespace dsn {
namespace security {

class negotiation_service : public serverlet<negotiation_service>,
public utils::singleton<negotiation_service>
{
public:
void open_service();

private:
negotiation_service();
void on_negotiation_request(negotiation_rpc rpc);
friend class utils::singleton<negotiation_service>;
neverchanje marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace security
} // namespace dsn
32 changes: 31 additions & 1 deletion src/runtime/security/negotiation_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,40 @@

#pragma once

#include "security_types.h"

namespace dsn {
namespace security {
inline const char *enum_to_string(negotiation_status::type s)
{
switch (s) {
case negotiation_status::type::SASL_LIST_MECHANISMS:
return "negotiation_list_mechanisms";
case negotiation_status::type::SASL_LIST_MECHANISMS_RESP:
return "negotiation_list_mechanisms_resp";
case negotiation_status::type::SASL_SELECT_MECHANISMS:
return "negotiation_select_mechanisms";
case negotiation_status::type::SASL_SELECT_MECHANISMS_OK:
return "negotiation_select_mechanisms_ok";
case negotiation_status::type::SASL_SUCC:
return "negotiation_succ";
case negotiation_status::type::SASL_AUTH_FAIL:
return "negotiation_auth_fail";
case negotiation_status::type::SASL_INITIATE:
return "negotiation_initiate";
case negotiation_status::type::SASL_CHALLENGE:
return "negotiation_challenge";
case negotiation_status::type::SASL_CHALLENGE_RESP:
return "negotiation_challenge_response";
case negotiation_status::type::SASL_AUTH_DISABLE:
return "negotiation_auth_disable";
case negotiation_status::type::INVALID:
return "negotiation_invalid";
default:
return "negotiation-unknown";
}
}

DEFINE_TASK_CODE_RPC(RPC_NEGOTIATION, TASK_PRIORITY_COMMON, dsn::THREAD_POOL_DEFAULT)

} // namespace security
} // namespace dsn
2 changes: 1 addition & 1 deletion src/runtime/security/security.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum negotiation_status {
SASL_SELECT_MECHANISMS_OK
SASL_INITIATE
SASL_CHALLENGE
SASL_CHANLLENGE_RESP
SASL_CHALLENGE_RESP
SASL_SUCC
SASL_AUTH_DISABLE
SASL_AUTH_FAIL
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/security/security_types.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/runtime/security/security_types.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/runtime/security/server_negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
// under the License.

#include "server_negotiation.h"
#include "negotiation_utils.h"

#include <boost/algorithm/string/join.hpp>
#include <dsn/utility/strings.h>
#include <dsn/dist/fmt_logging.h>

namespace dsn {
Expand All @@ -33,5 +36,37 @@ void server_negotiation::start()
ddebug_f("{}: start negotiation", _name);
}

void server_negotiation::handle_request(negotiation_rpc rpc)
{
if (_status == negotiation_status::type::SASL_LIST_MECHANISMS) {
on_list_mechanisms(rpc);
return;
}
}

void server_negotiation::on_list_mechanisms(negotiation_rpc rpc)
{
if (rpc.request().status == negotiation_status::type::SASL_LIST_MECHANISMS) {
std::string mech_list = boost::join(supported_mechanisms, ",");
negotiation_response &response = rpc.response();
_status = response.status = negotiation_status::type::SASL_LIST_MECHANISMS_RESP;
response.msg = std::move(mech_list);
} else {
ddebug_f("{}: got message({}) while expect({})",
_name,
enum_to_string(rpc.request().status),
enum_to_string(negotiation_status::type::SASL_LIST_MECHANISMS));
fail_negotiation(rpc, "invalid_client_message_status");
}
return;
}

void server_negotiation::fail_negotiation(negotiation_rpc rpc, const std::string &reason)
{
negotiation_response &response = rpc.response();
_status = response.status = negotiation_status::type::SASL_AUTH_FAIL;
response.msg = reason;
}

} // namespace security
} // namespace dsn
Loading