viewer_acl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #pragma once
  2. #include "json_handlers.h"
  3. #include "json_pipe_req.h"
  4. #include "log.h"
  5. namespace NKikimr::NViewer {
  6. using namespace NActors;
  7. class TJsonACL : public TViewerPipeClient {
  8. using TThis = TJsonACL;
  9. using TBase = TViewerPipeClient;
  10. using TBase::ReplyAndPassAway;
  11. TAutoPtr<TEvTxProxySchemeCache::TEvNavigateKeySetResult> CacheResult;
  12. TJsonSettings JsonSettings;
  13. bool MergeRules = false;
  14. ui32 Timeout = 0;
  15. public:
  16. TJsonACL(IViewer* viewer, NMon::TEvHttpInfo::TPtr& ev)
  17. : TViewerPipeClient(viewer, ev)
  18. {}
  19. void Bootstrap() override {
  20. if (NeedToRedirect()) {
  21. return;
  22. }
  23. const auto& params(Event->Get()->Request.GetParams());
  24. Timeout = FromStringWithDefault<ui32>(params.Get("timeout"), 10000);
  25. if (params.Has("path")) {
  26. RequestSchemeCacheNavigate(params.Get("path"));
  27. } else {
  28. return ReplyAndPassAway(Viewer->GetHTTPBADREQUEST(Event->Get(), "text/plain", "field 'path' is required"));
  29. }
  30. MergeRules = FromStringWithDefault<bool>(params.Get("merge_rules"), MergeRules);
  31. Become(&TThis::StateRequestedDescribe, TDuration::MilliSeconds(Timeout), new TEvents::TEvWakeup());
  32. }
  33. STATEFN(StateRequestedDescribe) {
  34. switch (ev->GetTypeRewrite()) {
  35. hFunc(TEvTxProxySchemeCache::TEvNavigateKeySetResult, Handle);
  36. cFunc(TEvents::TSystem::Wakeup, HandleTimeout);
  37. }
  38. }
  39. void Handle(TEvTxProxySchemeCache::TEvNavigateKeySetResult::TPtr& ev) {
  40. CacheResult = ev->Release();
  41. RequestDone();
  42. }
  43. static bool Has(ui32 accessRights, ui32 mask) {
  44. return (accessRights & mask) == mask;
  45. }
  46. void FillACE(const NACLibProto::TACE& ace, NKikimrViewer::TMetaCommonInfo::TACE& pbAce) {
  47. if (static_cast<NACLib::EAccessType>(ace.GetAccessType()) == NACLib::EAccessType::Deny) {
  48. pbAce.SetAccessType("Deny");
  49. }
  50. if (static_cast<NACLib::EAccessType>(ace.GetAccessType()) == NACLib::EAccessType::Allow) {
  51. pbAce.SetAccessType("Allow");
  52. }
  53. auto ar = ace.GetAccessRight();
  54. static std::pair<ui32, TString> accessRules[] = {
  55. {NACLib::EAccessRights::GenericFull, "Full"},
  56. {NACLib::EAccessRights::GenericFullLegacy, "FullLegacy"},
  57. {NACLib::EAccessRights::GenericManage, "Manage"},
  58. {NACLib::EAccessRights::GenericUse, "Use"},
  59. {NACLib::EAccessRights::GenericUseLegacy, "UseLegacy"},
  60. {NACLib::EAccessRights::GenericWrite, "Write"},
  61. {NACLib::EAccessRights::GenericRead, "Read"},
  62. {NACLib::EAccessRights::GenericList, "List"},
  63. };
  64. if (MergeRules) {
  65. for (const auto& [rule, name] : accessRules) {
  66. if (Has(ar, rule)) {
  67. pbAce.AddAccessRules(name);
  68. ar &= ~rule;
  69. }
  70. }
  71. }
  72. static std::pair<ui32, TString> accessRights[] = {
  73. {NACLib::EAccessRights::SelectRow, "SelectRow"},
  74. {NACLib::EAccessRights::UpdateRow, "UpdateRow"},
  75. {NACLib::EAccessRights::EraseRow, "EraseRow"},
  76. {NACLib::EAccessRights::ReadAttributes, "ReadAttributes"},
  77. {NACLib::EAccessRights::WriteAttributes, "WriteAttributes"},
  78. {NACLib::EAccessRights::CreateDirectory, "CreateDirectory"},
  79. {NACLib::EAccessRights::CreateTable, "CreateTable"},
  80. {NACLib::EAccessRights::CreateQueue, "CreateQueue"},
  81. {NACLib::EAccessRights::RemoveSchema, "RemoveSchema"},
  82. {NACLib::EAccessRights::DescribeSchema, "DescribeSchema"},
  83. {NACLib::EAccessRights::AlterSchema, "AlterSchema"},
  84. {NACLib::EAccessRights::CreateDatabase, "CreateDatabase"},
  85. {NACLib::EAccessRights::DropDatabase, "DropDatabase"},
  86. {NACLib::EAccessRights::GrantAccessRights, "GrantAccessRights"},
  87. {NACLib::EAccessRights::WriteUserAttributes, "WriteUserAttributes"},
  88. {NACLib::EAccessRights::ConnectDatabase, "ConnectDatabase"},
  89. {NACLib::EAccessRights::ReadStream, "ReadStream"},
  90. {NACLib::EAccessRights::WriteStream, "WriteStream"},
  91. {NACLib::EAccessRights::ReadTopic, "ReadTopic"},
  92. {NACLib::EAccessRights::WriteTopic, "WriteTopic"}
  93. };
  94. for (const auto& [right, name] : accessRights) {
  95. if (Has(ar, right)) {
  96. pbAce.AddAccessRights(name);
  97. ar &= ~right;
  98. }
  99. }
  100. if (ar != 0) {
  101. pbAce.AddAccessRights(NACLib::AccessRightsToString(ar));
  102. }
  103. pbAce.SetSubject(ace.GetSID());
  104. auto inht = ace.GetInheritanceType();
  105. if ((inht & NACLib::EInheritanceType::InheritObject) != 0) {
  106. pbAce.AddInheritanceType("Object");
  107. }
  108. if ((inht & NACLib::EInheritanceType::InheritContainer) != 0) {
  109. pbAce.AddInheritanceType("Container");
  110. }
  111. if ((inht & NACLib::EInheritanceType::InheritOnly) != 0) {
  112. pbAce.AddInheritanceType("Only");
  113. }
  114. }
  115. void ReplyAndPassAway() override {
  116. if (CacheResult == nullptr) {
  117. return ReplyAndPassAway(GetHTTPINTERNALERROR("text/plain", "no SchemeCache response"));
  118. }
  119. if (CacheResult->Request == nullptr) {
  120. return ReplyAndPassAway(GetHTTPINTERNALERROR("text/plain", "wrong SchemeCache response"));
  121. }
  122. if (CacheResult->Request.Get()->ResultSet.empty()) {
  123. return ReplyAndPassAway(GetHTTPINTERNALERROR("text/plain", "SchemeCache response is empty"));
  124. }
  125. if (CacheResult->Request.Get()->ErrorCount != 0) {
  126. return ReplyAndPassAway(GetHTTPBADREQUEST("text/plain", TStringBuilder() << "SchemeCache response error " << static_cast<int>(CacheResult->Request.Get()->ResultSet.front().Status)));
  127. }
  128. const auto& entry = CacheResult->Request.Get()->ResultSet.front();
  129. NKikimrViewer::TMetaInfo metaInfo;
  130. NKikimrViewer::TMetaCommonInfo& pbCommon = *metaInfo.MutableCommon();
  131. pbCommon.SetPath(CanonizePath(entry.Path));
  132. if (entry.Self) {
  133. pbCommon.SetOwner(entry.Self->Info.GetOwner());
  134. if (entry.Self->Info.HasACL()) {
  135. NACLib::TACL acl(entry.Self->Info.GetACL());
  136. for (const NACLibProto::TACE& ace : acl.GetACE()) {
  137. auto& pbAce = *pbCommon.AddACL();
  138. FillACE(ace, pbAce);
  139. }
  140. if (acl.GetInterruptInheritance()) {
  141. pbCommon.SetInterruptInheritance(true);
  142. }
  143. }
  144. if (entry.Self->Info.HasEffectiveACL()) {
  145. NACLib::TACL acl(entry.Self->Info.GetEffectiveACL());
  146. for (const NACLibProto::TACE& ace : acl.GetACE()) {
  147. auto& pbAce = *pbCommon.AddEffectiveACL();
  148. FillACE(ace, pbAce);
  149. }
  150. }
  151. }
  152. TStringStream json;
  153. TProtoToJson::ProtoToJson(json, metaInfo, JsonSettings);
  154. ReplyAndPassAway(GetHTTPOKJSON(json.Str()));
  155. }
  156. static YAML::Node GetSwagger() {
  157. YAML::Node node = YAML::Load(R"___(
  158. get:
  159. tags:
  160. - viewer
  161. summary: ACL information
  162. description: Returns information about ACL of an object
  163. parameters:
  164. - name: database
  165. in: query
  166. description: database name
  167. type: string
  168. required: false
  169. - name: path
  170. in: query
  171. description: schema path
  172. required: true
  173. type: string
  174. - name: merge_rules
  175. in: query
  176. description: merge access rights into access rules
  177. type: boolean
  178. - name: timeout
  179. in: query
  180. description: timeout in ms
  181. required: false
  182. type: integer
  183. responses:
  184. 200:
  185. description: OK
  186. content:
  187. application/json:
  188. schema:
  189. type: object
  190. properties:
  191. Common:
  192. type: object
  193. properties:
  194. Path:
  195. type: string
  196. Owner:
  197. type: string
  198. ACL:
  199. type: array
  200. items:
  201. type: object
  202. properties:
  203. AccessType:
  204. type: string
  205. Subject:
  206. type: string
  207. AccessRules:
  208. type: array
  209. items:
  210. type: string
  211. AccessRights:
  212. type: array
  213. items:
  214. type: string
  215. InheritanceType:
  216. type: array
  217. items:
  218. type: string
  219. InterruptInheritance:
  220. type: boolean
  221. EffectiveACL:
  222. type: array
  223. items:
  224. type: object
  225. properties:
  226. AccessType:
  227. type: string
  228. Subject:
  229. type: string
  230. AccessRules:
  231. type: array
  232. items:
  233. type: string
  234. AccessRights:
  235. type: array
  236. items:
  237. type: string
  238. InheritanceType:
  239. type: array
  240. items:
  241. type: string
  242. 400:
  243. description: Bad Request
  244. 403:
  245. description: Forbidden
  246. 504:
  247. description: Gateway Timeout
  248. )___");
  249. return node;
  250. }
  251. };
  252. }