Other: implemented tokens in bridge-gui-tester.

This commit is contained in:
Xavier Michelon
2022-10-10 13:19:51 +02:00
parent 6b1d689621
commit 9a3900114b
13 changed files with 287 additions and 29 deletions

View File

@ -65,8 +65,10 @@ endif()
add_executable(bridge-gui-tester
AppController.cpp AppController.h
Cert.cpp Cert.h
main.cpp
MainWindow.cpp MainWindow.h
GRPCMetaDataProcessor.cpp GRPCMetaDataProcessor.h
GRPCQtProxy.cpp GRPCQtProxy.h
GRPCService.cpp GRPCService.h
GRPCServerWorker.cpp GRPCServerWorker.h

View File

@ -0,0 +1,74 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#include "Cert.h"
// The following test TLS certificate and key are self-signed, and valid until October 2042.
QString const testTLSCert = R"(-----BEGIN CERTIFICATE-----
MIIDkzCCAnugAwIBAgIRANAfjBkSRx8LY96XwVNGT4MwDQYJKoZIhvcNAQELBQAw
SzELMAkGA1UEBhMCQ0gxEjAQBgNVBAoTCVByb3RvbiBBRzEUMBIGA1UECxMLUHJv
dG9uIE1haWwxEjAQBgNVBAMTCTEyNy4wLjAuMTAeFw0yMjEwMTAwNzI1MjFaFw00
MjEwMDUwNzI1MjFaMEsxCzAJBgNVBAYTAkNIMRIwEAYDVQQKEwlQcm90b24gQUcx
FDASBgNVBAsTC1Byb3RvbiBNYWlsMRIwEAYDVQQDEwkxMjcuMC4wLjEwggEiMA0G
CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC3jJiMtwQMzsASB51LwWTd9TIXq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Uf3WC1W3kZuo5xlDnRgV/ECgYEAnvZLZrYZ5I2nAWm3XVarHIX7Iz9f5y/5NVos
vjVI30/MXksav8xCAZnqq4OcuNFOZVOPrbjPCMGnu9MR91/qgtvdG6Y5lfsyCtMB
z/DgXuFOqd6A0SySyZtzP52hnUvlgijyshSXB1tslvSMxL9joFTs/Xb6dU3Opm/P
FNJOtkUCgYEAjxBLJt2cbE7LjAM2GA5txILUFJBh6Q2U7M922rzr+C7p9oUlx00Z
ZD7atctAQGMqhfBTL1Lxu2gJ3rr5NGVCFDUv1SaYC68nuRkbRD6mHbB8IGpUcmCi
YBADKClADQt7+Nn+ufSsypTpAo9X2gMGQqplSQlmPgoP8wCGbycC5iQ=
-----END RSA PRIVATE KEY-----)";

View File

@ -0,0 +1,27 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#ifndef BRIDGE_GUI_TESTER_CERT_H
#define BRIDGE_GUI_TESTER_CERT_H
extern QString const testTLSCert; ///< The test TLS Cert used by the app, in PEM format.
extern QString const testTLSKey; ///< The test TLS Key used by the app, in PEM format.
#endif //BRIDGE_GUI_TESTER_CERT_H

View File

@ -0,0 +1,81 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#include "GRPCMetaDataProcessor.h"
#include <bridgepp/GRPC/GRPCUtils.h>
#include <bridgepp/Exception/Exception.h>
using namespace bridgepp;
using namespace grpc;
//****************************************************************************************************************************************************
/// \param[in] The server token expected from gRPC calls
//****************************************************************************************************************************************************
GRPCMetadataProcessor::GRPCMetadataProcessor(QString const &serverToken)
: serverToken_(serverToken.toStdString())
{
}
//****************************************************************************************************************************************************
/// \return false.
//****************************************************************************************************************************************************
bool GRPCMetadataProcessor::IsBlocking() const
{
return false;
}
//****************************************************************************************************************************************************
/// \param[in] inputMeta
/// \return the result of the metadata processing.
//****************************************************************************************************************************************************
Status GRPCMetadataProcessor::Process(AuthMetadataProcessor::InputMetadata const &auth_metadata, AuthContext *,
AuthMetadataProcessor::OutputMetadata *, AuthMetadataProcessor::OutputMetadata *)
{
try
{
AuthMetadataProcessor::InputMetadata::const_iterator pathIt = auth_metadata.find(":path");
QString const callName = (pathIt == auth_metadata.end()) ? ("unkown gRPC call") : QString::fromLocal8Bit(pathIt->second);
AuthMetadataProcessor::InputMetadata::size_type const count = auth_metadata.count(grpcMetadataServerTokenKey);
if (count == 0)
throw Exception(QString("Missing server token in gRPC client call '%1'.").arg(callName));
if (count > 1)
throw Exception(QString("Several server tokens were provided in gRPC client call '%1'.").arg(callName));
if (auth_metadata.find(grpcMetadataServerTokenKey)->second != serverToken_)
throw Exception(QString("Invalid server token provided by gRPC client call '%1'.").arg(callName));
app().log().trace(QString("Server token for gRPC call '%1' was validated.").arg(callName));
return Status::OK;
}
catch (Exception const &e)
{
app().log().error(e.qwhat());
return Status(StatusCode::UNAUTHENTICATED, e.qwhat().toStdString());
}
}

View File

@ -0,0 +1,50 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#ifndef BRIDGE_GUI_TESTER_GRPC_METADATA_PROCESSOR_H
#define BRIDGE_GUI_TESTER_GRPC_METADATA_PROCESSOR_H
#include <grpc++/grpc++.h>
//****************************************************************************************************************************************************
/// \brief Metadata processor class in charge of checking the server token provided by client calls and stream requests.
//****************************************************************************************************************************************************
class GRPCMetadataProcessor: public grpc::AuthMetadataProcessor
{
public: // member functions.
GRPCMetadataProcessor(QString const &serverToken); ///< Default constructor.
GRPCMetadataProcessor(GRPCMetadataProcessor const&) = delete; ///< Disabled copy-constructor.
GRPCMetadataProcessor(GRPCMetadataProcessor&&) = delete; ///< Disabled assignment copy-constructor.
~GRPCMetadataProcessor() = default; ///< Destructor.
GRPCMetadataProcessor& operator=(GRPCMetadataProcessor const&) = delete; ///< Disabled assignment operator.
GRPCMetadataProcessor& operator=(GRPCMetadataProcessor&&) = delete; ///< Disabled move assignment operator.
bool IsBlocking() const override; ///< Is the processor blocking?
grpc::Status Process(InputMetadata const &auth_metadata, grpc::AuthContext *context, OutputMetadata *consumed_auth_metadata,
OutputMetadata *response_metadata) override; ///< Process the metadata
private:
std::string serverToken_; ///< The server token, as a std::string.
};
typedef std::shared_ptr<GRPCMetadataProcessor> SPMetadataProcessor; ///< Type definition for shared pointer to MetadataProcessor.
#endif //BRIDGE_GUI_TESTER_GRPC_METADATA_PROCESSOR_H

View File

@ -17,31 +17,17 @@
#include "GRPCServerWorker.h"
#include "Cert.h"
#include "GRPCService.h"
#include <bridgepp/Exception/Exception.h>
#include <bridgepp/GRPC/GRPCUtils.h>
#include <bridgepp/GRPC/GRPCConfig.h>
using namespace bridgepp;
using namespace grpc;
namespace
{
//****************************************************************************************************************************************************
/// \return The content of the file.
//****************************************************************************************************************************************************
QString loadAsciiTextFile(QString const &path) {
QFile file(path);
return file.open(QIODevice::ReadOnly | QIODevice::Text) ? QString::fromLocal8Bit(file.readAll()) : QString();
}
}
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
@ -61,30 +47,34 @@ void GRPCServerWorker::run()
{
emit started();
QString cert = loadAsciiTextFile(serverCertificatePath());
if (cert.isEmpty())
throw Exception("Could not locate server certificate. Make sure to launch bridge once before starting this application");
QString key = loadAsciiTextFile(serverKeyPath());
if (key.isEmpty())
throw Exception("Could not locate server key. Make sure to launch bridge once before starting this application");
SslServerCredentialsOptions::PemKeyCertPair pair;
pair.private_key = key.toStdString();
pair.cert_chain = cert.toStdString();
pair.private_key = testTLSKey.toStdString();
pair.cert_chain = testTLSCert.toStdString();
SslServerCredentialsOptions ssl_opts;
ssl_opts.pem_root_certs="";
ssl_opts.pem_key_cert_pairs.push_back(pair);
std::shared_ptr<ServerCredentials> credentials = grpc::SslServerCredentials(ssl_opts);
GRPCConfig config;
config.cert = testTLSCert;
config.token = QUuid::createUuid().toString();
processor_ = std::make_shared<GRPCMetadataProcessor>(config.token);
credentials->SetAuthMetadataProcessor(processor_); // gRPC interceptors are still experimental in C++, so we use AuthMetadataProcessor
ServerBuilder builder;
builder.AddListeningPort("127.0.0.1:9292", credentials);
int port = 0; // Port will not be known until ServerBuilder::BuildAndStart() is called
builder.AddListeningPort("127.0.0.1:0", credentials, &port);
builder.RegisterService(&app().grpc());
server_ = builder.BuildAndStart();
if (!server_)
throw Exception("Could not create gRPC server.");
app().log().debug("gRPC Server started.");
config.port = port;
QString err;
if (!config.save(grpcServerConfigPath(), &err))
throw Exception(QString("Could not save gRPC server config. %1").arg(err));
server_->Wait();
emit finished();
app().log().debug("gRPC Server exited.");
@ -107,3 +97,5 @@ void GRPCServerWorker::stop()
if (server_)
server_->Shutdown();
}

View File

@ -21,6 +21,7 @@
#include <bridgepp/Worker/Worker.h>
#include "GRPCMetaDataProcessor.h"
#include "GRPCService.h"
#include <grpcpp/grpcpp.h>
@ -44,6 +45,7 @@ public: // member functions.
private: // data members
std::unique_ptr<grpc::Server> server_ { nullptr }; ///< The gRPC server.
SPMetadataProcessor processor_;
};

View File

@ -20,6 +20,7 @@
#include "MainWindow.h"
#include <bridgepp/BridgeUtils.h>
#include <bridgepp/GRPC/EventFactory.h>
#include <bridgepp/GRPC/GRPCConfig.h>
using namespace grpc;
@ -55,6 +56,28 @@ bool GRPCService::isStreaming() const
}
//****************************************************************************************************************************************************
/// \param[in] request The request.
/// \param[out] response The response.
//****************************************************************************************************************************************************
Status GRPCService::CheckTokens(::grpc::ServerContext *, ::google::protobuf::StringValue const *request, ::google::protobuf::StringValue *response)
{
Log& log = app().log();
log.debug(__FUNCTION__);
GRPCConfig config;
QString error;
if (!config.load(QString::fromStdString(request->value()), &error))
{
QString const err = "Could not load gRPC client config";
log.error(err);
return grpc::Status(StatusCode::UNAUTHENTICATED, err.toStdString());
}
response->set_value(config.token.toStdString());
return grpc::Status::OK;
}
//****************************************************************************************************************************************************
/// \param[in] request the request.
/// \return The status for the call.

View File

@ -40,7 +40,7 @@ public: // member functions.
GRPCService &operator=(GRPCService &&) = delete; ///< Disabled move assignment operator.
void connectProxySignals(); ///< Connect the signals of the Qt Proxy to the GUI components
bool isStreaming() const; ///< Check if the service is currently streaming events.
grpc::Status CheckTokens(::grpc::ServerContext *context, ::google::protobuf::StringValue const *request, ::google::protobuf::StringValue *response) override;
grpc::Status AddLogEntry(::grpc::ServerContext *, ::grpc::AddLogEntryRequest const *request, ::google::protobuf::Empty *) override;
grpc::Status GuiReady(::grpc::ServerContext *, ::google::protobuf::Empty const*, ::google::protobuf::Empty *) override;
grpc::Status Quit(::grpc::ServerContext *, ::google::protobuf::Empty const*, ::google::protobuf::Empty *) override;

View File

@ -59,6 +59,7 @@ int main(int argc, char **argv)
Log& log = app().log();
log.setEchoInConsole(true);
log.setLevel(Log::Level::Debug);
log.info(QString("%1 started.").arg(applicationName));
MainWindow window(nullptr);

View File

@ -1443,7 +1443,7 @@ void GRPCClient::processUserEvent(UserEvent const &event)
UPClientContext GRPCClient::clientContext() const
{
auto ctx = std::make_unique<grpc::ClientContext>();
ctx->AddMetadata("server-token", serverToken_);
ctx->AddMetadata(grpcMetadataServerTokenKey, serverToken_);
return ctx;
}

View File

@ -26,6 +26,9 @@ namespace bridgepp
{
std::string const grpcMetadataServerTokenKey = "server-token";
namespace
{

View File

@ -29,6 +29,9 @@ namespace bridgepp
{
extern std::string const grpcMetadataServerTokenKey; ///< The key for the server token stored in the gRPC calls context metadata.
typedef std::shared_ptr<grpc::StreamEvent> SPStreamEvent; ///< Type definition for shared pointer to grpc::StreamEvent.
QString grpcServerConfigPath(); ///< Return the path of the gRPC server config file.