diff options
author | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
---|---|---|
committer | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
commit | 22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch) | |
tree | bffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/monlib/service | |
parent | 332b99e2173f0425444abb759eebcb2fafaa9209 (diff) | |
download | ydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz |
validate canons without yatest_common
Diffstat (limited to 'library/cpp/monlib/service')
-rw-r--r-- | library/cpp/monlib/service/auth/tvm/auth.cpp | 93 | ||||
-rw-r--r-- | library/cpp/monlib/service/auth/tvm/auth.h | 33 |
2 files changed, 126 insertions, 0 deletions
diff --git a/library/cpp/monlib/service/auth/tvm/auth.cpp b/library/cpp/monlib/service/auth/tvm/auth.cpp new file mode 100644 index 0000000000..e071c11ebc --- /dev/null +++ b/library/cpp/monlib/service/auth/tvm/auth.cpp @@ -0,0 +1,93 @@ +#include "auth.h" + +#include <util/generic/hash_set.h> + + +using namespace NTvmAuth; + + +namespace NMonitoring { +namespace { + template <class TTvmClientPtr = THolder<TTvmClient>> + class TTvmManager final: public ITvmManager { + public: + TTvmManager(NTvmApi::TClientSettings settings, TVector<TTvmId> clients, TLoggerPtr logger) + : AllowedClients_{clients.begin(), clients.end()} + , Tvm_(new TTvmClient{std::move(settings), std::move(logger)}) + { + } + + TTvmManager(NTvmTool::TClientSettings settings, TVector<TTvmId> clients, TLoggerPtr logger) + : AllowedClients_{clients.begin(), clients.end()} + , Tvm_(new TTvmClient{std::move(settings), std::move(logger)}) + { + } + + TTvmManager(TTvmClientPtr tvm, TVector<TTvmId> clients) + : AllowedClients_{clients.begin(), clients.end()} + , Tvm_(std::move(tvm)) + { + } + + bool IsAllowedClient(TTvmId clientId) override { + return AllowedClients_.contains(clientId); + } + + TCheckedServiceTicket CheckServiceTicket(TStringBuf ticket) override { + return Tvm_->CheckServiceTicket(ticket); + } + + private: + THashSet<TTvmId> AllowedClients_; + TTvmClientPtr Tvm_; + }; + + class TTvmAuthProvider final: public IAuthProvider { + public: + TTvmAuthProvider(THolder<ITvmManager> manager) + : TvmManager_{std::move(manager)} + { + } + + TAuthResult Check(const IHttpRequest& req) override { + auto ticketHeader = req.GetHeaders().FindHeader("X-Ya-Service-Ticket"); + if (!ticketHeader) { + return TAuthResult::NoCredentials(); + } + + const auto ticket = TvmManager_->CheckServiceTicket(ticketHeader->Value()); + if (!ticket) { + return TAuthResult::Denied(); + } + + return TvmManager_->IsAllowedClient(ticket.GetSrc()) + ? TAuthResult::Ok() + : TAuthResult::Denied(); + } + + private: + THolder<ITvmManager> TvmManager_; + }; +} // namespace + +THolder<ITvmManager> CreateDefaultTvmManager(NTvmApi::TClientSettings settings, TVector<TTvmId> allowedClients, TLoggerPtr logger) { + return MakeHolder<TTvmManager<>>(std::move(settings), std::move(allowedClients), std::move(logger)); +} + +THolder<ITvmManager> CreateDefaultTvmManager(NTvmTool::TClientSettings settings, TVector<TTvmId> allowedClients, TLoggerPtr logger) { + return MakeHolder<TTvmManager<>>(std::move(settings), std::move(allowedClients), std::move(logger)); +} + +THolder<ITvmManager> CreateDefaultTvmManager(TAtomicSharedPtr<NTvmAuth::TTvmClient> client, TVector<TTvmId> allowedClients) { + return MakeHolder<TTvmManager<TAtomicSharedPtr<NTvmAuth::TTvmClient>>>(std::move(client), std::move(allowedClients)); +} + +THolder<ITvmManager> CreateDefaultTvmManager(std::shared_ptr<NTvmAuth::TTvmClient> client, TVector<TTvmId> allowedClients) { + return MakeHolder<TTvmManager<std::shared_ptr<NTvmAuth::TTvmClient>>>(std::move(client), std::move(allowedClients)); +} + +THolder<IAuthProvider> CreateTvmAuth(THolder<ITvmManager> manager) { + return MakeHolder<TTvmAuthProvider>(std::move(manager)); +} + +} // namespace NMonitoring diff --git a/library/cpp/monlib/service/auth/tvm/auth.h b/library/cpp/monlib/service/auth/tvm/auth.h new file mode 100644 index 0000000000..432beff9d6 --- /dev/null +++ b/library/cpp/monlib/service/auth/tvm/auth.h @@ -0,0 +1,33 @@ +#pragma once + +#include <library/cpp/monlib/service/mon_service_http_request.h> +#include <library/cpp/monlib/service/auth.h> +#include <library/cpp/tvmauth/client/facade.h> + +namespace NMonitoring { + struct ITvmManager { + virtual ~ITvmManager() = default; + virtual bool IsAllowedClient(NTvmAuth::TTvmId clientId) = 0; + virtual NTvmAuth::TCheckedServiceTicket CheckServiceTicket(TStringBuf ticket) = 0; + }; + + THolder<ITvmManager> CreateDefaultTvmManager( + NTvmAuth::NTvmApi::TClientSettings settings, + TVector<NTvmAuth::TTvmId> allowedClients, + NTvmAuth::TLoggerPtr logger = NTvmAuth::TDevNullLogger::IAmBrave()); + + THolder<ITvmManager> CreateDefaultTvmManager( + NTvmAuth::NTvmTool::TClientSettings settings, + TVector<NTvmAuth::TTvmId> allowedClients, + NTvmAuth::TLoggerPtr logger = NTvmAuth::TDevNullLogger::IAmBrave()); + + THolder<ITvmManager> CreateDefaultTvmManager( + TAtomicSharedPtr<NTvmAuth::TTvmClient> client, + TVector<NTvmAuth::TTvmId> allowedClients); + + THolder<ITvmManager> CreateDefaultTvmManager( + std::shared_ptr<NTvmAuth::TTvmClient> client, + TVector<NTvmAuth::TTvmId> allowedClients); + + THolder<IAuthProvider> CreateTvmAuth(THolder<ITvmManager> tvmManager); +} // namespace NMonitoring |