hive.ko.js 37 KB

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