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

refactor(security): close the connection when something is wrong with sever_negotiation #606

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 2 additions & 8 deletions src/runtime/security/client_negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void client_negotiation::handle_response(error_code err, const negotiation_respo

switch (_status) {
case negotiation_status::type::SASL_LIST_MECHANISMS:
recv_mechanisms(response);
on_recv_mechanisms(response);
break;
case negotiation_status::type::SASL_SELECT_MECHANISMS:
// TBD(zlw)
Expand All @@ -77,7 +77,7 @@ void client_negotiation::handle_response(error_code err, const negotiation_respo
}
}

void client_negotiation::recv_mechanisms(const negotiation_response &resp)
void client_negotiation::on_recv_mechanisms(const negotiation_response &resp)
{
if (resp.status != negotiation_status::type::SASL_LIST_MECHANISMS_RESP) {
dwarn_f("{}: get message({}) while expect({})",
Expand Down Expand Up @@ -129,12 +129,6 @@ void client_negotiation::send(std::unique_ptr<negotiation_request> request)
});
}

void client_negotiation::fail_negotiation()
{
_status = negotiation_status::type::SASL_AUTH_FAIL;
_session->on_failure(true);
}

void client_negotiation::succ_negotiation()
{
_status = negotiation_status::type::SASL_SUCC;
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/security/client_negotiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class client_negotiation : public negotiation

private:
void handle_response(error_code err, const negotiation_response &&response);
void on_recv_mechanisms(const negotiation_response &resp);

void list_mechanisms();
void recv_mechanisms(const negotiation_response &resp);
void select_mechanism(const std::string &mechanism);
void send(std::unique_ptr<negotiation_request> request);
void fail_negotiation();
void succ_negotiation();
};

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/security/negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ std::unique_ptr<negotiation> create_negotiation(bool is_client, rpc_session *ses
}
}

void negotiation::fail_negotiation()
{
_status = negotiation_status::type::SASL_AUTH_FAIL;
_session->on_failure(true);
}

} // namespace security
} // namespace dsn
1 change: 1 addition & 0 deletions src/runtime/security/negotiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class negotiation

virtual void start() = 0;
bool negotiation_succeed() const { return _status == negotiation_status::type::SASL_SUCC; }
void fail_negotiation();

protected:
// The ownership of the negotiation instance is held by rpc_session.
Expand Down
17 changes: 5 additions & 12 deletions src/runtime/security/server_negotiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void server_negotiation::handle_request(negotiation_rpc rpc)
// TBD(zlw)
break;
default:
fail_negotiation(rpc, "wrong status");
fail_negotiation();
}
}

Expand All @@ -66,7 +66,7 @@ void server_negotiation::on_list_mechanisms(negotiation_rpc rpc)
_name,
enum_to_string(rpc.request().status),
enum_to_string(negotiation_status::type::SASL_LIST_MECHANISMS));
fail_negotiation(rpc, "invalid_client_message_status");
fail_negotiation();
}
return;
}
Expand All @@ -80,7 +80,7 @@ void server_negotiation::on_select_mechanism(negotiation_rpc rpc)
std::string error_msg =
fmt::format("the mechanism of {} is not supported", _selected_mechanism);
dwarn_f("{}", error_msg);
fail_negotiation(rpc, error_msg);
fail_negotiation();
return;
}

Expand All @@ -90,7 +90,7 @@ void server_negotiation::on_select_mechanism(negotiation_rpc rpc)
_name,
err_s.code().to_string(),
err_s.description());
fail_negotiation(rpc, err_s.description());
fail_negotiation();
return;
}

Expand All @@ -101,7 +101,7 @@ void server_negotiation::on_select_mechanism(negotiation_rpc rpc)
_name,
enum_to_string(request.status),
negotiation_status::type::SASL_SELECT_MECHANISMS);
fail_negotiation(rpc, "invalid_client_message_status");
fail_negotiation();
return;
}
}
Expand All @@ -112,12 +112,5 @@ error_s server_negotiation::do_sasl_server_init()
return error_s::make(ERR_OK);
}

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In previous implementation, there will be error reason. Is it totally useless?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only used for sending a message to client to show why it is failed. I think it is useless

}

} // namespace security
} // namespace dsn
2 changes: 1 addition & 1 deletion src/runtime/security/server_negotiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class server_negotiation : public negotiation
private:
void on_list_mechanisms(negotiation_rpc rpc);
void on_select_mechanism(negotiation_rpc rpc);

error_s do_sasl_server_init();
void fail_negotiation(negotiation_rpc rpc, const std::string &reason);
};

} // namespace security
Expand Down