-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathLogin.js
More file actions
557 lines (462 loc) · 20.1 KB
/
Login.js
File metadata and controls
557 lines (462 loc) · 20.1 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* global */
import {BeaconsMinionPanel} from "../panels/BeaconsMinion.js";
import {Character} from "../Character.js";
import {Panel} from "./Panel.js";
import {Router} from "../Router.js";
import {Utils} from "../Utils.js";
export class LoginPanel extends Panel {
constructor () {
LoginPanel.version = "SaltGUI v1.34.0-SNAPSHOT";
super("login");
this.addTitle("SaltGUI");
// The FORM is important, so that the ENTER key
// in any field is redirected to the submit button
const form = Utils.createElem("form");
this.div.append(form);
const motdTxt = Utils.createDiv("motd");
form.append(motdTxt);
this.motdTxtDiv = motdTxt;
const motdHtml = Utils.createDiv("motd");
form.append(motdHtml);
this.motdHtmlDiv = motdHtml;
const noticeWrapper = Utils.createDiv("notice-wrapper", "", "notice-wrapper");
form.append(noticeWrapper);
this.noticeWrapperDiv = noticeWrapper;
const username = Utils.createElem("input");
username.type = "text";
username.id = "username";
username.placeholder = "Username";
username.autofocus = "";
form.append(username);
this.usernameField = username;
const password = Utils.createElem("input");
password.type = "password";
password.id = "password";
password.placeholder = "Password";
form.append(password);
this.passwordField = password;
// see https://docs.saltproject.io/en/latest/ref/auth/all/index.html
const select = Utils.createElem("select");
form.append(select);
this.eauthField = select;
this._updateEauthField();
const submit = Utils.createElem("input");
submit.id = "login-button";
submit.type = "submit";
submit.value = "Login";
form.append(submit);
this.loginButton = submit;
const aa = Utils.createElem("a", "attribution");
aa.href = "https://github.com/erwindon/SaltGUI";
aa.target = "_blank";
aa.rel = "noopener";
const img = Utils.createElem("img");
img.src = "static/images/GitHub_Invertocat_Black.png";
img.style = "width: 1em; margin-right: 5px";
aa.append(img);
const txt = document.createTextNode(LoginPanel.version);
aa.append(txt);
form.append(aa);
this.div.append(form);
this._registerEventListeners(form);
}
_addEauthSection (pSectionName, pOptionValues) {
if (pOptionValues.length === 0) {
// no optionValues --> no section
return;
}
let parent = this.eauthField;
if (pSectionName) {
parent = Utils.createElem("optgroup");
parent.label = pSectionName;
this.eauthField.append(parent);
}
for (const optionValue of pOptionValues) {
const option = Utils.createElem("option", "", optionValue);
option.value = optionValue;
parent.append(option);
}
}
_updateEauthField () {
// start fresh
this.eauthField.innerHTML = "";
const option1 = Utils.createElem("option", "", "Type", "eauth-default");
option1.value = "default";
this.eauthField.append(option1);
this._addEauthSection("standard", ["pam"]);
// move items to this optgroup only when at least
// one user reports a succesful use
// see https://github.com/saltstack/salt/tree/master/salt/auth
// for information and configuration
this._addEauthSection("other", ["file", "ldap", "mysql", "yubico"]);
// auto and sharedsecret already tested but not suitable for general use
// other values are: django, keystone, pki, rest
// these can be added after testing to optgroup 'other'
// add untested values to (new) optgroup 'experimental' on explicit user request
// and only while the code is on a branch
// allow user to add any value they want
let saltAuth = Utils.getStorageItemList("local", "salt-auth-txt");
if (saltAuth.includes("CLEAR")) {
saltAuth = saltAuth.filter((item) => item !== "CLEAR");
if (saltAuth.length === 0) {
// no cheating
/* eslint-disable no-console */
console.warn("salt-auth-txt has no entries, except 'CLEAR', assuming 'pam'");
/* eslint-enable no-console */
saltAuth = ["pam"];
}
this.eauthField.innerHTML = "";
this._addEauthSection(null, saltAuth);
if (saltAuth.length === 1) {
this.eauthField.style.display = "none";
Utils.setStorageItem("local", "eauth", saltAuth[0]);
}
this.eauthField.value = Utils.getStorageItem("local", "eauth", saltAuth[0]);
} else {
this._addEauthSection("salt-auth.txt", saltAuth);
this.eauthField.value = Utils.getStorageItem("local", "eauth", "pam");
}
}
_updateMotdField () {
const saltMotdTxt = Utils.getStorageItem("local", "salt-motd-txt", "");
this.motdTxtDiv.innerText = saltMotdTxt;
this.motdTxtDiv.style.display = saltMotdTxt ? "" : "none";
const saltMotdHtml = Utils.getStorageItem("local", "salt-motd-html", "");
this.motdHtmlDiv.innerHTML = saltMotdHtml;
this.motdHtmlDiv.style.display = saltMotdHtml ? "" : "none";
}
_registerEventListeners (pLoginForm) {
pLoginForm.addEventListener("submit", (ev) => {
this._onLogin(ev);
});
}
_showNoticeText (pBackgroundColour, pText, pInfoClass) {
// create a new child every time to restart the animation
const noticeDiv = Utils.createDiv(pInfoClass, pText, "notice");
noticeDiv.style.backgroundColor = pBackgroundColour;
while (this.noticeWrapperDiv.hasChildNodes()) {
this.noticeWrapperDiv.removeChild(this.noticeWrapperDiv.firstChild);
}
this.noticeWrapperDiv.appendChild(noticeDiv);
}
_loadSaltAuthTxt () {
const staticSaltAuthTxtPromise = this.api.getStaticSaltAuthTxt();
staticSaltAuthTxtPromise.then((pStaticSaltAuthTxt) => {
if (pStaticSaltAuthTxt) {
const lines = pStaticSaltAuthTxt.
trim().
split(/\r?\n/).
filter((item) => !item.startsWith("#"));
const saltAuth = [];
for (const line of lines) {
const fields = line.split(/[ \t]+/);
if (fields.length === 1) {
saltAuth.push(fields[0]);
} else {
/* eslint-disable no-console */
console.warn("lines in 'salt-auth.txt' must have 1 word, not " + fields.length + " like in: " + line);
/* eslint-enable no-console */
}
}
Utils.setStorageItem("local", "salt-auth-txt", JSON.stringify(saltAuth));
this._updateEauthField();
} else {
Utils.setStorageItem("local", "salt-auth-txt", "[]");
this._updateEauthField();
}
return true;
}, () => {
Utils.setStorageItem("local", "salt-auth-txt", "[]");
this._updateEauthField();
return false;
});
}
_loadSaltMotdTxt () {
const staticSaltMotdTxtPromise = this.api.getStaticSaltMotdTxt();
staticSaltMotdTxtPromise.then((pStaticSaltMotdTxt) => {
if (pStaticSaltMotdTxt) {
const lines = pStaticSaltMotdTxt.trim();
Utils.setStorageItem("local", "salt-motd-txt", lines);
this._updateMotdField();
} else {
Utils.setStorageItem("local", "salt-motd-txt", "");
this._updateMotdField();
}
return true;
}, () => {
Utils.setStorageItem("local", "salt-motd-txt", "");
this._updateMotdField();
return false;
});
}
_loadSaltMotdHtml () {
const staticSaltMotdHtmlPromise = this.api.getStaticSaltMotdHtml();
staticSaltMotdHtmlPromise.then((pStaticSaltMotdHtml) => {
if (pStaticSaltMotdHtml) {
const lines = pStaticSaltMotdHtml.trim();
Utils.setStorageItem("local", "salt-motd-html", lines);
this._updateMotdField();
} else {
Utils.setStorageItem("local", "salt-motd-html", "");
this._updateMotdField();
}
return true;
}, () => {
Utils.setStorageItem("local", "salt-motd-html", "");
this._updateMotdField();
return false;
});
}
onShow () {
this._loadSaltAuthTxt();
this._loadSaltMotdTxt();
this._loadSaltMotdHtml();
const reason = decodeURIComponent(Utils.getQueryParam("reason"));
switch (reason) {
case null:
case "":
case "undefined":
break;
case "no-session":
// gray because we cannot prove that the user was/wasnt logged in
this._showNoticeText("var(--color-notice-muted)", "Not logged in", "notice_not_logged_in");
break;
case "session-cancelled":
this._showNoticeText("var(--color-notice-danger)", "Session cancelled", "notice-session-cancelled");
break;
case "session-expired":
this._showNoticeText("var(--color-notice-danger)", "Session expired", "notice-session-expired");
break;
case "logout":
// gray because this is the result of a user action
this._showNoticeText("var(--color-notice-muted)", "Logout", "notice_logout");
break;
default:
// should not occur
this._showNoticeText("var(--color-notice-danger)", reason, "notice_other:" + reason);
}
this._enableLoginControls(true);
}
_onLogin (pSubmitEvent) {
pSubmitEvent.preventDefault();
const username = this.usernameField.value;
const password = this.passwordField.value;
const eauth = this.eauthField.value;
if (eauth === "default") {
this._onLoginFailure("Invalid login-type");
return;
}
this._enableLoginControls(false);
this.api.login(username, password, eauth).then(() => {
this._onLoginSuccess();
return true;
}, (pErr) => {
this._onLoginFailure(pErr);
return false;
});
}
_onLoginSuccess () {
this._showNoticeText("var(--color-text-accent)", "Please wait" + Character.HORIZONTAL_ELLIPSIS, "notice_please_wait");
Utils.setStorageItem("local", "salt-motd-txt", "");
Utils.setStorageItem("local", "salt-motd-html", "");
this.bootstrapSession();
// allow the success message to be seen
window.setTimeout(() => {
// erase credentials since we don't do page-refresh
this.usernameField.value = "";
this.passwordField.value = "";
if (Utils.getStorageItem("session", "login_response") !== null) {
// we might have been logged out in this first second
// e.g. when clock between client and server differs more than the session timout
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("page")) {
// a redirect page is specified
const params = {};
for (const pair of urlParams.entries()) {
params[pair[0]] = pair[1];
}
const page = params["page"];
delete params["page"];
this.router.goTo(page, params);
} else {
this.router.goTo("");
}
}
}, 1000);
}
bootstrapSession () {
// We need these functions to populate the dropdown boxes
const wheelConfigValuesPromise = this.api.getWheelConfigValues();
const runnerStateOrchestrateShowSlsPromise = this.api.getRunnerStateOrchestrateShowSls();
const wheelKeyListAllPromise = this.api.getWheelKeyListAll();
// these may have been hidden on a previous logout
Utils.hideAllMenus(false);
// We need these functions to populate the dropdown boxes
// or determine visibility of menu items
wheelConfigValuesPromise.then((pWheelConfigValuesData) => {
LoginPanel._handleLoginWheelConfigValues(pWheelConfigValuesData);
Router.updateMainMenu();
return true;
}, () => false);
runnerStateOrchestrateShowSlsPromise.then((pRunnerStateOrchestrateShowSlsData) => {
LoginPanel._handleRunnerStateOrchestrateShowSls(pRunnerStateOrchestrateShowSlsData);
Router.updateMainMenu();
return true;
}, () => false);
// save for the autocompletion
/* eslint-disable no-unused-vars */
wheelKeyListAllPromise.then((pWheelKeyListAllData) => {
const minions = pWheelKeyListAllData.return[0].data.return.minions;
Utils.setStorageItem("session", "minions", JSON.stringify(minions));
}, (pWheelKeyListAllMsg) => {
// VOID
});
/* eslint-enable no-unused-vars */
BeaconsMinionPanel.getAvailableBeacons(this.api);
}
static _handleRunnerStateOrchestrateShowSls (pRunnerStateOrchestrateShowSlsData) {
// until we prove it it available
Utils.setStorageItem("session", "orchestrations", "false");
const ret = pRunnerStateOrchestrateShowSlsData.return[0];
for (const key in ret) {
const obj = ret[key];
for (const stepkey in obj) {
const step = obj[stepkey].salt;
if (step === undefined) {
continue;
}
for (const item of step) {
if (item === "function" || item === "state" || item === "runner" || item === "wheel") {
Utils.setStorageItem("session", "orchestrations", "true");
return;
}
}
}
}
}
static _handleLoginWheelConfigValues (pWheelConfigValuesData) {
const wheelConfigValuesData = pWheelConfigValuesData.return[0].data.return;
// store for later use
const templates = wheelConfigValuesData.saltgui_templates;
Utils.setStorageItem("session", "templates", JSON.stringify(templates));
for (const templateName in templates) {
const template = templates[templateName];
if (template.key !== undefined) {
Utils.setStorageItem("session", "template_" + template.key, templateName);
}
}
const reactors = wheelConfigValuesData.reactor;
Utils.setStorageItem("session", "reactors", JSON.stringify(reactors));
const pages = wheelConfigValuesData.saltgui_pages;
Utils.setStorageItem("session", "pages", JSON.stringify(pages));
const publicPillars = wheelConfigValuesData.saltgui_public_pillars;
Utils.setStorageItem("session", "public_pillars", JSON.stringify(publicPillars));
const previewGrains = wheelConfigValuesData.saltgui_preview_grains;
Utils.setStorageItem("session", "preview_grains", JSON.stringify(previewGrains));
const ipNumberField = wheelConfigValuesData.saltgui_ipnumber_field;
Utils.setStorageItem("session", "ipnumber_field", ipNumberField);
const ipNumberPrefix = wheelConfigValuesData.saltgui_ipnumber_prefix;
Utils.setStorageItem("session", "ipnumber_prefix", JSON.stringify(ipNumberPrefix));
const maxShowHighstates = wheelConfigValuesData.saltgui_max_show_highstates;
Utils.setStorageItem("session", "max_show_highstates", JSON.stringify(maxShowHighstates));
const maxHighstateStates = wheelConfigValuesData.saltgui_max_highstate_states;
Utils.setStorageItem("session", "max_highstate_states", JSON.stringify(maxHighstateStates));
const showSaltEnvs = wheelConfigValuesData.saltgui_show_saltenvs;
Utils.setStorageItem("session", "show_saltenvs", JSON.stringify(showSaltEnvs));
const hideSaltEnvs = wheelConfigValuesData.saltgui_hide_saltenvs;
Utils.setStorageItem("session", "hide_saltenvs", JSON.stringify(hideSaltEnvs));
const showJobs = wheelConfigValuesData.saltgui_show_jobs;
Utils.setStorageItem("session", "show_jobs", JSON.stringify(showJobs));
const hideJobs = wheelConfigValuesData.saltgui_hide_jobs;
Utils.setStorageItem("session", "hide_jobs", JSON.stringify(hideJobs));
const useCacheForGrains = wheelConfigValuesData.saltgui_use_cache_for_grains;
Utils.setStorageItem("session", "use_cache_for_grains", JSON.stringify(useCacheForGrains));
const useCacheForPillar = wheelConfigValuesData.saltgui_use_cache_for_pillar;
Utils.setStorageItem("session", "use_cache_for_pillar", JSON.stringify(useCacheForPillar));
const syndicMaster = wheelConfigValuesData.syndic_master;
Utils.setStorageItem("session", "syndic_master", syndicMaster);
const orderMasters = wheelConfigValuesData.order_masters;
Utils.setStorageItem("session", "order_masters", orderMasters);
let nodeGroups = wheelConfigValuesData.nodegroups;
// Even when not set, the api server gives this an actual value "{}" here.
// Let's assume the user never sets that value. Sounds reasonable because
// when it is set, it is normally set to an actual value/list.
if (!nodeGroups || !Object.keys(nodeGroups).length) {
nodeGroups = undefined;
}
Utils.setStorageItem("session", "nodegroups", JSON.stringify(nodeGroups));
const stateVerbose = wheelConfigValuesData.saltgui_state_verbose;
Utils.setStorageItem("session", "state_verbose", JSON.stringify(stateVerbose));
const stateCompressIds = wheelConfigValuesData.state_compress_ids;
Utils.setStorageItem("session", "state_compress_ids", stateCompressIds);
const stateOutput = wheelConfigValuesData.saltgui_state_output;
Utils.setStorageItem("session", "state_output", stateOutput);
const stateOutputPct = wheelConfigValuesData.saltgui_state_output_pct;
Utils.setStorageItem("session", "state_output_pct", stateOutputPct);
const outputFormats = wheelConfigValuesData.saltgui_output_formats;
Utils.setStorageItem("session", "output_formats", outputFormats);
const dateTimeFractionDigits = wheelConfigValuesData.saltgui_datetime_fraction_digits;
Utils.setStorageItem("session", "datetime_fraction_digits", JSON.stringify(dateTimeFractionDigits));
const skipWheelMinionsConnected = wheelConfigValuesData.saltgui_skip_wheel_minions_connected;
Utils.setStorageItem("session", "skip_wheel_minions_connected", JSON.stringify(skipWheelMinionsConnected));
const dateTimeRepresentation = wheelConfigValuesData.saltgui_datetime_representation;
Utils.setStorageItem("session", "datetime_representation", dateTimeRepresentation);
const toolTipMode = wheelConfigValuesData.saltgui_tooltip_mode;
Utils.setStorageItem("session", "tooltip_mode", toolTipMode);
const motdTxt = wheelConfigValuesData.saltgui_motd_txt;
Utils.setStorageItem("session", "motd_txt", motdTxt);
const motdHtml = wheelConfigValuesData.saltgui_motd_html;
Utils.setStorageItem("session", "motd_html", motdHtml);
const customHelp = wheelConfigValuesData.saltgui_custom_command_help;
Utils.setStorageItem("session", "custom_command_help", customHelp);
const fullReturn = wheelConfigValuesData.saltgui_full_return;
Utils.setStorageItem("session", "full_return", fullReturn);
Utils.setStorageItem("session", "select_visible", "false");
Utils.setStorageItem("session", "select_minions", ",");
const id = wheelConfigValuesData.id;
const clusterId = wheelConfigValuesData.cluster_id;
const clusterPeers = wheelConfigValuesData.cluster_peers;
if (id && clusterId && clusterPeers) {
const clusterInfo = "This is node " + id + " from cluster " + clusterId + " " + JSON.stringify(clusterPeers).replace(/"/g, "");
Utils.setStorageItem("session", "cluster_info", clusterInfo);
}
let testProvidersTarget = wheelConfigValuesData.test_providers_target;
if (!testProvidersTarget) {
testProvidersTarget = "*";
}
Utils.setStorageItem("session", "test_providers_target", testProvidersTarget);
}
_onLoginFailure (error) {
if (typeof error === "string") {
// something detected before trying to login
this._showNoticeText("var(--color-notice-danger)", error, "notice_login_string_error");
} else if (error && error.status === 503) {
// Service Unavailable
// e.g. salt-api running but salt-master not running
this._showNoticeText("var(--color-notice-danger)", error.message, "notice_login_service_unavailable");
} else if (error && error.status === -1) {
// No permissions: login valid, but no api functions executable
// e.g. PAM says OK and /etc/salt/master says NO
this._showNoticeText("var(--color-notice-danger)", error.message, "notice_login_other_error");
} else if (error.toString().startsWith("TypeError: NetworkError")) {
this._showNoticeText("var(--color-notice-danger)", "Network Error", "notice_login_other_error");
} else {
this._showNoticeText("var(--color-notice-danger)", "Authentication failed", "notice_auth_failed");
}
this._enableLoginControls(true);
}
_enableLoginControls (pEnable) {
this.usernameField.disabled = !pEnable;
this.passwordField.disabled = !pEnable;
this.eauthField.disabled = !pEnable;
this.loginButton.disabled = !pEnable;
if (pEnable) {
this.usernameField.focus();
} else {
this.usernameField.blur();
this.passwordField.blur();
this.eauthField.blur();
this.loginButton.blur();
}
}
}