hive.ko.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.privilegeScope = ko.observable(typeof privilege.privilegeScope != "undefined" && privilege.privilegeScope != null ? privilege.privilegeScope : "");
  19. self.serverName = ko.observable(typeof privilege.serverName != "undefined" && privilege.serverName != null ? privilege.serverName : "");
  20. self.dbName = ko.observable(typeof privilege.dbName != "undefined" && privilege.dbName != null ? privilege.dbName : "");
  21. self.tableName = ko.observable(typeof privilege.tableName != "undefined" && privilege.tableName != null ? privilege.tableName : "");
  22. self.URI = ko.observable(typeof privilege.URI != "undefined" && privilege.URI != null ? privilege.URI : "");
  23. self.action = ko.observable(typeof privilege.action != "undefined" && privilege.action != null ? privilege.action : "");
  24. self.status = ko.observable(typeof privilege.status != "undefined" && privilege.status != null ? privilege.status : "");
  25. self.edition = ko.observable(typeof privilege.edition != "undefined" && privilege.edition != null ? privilege.edition : false);
  26. self.availablePrivileges = ko.observableArray(['SERVER', 'DATABASE', 'TABLE']);
  27. self.availableActions = ko.observableArray(['SELECT', 'INSERT', 'ALL', '']);
  28. self.remove = function(privilege) {
  29. privilege.status('deleted');
  30. }
  31. }
  32. var Role = function(vm, role) {
  33. var self = this;
  34. self.name = ko.observable(typeof role.name != "undefined" && role.name != null ? role.name : "");
  35. self.grantorPrincipal = ko.observable(typeof role.grantorPrincipal != "undefined" && role.grantorPrincipal != null ? role.grantorPrincipal : "");
  36. self.groups = ko.observableArray();
  37. $.each(typeof role.groups != "undefined" && role.groups != null ? role.groups : [], function(index, group) {
  38. self.groups.push(group);
  39. });
  40. self.privileges = ko.observableArray(); // Not included in the API
  41. self.showPrivileges = ko.observable(false);
  42. self.privilegesChanged = ko.computed(function () {
  43. return $.grep(self.privileges(), function (privilege) {
  44. return ['new', 'deleted', 'modified'].indexOf(privilege.status()) != -1;
  45. });
  46. });
  47. self.reset = function() {
  48. self.name('');
  49. self.groups.removeAll();
  50. self.privileges.removeAll();
  51. }
  52. self.addGroup = function() {
  53. self.groups.push('');
  54. }
  55. self.addPrivilege = function() {
  56. self.privileges.push(new Privilege(vm, {'serverName': vm.assist.server(), 'status': 'new', 'edition': true}));
  57. }
  58. self.create = function() {
  59. $(".jHueNotify").hide();
  60. $.post("/security/api/hive/create_role", {
  61. role: ko.mapping.toJSON(self)
  62. }, function (data) {
  63. if (data.status == 0) {
  64. $(document).trigger("info", data.message);
  65. vm.roles.unshift(new Role(vm, data.role));
  66. self.reset();
  67. vm.showCreateRole(false);
  68. }
  69. else {
  70. $(document).trigger("error", data.message);
  71. }
  72. }).fail(function (xhr, textStatus, errorThrown) {
  73. $(document).trigger("error", xhr.responseText);
  74. });
  75. }
  76. self.remove = function(role) {
  77. $(".jHueNotify").hide();
  78. $.post("/security/api/hive/drop_sentry_role", {
  79. roleName: role.name
  80. }, function (data) {
  81. if (data.status == 0) {
  82. $(document).trigger("info", data.message);
  83. vm.removeRole(role.name);
  84. }
  85. else {
  86. $(document).trigger("error", data.message);
  87. }
  88. }).fail(function (xhr, textStatus, errorThrown) {
  89. $(document).trigger("error", xhr.responseText);
  90. });
  91. }
  92. self.saveNewPrivileges = function(role) {
  93. $(".jHueNotify").hide();
  94. $.post("/security/api/hive/add_privileges", {
  95. role: ko.mapping.toJSON(role)
  96. }, function (data) {
  97. if (data.status == 0) {
  98. $.each(data.privileges, function(index, privileges) { // TODO: get back a set<TSentryPrivilege>
  99. //role['privileges'] = ko.observableArray();
  100. //vm.roles.unshift(data.role); privileges
  101. });
  102. // self.reset();
  103. }
  104. else {
  105. $(document).trigger("error", data.message);
  106. }
  107. }).fail(function (xhr, textStatus, errorThrown) {
  108. $(document).trigger("error", xhr.responseText);
  109. });
  110. }
  111. }
  112. var Assist = function (vm) {
  113. var self = this;
  114. self.path = ko.observable('');
  115. self.path.subscribe(function() {
  116. self.fetchDatabases();
  117. });
  118. self.server = ko.observable('server1');
  119. self.db = ko.computed(function() {
  120. return self.path().split(/[.]/)[0];
  121. });
  122. self.table = ko.computed(function() {
  123. return self.path().split(/[.]/)[1];
  124. });
  125. self.files = ko.observableArray();
  126. self.privilege = ko.observable();
  127. self.fetchDatabases = function() {
  128. var path = self.path().replace('.', '/');
  129. var request = {
  130. url: '/beeswax/api/autocomplete/' + path,
  131. dataType: 'json',
  132. type: 'GET',
  133. success: function(data) {
  134. if (data.databases) {
  135. self.files(data.databases);
  136. } else if (data.tables && data.tables.length > 0) {
  137. var tables = $.map(data.tables, function(table, index) {return self.db() + '.' + table;});
  138. self.files(tables);
  139. }
  140. },
  141. cache: false
  142. };
  143. $.ajax(request).fail(function (xhr, textStatus, errorThrown) {
  144. $(document).trigger("error", xhr.responseText);
  145. });
  146. };
  147. }
  148. var HiveViewModel = function (initial) {
  149. var self = this;
  150. // Models
  151. self.roles = ko.observableArray();
  152. self.privileges = ko.observableArray();
  153. self.availableHadoopGroups = ko.mapping.fromJS(initial.hadoop_groups);
  154. self.assist = new Assist(self);
  155. // Edition
  156. self.showCreateRole = ko.observable(false);
  157. self.role = new Role(self, {});
  158. self.privilege = new Privilege(self, {});
  159. self.doAs = ko.observable('');
  160. self.doAs.subscribe(function() {
  161. self.assist.fetchDatabases();
  162. });
  163. self.availableHadoopUsers = ko.observableArray();
  164. self.availableHadoopGroups = ko.observableArray();
  165. self.init = function() {
  166. self.fetchUsers();
  167. self.list_sentry_roles_by_group();
  168. self.assist.fetchDatabases();
  169. };
  170. self.removeRole = function(roleName) {
  171. $.each(self.roles(), function (index, role) {
  172. if (role.name == roleName) {
  173. self.roles.remove(role);
  174. return false;
  175. }
  176. });
  177. };
  178. self.list_sentry_roles_by_group = function() {
  179. $.getJSON('/security/api/hive/list_sentry_roles_by_group', {
  180. }, function (data) {
  181. $.each(data.roles, function(index, item) {
  182. self.roles.push(new Role(self, item));
  183. });
  184. }).fail(function (xhr, textStatus, errorThrown) {
  185. $(document).trigger("error", xhr.responseText);
  186. });
  187. };
  188. self.list_sentry_privileges_by_role = function(role) {
  189. $.ajax({
  190. type: "POST",
  191. url: "/security/api/hive/list_sentry_privileges_by_role",
  192. data: {
  193. 'roleName': role.name
  194. },
  195. success: function (data) {
  196. role.privileges.removeAll();
  197. $.each(data.sentry_privileges, function(index, item) {
  198. var privilege = new Privilege(self, {
  199. 'privilegeScope': item.scope,
  200. 'serverName': item.server,
  201. 'dbName': item.database,
  202. 'tableName': item.table,
  203. 'URI': item.URI,
  204. 'action': item.action
  205. });
  206. privilege.properties = ko.mapping.fromJS(item);
  207. role.privileges.push(privilege);
  208. });
  209. role.showPrivileges(true);
  210. }
  211. }).fail(function (xhr, textStatus, errorThrown) {
  212. $(document).trigger("error", xhr.responseText);
  213. });
  214. };
  215. self.list_sentry_privileges_by_authorizable = function(path) {
  216. self.assist.path(path);
  217. $.ajax({
  218. type: "POST",
  219. url: "/security/api/hive/list_sentry_privileges_by_authorizable",
  220. data: {
  221. groups: ko.mapping.toJSON(['sambashare', 'hadoop']),
  222. roleSet: ko.mapping.toJSON({all: true, roles: []}),
  223. authorizableHierarchy: ko.mapping.toJSON({
  224. 'server': self.assist.server(),
  225. 'db': self.assist.db(),
  226. 'table': self.assist.table(),
  227. }),
  228. },
  229. success: function (data) {
  230. self.assist.privilege(ko.mapping.fromJS(data));
  231. }
  232. }).fail(function (xhr, textStatus, errorThrown) {
  233. $(document).trigger("error", xhr.responseText);
  234. });
  235. };
  236. self.fetchUsers = function () {
  237. $.getJSON('/desktop/api/users/autocomplete', {
  238. }, function (data) {
  239. $.each(data.users, function (i, user) {
  240. self.availableHadoopUsers.push(user.username);
  241. });
  242. $.each(data.groups, function (i, group) {
  243. self.availableHadoopGroups.push(group.name);
  244. });
  245. });
  246. }
  247. };
  248. function logGA(page) {
  249. if (typeof trackOnGA == 'function') {
  250. trackOnGA('security/' + page);
  251. }
  252. }