forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-43677: [C++][FlightRPC] Move the FlightTestServer to its own…
… .cc and .h files (apache#43678) ### Rationale for this change One way of learning about a codebase is reading the tests. As it is now, it's hard to see the minimal `FlightServerBase` sub-class in `flight/test_util.cc`, so I moved it to its own file. ### What changes are included in this PR? - Renaming `FlightTestServer` to `TestFlightServer` - Moving the class to `test_flight_server.{h,cc}` - Bonus: Moving the server and client auth handlers to `test_auth_handlers.{h,cc}` ### Are these changes tested? By existing tests. ### Are there any user-facing changes? `ExampleTestServer` is removed from the testing library in favor of `FlightTestServer::Make`. * GitHub Issue: apache#43677 Authored-by: Felipe Oliveira Carvalho <[email protected]> Signed-off-by: Felipe Oliveira Carvalho <[email protected]>
- Loading branch information
Showing
11 changed files
with
759 additions
and
560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// 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 <string> | ||
|
||
#include "arrow/flight/client_auth.h" | ||
#include "arrow/flight/server.h" | ||
#include "arrow/flight/server_auth.h" | ||
#include "arrow/flight/test_auth_handlers.h" | ||
#include "arrow/flight/types.h" | ||
#include "arrow/flight/visibility.h" | ||
#include "arrow/status.h" | ||
|
||
namespace arrow::flight { | ||
|
||
// TestServerAuthHandler | ||
|
||
TestServerAuthHandler::TestServerAuthHandler(const std::string& username, | ||
const std::string& password) | ||
: username_(username), password_(password) {} | ||
|
||
TestServerAuthHandler::~TestServerAuthHandler() {} | ||
|
||
Status TestServerAuthHandler::Authenticate(const ServerCallContext& context, | ||
ServerAuthSender* outgoing, | ||
ServerAuthReader* incoming) { | ||
std::string token; | ||
RETURN_NOT_OK(incoming->Read(&token)); | ||
if (token != password_) { | ||
return MakeFlightError(FlightStatusCode::Unauthenticated, "Invalid token"); | ||
} | ||
RETURN_NOT_OK(outgoing->Write(username_)); | ||
return Status::OK(); | ||
} | ||
|
||
Status TestServerAuthHandler::IsValid(const ServerCallContext& context, | ||
const std::string& token, | ||
std::string* peer_identity) { | ||
if (token != password_) { | ||
return MakeFlightError(FlightStatusCode::Unauthenticated, "Invalid token"); | ||
} | ||
*peer_identity = username_; | ||
return Status::OK(); | ||
} | ||
|
||
// TestServerBasicAuthHandler | ||
|
||
TestServerBasicAuthHandler::TestServerBasicAuthHandler(const std::string& username, | ||
const std::string& password) { | ||
basic_auth_.username = username; | ||
basic_auth_.password = password; | ||
} | ||
|
||
TestServerBasicAuthHandler::~TestServerBasicAuthHandler() {} | ||
|
||
Status TestServerBasicAuthHandler::Authenticate(const ServerCallContext& context, | ||
ServerAuthSender* outgoing, | ||
ServerAuthReader* incoming) { | ||
std::string token; | ||
RETURN_NOT_OK(incoming->Read(&token)); | ||
ARROW_ASSIGN_OR_RAISE(BasicAuth incoming_auth, BasicAuth::Deserialize(token)); | ||
if (incoming_auth.username != basic_auth_.username || | ||
incoming_auth.password != basic_auth_.password) { | ||
return MakeFlightError(FlightStatusCode::Unauthenticated, "Invalid token"); | ||
} | ||
RETURN_NOT_OK(outgoing->Write(basic_auth_.username)); | ||
return Status::OK(); | ||
} | ||
|
||
Status TestServerBasicAuthHandler::IsValid(const ServerCallContext& context, | ||
const std::string& token, | ||
std::string* peer_identity) { | ||
if (token != basic_auth_.username) { | ||
return MakeFlightError(FlightStatusCode::Unauthenticated, "Invalid token"); | ||
} | ||
*peer_identity = basic_auth_.username; | ||
return Status::OK(); | ||
} | ||
|
||
// TestClientAuthHandler | ||
|
||
TestClientAuthHandler::TestClientAuthHandler(const std::string& username, | ||
const std::string& password) | ||
: username_(username), password_(password) {} | ||
|
||
TestClientAuthHandler::~TestClientAuthHandler() {} | ||
|
||
Status TestClientAuthHandler::Authenticate(ClientAuthSender* outgoing, | ||
ClientAuthReader* incoming) { | ||
RETURN_NOT_OK(outgoing->Write(password_)); | ||
std::string username; | ||
RETURN_NOT_OK(incoming->Read(&username)); | ||
if (username != username_) { | ||
return MakeFlightError(FlightStatusCode::Unauthenticated, "Invalid token"); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
Status TestClientAuthHandler::GetToken(std::string* token) { | ||
*token = password_; | ||
return Status::OK(); | ||
} | ||
|
||
// TestClientBasicAuthHandler | ||
|
||
TestClientBasicAuthHandler::TestClientBasicAuthHandler(const std::string& username, | ||
const std::string& password) { | ||
basic_auth_.username = username; | ||
basic_auth_.password = password; | ||
} | ||
|
||
TestClientBasicAuthHandler::~TestClientBasicAuthHandler() {} | ||
|
||
Status TestClientBasicAuthHandler::Authenticate(ClientAuthSender* outgoing, | ||
ClientAuthReader* incoming) { | ||
ARROW_ASSIGN_OR_RAISE(std::string pb_result, basic_auth_.SerializeToString()); | ||
RETURN_NOT_OK(outgoing->Write(pb_result)); | ||
RETURN_NOT_OK(incoming->Read(&token_)); | ||
return Status::OK(); | ||
} | ||
|
||
Status TestClientBasicAuthHandler::GetToken(std::string* token) { | ||
*token = token_; | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace arrow::flight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 <string> | ||
|
||
#include "arrow/flight/client_auth.h" | ||
#include "arrow/flight/server.h" | ||
#include "arrow/flight/server_auth.h" | ||
#include "arrow/flight/types.h" | ||
#include "arrow/flight/visibility.h" | ||
#include "arrow/status.h" | ||
|
||
// A pair of authentication handlers that check for a predefined password | ||
// and set the peer identity to a predefined username. | ||
|
||
namespace arrow::flight { | ||
|
||
class ARROW_FLIGHT_EXPORT TestServerAuthHandler : public ServerAuthHandler { | ||
public: | ||
explicit TestServerAuthHandler(const std::string& username, | ||
const std::string& password); | ||
~TestServerAuthHandler() override; | ||
Status Authenticate(const ServerCallContext& context, ServerAuthSender* outgoing, | ||
ServerAuthReader* incoming) override; | ||
Status IsValid(const ServerCallContext& context, const std::string& token, | ||
std::string* peer_identity) override; | ||
|
||
private: | ||
std::string username_; | ||
std::string password_; | ||
}; | ||
|
||
class ARROW_FLIGHT_EXPORT TestServerBasicAuthHandler : public ServerAuthHandler { | ||
public: | ||
explicit TestServerBasicAuthHandler(const std::string& username, | ||
const std::string& password); | ||
~TestServerBasicAuthHandler() override; | ||
Status Authenticate(const ServerCallContext& context, ServerAuthSender* outgoing, | ||
ServerAuthReader* incoming) override; | ||
Status IsValid(const ServerCallContext& context, const std::string& token, | ||
std::string* peer_identity) override; | ||
|
||
private: | ||
BasicAuth basic_auth_; | ||
}; | ||
|
||
class ARROW_FLIGHT_EXPORT TestClientAuthHandler : public ClientAuthHandler { | ||
public: | ||
explicit TestClientAuthHandler(const std::string& username, | ||
const std::string& password); | ||
~TestClientAuthHandler() override; | ||
Status Authenticate(ClientAuthSender* outgoing, ClientAuthReader* incoming) override; | ||
Status GetToken(std::string* token) override; | ||
|
||
private: | ||
std::string username_; | ||
std::string password_; | ||
}; | ||
|
||
class ARROW_FLIGHT_EXPORT TestClientBasicAuthHandler : public ClientAuthHandler { | ||
public: | ||
explicit TestClientBasicAuthHandler(const std::string& username, | ||
const std::string& password); | ||
~TestClientBasicAuthHandler() override; | ||
Status Authenticate(ClientAuthSender* outgoing, ClientAuthReader* incoming) override; | ||
Status GetToken(std::string* token) override; | ||
|
||
private: | ||
BasicAuth basic_auth_; | ||
std::string token_; | ||
}; | ||
|
||
} // namespace arrow::flight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.