hive.ko.js 20 KB

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