step4.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. App.MainAdminSecurityAddStep4Controller = App.MainAdminSecurityProgressController.extend({
  20. name: 'mainAdminSecurityAddStep4Controller',
  21. serviceUsersBinding: 'App.router.mainAdminSecurityController.serviceUsers',
  22. secureServices: function() {
  23. return this.get('content.services');
  24. }.property('content.services'),
  25. isBackBtnDisabled: function () {
  26. return !this.get('stages').someProperty('isError', true);
  27. }.property('stages.@each.isCompleted'),
  28. isOozieSelected: function () {
  29. return this.get('secureServices').someProperty('serviceName', 'OOZIE');
  30. }.property('secureServices'),
  31. isHiveSelected: function () {
  32. return this.get('secureServices').someProperty('serviceName', 'HIVE');
  33. }.property('secureServices'),
  34. isNagiosSelected: function () {
  35. return this.get('secureServices').someProperty('serviceName', 'NAGIOS');
  36. }.property('secureServices'),
  37. isZkSelected: function () {
  38. return this.get('secureServices').someProperty('serviceName', 'ZOOKEEPER');
  39. }.property('secureServices'),
  40. isWebHcatSelected: function () {
  41. var installedServices = App.Service.find().mapProperty('serviceName');
  42. return installedServices.contains('WEBHCAT');
  43. },
  44. isSecurityApplied: function () {
  45. return this.get('stages').someProperty('stage', 'stage3') && this.get('stages').findProperty('stage', 'stage3').get('isSuccess');
  46. }.property('stages.@each.isCompleted'),
  47. clearStep: function () {
  48. this.get('stages').clear();
  49. this.set('isSubmitDisabled', true);
  50. this.set('isBackBtnDisabled', true);
  51. this.get('serviceConfigTags').clear();
  52. },
  53. loadStep: function () {
  54. this.set('secureMapping', require('data/secure_mapping').slice(0));
  55. this.clearStep();
  56. var stages = App.db.getSecurityDeployStages();
  57. this.prepareSecureConfigs();
  58. if (stages && stages.length > 0) {
  59. stages.forEach(function (_stage, index) {
  60. stages[index] = App.Poll.create(_stage);
  61. }, this);
  62. if (stages.someProperty('isError', true)) {
  63. this.get('stages').pushObjects(stages);
  64. this.addObserver('stages.@each.isSuccess', this, 'onCompleteStage');
  65. return;
  66. } else if (stages.filterProperty('isStarted', true).someProperty('isCompleted', false)) {
  67. var runningStage = stages.filterProperty('isStarted', true).findProperty('isCompleted', false);
  68. runningStage.set('isStarted', false);
  69. this.get('stages').pushObjects(stages);
  70. } else {
  71. this.get('stages').pushObjects(stages);
  72. }
  73. } else {
  74. this.loadStages();
  75. this.addInfoToStages();
  76. var runningOperations = App.router.get('backgroundOperationsController.services').filterProperty('isRunning');
  77. var stopAllOperation = runningOperations.findProperty('name', 'Stop All Services');
  78. var stopStage = this.get('stages').findProperty('name', 'STOP_SERVICES');
  79. if (stopStage.get('name') === 'STOP_SERVICES' && stopAllOperation) {
  80. stopStage.set('requestId', stopAllOperation.get('id'));
  81. }
  82. }
  83. this.addObserver('stages.@each.isSuccess', this, 'onCompleteStage');
  84. this.moveToNextStage();
  85. },
  86. enableSubmit: function () {
  87. var addSecurityController = App.router.get('addSecurityController');
  88. if (this.get('stages').someProperty('isError', true) || this.get('stages').everyProperty('isSuccess', true)) {
  89. this.set('isSubmitDisabled', false);
  90. if (this.get('stages').someProperty('isError', true)) {
  91. addSecurityController.setStepsEnable();
  92. }
  93. } else {
  94. this.set('isSubmitDisabled', true);
  95. addSecurityController.setLowerStepsDisable(4);
  96. }
  97. }.observes('stages.@each.isCompleted'),
  98. loadUiSideConfigs: function () {
  99. var uiConfig = [];
  100. var configs = this.get('secureMapping').filterProperty('foreignKey', null);
  101. configs.forEach(function (_config) {
  102. var value = _config.value;
  103. if (_config.hasOwnProperty('dependedServiceName')) {
  104. value = this.checkServiceForConfigValue(value, _config.dependedServiceName);
  105. }
  106. value = this.getGlobConfigValue(_config.templateName, value, _config.name);
  107. uiConfig.pushObject({
  108. "id": "site property",
  109. "name": _config.name,
  110. "value": value,
  111. "filename": _config.filename
  112. });
  113. }, this);
  114. var dependentConfig = this.get('secureMapping').filterProperty('foreignKey');
  115. dependentConfig.forEach(function (_config) {
  116. if (App.Service.find().mapProperty('serviceName').contains(_config.serviceName)) {
  117. this.setConfigValue(uiConfig, _config);
  118. uiConfig.pushObject({
  119. "id": "site property",
  120. "name": _config._name || _config.name,
  121. "value": _config.value,
  122. "filename": _config.filename
  123. });
  124. }
  125. }, this);
  126. return uiConfig;
  127. },
  128. checkServiceForConfigValue: function (value, serviceNames) {
  129. serviceNames.forEach(function (_serviceName) {
  130. if (!App.Service.find().mapProperty('serviceName').contains(_serviceName.name)) {
  131. value = value.replace(_serviceName.replace, '');
  132. }
  133. }, this);
  134. return value;
  135. },
  136. /**
  137. * Set all site property that are derived from other puppet-variable
  138. */
  139. getGlobConfigValue: function (templateName, expression, name) {
  140. var express = expression.match(/<(.*?)>/g);
  141. var value = expression;
  142. if (express == null) {
  143. return expression;
  144. }
  145. express.forEach(function (_express) {
  146. //console.log("The value of template is: " + _express);
  147. var index = parseInt(_express.match(/\[([\d]*)(?=\])/)[1]);
  148. var globValue = this.get('globalProperties').findProperty('name', templateName[index]);
  149. if (globValue) {
  150. console.log('The template value of templateName ' + '[' + index + ']' + ': ' + templateName[index] + ' is: ' + globValue);
  151. if (value !== null) { // if the property depends on more than one template name like <templateName[0]>/<templateName[1]> then don't proceed to the next if the prior is null or not found in the global configs
  152. value = value.replace(_express, globValue.value);
  153. }
  154. } else {
  155. /*
  156. console.log("ERROR: The variable name is: " + templateName[index]);
  157. console.log("ERROR: mapped config from secureMapping file has no corresponding variable in " +
  158. "content.serviceConfigProperties. Two possible reasons for the error could be: 1) The service is not selected. " +
  159. "and/OR 2) The service_config metadata file has no corresponding global var for the site property variable");
  160. */
  161. value = null;
  162. }
  163. }, this);
  164. return value;
  165. },
  166. /**
  167. * Set all site property that are derived from other site-properties
  168. */
  169. setConfigValue: function (uiConfig, config) {
  170. if (config.value == null) {
  171. return;
  172. }
  173. var fkValue = config.name.match(/<(foreignKey.*?)>/g);
  174. if (fkValue) {
  175. fkValue.forEach(function (_fkValue) {
  176. var index = parseInt(_fkValue.match(/\[([\d]*)(?=\])/)[1]);
  177. var globalValue;
  178. if (uiConfig.someProperty('name', config.foreignKey[index])) {
  179. globalValue = uiConfig.findProperty('name', config.foreignKey[index]).value;
  180. config._name = config.name.replace(_fkValue, globalValue);
  181. } else if (this.get('globalProperties').someProperty('name', config.foreignKey[index])) {
  182. globalValue = this.get('globalProperties').findProperty('name', config.foreignKey[index]).value;
  183. config._name = config.name.replace(_fkValue, globalValue);
  184. }
  185. }, this);
  186. }
  187. //For properties in the configMapping file having foreignKey and templateName properties.
  188. var templateValue = config.value.match(/<(templateName.*?)>/g);
  189. if (templateValue) {
  190. templateValue.forEach(function (_value) {
  191. var index = parseInt(_value.match(/\[([\d]*)(?=\])/)[1]);
  192. var globValue = this.get('globalProperties').findProperty('name', config.templateName[index]);
  193. if (globValue) {
  194. config.value = config.value.replace(_value, globValue.value);
  195. } else {
  196. config.value = null;
  197. }
  198. }, this);
  199. }
  200. },
  201. prepareSecureConfigs: function () {
  202. this.loadGlobals();
  203. var storedConfigs = this.get('content.serviceConfigProperties').filterProperty('id', 'site property');
  204. var uiConfigs = this.loadUiSideConfigs();
  205. this.set('configs', storedConfigs.concat(uiConfigs));
  206. },
  207. loadGlobals: function () {
  208. var globals = this.get('content.serviceConfigProperties').filterProperty('id', 'puppet var');
  209. this.set('globalProperties', globals);
  210. this.loadStaticGlobal(); //Hack for properties which are declared in global_properties.js and not able to retrieve values declared in secure_properties.js
  211. this.loadUsersToGlobal();
  212. this.loadHostNamesToGlobal();
  213. this.loadPrimaryNamesToGlobals();
  214. },
  215. loadUsersToGlobal: function () {
  216. if (!this.get('serviceUsers').length) {
  217. this.loadUsersFromServer();
  218. }
  219. App.router.get('mainAdminSecurityController.serviceUsers').forEach(function (_user) {
  220. this.get('globalProperties').pushObject(_user);
  221. }, this);
  222. },
  223. loadHostNamesToGlobal: function () {
  224. var oozieHostComponent = App.Service.find('OOZIE').get('hostComponents').findProperty('componentName', 'OOZIE_SERVER');
  225. if (this.get('isOozieSelected') && oozieHostComponent) {
  226. var oozieHostName = oozieHostComponent.get('host.hostName');
  227. this.get('globalProperties').pushObject({
  228. id: 'puppet var',
  229. name: 'oozieserver_host',
  230. value: oozieHostName
  231. });
  232. }
  233. var hiveHostComponent = App.Service.find('HIVE').get('hostComponents').findProperty('componentName', 'HIVE_METASTORE');
  234. if (this.get('isHiveSelected') && hiveHostComponent) {
  235. var hiveHostName = hiveHostComponent.get('host.hostName');
  236. this.get('globalProperties').pushObject({
  237. id: 'puppet var',
  238. name: 'hivemetastore_host',
  239. value: hiveHostName
  240. });
  241. }
  242. var webHcatComponent = App.Service.find('WEBHCAT').get('hostComponents').findProperty('componentName', 'WEBHCAT_SERVER');
  243. if (this.isWebHcatSelected() && webHcatComponent) {
  244. var webHcatHostName = webHcatComponent.get('host.hostName');
  245. this.get('globalProperties').pushObject({
  246. id: 'puppet var',
  247. name: 'webhcat_server',
  248. value: webHcatHostName
  249. });
  250. }
  251. },
  252. loadStaticGlobal: function () {
  253. var globalProperties = this.get('globalProperties');
  254. this.get('globalProperties').forEach(function (_property) {
  255. switch (_property.name) {
  256. case 'security_enabled':
  257. _property.value = 'true';
  258. break;
  259. }
  260. }, this);
  261. },
  262. loadPrimaryNamesToGlobals: function () {
  263. var principalProperties = this.getPrincipalNames();
  264. principalProperties.forEach(function (_principalProperty) {
  265. var name = _principalProperty.name.replace('principal', 'primary');
  266. var value = _principalProperty.value.split('/')[0];
  267. this.get('globalProperties').pushObject({name: name, value: value});
  268. }, this);
  269. },
  270. getPrincipalNames: function () {
  271. var principalNames = [];
  272. var allPrincipalNames = [];
  273. this.get('globalProperties').forEach(function (_globalProperty) {
  274. if (/principal_name?$/.test(_globalProperty.name)) {
  275. principalNames.pushObject(_globalProperty);
  276. }
  277. }, this);
  278. this.get('secureProperties').forEach(function (_secureProperty) {
  279. if (/principal_name?$/.test(_secureProperty.name)) {
  280. var principalName = principalNames.findProperty('name', _secureProperty.name);
  281. if (!principalName) {
  282. _secureProperty.value = _secureProperty.defaultValue;
  283. principalNames.pushObject(_secureProperty);
  284. }
  285. }
  286. }, this);
  287. return principalNames;
  288. },
  289. loadUsersFromServer: function () {
  290. if (App.testMode) {
  291. var serviceUsers = this.get('serviceUsers');
  292. serviceUsers.pushObject({id: 'puppet var', name: 'hdfs_user', value: 'hdfs'});
  293. serviceUsers.pushObject({id: 'puppet var', name: 'mapred_user', value: 'mapred'});
  294. serviceUsers.pushObject({id: 'puppet var', name: 'hbase_user', value: 'hbase'});
  295. serviceUsers.pushObject({id: 'puppet var', name: 'hive_user', value: 'hive'});
  296. } else {
  297. App.router.set('mainAdminSecurityController.serviceUsers', App.db.getSecureUserInfo());
  298. }
  299. },
  300. manageSecureConfigs: function () {
  301. try {
  302. this.get('serviceConfigTags').forEach(function (_serviceConfigTags) {
  303. _serviceConfigTags.newTagName = 'version' + (new Date).getTime();
  304. if (_serviceConfigTags.siteName === 'global') {
  305. var realmName = this.get('globalProperties').findProperty('name', 'kerberos_domain');
  306. if (this.get('isNagiosSelected')) {
  307. var nagiosPrincipalName = this.get('globalProperties').findProperty('name', 'nagios_principal_name');
  308. nagiosPrincipalName.value = nagiosPrincipalName.value + '@' + realmName.value;
  309. }
  310. if (this.get('isZkSelected')) {
  311. var zkPrincipalName = this.get('globalProperties').findProperty('name', 'zookeeper_principal_name');
  312. zkPrincipalName.value = zkPrincipalName.value + '@' + realmName.value;
  313. }
  314. this.get('globalProperties').forEach(function (_globalProperty) {
  315. if (!/_hosts?$/.test(_globalProperty.name)) {
  316. _serviceConfigTags.configs[_globalProperty.name] = _globalProperty.value;
  317. }
  318. }, this);
  319. }
  320. else {
  321. this.get('configs').filterProperty('id', 'site property').filterProperty('filename', _serviceConfigTags.siteName + '.xml').forEach(function (_config) {
  322. _serviceConfigTags.configs[_config.name] = _config.value;
  323. }, this);
  324. }
  325. }, this);
  326. } catch (err) {
  327. var stage3 = this.get('stages').findProperty('stage', 'stage3');
  328. if (stage3) {
  329. stage3.set('isSuccess', false);
  330. stage3.set('isError', true);
  331. }
  332. if (err) {
  333. console.log("Error: Error occurred while applying secure configs to the server. Error message: " + err);
  334. }
  335. this.onJsError();
  336. return false;
  337. }
  338. return true;
  339. },
  340. onJsError: function () {
  341. App.ModalPopup.show({
  342. header: Em.I18n.t('common.error'),
  343. secondary: false,
  344. onPrimary: function () {
  345. this.hide();
  346. },
  347. bodyClass: Ember.View.extend({
  348. template: Ember.Handlebars.compile('<p>{{t admin.security.apply.configuration.error}}</p>')
  349. })
  350. });
  351. }
  352. });