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