Skip to content

Commit

Permalink
YQL-18117: Implement block tz date (#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedor-miron authored and adameat committed May 23, 2024
1 parent b530042 commit a3b340a
Show file tree
Hide file tree
Showing 18 changed files with 869 additions and 146 deletions.
2 changes: 1 addition & 1 deletion ydb/core/viewer/json_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct TJsonHandlers {
json << ',';
}
TString name = itJson->first;
json << "\"/" << name << '"' << ":{";
json << "\"/viewer" << name << '"' << ":{";
json << "\"get\":{";
json << "\"tags\":[\"" << TTagInfo::TagName << "\"],";
json << "\"produces\":[\"application/json\"],";
Expand Down
90 changes: 78 additions & 12 deletions ydb/core/viewer/json_whoami.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <ydb/library/actors/core/actor_bootstrapped.h>
#include <ydb/library/actors/core/mon.h>
#include <library/cpp/json/json_value.h>
#include <library/cpp/json/json_writer.h>
#include <ydb/core/base/tablet_pipe.h>
#include <ydb/library/services/services.pb.h>
#include <ydb/core/tx/schemeshard/schemeshard.h>
Expand All @@ -14,7 +16,6 @@ using namespace NActors;

class TJsonWhoAmI : public TActorBootstrapped<TJsonWhoAmI> {
IViewer* Viewer;
TJsonSettings JsonSettings;
NMon::TEvHttpInfo::TPtr Event;

public:
Expand All @@ -28,18 +29,48 @@ class TJsonWhoAmI : public TActorBootstrapped<TJsonWhoAmI> {
{}

void Bootstrap(const TActorContext& ctx) {
const auto& params(Event->Get()->Request.GetParams());
JsonSettings.EnumAsNumbers = !FromStringWithDefault<bool>(params.Get("enums"), false);
JsonSettings.UI64AsString = !FromStringWithDefault<bool>(params.Get("ui64"), false);
ReplyAndDie(ctx);
}

bool CheckGroupMembership(std::unique_ptr<NACLib::TUserToken>& token, const NProtoBuf::RepeatedPtrField<TString>& sids) {
if (sids.empty()) {
return true;
}
for (const auto& sid : sids) {
if (token->IsExist(sid)) {
return true;
}
}
return false;
}

void ReplyAndDie(const TActorContext &ctx) {
NACLibProto::TUserToken userToken;
Y_PROTOBUF_SUPPRESS_NODISCARD userToken.ParseFromString(Event->Get()->UserToken);
TStringStream json;
TProtoToJson::ProtoToJson(json, userToken, JsonSettings);
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPOKJSON(Event->Get()) + json.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
NJson::TJsonValue json(NJson::JSON_MAP);
if (userToken.HasUserSID()) {
json["UserSID"] = userToken.GetUserSID();
}
if (userToken.HasGroupSIDs() && userToken.GetGroupSIDs().BucketsSize() > 0) {
NJson::TJsonValue& groupSIDs(json["GroupSIDs"]);
groupSIDs.SetType(NJson::JSON_ARRAY);
for (const auto& buckets : userToken.GetGroupSIDs().GetBuckets()) {
for (const auto& group : buckets.GetValues()) {
groupSIDs.AppendValue(group);
}
}
}
if (userToken.HasOriginalUserToken()) {
json["OriginalUserToken"] = userToken.GetOriginalUserToken();
}
if (userToken.HasAuthType()) {
json["AuthType"] = userToken.GetAuthType();
}
auto token = std::make_unique<NACLib::TUserToken>(userToken);
json["IsViewerAllowed"] = CheckGroupMembership(token, AppData()->DomainsConfig.GetSecurityConfig().GetViewerAllowedSIDs());
json["IsMonitoringAllowed"] = CheckGroupMembership(token, AppData()->DomainsConfig.GetSecurityConfig().GetMonitoringAllowedSIDs());
json["IsAdministrationAllowed"] = CheckGroupMembership(token, AppData()->DomainsConfig.GetSecurityConfig().GetAdministrationAllowedSIDs());
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPOKJSON(Event->Get()) + NJson::WriteJson(json, false), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
Die(ctx);
}

Expand All @@ -52,17 +83,52 @@ class TJsonWhoAmI : public TActorBootstrapped<TJsonWhoAmI> {
template <>
struct TJsonRequestSchema<TJsonWhoAmI> {
static TString GetSchema() {
TStringStream stream;
TProtoToJson::ProtoToJsonSchema<NACLibProto::TUserToken>(stream);
return stream.Str();
return R"___(
{
"type": "object",
"title": "WhoAmI",
"properties": {
"UserSID": {
"type": "string",
"description": "User ID / name"
},
"GroupSID": {
"type": "array",
"items": {
"type": "string"
},
"description": "User groups"
},
"OriginalUserToken": {
"type": "string",
"description": "User's token used to authenticate"
},
"AuthType": {
"type": "string",
"description": "Authentication type"
},
"IsViewerAllowed": {
"type": "boolean",
"description": "Is user allowed to view data"
},
"IsMonitoringAllowed": {
"type": "boolean",
"description": "Is user allowed to view deeper and make simple changes"
},
"IsAdministrationAllowed": {
"type": "boolean",
"description": "Is user allowed to do unrestricted changes in the system"
}
}
}
)___";
}
};

template <>
struct TJsonRequestParameters<TJsonWhoAmI> {
static TString GetParameters() {
return R"___([{"name":"enums","in":"query","description":"convert enums to strings","required":false,"type":"boolean"},
{"name":"ui64","in":"query","description":"return ui64 as numbers","required":false,"type":"boolean"}])___";
return "[]";
}
};

Expand Down
42 changes: 42 additions & 0 deletions ydb/library/yql/minikql/computation/mkql_block_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,37 @@ class TTupleBlockItemConverter : public IBlockItemConverter {
mutable TVector<TBlockItem> Items;
};

template <typename TTzDate, bool Nullable>
class TTzDateBlockItemConverter : public IBlockItemConverter {
public:
using TLayout = typename NYql::NUdf::TDataType<TTzDate>::TLayout;

NUdf::TUnboxedValuePod MakeValue(TBlockItem item, const THolderFactory& holderFactory) const final {
Y_UNUSED(holderFactory);
if constexpr (Nullable) {
if (!item) {
return {};
}
}

NUdf::TUnboxedValuePod value {item.Get<TLayout>()};
value.SetTimezoneId(item.GetTimezoneId());
return value;
}

TBlockItem MakeItem(const NUdf::TUnboxedValuePod& value) const final {
if constexpr (Nullable) {
if (!value) {
return {};
}
}

TBlockItem item {value.Get<TLayout>()};
item.SetTimezoneId(value.GetTimezoneId());
return item;
}
};

class TExternalOptionalBlockItemConverter : public IBlockItemConverter {
public:
TExternalOptionalBlockItemConverter(std::unique_ptr<IBlockItemConverter>&& inner)
Expand Down Expand Up @@ -229,6 +260,8 @@ struct TConverterTraits {
template <typename TStringType, bool Nullable, NUdf::EDataSlot TOriginal = NUdf::EDataSlot::String, NUdf::EPgStringType PgString = NUdf::EPgStringType::None>
using TStrings = TStringBlockItemConverter<TStringType, Nullable, PgString>;
using TExtOptional = TExternalOptionalBlockItemConverter;
template<typename TTzDate, bool Nullable>
using TTzDateConverter = TTzDateBlockItemConverter<TTzDate, Nullable>;

static std::unique_ptr<TResult> MakePg(const NUdf::TPgTypeDescription& desc, const NUdf::IPgBuilder* pgBuilder) {
if (desc.PassByValue) {
Expand Down Expand Up @@ -258,6 +291,15 @@ struct TConverterTraits {
return std::make_unique<TResourceBlockItemConverter<false>>();
}
}

template<typename TTzDate>
static std::unique_ptr<TResult> MakeTzDate(bool isOptional) {
if (isOptional) {
return std::make_unique<TTzDateConverter<TTzDate, true>>();
} else {
return std::make_unique<TTzDateConverter<TTzDate, false>>();
}
}
};

} // namespace
Expand Down
Loading

0 comments on commit a3b340a

Please sign in to comment.