|
| 1 | +#include "service_import.h" |
| 2 | +#include "rpc_deferrable.h" |
| 3 | + |
| 4 | +#include <ydb/core/base/tablet_pipe.h> |
| 5 | +#include <ydb/core/grpc_services/base/base.h> |
| 6 | +#include <ydb/core/tx/scheme_cache/scheme_cache.h> |
| 7 | +#include <ydb/core/tx/schemeshard/schemeshard_import.h> |
| 8 | +#include <ydb/public/api/protos/ydb_import.pb.h> |
| 9 | + |
| 10 | +#define LOG_T(stream) LOG_TRACE_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 11 | +#define LOG_D(stream) LOG_DEBUG_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 12 | +#define LOG_I(stream) LOG_INFO_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 13 | +#define LOG_N(stream) LOG_NOTICE_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 14 | +#define LOG_W(stream) LOG_WARN_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 15 | +#define LOG_E(stream) LOG_ERROR_S(*TlsActivationContext, NKikimrServices::TX_PROXY, "[ListObjectsInS3Export] " << SelfId() << " " << stream) |
| 16 | + |
| 17 | +namespace NKikimr::NGRpcService { |
| 18 | + |
| 19 | +using TEvListObjectsInS3ExportRequest = TGrpcRequestOperationCall<Ydb::Import::ListObjectsInS3ExportRequest, |
| 20 | + Ydb::Import::ListObjectsInS3ExportResponse>; |
| 21 | + |
| 22 | +class TListObjectsInS3ExportRPC: public TRpcOperationRequestActor<TListObjectsInS3ExportRPC, TEvListObjectsInS3ExportRequest> { |
| 23 | +public: |
| 24 | + using TBase = TRpcOperationRequestActor<TListObjectsInS3ExportRPC, TEvListObjectsInS3ExportRequest>; |
| 25 | + using TRpcOperationRequestActor<TListObjectsInS3ExportRPC, TEvListObjectsInS3ExportRequest>::TRpcOperationRequestActor; |
| 26 | + |
| 27 | + explicit TListObjectsInS3ExportRPC(IRequestOpCtx* request) |
| 28 | + : TBase(request) |
| 29 | + , UserToken(CreateUserToken(request)) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + STATEFN(StateFunc) { |
| 34 | + switch (ev->GetTypeRewrite()) { |
| 35 | + hFunc(NKikimr::NSchemeShard::TEvImport::TEvListObjectsInS3ExportResponse, Handle); |
| 36 | + hFunc(TEvTxProxySchemeCache::TEvNavigateKeySetResult, Handle); |
| 37 | + |
| 38 | + hFunc(TEvTabletPipe::TEvClientConnected, Handle); |
| 39 | + hFunc(TEvTabletPipe::TEvClientDestroyed, Handle); |
| 40 | + default: |
| 41 | + return StateFuncBase(ev); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + void Bootstrap() { |
| 46 | + if (!Request_ || !Request_->GetDatabaseName()) { |
| 47 | + return Reply(Ydb::StatusIds::BAD_REQUEST, "Database name is not specified", NKikimrIssues::TIssuesIds::YDB_API_VALIDATION_ERROR, NActors::TActivationContext::AsActorContext()); |
| 48 | + } |
| 49 | + |
| 50 | + ResolveDatabase(); |
| 51 | + |
| 52 | + Become(&TListObjectsInS3ExportRPC::StateFunc); |
| 53 | + } |
| 54 | + |
| 55 | + void ResolveDatabase() { |
| 56 | + LOG_D("Resolve database" |
| 57 | + << ": name# " << Request_->GetDatabaseName()); |
| 58 | + |
| 59 | + auto request = MakeHolder<NSchemeCache::TSchemeCacheNavigate>(); |
| 60 | + request->DatabaseName = *Request_->GetDatabaseName(); |
| 61 | + |
| 62 | + auto& entry = request->ResultSet.emplace_back(); |
| 63 | + entry.Operation = NSchemeCache::TSchemeCacheNavigate::OpPath; |
| 64 | + entry.Path = NKikimr::SplitPath(*Request_->GetDatabaseName()); |
| 65 | + |
| 66 | + Send(MakeSchemeCacheID(), new TEvTxProxySchemeCache::TEvNavigateKeySet(request.Release())); |
| 67 | + } |
| 68 | + |
| 69 | + void Handle(TEvTxProxySchemeCache::TEvNavigateKeySetResult::TPtr& ev) { |
| 70 | + const auto& request = ev->Get()->Request; |
| 71 | + |
| 72 | + LOG_D("Handle TEvTxProxySchemeCache::TEvNavigateKeySetResult" |
| 73 | + << ": request# " << (request ? request->ToString(*AppData()->TypeRegistry) : "nullptr")); |
| 74 | + |
| 75 | + if (request->ResultSet.empty()) { |
| 76 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, "Scheme error", NKikimrIssues::TIssuesIds::GENERIC_RESOLVE_ERROR, NActors::TActivationContext::AsActorContext()); |
| 77 | + } |
| 78 | + |
| 79 | + const auto& entry = request->ResultSet.front(); |
| 80 | + |
| 81 | + if (request->ErrorCount > 0) { |
| 82 | + switch (entry.Status) { |
| 83 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::Ok: |
| 84 | + break; |
| 85 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::AccessDenied: |
| 86 | + return Reply(Ydb::StatusIds::UNAUTHORIZED, "Access denied", NKikimrIssues::TIssuesIds::ACCESS_DENIED, NActors::TActivationContext::AsActorContext()); |
| 87 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::RootUnknown: |
| 88 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::PathErrorUnknown: |
| 89 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, "Unknown database", NKikimrIssues::TIssuesIds::PATH_NOT_EXIST, NActors::TActivationContext::AsActorContext()); |
| 90 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::LookupError: |
| 91 | + case NSchemeCache::TSchemeCacheNavigate::EStatus::RedirectLookupError: |
| 92 | + return Reply(Ydb::StatusIds::UNAVAILABLE, "Database lookup error", NKikimrIssues::TIssuesIds::RESOLVE_LOOKUP_ERROR, NActors::TActivationContext::AsActorContext()); |
| 93 | + default: |
| 94 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, "Scheme error", NKikimrIssues::TIssuesIds::GENERIC_RESOLVE_ERROR, NActors::TActivationContext::AsActorContext()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (!this->CheckDatabaseAccess(CanonizePath(entry.Path), entry.SecurityObject)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + auto domainInfo = entry.DomainInfo; |
| 103 | + if (!domainInfo) { |
| 104 | + LOG_E("Got empty domain info"); |
| 105 | + return Reply(Ydb::StatusIds::INTERNAL_ERROR, "Internal error", NKikimrIssues::TIssuesIds::GENERIC_RESOLVE_ERROR, NActors::TActivationContext::AsActorContext()); |
| 106 | + } |
| 107 | + |
| 108 | + SchemeShardId = domainInfo->ExtractSchemeShard(); |
| 109 | + SendRequestToSchemeShard(); |
| 110 | + } |
| 111 | + |
| 112 | + bool CheckDatabaseAccess(const TString& path, TIntrusivePtr<TSecurityObject> securityObject) { |
| 113 | + const ui32 access = NACLib::DescribeSchema; |
| 114 | + |
| 115 | + if (!UserToken || !securityObject) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + if (securityObject->CheckAccess(access, *UserToken)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + Reply(Ydb::StatusIds::UNAUTHORIZED, |
| 124 | + TStringBuilder() << "Access denied" |
| 125 | + << ": for# " << UserToken->GetUserSID() |
| 126 | + << ", path# " << path |
| 127 | + << ", access# " << NACLib::AccessRightsToString(access), |
| 128 | + NKikimrIssues::TIssuesIds::ACCESS_DENIED, |
| 129 | + NActors::TActivationContext::AsActorContext()); |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + void SendRequestToSchemeShard() { |
| 134 | + LOG_D("Send request: schemeShardId# " << SchemeShardId); |
| 135 | + |
| 136 | + if (!PipeClient) { |
| 137 | + NTabletPipe::TClientConfig config; |
| 138 | + config.RetryPolicy = {.RetryLimitCount = 3}; |
| 139 | + PipeClient = this->RegisterWithSameMailbox(NTabletPipe::CreateClient(this->SelfId(), SchemeShardId, config)); |
| 140 | + } |
| 141 | + |
| 142 | + auto request = MakeHolder<NSchemeShard::TEvImport::TEvListObjectsInS3ExportRequest>(); |
| 143 | + |
| 144 | + *request->Record.MutableOperationParams() = GetProtoRequest()->operation_params(); |
| 145 | + *request->Record.MutableSettings() = GetProtoRequest()->settings(); |
| 146 | + request->Record.SetPageSize(GetProtoRequest()->page_size()); |
| 147 | + request->Record.SetPageToken(GetProtoRequest()->page_token()); |
| 148 | + |
| 149 | + NTabletPipe::SendData(this->SelfId(), PipeClient, std::move(request), 0, Span_.GetTraceId()); |
| 150 | + } |
| 151 | + |
| 152 | + void Handle(NKikimr::NSchemeShard::TEvImport::TEvListObjectsInS3ExportResponse::TPtr& ev) { |
| 153 | + const auto& record = ev->Get()->Record; |
| 154 | + |
| 155 | + LOG_D("Handle TListObjectsInS3ExportRPC::TEvListObjectsInS3ExportResponse" |
| 156 | + << ": record# " << record.ShortDebugString()); |
| 157 | + |
| 158 | + if (record.GetStatus() != Ydb::StatusIds::SUCCESS) { |
| 159 | + return Reply(record.GetStatus(), record.GetIssues(), NActors::TActivationContext::AsActorContext()); |
| 160 | + } else { |
| 161 | + return ReplyWithResult(record.GetStatus(), record.GetIssues(), record.GetResult(), NActors::TActivationContext::AsActorContext()); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + void Handle(TEvTabletPipe::TEvClientConnected::TPtr& ev) { |
| 166 | + if (ev->Get()->Status != NKikimrProto::OK) { |
| 167 | + DeliveryProblem(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + void Handle(TEvTabletPipe::TEvClientDestroyed::TPtr&) { |
| 172 | + DeliveryProblem(); |
| 173 | + } |
| 174 | + |
| 175 | + void DeliveryProblem() { |
| 176 | + LOG_W("Delivery problem"); |
| 177 | + Reply(Ydb::StatusIds::UNAVAILABLE, "Delivery problem", NKikimrIssues::TIssuesIds::DEFAULT_ERROR, NActors::TActivationContext::AsActorContext()); |
| 178 | + } |
| 179 | + |
| 180 | + void PassAway() override { |
| 181 | + NTabletPipe::CloseClient(this->SelfId(), PipeClient); |
| 182 | + TBase::PassAway(); |
| 183 | + } |
| 184 | + |
| 185 | + static THolder<const NACLib::TUserToken> CreateUserToken(IRequestOpCtx* request) { |
| 186 | + if (const auto& userToken = request->GetSerializedToken()) { |
| 187 | + return MakeHolder<NACLib::TUserToken>(userToken); |
| 188 | + } else { |
| 189 | + return {}; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +private: |
| 194 | + ui64 SchemeShardId = 0; |
| 195 | + TActorId PipeClient; |
| 196 | + const THolder<const NACLib::TUserToken> UserToken; |
| 197 | +}; |
| 198 | + |
| 199 | +void DoListObjectsInS3ExportRequest(std::unique_ptr<IRequestOpCtx> p, const IFacilityProvider& f) { |
| 200 | + f.RegisterActor(new TListObjectsInS3ExportRPC(p.release())); |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace NKikimr::NGRpcService |
0 commit comments