addSecurityConfigs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Mixin for loading and setting secure configs
  21. *
  22. * @type {Ember.Mixin}
  23. */
  24. App.AddSecurityConfigs = Em.Mixin.create({
  25. secureProperties: function () {
  26. if (App.get('isHadoop2Stack')) {
  27. return require('data/HDP2/secure_properties').configProperties;
  28. } else {
  29. return require('data/secure_properties').configProperties;
  30. }
  31. }.property('App.isHadoop2Stack'),
  32. secureMapping: function () {
  33. return (App.get('isHadoop2Stack')) ? require('data/HDP2/secure_mapping') : require('data/secure_mapping');
  34. }.property('App.isHadoop2Stack'),
  35. serviceUsersBinding: 'App.router.mainAdminSecurityController.serviceUsers',
  36. componentsConfig: [
  37. {
  38. serviceName: 'OOZIE',
  39. componentName: 'OOZIE_SERVER',
  40. configName: 'oozieserver_host'
  41. },
  42. {
  43. serviceName: 'HIVE',
  44. componentName: 'HIVE_METASTORE',
  45. configName: 'hivemetastore_host'
  46. },
  47. {
  48. serviceName: 'HIVE',
  49. componentName: 'WEBHCAT_SERVER',
  50. configName: 'webhcat_server'
  51. }
  52. ],
  53. /**
  54. * mock users used in testMode
  55. */
  56. testModeUsers: [
  57. {
  58. name: 'hdfs_user',
  59. value: 'hdfs'
  60. },
  61. {
  62. name: 'mapred_user',
  63. value: 'mapred'
  64. },
  65. {
  66. name: 'hbase_user',
  67. value: 'hbase'
  68. },
  69. {
  70. name: 'hive_user',
  71. value: 'hive'
  72. }
  73. ],
  74. /**
  75. * security configs, which values should be modified after APPLY CONFIGURATIONS stage
  76. */
  77. secureConfigs: function () {
  78. var configs = [
  79. {
  80. name: 'nagios_principal_name',
  81. serviceName: 'NAGIOS'
  82. },
  83. {
  84. name: 'zookeeper_principal_name',
  85. serviceName: 'ZOOKEEPER'
  86. },
  87. {
  88. name: 'storm_principal_name',
  89. serviceName: 'STORM'
  90. }
  91. ];
  92. if (App.get('isHadoop22Stack')) {
  93. configs.push({
  94. name: 'nimbus_principal_name',
  95. serviceName: 'STORM'
  96. })
  97. }
  98. return configs;
  99. }.property('App.isHadoop22Stack'),
  100. secureServices: function() {
  101. return this.get('content.services');
  102. }.property('content.services'),
  103. /**
  104. * prepare secure configs
  105. */
  106. prepareSecureConfigs: function () {
  107. var configs = this.get('content.serviceConfigProperties');
  108. this.set('configs', configs);
  109. this.loadStaticConfigs(); //Hack for properties which are declared in site_properties.js and not able to retrieve values declared in secure_properties.js
  110. this.loadUsersToConfigs();
  111. this.loadHostNames();
  112. this.loadPrimaryNames();
  113. var uiConfigs = this.loadUiSideSecureConfigs();
  114. this.set('configs', this.get('configs').concat(uiConfigs));
  115. },
  116. /**
  117. * push users to configs
  118. */
  119. loadUsersToConfigs: function () {
  120. if (!this.get('serviceUsers').length) {
  121. this.loadUsersFromServer();
  122. }
  123. App.router.get('mainAdminSecurityController.serviceUsers').forEach(function (_user) {
  124. this.get('configs').pushObject(_user);
  125. }, this);
  126. },
  127. /**
  128. * add component config that contain host name as value
  129. * @param serviceName
  130. * @param componentName
  131. * @param configName
  132. * @return {Boolean}
  133. */
  134. addHostConfig: function (serviceName, componentName, configName) {
  135. var service = App.Service.find(serviceName);
  136. var isServiceSecure = this.get('secureServices').someProperty('serviceName', serviceName);
  137. if (service.get('isLoaded') && isServiceSecure) {
  138. var hostComponent = service.get('hostComponents').findProperty('componentName', componentName);
  139. if (hostComponent) {
  140. var hostName = hostComponent.get('hostName');
  141. this.get('configs').push({
  142. id: 'puppet var',
  143. name: configName,
  144. value: hostName
  145. });
  146. return true;
  147. }
  148. }
  149. return false;
  150. },
  151. /**
  152. * add hosts' names to configs
  153. */
  154. loadHostNames: function () {
  155. var componentsConfig = this.get('componentsConfig');
  156. componentsConfig.forEach(function (host) {
  157. this.addHostConfig(host.serviceName, host.componentName, host.configName);
  158. }, this);
  159. },
  160. /**
  161. * load static configs
  162. */
  163. loadStaticConfigs: function () {
  164. this.get('configs').forEach(function (_property) {
  165. switch (_property.name) {
  166. case 'security_enabled':
  167. _property.value = 'true';
  168. break;
  169. }
  170. }, this);
  171. },
  172. /**
  173. * add principals to properties
  174. */
  175. loadPrimaryNames: function () {
  176. var principalProperties = this.getPrincipalNames();
  177. principalProperties.forEach(function (_principalProperty) {
  178. var name = _principalProperty.name.replace('principal', 'primary');
  179. var value = _principalProperty.value.split('/')[0];
  180. this.get('configs').push({name: name, value: value});
  181. }, this);
  182. },
  183. /**
  184. * gather and return properties with "principal_name"
  185. * @return {Array}
  186. */
  187. getPrincipalNames: function () {
  188. var principalNames = [];
  189. this.get('configs').forEach(function (_property) {
  190. if (/principal_name?$/.test(_property.name)) {
  191. principalNames.push(_property);
  192. }
  193. }, this);
  194. this.get('secureProperties').forEach(function (_secureProperty) {
  195. if (/principal_name?$/.test(_secureProperty.name)) {
  196. var principalName = principalNames.findProperty('name', _secureProperty.name);
  197. if (!principalName) {
  198. _secureProperty.value = _secureProperty.defaultValue;
  199. principalNames.push(_secureProperty);
  200. }
  201. }
  202. }, this);
  203. return principalNames;
  204. },
  205. /**
  206. * load users from server
  207. */
  208. loadUsersFromServer: function () {
  209. if (App.get('testMode')) {
  210. var serviceUsers = this.get('serviceUsers');
  211. this.get('testModeUsers').forEach(function (user) {
  212. user.id = 'puppet var';
  213. serviceUsers.push(user);
  214. }, this);
  215. } else {
  216. App.router.set('mainAdminSecurityController.serviceUsers', App.db.getSecureUserInfo());
  217. }
  218. },
  219. /**
  220. * load configs from UI side
  221. * @return {Array}
  222. */
  223. loadUiSideSecureConfigs: function () {
  224. var uiConfig = [];
  225. var configs = this.get('secureMapping').filterProperty('foreignKey', null);
  226. configs.forEach(function (_config) {
  227. var value = _config.value;
  228. if (_config.hasOwnProperty('dependedServiceName')) {
  229. value = this.checkServiceForConfigValue(value, _config.dependedServiceName);
  230. }
  231. value = this.getConfigValue(_config.templateName, value);
  232. uiConfig.push({
  233. "id": "site property",
  234. "name": _config.name,
  235. "value": value,
  236. "filename": _config.filename
  237. });
  238. }, this);
  239. var dependentConfig = this.get('secureMapping').filterProperty('foreignKey');
  240. dependentConfig.forEach(function (_config) {
  241. if (App.Service.find().mapProperty('serviceName').contains(_config.serviceName)) {
  242. this.setConfigValue(_config);
  243. this.formatConfigName(uiConfig, _config);
  244. uiConfig.push({
  245. "id": "site property",
  246. "name": _config._name || _config.name,
  247. "value": _config.value,
  248. "filename": _config.filename
  249. });
  250. }
  251. }, this);
  252. return uiConfig;
  253. },
  254. /**
  255. * erase template rules from config value if service is not loaded
  256. * @param value
  257. * @param services
  258. * @return {*}
  259. */
  260. checkServiceForConfigValue: function (value, services) {
  261. services.forEach(function (_service) {
  262. if (!App.Service.find(_service.name).get('isLoaded')) {
  263. value = value.replace(_service.replace, '');
  264. }
  265. }, this);
  266. return value;
  267. },
  268. /**
  269. * Set all site property that are derived from other puppet-variable
  270. * @param templateName
  271. * @param expression
  272. * @return {String|null}
  273. */
  274. getConfigValue: function (templateName, expression) {
  275. var express = expression.match(/<(.*?)>/g);
  276. var value = expression;
  277. if (Em.isNone(express)) return expression;
  278. express.forEach(function (_express) {
  279. var index = parseInt(_express.match(/\[([\d]*)(?=\])/)[1]);
  280. var configs = this.get('configs').findProperty('name', templateName[index]);
  281. if (!!value) {
  282. value = (configs) ? value.replace(_express, configs.value) : null;
  283. }
  284. }, this);
  285. return value;
  286. },
  287. /**
  288. * format name of config values of configs which match foreignKey
  289. * @param uiConfig
  290. * @param config
  291. * @return {Boolean}
  292. */
  293. formatConfigName: function (uiConfig, config) {
  294. if (Em.isNone(config.value)) return false;
  295. var fkValue = config.name.match(/<(foreignKey.*?)>/g);
  296. if (fkValue) {
  297. fkValue.forEach(function (_fkValue) {
  298. var index = parseInt(_fkValue.match(/\[([\d]*)(?=\])/)[1]);
  299. var value;
  300. if (uiConfig.someProperty('name', config.foreignKey[index])) {
  301. value = uiConfig.findProperty('name', config.foreignKey[index]).value;
  302. config._name = config.name.replace(_fkValue, value);
  303. } else if (this.get('configs').someProperty('name', config.foreignKey[index])) {
  304. value = this.get('configs').findProperty('name', config.foreignKey[index]).value;
  305. config._name = config.name.replace(_fkValue, value);
  306. }
  307. }, this);
  308. return true;
  309. }
  310. return false;
  311. },
  312. /**
  313. * Set config value with values of configs which match template
  314. * @param config
  315. * @return {Boolean}
  316. */
  317. setConfigValue: function (config) {
  318. if (Em.isNone(config.value)) return false;
  319. //For properties in the configMapping file having foreignKey and templateName properties.
  320. var templateValue = config.value.match(/<(templateName.*?)>/g);
  321. if (templateValue) {
  322. templateValue.forEach(function (_value) {
  323. var index = parseInt(_value.match(/\[([\d]*)(?=\])/)[1]);
  324. var cfgValue = this.get('configs').findProperty('name', config.templateName[index]);
  325. config.value = (cfgValue) ? config.value.replace(_value, cfgValue.value) : null;
  326. }, this);
  327. return true;
  328. }
  329. return false;
  330. },
  331. /**
  332. * set value of principal property
  333. * @param serviceName
  334. * @param principalName
  335. * @return {Boolean}
  336. */
  337. setPrincipalValue: function (serviceName, principalName) {
  338. var siteProperties = this.get('configs');
  339. var realmName = siteProperties.findProperty('name', 'kerberos_domain');
  340. if (this.get('secureServices').someProperty('serviceName', serviceName)) {
  341. var principalProperty = siteProperties.findProperty('name', principalName);
  342. principalProperty.value = principalProperty.value + '@' + realmName.value;
  343. return true;
  344. }
  345. return false;
  346. }
  347. });