hive.ko.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. var Privilege = function (vm, privilege) {
  17. var self = this;
  18. self.status = ko.observable(typeof privilege.status != "undefined" && privilege.status != null ? privilege.status : "");
  19. self.editing = ko.observable(typeof privilege.editing != "undefined" && privilege.editing != null ? privilege.editing : false);
  20. //self.privilegeScope = ko.observable(typeof privilege.privilegeScope != "undefined" && privilege.privilegeScope != null ? privilege.privilegeScope : "");
  21. self.serverName = ko.observable(typeof privilege.serverName != "undefined" && privilege.serverName != null ? privilege.serverName : "");
  22. self.serverName.subscribe(function () {
  23. if (self.status() == '') {
  24. self.status('modified');
  25. }
  26. });
  27. self.dbName = ko.observable(typeof privilege.dbName != "undefined" && privilege.dbName != null ? privilege.dbName : "");
  28. self.dbName.subscribe(function () {
  29. if (self.status() == '') {
  30. self.status('modified');
  31. }
  32. });
  33. self.tableName = ko.observable(typeof privilege.tableName != "undefined" && privilege.tableName != null ? privilege.tableName : "");
  34. self.tableName.subscribe(function () {
  35. if (self.status() == '') {
  36. self.status('modified');
  37. }
  38. });
  39. self.URI = ko.observable(typeof privilege.URI != "undefined" && privilege.URI != null ? privilege.URI : "");
  40. self.URI.subscribe(function () {
  41. if (self.status() == '') {
  42. self.status('modified');
  43. }
  44. });
  45. self.action = ko.observable(typeof privilege.action != "undefined" && privilege.action != null ? privilege.action : "");
  46. self.action.subscribe(function () {
  47. if (self.status() == '') {
  48. self.status('modified');
  49. }
  50. });
  51. // UI
  52. self.showAdvanced = ko.observable(false);
  53. self.path = ko.computed({
  54. read: function () {
  55. if (self.tableName().length > 0) {
  56. return self.dbName() + "." + self.tableName();
  57. } else {
  58. return self.dbName();
  59. }
  60. },
  61. write: function (value) {
  62. var lastSpacePos = value.lastIndexOf(".");
  63. if (lastSpacePos > 0) {
  64. this.dbName(value.substring(0, lastSpacePos));
  65. this.tableName(value.substring(lastSpacePos + 1));
  66. } else {
  67. this.dbName(value);
  68. this.tableName('');
  69. }
  70. },
  71. owner: self
  72. });
  73. self.privilegeScope = ko.computed(function() {
  74. if (self.tableName().length > 0) {
  75. return 'TABLE';
  76. } else if (self.dbName().length > 0) {
  77. return 'DATABASE';
  78. } else {
  79. return 'SERVER';
  80. }
  81. });
  82. self.remove = function (privilege) {
  83. privilege.status('deleted');
  84. }
  85. }
  86. var Role = function (vm, role) {
  87. var self = this;
  88. self.name = ko.observable(typeof role.name != "undefined" && role.name != null ? role.name : "");
  89. self.grantorPrincipal = ko.observable(typeof role.grantorPrincipal != "undefined" && role.grantorPrincipal != null ? role.grantorPrincipal : "");
  90. self.groups = ko.observableArray();
  91. $.each(typeof role.groups != "undefined" && role.groups != null ? role.groups : [], function (index, group) {
  92. self.groups.push(group);
  93. });
  94. self.privileges = ko.observableArray(); // Not included in the API
  95. self.originalPrivileges = ko.observableArray();
  96. self.showPrivileges = ko.observable(false);
  97. self.privilegesChanged = ko.computed(function () {
  98. return $.grep(self.privileges(), function (privilege) {
  99. return ['new', 'deleted', 'modified'].indexOf(privilege.status()) != -1;
  100. });
  101. });
  102. self.reset = function () {
  103. self.name('');
  104. self.groups.removeAll();
  105. self.privileges.removeAll();
  106. self.originalPrivileges.removeAll();
  107. }
  108. self.addGroup = function () {
  109. self.groups.push('');
  110. }
  111. self.addPrivilege = function () {
  112. self.privileges.push(new Privilege(vm, {'serverName': vm.assist.server(), 'status': 'new', 'editing': true}));
  113. }
  114. self.create = function () {
  115. $(".jHueNotify").hide();
  116. $.post("/security/api/hive/create_role", {
  117. role: ko.mapping.toJSON(self)
  118. }, function (data) {
  119. if (data.status == 0) {
  120. $(document).trigger("info", data.message);
  121. vm.roles.unshift(new Role(vm, data.role));
  122. self.reset();
  123. vm.showCreateRole(false);
  124. } else {
  125. $(document).trigger("error", data.message);
  126. }
  127. }).fail(function (xhr, textStatus, errorThrown) {
  128. $(document).trigger("error", xhr.responseText);
  129. });
  130. }
  131. self.remove = function (role) {
  132. $(".jHueNotify").hide();
  133. $.post("/security/api/hive/drop_sentry_role", {
  134. roleName: role.name
  135. }, function (data) {
  136. if (data.status == 0) {
  137. $(document).trigger("info", data.message);
  138. vm.removeRole(role.name);
  139. }
  140. else {
  141. $(document).trigger("error", data.message);
  142. }
  143. }).fail(function (xhr, textStatus, errorThrown) {
  144. $(document).trigger("error", xhr.responseText);
  145. });
  146. }
  147. self.savePrivileges = function (role) {
  148. $(".jHueNotify").hide();
  149. $.post("/security/api/hive/save_privileges", {
  150. role: ko.mapping.toJSON(role)
  151. }, function (data) {
  152. if (data.status == 0) {
  153. vm.list_sentry_privileges_by_role(role); // Refresh all role privileges
  154. } else {
  155. $(document).trigger("error", data.message);
  156. }
  157. }).fail(function (xhr, textStatus, errorThrown) {
  158. $(document).trigger("error", xhr.responseText);
  159. });
  160. }
  161. }
  162. var Assist = function (vm) {
  163. var self = this;
  164. self.compareNames = function (a, b) {
  165. if (a.name.toLowerCase() < b.name.toLowerCase())
  166. return -1;
  167. if (a.name.toLowerCase() > b.name.toLowerCase())
  168. return 1;
  169. return 0;
  170. }
  171. self.path = ko.observable("");
  172. self.path.subscribe(function (path) {
  173. //self.fetchHivePath();
  174. window.location.hash = path;
  175. });
  176. self.server = ko.observable('');
  177. self.db = ko.computed(function () {
  178. return self.path().split(/[.]/)[0];
  179. });
  180. self.table = ko.computed(function () {
  181. return self.path().split(/[.]/)[1];
  182. });
  183. self.privileges = ko.observableArray();
  184. self.isDiffMode = ko.observable(false);
  185. self.isDiffMode = ko.observable(false);
  186. self.isDiffMode.subscribe(function () {
  187. //self.refreshTree();
  188. });
  189. self.isLoadingTree = ko.observable(false);
  190. self.treeAdditionalData = {};
  191. self.treeData = ko.observable({nodes: []});
  192. self.loadData = function (data) {
  193. self.treeData(new TreeNodeModel(data));
  194. };
  195. self.initialGrowingTree = {
  196. path: "__HUEROOT__",
  197. name: "__HUEROOT__",
  198. selected: false,
  199. nodes: []
  200. };
  201. self.growingTree = ko.observable(jQuery.extend(true, {}, self.initialGrowingTree));
  202. self.addDatabases = function (path, databases, skipLoading) {
  203. var _tree = self.growingTree();
  204. databases.forEach(function (db) {
  205. var _mainFound = false;
  206. _tree.nodes.forEach(function (node) {
  207. if (node.path == db) {
  208. _mainFound = true;
  209. }
  210. });
  211. if (!_mainFound) {
  212. var _item = {
  213. path: db,
  214. name: db,
  215. isDb: true,
  216. isTable: false,
  217. isColumn: false,
  218. isExpanded: false,
  219. nodes: []
  220. };
  221. _tree.nodes.push(_item);
  222. }
  223. });
  224. if (typeof skipLoading == "undefined" || !skipLoading){
  225. self.loadData(self.growingTree());
  226. }
  227. }
  228. self.addTables = function (path, tables, skipLoading) {
  229. var _branch = self.growingTree();
  230. _branch.nodes.forEach(function (node) {
  231. if (node.path == path) {
  232. _branch = node;
  233. }
  234. });
  235. tables.forEach(function (table) {
  236. var _mainFound = false;
  237. var _path = path + "." + table;
  238. _branch.nodes.forEach(function (node) {
  239. if (node.path == _path) {
  240. _mainFound = true;
  241. }
  242. });
  243. if (!_mainFound) {
  244. var _item = {
  245. path: _path,
  246. name: table,
  247. isDb: false,
  248. isTable: true,
  249. isColumn: false,
  250. isExpanded: false,
  251. nodes: []
  252. };
  253. _branch.nodes.push(_item);
  254. }
  255. });
  256. if (typeof skipLoading == "undefined" || !skipLoading){
  257. self.loadData(self.growingTree());
  258. }
  259. }
  260. self.addColumns = function (path, columns, skipLoading) {
  261. var _branch = self.growingTree();
  262. _branch.nodes.forEach(function (node) {
  263. if (node.path == path.split(".")[0]) {
  264. node.nodes.forEach(function (inode) {
  265. if (inode.path == path) {
  266. _branch = inode;
  267. }
  268. });
  269. }
  270. });
  271. columns.forEach(function (column) {
  272. var _mainFound = false;
  273. var _path = path + "." + column;
  274. _branch.nodes.forEach(function (node) {
  275. if (node.path == _path) {
  276. _mainFound = true;
  277. }
  278. });
  279. if (!_mainFound) {
  280. var _item = {
  281. path: _path,
  282. name: column,
  283. isDb: false,
  284. isTable: false,
  285. isColumn: true,
  286. isExpanded: false,
  287. nodes: []
  288. };
  289. _branch.nodes.push(_item);
  290. }
  291. });
  292. if (typeof skipLoading == "undefined" || !skipLoading){
  293. self.loadData(self.growingTree());
  294. }
  295. }
  296. self.collapseTree = function () {
  297. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  298. self.updatePathProperty(self.growingTree(), "__HUEROOT__", "isExpanded", true);
  299. self.loadData(self.growingTree());
  300. }
  301. self.collapseOthers = function () {
  302. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  303. self.updatePathProperty(self.growingTree(), "__HUEROOT__", "isExpanded", true);
  304. var _path = self.path();
  305. var _crumb = "";
  306. for (var i = 0; i < _path.length; i++) {
  307. if ((_path[i] === "." && _crumb != "")) {
  308. self.updatePathProperty(self.growingTree(), _crumb, "isExpanded", true);
  309. }
  310. _crumb += _path[i];
  311. }
  312. self.updatePathProperty(self.growingTree(), _path, "isExpanded", true);
  313. self.loadData(self.growingTree());
  314. }
  315. self.expandTree = function () {
  316. self.updateTreeProperty(self.growingTree(), "isExpanded", true);
  317. self.loadData(self.growingTree());
  318. }
  319. self.refreshTree = function (force) {
  320. self.growingTree(jQuery.extend(true, {}, self.initialGrowingTree));
  321. // load root first
  322. self.fetchHivePath("", function(){
  323. Object.keys(self.treeAdditionalData).forEach(function (path) {
  324. if (path.indexOf(".") == -1 && path != ""){
  325. if (typeof force == "boolean" && force) {
  326. self.fetchHivePath(path);
  327. }
  328. else {
  329. if (self.treeAdditionalData[path].loaded) {
  330. self.fetchHivePath(path, function(){
  331. Object.keys(self.treeAdditionalData).forEach(function (ipath) {
  332. if (ipath.split(".").length == 2 && ipath.split(".")[0] == path){
  333. self.fetchHivePath(ipath, function(){
  334. self.updateTreeProperty(self.growingTree(), "isExpanded", true);
  335. });
  336. }
  337. });
  338. });
  339. }
  340. }
  341. }
  342. });
  343. });
  344. }
  345. self.setPath = function (obj, toggle) {
  346. if (self.getTreeAdditionalDataForPath(obj.path()).loaded || (!obj.isExpanded() && !self.getTreeAdditionalDataForPath(obj.path()).loaded)) {
  347. if (typeof toggle == "boolean" && toggle) {
  348. obj.isExpanded(!obj.isExpanded());
  349. }
  350. else {
  351. obj.isExpanded(true);
  352. }
  353. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  354. }
  355. self.path(obj.path());
  356. self.fetchHivePath();
  357. }
  358. self.togglePath = function (obj) {
  359. self.setPath(obj, true);
  360. }
  361. self.getTreeAdditionalDataForPath = function (path) {
  362. if (typeof self.treeAdditionalData[path] == "undefined") {
  363. var _add = {
  364. loaded: false
  365. }
  366. self.treeAdditionalData[path] = _add;
  367. }
  368. return self.treeAdditionalData[path];
  369. }
  370. self.updatePathProperty = function (leaf, path, property, value) {
  371. if (leaf.path == path) {
  372. leaf[property] = value;
  373. }
  374. if (leaf.nodes.length > 0) {
  375. leaf.nodes.forEach(function (node) {
  376. self.updatePathProperty(node, path, property, value);
  377. });
  378. }
  379. return leaf;
  380. }
  381. self.updateTreeProperty = function (leaf, property, value) {
  382. leaf[property] = value;
  383. if (leaf.nodes.length > 0) {
  384. leaf.nodes.forEach(function (node) {
  385. self.updateTreeProperty(node, property, value);
  386. });
  387. }
  388. return leaf;
  389. }
  390. self.loadParents = function (callback) {
  391. self.fetchHivePath("", function(){
  392. self.updatePathProperty(self.growingTree(), "", "isExpanded", true);
  393. var _crumbs = self.path().split(".");
  394. self.fetchHivePath(_crumbs[0], function(){
  395. self.updatePathProperty(self.growingTree(), _crumbs[0], "isExpanded", true);
  396. if (_crumbs.length > 1){
  397. self.fetchHivePath(_crumbs[0] + "." + _crumbs[1], function(){
  398. self.updatePathProperty(self.growingTree(), _crumbs[0] + "." + _crumbs[1], "isExpanded", true);
  399. self.loadData(self.growingTree());
  400. if (typeof callback != "undefined"){
  401. callback();
  402. }
  403. else {
  404. self.collapseOthers();
  405. }
  406. });
  407. }
  408. else {
  409. self.loadData(self.growingTree());
  410. if (typeof callback != "undefined"){
  411. callback();
  412. }
  413. else {
  414. self.collapseOthers();
  415. }
  416. }
  417. });
  418. });
  419. }
  420. self.fetchHivePath = function (optionalPath, loadCallback) {
  421. var _originalPath = typeof optionalPath != "undefined" ? optionalPath : self.path();
  422. if (_originalPath.split(".").length < 3) {
  423. var _path = _originalPath.replace('.', '/');
  424. var request = {
  425. url: '/beeswax/api/autocomplete/' + _path,
  426. dataType: 'json',
  427. type: 'GET',
  428. success: function (data) {
  429. var _hasCallback = typeof loadCallback != "undefined";
  430. //self.getTreeAdditionalDataForPath(self.path()).loaded = true;
  431. self.getTreeAdditionalDataForPath(_originalPath).loaded = true;
  432. if (data.databases) {
  433. self.addDatabases(_originalPath, data.databases, _hasCallback);
  434. }
  435. else if (data.tables && data.tables.length > 0) {
  436. self.addTables(_originalPath, data.tables, _hasCallback);
  437. }
  438. else if (data.columns && data.columns.length > 0) {
  439. self.addColumns(_originalPath, data.columns, _hasCallback);
  440. }
  441. if (_hasCallback){
  442. loadCallback(data);
  443. }
  444. else {
  445. vm.list_sentry_privileges_by_authorizable();
  446. }
  447. },
  448. cache: false
  449. };
  450. $.ajax(request).fail(function (xhr, textStatus, errorThrown) {
  451. $(document).trigger("error", xhr.responseText);
  452. });
  453. }
  454. else {
  455. vm.list_sentry_privileges_by_authorizable();
  456. }
  457. };
  458. self.afterRender = function () {
  459. $(document).trigger("rendered.tree");
  460. }
  461. }
  462. var HiveViewModel = function (initial) {
  463. var self = this;
  464. self.availablePrivileges = ko.observableArray(['SERVER', 'DATABASE', 'TABLE']);
  465. self.availableActions = ko.observableArray(['SELECT', 'INSERT', 'ALL', '']);
  466. // Models
  467. self.roles = ko.observableArray();
  468. self.availableHadoopGroups = ko.mapping.fromJS(initial.hadoop_groups);
  469. self.assist = new Assist(self);
  470. // Editing
  471. self.showCreateRole = ko.observable(false);
  472. self.role = new Role(self, {});
  473. self.privilege = new Privilege(self, {});
  474. self.doAs = ko.observable(initial.user);
  475. self.doAs.subscribe(function () {
  476. self.assist.fetchHivePath();
  477. });
  478. self.availableHadoopUsers = ko.observableArray();
  479. self.selectableHadoopUsers = ko.computed(function () {
  480. var _users = ko.utils.arrayMap(self.availableHadoopUsers(), function (user) {
  481. return user.username;
  482. });
  483. return _users.sort();
  484. }, self);
  485. self.selectableHadoopGroups = ko.computed(function () {
  486. var _users = ko.utils.arrayMap(self.availableHadoopGroups(), function (group) {
  487. return group.name;
  488. });
  489. return _users.sort();
  490. }, self);
  491. self.init = function (path) {
  492. self.fetchUsers();
  493. self.assist.path(path);
  494. self.list_sentry_roles_by_group();
  495. if (path != ""){
  496. self.assist.loadParents();
  497. }
  498. else {
  499. self.assist.fetchHivePath();
  500. }
  501. };
  502. self.removeRole = function (roleName) {
  503. $.each(self.roles(), function (index, role) {
  504. if (role.name == roleName) {
  505. self.roles.remove(role);
  506. return false;
  507. }
  508. });
  509. };
  510. self.list_sentry_roles_by_group = function () {
  511. $.getJSON('/security/api/hive/list_sentry_roles_by_group', function (data) {
  512. if (typeof data.status !== "undefined" && data.status == -1) {
  513. $(document).trigger("error", data.message);
  514. }
  515. else {
  516. $.each(data.roles, function (index, item) {
  517. self.roles.push(new Role(self, item));
  518. });
  519. }
  520. }).fail(function (xhr, textStatus, errorThrown) {
  521. $(document).trigger("error", xhr.responseText);
  522. });
  523. };
  524. self.list_sentry_privileges_by_role = function (role) {
  525. $.ajax({
  526. type: "POST",
  527. url: "/security/api/hive/list_sentry_privileges_by_role",
  528. data: {
  529. 'roleName': role.name
  530. },
  531. success: function (data) {
  532. if (typeof data.status !== "undefined" && data.status == -1) {
  533. $(document).trigger("error", data.message);
  534. }
  535. else {
  536. role.privileges.removeAll();
  537. role.originalPrivileges.removeAll();
  538. $.each(data.sentry_privileges, function (index, item) {
  539. var privilege = _create_ko_privilege(item);
  540. role.privileges.push(privilege);
  541. role.originalPrivileges.push(privilege);
  542. });
  543. role.showPrivileges(true);
  544. }
  545. }
  546. }).fail(function (xhr, textStatus, errorThrown) {
  547. $(document).trigger("error", xhr.responseText);
  548. });
  549. };
  550. function _create_ko_privilege(privilege) {
  551. var _privilege = new Privilege(self, {
  552. 'privilegeScope': privilege.scope,
  553. 'serverName': privilege.server,
  554. 'dbName': privilege.database,
  555. 'tableName': privilege.table,
  556. 'URI': privilege.URI,
  557. 'action': privilege.action
  558. });
  559. _privilege.properties = ko.mapping.fromJS(privilege);
  560. return _privilege;
  561. }
  562. self.list_sentry_privileges_by_authorizable = function () {
  563. if (self.assist.path() != ""){
  564. $.ajax({
  565. type: "POST",
  566. url: "/security/api/hive/list_sentry_privileges_by_authorizable",
  567. data: {
  568. groups: ko.mapping.toJSON(['sambashare', 'hadoop']),
  569. roleSet: ko.mapping.toJSON({all: true, roles: []}),
  570. authorizableHierarchy: ko.mapping.toJSON({
  571. 'server': self.assist.server(),
  572. 'db': self.assist.db(),
  573. 'table': self.assist.table()
  574. })
  575. },
  576. success: function (data) {
  577. self.assist.privileges.removeAll();
  578. $.each(data.privileges, function (index, item) {
  579. self.assist.privileges.push(_create_ko_privilege(item));
  580. });
  581. }
  582. }).fail(function (xhr, textStatus, errorThrown) {
  583. $(document).trigger("error", xhr.responseText);
  584. });
  585. }
  586. };
  587. self.fetchUsers = function () {
  588. $.getJSON('/desktop/api/users/autocomplete', {
  589. 'include_myself': true,
  590. 'extend_user': true
  591. }, function (data) {
  592. self.availableHadoopUsers(data.users);
  593. self.availableHadoopGroups(data.groups);
  594. $(document).trigger("loaded.users");
  595. });
  596. }
  597. };
  598. function logGA(page) {
  599. if (typeof trackOnGA == 'function') {
  600. trackOnGA('security/' + page);
  601. }
  602. }