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

@ -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();
}