credentials.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * 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, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. /** @module utils.credentials **/
  19. /**
  20. * Credential Resource format.
  21. * @typedef {object} credentialResourceObject
  22. * @property {string} principal user principal name
  23. * @property {string} key user password
  24. * @property {string} type type of credential store e.g. <b>persistent</b> or <b>temporary</b>
  25. */
  26. module.exports = {
  27. STORE_TYPES: {
  28. TEMPORARY: 'temporary',
  29. PERSISTENT: 'persisted',
  30. PERSISTENT_KEY: 'persistent',
  31. TEMPORARY_KEY: 'temporary',
  32. PERSISTENT_PATH: 'storage.persistent',
  33. TEMPORARY_PATH: 'storage.temporary'
  34. },
  35. ALIAS: {
  36. KDC_CREDENTIALS: 'kdc.admin.credential'
  37. },
  38. /**
  39. * Store credentials to server
  40. *
  41. * @member utils.credentials
  42. * @param {string} clusterName cluster name
  43. * @param {string} alias credential alias name e.g. "kdc.admin.credentials"
  44. * @param {credentialResourceObject} resource resource info to set e.g.
  45. * <code>
  46. * {
  47. * principal: "USERNAME",
  48. * key: "SecretKey",
  49. * type: "persisted"
  50. * }
  51. * </code>
  52. *
  53. * Where:
  54. * <ul>
  55. * <li>principal: the principal (or username) part of the credential to store</li>
  56. * <li>key: the secret key part of the credential to store</li>
  57. * <li>type: declares the storage facility type: "persisted" or "temporary"</li>
  58. * </ul>
  59. * @returns {$.Deferred} promise object
  60. */
  61. createCredentials: function(clusterName, alias, resource) {
  62. return App.ajax.send({
  63. sender: this,
  64. name: 'credentials.create',
  65. data: {
  66. clusterName: clusterName,
  67. resource: resource,
  68. alias: alias
  69. },
  70. error: 'createCredentialsErrorCallback'
  71. });
  72. },
  73. credentialsSuccessCallback: function(data, opt, params) {
  74. params.callback(data.items.length ? data.items.mapProperty('Credential') : []);
  75. },
  76. createCredentialsErrorCallback: function(req, ajaxOpts, error) {
  77. },
  78. /**
  79. * @see createCredentials
  80. * @member utils.credentials
  81. * @param {string} clusterName
  82. * @param {string} alias
  83. * @param {credentialResourceObject} resource
  84. * @returns {$.Deferred} promise object
  85. */
  86. createOrUpdateCredentials: function(clusterName, alias, resource) {
  87. var self = this;
  88. var dfd = $.Deferred();
  89. this.getCredential(clusterName, alias).then(function() {
  90. // update previously stored credentials
  91. self.updateCredentials(clusterName, alias, resource).always(function() {
  92. var status = arguments[1];
  93. var result = arguments[2];
  94. dfd.resolve(status === "success", result);
  95. });
  96. }, function() {
  97. // create credentials if they not exist
  98. self.createCredentials(clusterName, alias, resource).always(function() {
  99. var status = arguments[1];
  100. var result = arguments[2];
  101. dfd.resolve(status === "success", result);
  102. });
  103. });
  104. return dfd.promise();
  105. },
  106. /**
  107. * Retrieve single credential from cluster by specified alias name
  108. *
  109. * @member utils.credentials
  110. * @param {string} clusterName cluster name
  111. * @param {string} alias credential alias name e.g. "kdc.admin.credentials"
  112. * @param {function} [callback] success callback to invoke, credential will be passed to first argument
  113. * @returns {$.Deferred} promise object
  114. */
  115. getCredential: function(clusterName, alias, callback) {
  116. return App.ajax.send({
  117. sender: this,
  118. name: 'credentials.get',
  119. data: {
  120. clusterName: clusterName,
  121. alias: alias,
  122. callback: callback
  123. },
  124. success: 'getCredentialSuccessCallback',
  125. error: 'getCredentialErrorCallback'
  126. });
  127. },
  128. getCredentialSuccessCallback: function(data, opt, params) {
  129. if (params.callback) {
  130. params.callback(Em.getWithDefault(data, 'Credential', null));
  131. }
  132. },
  133. getCredentialErrorCallback: function() {},
  134. /**
  135. * Update credential by alias and cluster name
  136. *
  137. * @see createCredentials
  138. * @param {string} clusterName
  139. * @param {string} alias
  140. * @param {object} resource
  141. * @returns {$.Deferred} promise object
  142. */
  143. updateCredentials: function(clusterName, alias, resource) {
  144. return App.ajax.send({
  145. sender: this,
  146. name: 'credentials.update',
  147. data: {
  148. clusterName: clusterName,
  149. alias: alias,
  150. resource: resource
  151. }
  152. });
  153. },
  154. /**
  155. * Get credenial list from server by specified cluster name
  156. *
  157. * @param {string} clusterName cluster name
  158. * @param {function} callback
  159. * @returns {$.Deferred} promise object
  160. */
  161. credentials: function(clusterName, callback) {
  162. return App.ajax.send({
  163. sender: this,
  164. name: 'credentials.list',
  165. data: {
  166. clusterName: clusterName,
  167. callback: callback
  168. },
  169. success: 'credentialsSuccessCallback'
  170. });
  171. },
  172. /**
  173. * Remove credential from server by specified cluster name and alias
  174. *
  175. * @param {string} clusterName cluster name
  176. * @param {string} alias credential alias name e.g. "kdc.admin.credentials"
  177. */
  178. removeCredentials: function(clusterName, alias) {
  179. return App.ajax.send({
  180. sender: this,
  181. name: 'credentials.delete',
  182. data: {
  183. clusterName: clusterName,
  184. alias: alias
  185. }
  186. });
  187. },
  188. /**
  189. * Get info regarding credential storage type like <code>persistent</code> and <code>temporary</code>
  190. *
  191. * @param {string} clusterName cluster name
  192. * @param {function} callback
  193. * @returns {$.Deferred} promise object
  194. */
  195. storageInfo: function(clusterName, callback) {
  196. return App.ajax.send({
  197. sender: this,
  198. name: 'credentials.store.info',
  199. data: {
  200. clusterName: clusterName,
  201. callback: callback
  202. },
  203. success: 'storageInfoSuccessCallback'
  204. });
  205. },
  206. storageInfoSuccessCallback: function(json, opt, params, request) {
  207. if (json.Clusters) {
  208. var storage = Em.getWithDefault(json, 'Clusters.credential_store_properties', {});
  209. var storeTypesObject = {};
  210. storeTypesObject[this.STORE_TYPES.PERSISTENT_KEY] = storage[this.STORE_TYPES.PERSISTENT_PATH] === "true";
  211. storeTypesObject[this.STORE_TYPES.TEMPORARY_KEY] = storage[this.STORE_TYPES.TEMPORARY_PATH] === "true";
  212. params.callback(storeTypesObject);
  213. } else {
  214. params.callback(null);
  215. }
  216. },
  217. /**
  218. * Resolves promise with <code>true</code> value if secure store is persistent
  219. *
  220. * @param {string} clusterName
  221. * @returns {$.Deferred} promise object
  222. */
  223. isStorePersisted: function(clusterName) {
  224. return this.storeTypeStatus(clusterName, this.STORE_TYPES.PERSISTENT_KEY);
  225. },
  226. /**
  227. * Resolves promise with <code>true</code> value if secure store is temporary
  228. *
  229. * @param {string} clusterName
  230. * @returns {$.Deferred} promise object
  231. */
  232. isStoreTemporary: function(clusterName) {
  233. return this.storeTypeStatus(clusterName, this.STORE_TYPES.TEMPORARY_KEY);
  234. },
  235. /**
  236. * Get store type value for specified cluster and store type e.g. <b>persistent</b> or <b>temporary</b>
  237. *
  238. * @member utils.credentials
  239. * @param {string} clusterName
  240. * @param {string} type store type e.g. <b>persistent</b> or <b>temporary</b>
  241. * @returns {$.Deferred} promise object
  242. */
  243. storeTypeStatus: function(clusterName, type) {
  244. var dfd = $.Deferred();
  245. this.storageInfo(clusterName, function(storage) {
  246. dfd.resolve(Em.get(storage, type));
  247. }).fail(function(error) {
  248. dfd.reject(error);
  249. });
  250. return dfd.promise();
  251. },
  252. /**
  253. * Generate payload for storing credential.
  254. *
  255. * @member utils.credentials
  256. * @param {string} principal principal name
  257. * @param {string} key secret key
  258. * @param {string} type storage type e.g. <b>persisted</b>, <b>temporary</b>
  259. * @returns {credentialResourceObject} resource template
  260. */
  261. createCredentialResource: function(principal, key, type) {
  262. return {
  263. principal: principal,
  264. key: key,
  265. type: type
  266. };
  267. },
  268. /**
  269. * Check that KDC credentials stored as <b>persisted</b> and not <b>temporary</b> from specified credentials list.
  270. *
  271. * @member utils.credentials
  272. * @param {object[]} credentials credentials list retrieved from API @see credentials
  273. * @returns {boolean} <code>true</code> if credentials are persisted
  274. */
  275. isKDCCredentialsPersisted: function(credentials) {
  276. var kdcCredentials = credentials.findProperty('alias', this.ALIAS.KDC_CREDENTIALS);
  277. if (kdcCredentials) {
  278. return Em.getWithDefault(kdcCredentials, 'type', this.STORE_TYPES.TEMPORARY) === this.STORE_TYPES.PERSISTENT;
  279. }
  280. return false;
  281. }
  282. };