hive.ko.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function s4() {
  17. return Math.floor((1 + Math.random()) * 0x10000)
  18. .toString(16)
  19. .substring(1);
  20. }
  21. function UUID() {
  22. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  23. }
  24. var Privilege = function (vm, privilege) {
  25. var self = this;
  26. self.id = ko.observable(typeof privilege.id != "undefined" && privilege.id != null ? privilege.id : "");
  27. self.roleName = ko.observable(typeof privilege.roleName != "undefined" && privilege.roleName != null ? privilege.roleName : "");
  28. self.status = ko.observable(typeof privilege.status != "undefined" && privilege.status != null ? privilege.status : "");
  29. self.editing = ko.observable(typeof privilege.editing != "undefined" && privilege.editing != null ? privilege.editing : false);
  30. self.serverName = ko.observable(typeof privilege.serverName != "undefined" && privilege.serverName != null ? privilege.serverName : "");
  31. self.serverName.subscribe(function () {
  32. if (self.status() == '') {
  33. self.status('modified');
  34. }
  35. });
  36. self.dbName = ko.observable(typeof privilege.dbName != "undefined" && privilege.dbName != null ? privilege.dbName : "");
  37. self.dbName.subscribe(function () {
  38. if (self.status() == '') {
  39. self.status('modified');
  40. }
  41. });
  42. self.tableName = ko.observable(typeof privilege.tableName != "undefined" && privilege.tableName != null ? privilege.tableName : "");
  43. self.tableName.subscribe(function () {
  44. if (self.status() == '') {
  45. self.status('modified');
  46. }
  47. });
  48. self.URI = ko.observable(typeof privilege.URI != "undefined" && privilege.URI != null ? privilege.URI : "");
  49. self.URI.subscribe(function () {
  50. if (self.status() == '') {
  51. self.status('modified');
  52. }
  53. });
  54. self.action = ko.observable(typeof privilege.action != "undefined" && privilege.action != null ? privilege.action : 'SELECT');
  55. self.action.subscribe(function () {
  56. if (self.status() == '') {
  57. self.status('modified');
  58. }
  59. });
  60. self.timestamp = ko.observable(typeof privilege.timestamp != "undefined" && privilege.timestamp != null ? privilege.timestamp : 0);
  61. self.grantor = ko.observable(typeof privilege.grantor != "undefined" && privilege.grantor != null ? privilege.grantor : "");
  62. // UI
  63. self.privilegeType = ko.observable("db");
  64. self.showAdvanced = ko.observable(false);
  65. self.path = ko.computed({
  66. read: function () {
  67. if (self.tableName().length > 0) {
  68. return self.dbName() + "." + self.tableName();
  69. } else {
  70. return self.dbName();
  71. }
  72. },
  73. write: function (value) {
  74. var lastSpacePos = value.lastIndexOf(".");
  75. if (lastSpacePos > 0) {
  76. this.dbName(value.substring(0, lastSpacePos));
  77. this.tableName(value.substring(lastSpacePos + 1));
  78. } else {
  79. this.dbName(value);
  80. this.tableName('');
  81. }
  82. },
  83. owner: self
  84. });
  85. self.privilegeScope = ko.computed(function () {
  86. if (self.tableName().length > 0) {
  87. return 'TABLE';
  88. } else if (self.dbName().length > 0) {
  89. return 'DATABASE';
  90. } else {
  91. return 'SERVER';
  92. }
  93. });
  94. self.remove = function (privilege) {
  95. if (privilege.status() == 'new') {
  96. privilege.status('alreadydeleted');
  97. } else {
  98. privilege.status('deleted');
  99. }
  100. }
  101. }
  102. var Role = function (vm, role) {
  103. var self = this;
  104. self.name = ko.observable(typeof role.name != "undefined" && role.name != null ? role.name : "");
  105. self.name.subscribe(function (value){
  106. var _found = false;
  107. vm.role().isEditing(false);
  108. ko.utils.arrayForEach(vm.roles(), function (role) {
  109. if (role.name() == value){
  110. vm.role(role);
  111. _found = true;
  112. }
  113. });
  114. if (_found) {
  115. vm.role().isEditing(true);
  116. vm.list_sentry_privileges_by_role(vm.role());
  117. $(document).trigger("destroy.typeahead");
  118. }
  119. });
  120. self.selected = ko.observable(false);
  121. self.handleSelect = function (row, e) {
  122. self.selected(!self.selected());
  123. }
  124. self.grantorPrincipal = ko.observable(typeof role.grantorPrincipal != "undefined" && role.grantorPrincipal != null ? role.grantorPrincipal : "");
  125. self.groups = ko.observableArray();
  126. self.originalGroups = ko.observableArray();
  127. $.each(typeof role.groups != "undefined" && role.groups != null ? role.groups : [], function (index, group) {
  128. self.groups.push(group);
  129. self.originalGroups.push(group);
  130. });
  131. self.privileges = ko.observableArray(); // Not included in the API
  132. self.privilegesForViewTo = ko.observable(49);
  133. self.originalPrivileges = ko.observableArray();
  134. self.showPrivileges = ko.observable(false);
  135. self.showPrivileges.subscribe(function (value) {
  136. var _expanded = vm.expandedRoles();
  137. if (value) {
  138. if (_expanded.indexOf(self.name()) == -1) {
  139. _expanded.push(self.name());
  140. }
  141. }
  142. else {
  143. if (_expanded.indexOf(self.name()) > -1) {
  144. _expanded.splice(_expanded.indexOf(self.name()), 1);
  145. }
  146. }
  147. vm.expandedRoles(_expanded);
  148. });
  149. self.showEditGroups = ko.observable(false);
  150. self.isEditing = ko.observable(false);
  151. self.privilegesChanged = ko.computed(function () {
  152. return $.grep(self.privileges(), function (privilege) {
  153. return ['new', 'deleted', 'modified'].indexOf(privilege.status()) != -1;
  154. });
  155. });
  156. self.groupsChanged = ko.computed(function () {
  157. return !($(self.groups()).not(self.originalGroups()).length == 0 && $(self.originalGroups()).not(self.groups()).length == 0);
  158. });
  159. self.privilegesForView = ko.computed(function() {
  160. var _filter = vm.privilegeFilter().toLowerCase();
  161. if (_filter == "") {
  162. return self.privileges().slice(0, self.privilegesForViewTo());
  163. }
  164. else {
  165. var _filtered = ko.utils.arrayFilter(self.privileges(), function (priv) {
  166. return priv.dbName().toLowerCase().indexOf(_filter) > -1 || priv.tableName().toLowerCase().indexOf(_filter) > -1 || priv.action().toLowerCase().indexOf(_filter) > -1;
  167. });
  168. return _filtered.slice(0, self.privilegesForViewTo());
  169. }
  170. });
  171. self.reset = function () {
  172. self.name('');
  173. self.groups.removeAll();
  174. self.privileges.removeAll();
  175. self.originalPrivileges.removeAll();
  176. self.isEditing(false);
  177. }
  178. self.addGroup = function () {
  179. self.groups.push('');
  180. }
  181. self.addPrivilege = function () {
  182. if (vm.getSectionHash() == 'edit') {
  183. self.privileges.push(new Privilege(vm, {'serverName': vm.assist.server(), 'status': 'new', 'editing': true, 'dbName': vm.assist.db(), 'tableName': vm.assist.table()}));
  184. } else {
  185. self.privileges.push(new Privilege(vm, {'serverName': vm.assist.server(), 'status': 'new', 'editing': true}));
  186. }
  187. }
  188. self.resetGroups = function () {
  189. self.groups.removeAll();
  190. $.each(self.originalGroups(), function (index, group) {
  191. self.groups.push(group);
  192. });
  193. }
  194. self.saveGroups = function () {
  195. $(".jHueNotify").hide();
  196. $.post("/security/api/hive/update_role_groups", {
  197. role: ko.mapping.toJSON(self)
  198. }, function (data) {
  199. if (data.status == 0) {
  200. self.showEditGroups(false);
  201. self.originalGroups.removeAll();
  202. $.each(self.groups(), function (index, group) {
  203. self.originalGroups.push(group);
  204. });
  205. } else {
  206. $(document).trigger("error", data.message);
  207. }
  208. }).fail(function (xhr, textStatus, errorThrown) {
  209. $(document).trigger("error", xhr.responseText);
  210. });
  211. }
  212. self.create = function () {
  213. $(".jHueNotify").hide();
  214. $.post("/security/api/hive/create_role", {
  215. role: ko.mapping.toJSON(self)
  216. }, function (data) {
  217. if (data.status == 0) {
  218. $(document).trigger("info", data.message);
  219. vm.showCreateRole(false);
  220. self.reset();
  221. $(document).trigger("created.role");
  222. var role = new Role(vm, data.role);
  223. vm.originalRoles.unshift(role);
  224. vm.assist.refreshTree();
  225. vm.refreshExpandedRoles();
  226. } else {
  227. $(document).trigger("error", data.message);
  228. }
  229. }).fail(function (xhr, textStatus, errorThrown) {
  230. $(document).trigger("error", xhr.responseText);
  231. });
  232. }
  233. self.update = function () {
  234. $(".jHueNotify").hide();
  235. $.post("/security/api/hive/update_role", {
  236. role: ko.mapping.toJSON(self)
  237. }, function (data) {
  238. if (data.status == 0) {
  239. $(document).trigger("info", data.message);
  240. vm.showCreateRole(false);
  241. self.reset();
  242. $(document).trigger("updated.role");
  243. vm.assist.refreshTree();
  244. vm.refreshExpandedRoles();
  245. } else {
  246. $(document).trigger("error", data.message);
  247. }
  248. }).fail(function (xhr, textStatus, errorThrown) {
  249. $(document).trigger("error", xhr.responseText);
  250. });
  251. }
  252. self.remove = function (role) {
  253. $(".jHueNotify").hide();
  254. $.post("/security/api/hive/drop_sentry_role", {
  255. roleName: role.name
  256. }, function (data) {
  257. if (data.status == 0) {
  258. vm.removeRole(role.name());
  259. vm.assist.refreshTree();
  260. } else {
  261. $(document).trigger("error", data.message);
  262. }
  263. }).fail(function (xhr, textStatus, errorThrown) {
  264. $(document).trigger("error", xhr.responseText);
  265. });
  266. }
  267. self.savePrivileges = function (role) {
  268. $(".jHueNotify").hide();
  269. $.post("/security/api/hive/save_privileges", {
  270. role: ko.mapping.toJSON(role)
  271. }, function (data) {
  272. if (data.status == 0) {
  273. vm.list_sentry_privileges_by_authorizable();
  274. vm.refreshExpandedRoles();
  275. } else {
  276. $(document).trigger("error", data.message);
  277. }
  278. }).fail(function (xhr, textStatus, errorThrown) {
  279. $(document).trigger("error", xhr.responseText);
  280. });
  281. }
  282. }
  283. var Assist = function (vm, initial) {
  284. var self = this;
  285. self.compareNames = function (a, b) {
  286. if (a.name.toLowerCase() < b.name.toLowerCase())
  287. return -1;
  288. if (a.name.toLowerCase() > b.name.toLowerCase())
  289. return 1;
  290. return 0;
  291. }
  292. self.path = ko.observable("");
  293. self.path.subscribe(function (path) {
  294. vm.updatePathHash(path);
  295. });
  296. self.server = ko.observable(initial.sentry_provider);
  297. self.db = ko.computed(function () {
  298. return self.path().split(/[.]/)[0];
  299. });
  300. self.table = ko.computed(function () {
  301. return self.path().split(/[.]/)[1];
  302. });
  303. self.privileges = ko.observableArray();
  304. self.roles = ko.observableArray();
  305. self.isDiffMode = ko.observable(false);
  306. self.isDiffMode = ko.observable(false);
  307. self.isDiffMode.subscribe(function () {
  308. self.refreshTree();
  309. });
  310. self.isLoadingTree = ko.observable(false);
  311. self.treeAdditionalData = {};
  312. self.treeData = ko.observable({nodes: []});
  313. self.loadData = function (data) {
  314. self.treeData(new TreeNodeModel(data));
  315. };
  316. self.initialGrowingTree = {
  317. path: "__HUEROOT__",
  318. name: "__HUEROOT__",
  319. selected: false,
  320. nodes: [{
  321. path: "",
  322. name: self.server(),
  323. withPrivileges: false,
  324. isServer: true,
  325. isDb: false,
  326. isTable: false,
  327. isColumn: false,
  328. isExpanded: true,
  329. isChecked: false,
  330. nodes: []
  331. }]
  332. };
  333. self.growingTree = ko.observable(jQuery.extend(true, {}, self.initialGrowingTree));
  334. self.checkedItems = ko.observableArray([]);
  335. self.addDatabases = function (path, databases, skipLoading) {
  336. var _tree = self.growingTree().nodes[0];
  337. databases.forEach(function (db) {
  338. var _mainFound = false;
  339. _tree.nodes.forEach(function (node) {
  340. if (node.path == db) {
  341. _mainFound = true;
  342. }
  343. });
  344. if (!_mainFound) {
  345. var _item = {
  346. path: db,
  347. name: db,
  348. withPrivileges: false,
  349. isServer: false,
  350. isDb: true,
  351. isTable: false,
  352. isColumn: false,
  353. isExpanded: false,
  354. isChecked: false,
  355. nodes: []
  356. };
  357. _tree.nodes.push(_item);
  358. }
  359. });
  360. if (typeof skipLoading == "undefined" || !skipLoading) {
  361. self.loadData(self.growingTree());
  362. }
  363. }
  364. self.addTables = function (path, tables, skipLoading) {
  365. var _branch = self.growingTree().nodes[0];
  366. _branch.nodes.forEach(function (node) {
  367. if (node.path == path) {
  368. _branch = node;
  369. }
  370. });
  371. tables.forEach(function (table) {
  372. var _mainFound = false;
  373. var _path = path + "." + table;
  374. _branch.nodes.forEach(function (node) {
  375. if (node.path == _path) {
  376. _mainFound = true;
  377. }
  378. });
  379. if (!_mainFound) {
  380. var _item = {
  381. path: _path,
  382. name: table,
  383. withPrivileges: false,
  384. isServer: false,
  385. isDb: false,
  386. isTable: true,
  387. isColumn: false,
  388. isExpanded: false,
  389. isChecked: false,
  390. nodes: []
  391. };
  392. _branch.nodes.push(_item);
  393. }
  394. });
  395. if (typeof skipLoading == "undefined" || !skipLoading) {
  396. self.loadData(self.growingTree());
  397. }
  398. }
  399. self.addColumns = function (path, columns, skipLoading) {
  400. var _branch = self.growingTree();
  401. _branch.nodes.forEach(function (node) {
  402. if (node.path == path.split(".")[0]) {
  403. node.nodes.forEach(function (inode) {
  404. if (inode.path == path) {
  405. _branch = inode;
  406. }
  407. });
  408. }
  409. });
  410. columns.forEach(function (column) {
  411. var _mainFound = false;
  412. var _path = path + "." + column;
  413. _branch.nodes.forEach(function (node) {
  414. if (node.path == _path) {
  415. _mainFound = true;
  416. }
  417. });
  418. if (!_mainFound) {
  419. var _item = {
  420. path: _path,
  421. name: column,
  422. withPrivileges: false,
  423. isServer: false,
  424. isDb: false,
  425. isTable: false,
  426. isColumn: true,
  427. isExpanded: false,
  428. isChecked: false,
  429. nodes: []
  430. };
  431. _branch.nodes.push(_item);
  432. }
  433. });
  434. if (typeof skipLoading == "undefined" || !skipLoading) {
  435. self.loadData(self.growingTree());
  436. }
  437. }
  438. self.collapseTree = function () {
  439. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  440. self.updatePathProperty(self.growingTree(), "__HUEROOT__", "isExpanded", true);
  441. self.updatePathProperty(self.growingTree(), "", "isExpanded", true);
  442. self.loadData(self.growingTree());
  443. }
  444. self.collapseOthers = function () {
  445. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  446. self.updatePathProperty(self.growingTree(), "__HUEROOT__", "isExpanded", true);
  447. self.updatePathProperty(self.growingTree(), "", "isExpanded", true);
  448. var _path = self.path();
  449. var _crumb = "";
  450. for (var i = 0; i < _path.length; i++) {
  451. if ((_path[i] === "." && _crumb != "")) {
  452. self.updatePathProperty(self.growingTree(), _crumb, "isExpanded", true);
  453. }
  454. _crumb += _path[i];
  455. }
  456. self.updatePathProperty(self.growingTree(), _path, "isExpanded", true);
  457. self.loadData(self.growingTree());
  458. }
  459. self.expandTree = function () {
  460. self.updateTreeProperty(self.growingTree(), "isExpanded", true);
  461. self.loadData(self.growingTree());
  462. }
  463. self.refreshTree = function (force) {
  464. self.growingTree(jQuery.extend(true, {}, self.initialGrowingTree));
  465. // load root first
  466. self.fetchHivePath("", function () {
  467. Object.keys(self.treeAdditionalData).forEach(function (path) {
  468. if (path.indexOf(".") == -1 && path != "") {
  469. if (typeof force == "boolean" && force) {
  470. self.fetchHivePath(path);
  471. }
  472. else {
  473. if (self.treeAdditionalData[path].loaded) {
  474. self.fetchHivePath(path, function () {
  475. self.updatePathProperty(self.growingTree(), path, "isExpanded", self.treeAdditionalData[path].expanded);
  476. Object.keys(self.treeAdditionalData).forEach(function (ipath) {
  477. if (ipath.split(".").length == 2 && ipath.split(".")[0] == path) {
  478. self.fetchHivePath(ipath, function () {
  479. self.updatePathProperty(self.growingTree(), ipath, "isExpanded", self.treeAdditionalData[ipath].expanded);
  480. self.loadData(self.growingTree());
  481. });
  482. }
  483. });
  484. });
  485. }
  486. }
  487. }
  488. });
  489. });
  490. vm.list_sentry_privileges_by_authorizable();
  491. }
  492. self.setPath = function (obj, toggle) {
  493. if (self.getTreeAdditionalDataForPath(obj.path()).loaded || (!obj.isExpanded() && !self.getTreeAdditionalDataForPath(obj.path()).loaded)) {
  494. if (typeof toggle == "boolean" && toggle) {
  495. obj.isExpanded(!obj.isExpanded());
  496. self.getTreeAdditionalDataForPath(obj.path()).expanded = obj.isExpanded();
  497. }
  498. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  499. }
  500. else {
  501. if (typeof toggle == "boolean" && toggle) {
  502. obj.isExpanded(!obj.isExpanded());
  503. } else {
  504. obj.isExpanded(false);
  505. }
  506. self.getTreeAdditionalDataForPath(obj.path()).expanded = obj.isExpanded();
  507. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  508. }
  509. self.path(obj.path());
  510. $(document).trigger("changed.path");
  511. if (self.getTreeAdditionalDataForPath(obj.path()).loaded){
  512. vm.list_sentry_privileges_by_authorizable();
  513. }
  514. else {
  515. self.fetchHivePath();
  516. }
  517. }
  518. self.togglePath = function (obj) {
  519. self.setPath(obj, true);
  520. }
  521. self.showHdfs = function (obj, e) {
  522. e.preventDefault();
  523. e.stopPropagation();
  524. e.stopImmediatePropagation();
  525. self.fetchHivePath(obj.path(), function(data){
  526. location.href = "/security/hdfs#" + data.hdfs_link.substring("/filebrowser/view".length);
  527. });
  528. }
  529. self.getCheckedItems = function (leaf, checked) {
  530. if (leaf == null){
  531. leaf = self.growingTree();
  532. }
  533. if (checked == null){
  534. checked = []
  535. }
  536. if (leaf.isChecked){
  537. checked.push(leaf);
  538. }
  539. if (leaf.nodes.length > 0) {
  540. leaf.nodes.forEach(function (node) {
  541. self.getCheckedItems(node, checked);
  542. });
  543. }
  544. return checked;
  545. }
  546. self.checkPath = function (obj) {
  547. obj.isChecked(!obj.isChecked());
  548. self.updatePathProperty(self.growingTree(), obj.path(), "isChecked", obj.isChecked());
  549. self.checkedItems(self.getCheckedItems());
  550. }
  551. self.getTreeAdditionalDataForPath = function (path) {
  552. if (typeof self.treeAdditionalData[path] == "undefined") {
  553. var _add = {
  554. loaded: false,
  555. expanded: true
  556. }
  557. self.treeAdditionalData[path] = _add;
  558. }
  559. return self.treeAdditionalData[path];
  560. }
  561. self.updatePathProperty = function (leaf, path, property, value) {
  562. if (leaf.path == path) {
  563. leaf[property] = value;
  564. }
  565. if (leaf.nodes.length > 0) {
  566. leaf.nodes.forEach(function (node) {
  567. self.updatePathProperty(node, path, property, value);
  568. });
  569. }
  570. return leaf;
  571. }
  572. self.updateTreeProperty = function (leaf, property, value) {
  573. leaf[property] = value;
  574. if (leaf.nodes.length > 0) {
  575. leaf.nodes.forEach(function (node) {
  576. self.updateTreeProperty(node, property, value);
  577. });
  578. }
  579. return leaf;
  580. }
  581. self.loadParents = function (callback) {
  582. self.fetchHivePath("", function () {
  583. self.updatePathProperty(self.growingTree(), "", "isExpanded", true);
  584. var _crumbs = self.path().split(".");
  585. self.fetchHivePath(_crumbs[0], function () {
  586. self.updatePathProperty(self.growingTree(), _crumbs[0], "isExpanded", true);
  587. if (_crumbs.length > 1) {
  588. self.fetchHivePath(_crumbs[0] + "." + _crumbs[1], function () {
  589. self.updatePathProperty(self.growingTree(), _crumbs[0] + "." + _crumbs[1], "isExpanded", true);
  590. self.loadData(self.growingTree());
  591. if (typeof callback != "undefined") {
  592. callback();
  593. }
  594. else {
  595. self.collapseOthers();
  596. vm.list_sentry_privileges_by_authorizable();
  597. }
  598. });
  599. }
  600. else {
  601. self.loadData(self.growingTree());
  602. if (typeof callback != "undefined") {
  603. callback();
  604. }
  605. else {
  606. self.collapseOthers();
  607. vm.list_sentry_privileges_by_authorizable();
  608. }
  609. }
  610. });
  611. });
  612. }
  613. self.fetchHivePath = function (optionalPath, loadCallback) {
  614. self.isLoadingTree(true);
  615. var _originalPath = typeof optionalPath != "undefined" ? optionalPath : self.path();
  616. if (_originalPath.split(".").length < 3) {
  617. var _path = _originalPath.replace('.', '/');
  618. var request = {
  619. url: '/beeswax/api/autocomplete/' + _path,
  620. dataType: 'json',
  621. type: 'GET',
  622. success: function (data) {
  623. var _hasCallback = typeof loadCallback != "undefined";
  624. self.getTreeAdditionalDataForPath(_originalPath).loaded = true;
  625. if (data.databases) {
  626. self.addDatabases(_originalPath, data.databases, _hasCallback);
  627. if (vm.getPathHash() == ""){
  628. self.setPath(self.treeData().nodes()[0]);
  629. }
  630. }
  631. else if (data.tables && data.tables.length > 0) {
  632. self.addTables(_originalPath, data.tables, _hasCallback);
  633. }
  634. self.isLoadingTree(false);
  635. //else if (data.columns && data.columns.length > 0) {
  636. //self.addColumns(_originalPath, data.columns, _hasCallback);
  637. //}
  638. if (_hasCallback) {
  639. loadCallback(data);
  640. } else {
  641. vm.list_sentry_privileges_by_authorizable();
  642. }
  643. },
  644. cache: false
  645. };
  646. $.ajax(request).fail(function (xhr, textStatus, errorThrown) {
  647. $(document).trigger("error", xhr.responseText);
  648. });
  649. }
  650. else {
  651. vm.list_sentry_privileges_by_authorizable();
  652. }
  653. };
  654. self.afterRender = function () {
  655. $(document).trigger("rendered.tree");
  656. }
  657. }
  658. var HiveViewModel = function (initial) {
  659. var self = this;
  660. self.isLoadingPrivileges = ko.observable(false);
  661. self.availablePrivileges = ko.observableArray(['SERVER', 'DATABASE', 'TABLE']);
  662. self.availableActions = ko.observableArray(['SELECT', 'INSERT', 'ALL']);
  663. self.privilegeFilter = ko.observable("");
  664. // Models
  665. self.roles = ko.observableArray();
  666. self.originalRoles = ko.observableArray();
  667. self.roleFilter = ko.observable("");
  668. self.filteredRoles = ko.computed(function () {
  669. var _filter = self.roleFilter().toLowerCase();
  670. if (!_filter) {
  671. return self.roles();
  672. } else {
  673. return ko.utils.arrayFilter(self.roles(), function (role) {
  674. var _inGroups = false;
  675. role.groups().forEach(function (group) {
  676. if (group.toLowerCase().indexOf(_filter) > -1) {
  677. _inGroups = true;
  678. }
  679. });
  680. var _inPrivileges = false;
  681. role.privileges().forEach(function (priv) {
  682. if (priv.dbName().toLowerCase().indexOf(_filter) > -1 || priv.tableName().toLowerCase().indexOf(_filter) > -1) {
  683. _inPrivileges = true;
  684. }
  685. });
  686. return role.name().toLowerCase().indexOf(_filter) > -1 || role.grantorPrincipal().toLowerCase().indexOf(_filter) > -1 || _inGroups || _inPrivileges;
  687. });
  688. }
  689. }, self);
  690. self.selectableRoles = ko.computed(function () {
  691. var _roles = ko.utils.arrayMap(self.roles(), function (role) {
  692. return role.name();
  693. });
  694. return _roles.sort();
  695. }, self);
  696. self.availableHadoopGroups = ko.observableArray();
  697. self.assist = new Assist(self, initial);
  698. // Editing
  699. self.showCreateRole = ko.observable(false);
  700. self.role = ko.observable(new Role(self, {}));
  701. self.resetCreateRole = function() {
  702. self.roles(self.originalRoles());
  703. self.role(new Role(self, {}));
  704. $(document).trigger("create.typeahead");
  705. };
  706. self.privilege = new Privilege(self, {});
  707. self.doAs = ko.observable(initial.user);
  708. self.doAs.subscribe(function () {
  709. self.assist.fetchHivePath();
  710. });
  711. self.availableHadoopUsers = ko.observableArray();
  712. self.selectableHadoopUsers = ko.computed(function () {
  713. var _users = ko.utils.arrayMap(self.availableHadoopUsers(), function (user) {
  714. return user.username;
  715. });
  716. return _users.sort();
  717. }, self);
  718. self.selectableHadoopGroups = ko.computed(function () {
  719. var _groups = ko.utils.arrayMap(self.availableHadoopGroups(), function (group) {
  720. return group.name;
  721. });
  722. _groups.push("");
  723. return _groups.sort();
  724. }, self);
  725. self.selectAllRoles = function () {
  726. self.allRolesSelected(!self.allRolesSelected());
  727. ko.utils.arrayForEach(self.roles(), function (role) {
  728. role.selected(self.allRolesSelected());
  729. });
  730. return true;
  731. };
  732. self.allRolesSelected = ko.observable(false);
  733. self.selectedRoles = ko.computed(function () {
  734. return ko.utils.arrayFilter(self.roles(), function (role) {
  735. return role.selected();
  736. });
  737. }, self);
  738. self.selectedRole = ko.computed(function () {
  739. return self.selectedRoles()[0];
  740. }, self);
  741. self.deleteSelectedRoles = function () {
  742. ko.utils.arrayForEach(self.selectedRoles(), function (role) {
  743. role.remove(role);
  744. });
  745. $(document).trigger("deleted.role");
  746. };
  747. self.expandSelectedRoles = function () {
  748. ko.utils.arrayForEach(self.selectedRoles(), function (role) {
  749. if (!role.showPrivileges()) {
  750. self.list_sentry_privileges_by_role(role);
  751. }
  752. });
  753. };
  754. self.expandedRoles = ko.observableArray([]);
  755. self.init = function (path) {
  756. self.assist.isLoadingTree(true);
  757. self.fetchUsers();
  758. self.assist.path(path);
  759. self.list_sentry_roles_by_group();
  760. if (path != "") {
  761. self.assist.loadParents();
  762. } else {
  763. self.assist.fetchHivePath();
  764. }
  765. };
  766. self.removeRole = function (roleName) {
  767. $.each(self.roles(), function (index, role) {
  768. if (role.name() == roleName) {
  769. self.roles.remove(role);
  770. return false;
  771. }
  772. });
  773. $.each(self.originalRoles(), function (index, role) {
  774. if (role.name() == roleName) {
  775. self.originalRoles.remove(role);
  776. return false;
  777. }
  778. });
  779. };
  780. self.list_sentry_roles_by_group = function () {
  781. $.ajax({
  782. type: "POST",
  783. url: "/security/api/hive/list_sentry_roles_by_group",
  784. data: {
  785. 'groupName': $('#selectedGroup').val()
  786. },
  787. success: function (data) {
  788. if (typeof data.status !== "undefined" && data.status == -1) {
  789. $(document).trigger("error", data.message);
  790. }
  791. else {
  792. self.roles.removeAll();
  793. $.each(data.roles, function (index, item) {
  794. self.roles.push(new Role(self, item));
  795. self.originalRoles.push(new Role(self, item));
  796. });
  797. }
  798. }
  799. }).fail(function (xhr, textStatus, errorThrown) {
  800. $(document).trigger("error", xhr.responseText);
  801. });
  802. };
  803. self.refreshExpandedRoles = function () {
  804. ko.utils.arrayForEach(self.filteredRoles(), function (r) {
  805. if (self.expandedRoles().indexOf(r.name()) > -1){
  806. self.list_sentry_privileges_by_role(r);
  807. }
  808. });
  809. }
  810. self.showRole = function (role) {
  811. $(document).trigger("show.role", role);
  812. ko.utils.arrayForEach(self.filteredRoles(), function (r) {
  813. if (r.name() == role.name()){
  814. self.list_sentry_privileges_by_role(r);
  815. }
  816. });
  817. }
  818. self.list_sentry_privileges_by_role = function (role) {
  819. $.ajax({
  820. type: "POST",
  821. url: "/security/api/hive/list_sentry_privileges_by_role",
  822. data: {
  823. 'roleName': role.name
  824. },
  825. success: function (data) {
  826. if (typeof data.status !== "undefined" && data.status == -1) {
  827. $(document).trigger("error", data.message);
  828. }
  829. else {
  830. role.privileges.removeAll();
  831. role.originalPrivileges.removeAll();
  832. $.each(data.sentry_privileges, function (index, item) {
  833. var privilege = _create_ko_privilege(item);
  834. var privilegeCopy = _create_ko_privilege(item);
  835. privilegeCopy.id(privilege.id());
  836. role.privileges.push(privilege);
  837. role.originalPrivileges.push(privilegeCopy);
  838. });
  839. role.showPrivileges(true);
  840. }
  841. }
  842. }).fail(function (xhr, textStatus, errorThrown) {
  843. $(document).trigger("error", xhr.responseText);
  844. });
  845. };
  846. function _create_ko_privilege(privilege) {
  847. var _privilege = new Privilege(self, {
  848. 'privilegeScope': privilege.scope,
  849. 'serverName': privilege.server,
  850. 'dbName': privilege.database,
  851. 'tableName': privilege.table,
  852. 'URI': privilege.URI,
  853. 'action': privilege.action,
  854. 'timestamp': privilege.timestamp,
  855. 'roleName': privilege.roleName,
  856. 'id': UUID()
  857. });
  858. return _privilege;
  859. }
  860. function _create_authorizable_from_ko(optionalPath) {
  861. if (optionalPath != null){
  862. return {
  863. 'server': self.assist.server(),
  864. 'db': optionalPath.split(/[.]/)[0],
  865. 'table': optionalPath.split(/[.]/)[1]
  866. }
  867. }
  868. return {
  869. 'server': self.assist.server(),
  870. 'db': self.assist.db(),
  871. 'table': self.assist.table()
  872. }
  873. }
  874. self.list_sentry_privileges_by_authorizable = function (optionalPath, skipList) {
  875. var _path = self.assist.path();
  876. if (optionalPath != null){
  877. _path = optionalPath;
  878. }
  879. self.isLoadingPrivileges(true);
  880. self.assist.roles.removeAll();
  881. self.assist.privileges.removeAll();
  882. $.ajax({
  883. type: "POST",
  884. url: "/security/api/hive/list_sentry_privileges_by_authorizable",
  885. data: {
  886. groupName: $('#selectedGroup').val(),
  887. roleSet: ko.mapping.toJSON({all: true, roles: []}),
  888. authorizableHierarchy: ko.mapping.toJSON(_create_authorizable_from_ko(_path))
  889. },
  890. success: function (data) {
  891. var _privileges = [];
  892. $.each(data.privileges, function (index, item) {
  893. if (item.table != ""){
  894. self.assist.updatePathProperty(self.assist.growingTree(), item.database + "." + item.table, "withPrivileges", true);
  895. }
  896. if (typeof skipList == "undefined" || (skipList != null && typeof skipList == "Boolean" && !skipList)){
  897. var _role = null;
  898. self.assist.roles().forEach(function (role) {
  899. if (role.name() == item.roleName) {
  900. _role = role;
  901. }
  902. });
  903. if (_role == null) {
  904. var _idx = self.assist.roles.push(new Role(self, { name: item.roleName }));
  905. _role = self.assist.roles()[_idx - 1];
  906. }
  907. var privilege = _create_ko_privilege(item);
  908. var privilegeCopy = _create_ko_privilege(item);
  909. privilegeCopy.id(privilege.id());
  910. _role.privileges.push(privilege);
  911. _role.originalPrivileges.push(privilegeCopy);
  912. _privileges.push(privilege);
  913. }
  914. });
  915. if (typeof skipList == "undefined" || (skipList != null && typeof skipList == "Boolean" && !skipList)) {
  916. self.assist.privileges(_privileges);
  917. }
  918. self.isLoadingPrivileges(false);
  919. self.assist.loadData(self.assist.growingTree());
  920. }
  921. }).fail(function (xhr, textStatus, errorThrown) {
  922. $(document).trigger("error", xhr.responseText);
  923. });
  924. };
  925. self.bulkAction = ko.observable("");
  926. self.bulkPerfomAction = function () {
  927. switch (self.bulkAction()) {
  928. case "add":
  929. self.bulk_add_privileges();
  930. break;
  931. case "sync":
  932. self.bulk_delete_privileges();
  933. self.bulk_add_privileges();
  934. break;
  935. case "delete":
  936. self.bulk_delete_privileges();
  937. break;
  938. }
  939. self.bulkAction("");
  940. }
  941. self.bulk_delete_privileges = function (role) {
  942. $(".jHueNotify").hide();
  943. var checkedPaths = self.assist.checkedItems();
  944. $.post("/security/api/hive/bulk_delete_privileges", {
  945. 'authorizableHierarchy': ko.mapping.toJSON(_create_authorizable_from_ko()),
  946. 'checkedPaths': ko.mapping.toJSON(checkedPaths),
  947. 'recursive': false
  948. }, function (data) {
  949. if (data.status == 0) {
  950. ko.utils.arrayForEach(self.assist.checkedItems(), function (item) {
  951. self.assist.updatePathProperty(self.assist.growingTree(), item.path, "withPrivileges", false);
  952. });
  953. self.list_sentry_privileges_by_authorizable(); // Refresh
  954. $(document).trigger("deleted.bulk.privileges");
  955. } else {
  956. $(document).trigger("error", data.message);
  957. }
  958. }).fail(function (xhr, textStatus, errorThrown) {
  959. $(document).trigger("error", xhr.responseText);
  960. });
  961. }
  962. self.bulk_add_privileges = function (role) {
  963. $(".jHueNotify").hide();
  964. var checkedPaths = self.assist.checkedItems();
  965. $.post("/security/api/hive/bulk_add_privileges", {
  966. 'privileges': ko.mapping.toJSON(self.assist.privileges),
  967. 'authorizableHierarchy': ko.mapping.toJSON(_create_authorizable_from_ko()),
  968. 'checkedPaths': ko.mapping.toJSON(checkedPaths),
  969. 'recursive': false
  970. }, function (data) {
  971. if (data.status == 0) {
  972. ko.utils.arrayForEach(self.assist.checkedItems(), function (item) {
  973. self.assist.updatePathProperty(self.assist.growingTree(), item.path, "withPrivileges", true);
  974. });
  975. self.list_sentry_privileges_by_authorizable(); // Refresh
  976. $(document).trigger("added.bulk.privileges");
  977. } else {
  978. $(document).trigger("error", data.message);
  979. }
  980. }).fail(function (xhr, textStatus, errorThrown) {
  981. $(document).trigger("error", xhr.responseText);
  982. });
  983. }
  984. self.bulk_refresh_privileges= function () {
  985. ko.utils.arrayForEach(self.assist.checkedItems(), function (item) {
  986. self.list_sentry_privileges_by_authorizable(item.path, true);
  987. });
  988. };
  989. self.fetchUsers = function () {
  990. $.getJSON('/desktop/api/users/autocomplete', {
  991. 'include_myself': true,
  992. 'extend_user': true
  993. }, function (data) {
  994. self.availableHadoopUsers(data.users);
  995. self.availableHadoopGroups(data.groups);
  996. $(document).trigger("loaded.users");
  997. });
  998. }
  999. self.updatePathHash = function (path) {
  1000. var _hash = window.location.hash;
  1001. if (_hash.indexOf("@") == -1) {
  1002. window.location.hash = path;
  1003. }
  1004. else {
  1005. window.location.hash = path + "@" + _hash.split("@")[1];
  1006. }
  1007. }
  1008. self.updateSectionHash = function (section) {
  1009. var _hash = window.location.hash;
  1010. if (_hash == "") {
  1011. window.location.hash = "@" + section;
  1012. }
  1013. if (_hash.indexOf("@") == -1) {
  1014. window.location.hash = _hash + "@" + section;
  1015. }
  1016. else {
  1017. window.location.hash = _hash.split("@")[0] + "@" + section;
  1018. }
  1019. }
  1020. self.getPathHash = function () {
  1021. if (window.location.hash != "") {
  1022. var _hash = window.location.hash.substr(1);
  1023. if (_hash.indexOf("@") > -1) {
  1024. return _hash.split("@")[0];
  1025. }
  1026. else {
  1027. return _hash;
  1028. }
  1029. }
  1030. return "";
  1031. }
  1032. self.getSectionHash = function () {
  1033. if (window.location.hash != "") {
  1034. var _hash = window.location.hash.substr(1);
  1035. if (_hash.indexOf("@") > -1) {
  1036. return _hash.split("@")[1];
  1037. }
  1038. }
  1039. return "edit";
  1040. }
  1041. };
  1042. function logGA(page) {
  1043. if (typeof trackOnGA == 'function') {
  1044. trackOnGA('security/' + page);
  1045. }
  1046. }