-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreeipa.jsx
562 lines (491 loc) · 16.6 KB
/
freeipa.jsx
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
558
559
560
561
562
import React from "react";
import $ from "jquery";
import cockpit from "cockpit";
import dialog from "cockpit-components-dialog.jsx"
import OnOff from "cockpit-components-onoff.jsx"
import "./freeipa.css";
import "table.css";
/* UTILITIES */
function left_click(fun) {
return function (event) {
if (!event || event.button !== 0)
return;
event.stopPropagation();
return fun(event);
};
}
function show_error(text) {
dialog.show_modal_dialog(
{
title: "Error",
body: (
<div className="modal-body">
<p>{text}</p>
</div>
)
},
{
cancel_caption: "Close",
actions: [ ]
});
}
/* FIREWALL */
class FirewallPorts extends React.Component {
constructor() {
super();
this.state = { };
}
componentDidMount() {
var self = this;
self.firewalld = cockpit.dbus("org.fedoraproject.FirewallD1");
self.zone = self.firewalld.proxy("org.fedoraproject.FirewallD1.zone",
"/org/fedoraproject/FirewallD1");
self.props.ports.forEach(p => {
self.zone.call("queryService", [ "", p ]).
done(r => {
var s = { }; s[p] = r[0];
self.setState(s);
});
});
$(self.zone).on("ServiceAdded", (event, zone, service) => {
// XXX - we ignore zone
if (service in self.state) {
var s = { }; s[service] = true;
self.setState(s);
}
});
$(self.zone).on("ServiceRemoved", (event, zone, service) => {
// XXX - we ignore zone
if (service in self.state) {
var s = { }; s[service] = false;
self.setState(s);
}
});
}
render() {
var self = this;
function row(p) {
function toggle(val) {
// XXX - this only affects the runtime config
if (val) {
self.zone.call("addService", [ "", p, 0 ]).
fail(err => {
console.warn("Failed to open port", p, err.message || err);
});
} else {
self.zone.call("removeService", [ "", p ]).
fail(err => {
console.warn("Failed to close port", p, err.message || err);
});
}
}
return (
<tr>
<td>{p}</td>
<td>
{ self.state[p] === undefined? null : <OnOff.OnOffSwitch state={self.state[p]}
onChange={toggle}/>
}
</td>
</tr>
);
}
return (
<div>
<h3>Network Ports</h3>
<table className="port-status-table">
{ this.props.ports.map(row) }
</table>
</div>
);
}
}
/* STATUS */
function parse_ipactl_status(text, conf) {
var config_re = /^(.+)=(.+)$/;
var config = { };
conf.split("\n").forEach(l => {
var m = config_re.exec(l);
if (m)
config[m[1].trim()] = m[2].trim();
});
var service_re = /^(.+) Service: (.+)$/;
var services = [ ];
var stopped = true;
text.split("\n").forEach(l => {
var m = service_re.exec(l);
if (m) {
var name = m[1];
var unit;
var status, status_class;
if (name == "Directory" && config.realm)
name = "dirsrv@" + config.realm;
// XXX - ipctl should tell us the unit
unit = name + ".service";
status = status_class = m[2];
if (status_class == "RUNNING")
status = "Running";
else if (status_class == "STOPPED")
status = "Not running";
services.push({ name: name, unit: unit,
status: status, status_class: status_class });
if (m[2] != "STOPPED")
stopped = false;
}
});
return {
stopped: stopped,
services: services,
config: config
};
}
class ServiceStatus extends React.Component {
render() {
var status = this.props.status;
return (
<div>
<p>The FreeIPA web interface can be accessed at <a href={"https://" + status.config.host}>
{status.config.host}</a>
</p>
<h3>Services</h3>
<table className="service-status-table">
{ status.services.map(s => (
<tr>
<td>
<a onClick={left_click(() => {
cockpit.jump("system/services#/" + encodeURIComponent(s.unit));
})}>
{s.name}
</a>
</td>
<td className={s.status_class}>{s.status}</td>
</tr>
))
}
</table>
</div>
);
}
}
class Status extends React.Component {
constructor() {
super();
this.state = { status: null, action: null };
}
componentDidMount() {
this.update_status();
}
update_status() {
this.setState({ status: { running: true } });
cockpit.spawn([ "ipactl", "status" ], { superuser: true, err: "message" })
.done(output => {
cockpit.file("/etc/ipa/default.conf").read().done(config => {
this.setState({ status: parse_ipactl_status(output, config) });
});
})
.fail((error) => {
if (error.exit_status == 4) {
this.setState({ status: { needs_config: true } });
} else {
this.setState({ status: { failure: error.message } });
}
});
}
start() {
this.setState({ action: { running: true,
title: "Starting" } });
cockpit.spawn([ "ipactl", "start" ], { superuser: true, err: "message" })
.done(() => {
this.setState({ action: { } });
this.update_status();
})
.fail((error) => {
this.setState({ action: { } });
show_error(error.message);
this.update_status();
});
}
stop() {
this.setState({ action: { running: true,
title: "Stopping" } });
cockpit.spawn([ "ipactl", "stop" ], { superuser: true, err: "message" })
.done(() => {
this.setState({ action: { } });
this.update_status();
})
.fail((error) => {
this.setState({ action: { } });
show_error(error.message);
this.update_status();
});
}
render() {
var self = this;
function show_setup_dialog() {
setup_dialog(() => {
self.update_status();
});
}
// XXX - hard coded
// XXX - just use freeipa-ldap?
var ports = [ "http", "https", "ldap", "ldaps", "kerberos", "kpasswd", "ntp" ];
var status = this.state.status;
if (!status || status.running)
return <div className="spinner spinner-lg status-spinner"/>;
if (status.needs_config)
return (
<center className="setup-message">
<p><img src="logo-big.png"/></p>
<p>FreeIPA needs to be setup before it can be used</p>
<p><button className="btn btn-primary"
onClick={left_click(show_setup_dialog)}>
Run Initial Setup
</button></p>
</center>
);
var status_text;
var status_button;
if (status.failure) {
status_text = (
<span>There was an error while checking the status. <a onClick={left_click(() => show_error(status.failure))}>More..</a></span>
);
status_button = null;
} else if (this.state.action && this.state.action.running) {
status_text = null;
status_button = (
<div className="spinner"/>
);
} else if (status.stopped) {
status_text = "Stopped";
status_button = (
<button className="btn btn-default"
onClick={left_click(() => { this.start(); })}>
Start
</button>
);
} else {
status_text = "Running";
status_button = (
<button className="btn btn-default"
onClick={left_click(() => { this.stop(); })}>
Stop
</button>
);
}
return (
<div>
<table className="table header">
<tbody>
<tr>
<td><img src="logo.png"/></td>
<td>FreeIPA</td>
<td>{status_text}</td>
{ status_button? <td>{status_button}</td> : null }
</tr>
</tbody>
</table>
<div>
<div className="pull-right">
<FirewallPorts ports={ports}/>
</div>
<ServiceStatus status={status}/>
</div>
</div>
);
}
}
/* SETUP */
function setup(options, progress_cb) {
var outbuf = "";
var cur_title, cur_perc, progress;
var perc_re = /^ {2}\[(\d+)\/(\d+)\]/;
function parse_progress(data) {
outbuf += data;
var lines = outbuf.split("\n");
for (var i = 0; i < lines.length-1; i++) {
var m = perc_re.exec(lines[i]);
if (m) {
cur_perc = parseInt(m[1])/parseInt(m[2]) * 100;
} else {
cur_title = lines[i];
}
}
if (cur_title) {
progress = cur_title;
if (cur_perc)
progress += " / " + cur_perc.toFixed(0) + "%";
progress_cb(progress);
}
outbuf = lines[lines.length-1];
}
var promise = cockpit.spawn([ "ipa-server-install",
"-U",
"-r", options.realm,
"-p", options.dirmanpw,
"-a", options.adminpw ],
{ superuser: true,
err: "message"
});
promise.stream(parse_progress);
promise.cancel = () => {
console.log("cancelling");
promise.close("terminated");
};
return promise;
}
class Validated extends React.Component {
render() {
var error = this.props.errors && this.props.errors[this.props.error_key];
// We need to always render the <div> for the has-error
// class so that the input field keeps the focus when
// errors are cleared. Otherwise the DOM changes enough
// for the Browser to remove focus.
return (
<div className={error? "has-error" : ""}>
{this.props.children}
{error? <span className="help-block">{error}</span> : null}
</div>
);
}
}
class SetupBody extends React.Component {
render() {
var props = this.props;
function input_box(field, type) {
return (
<Validated errors={props.errors} error_key={field}>
<input className="form-control" type={type}
value={props.values[field]}
onChange={
(event) => {
props.values[field] = event.target.value;
props.onchanged();
}}/>
</Validated>
);
}
function dialog_row(title, field, type) {
return (
<tr>
<td className="top">
<label className="control-label">{title}</label>
</td>
<td>
{ input_box(field, type) }
</td>
</tr>
);
}
return (
<div className="modal-body">
<table className="form-table-ct">
{ dialog_row("Realm", 'realm', 'text') }
{ dialog_row("Directory Manager password", 'dirmanpw', 'password') }
{ dialog_row("Confirm Directory Manager password", 'dirmanpw2', 'password') }
{ dialog_row("Admin password", 'adminpw', 'password') }
{ dialog_row("Confirm Admin password", 'adminpw2', 'password') }
</table>
</div>
);
}
}
function setup_dialog(done_callback) {
var dlg;
var errors = null;
var values = {
realm: "",
dirmanpw: "",
dirmanpw2: "",
adminpw: "",
adminpw2: ""
};
function onchanged() {
if (errors) {
errors = null;
update();
}
}
function body_props() {
return {
title: "Setup FreeIPA",
body: <SetupBody values={values}
errors={errors}
onchanged={onchanged}/>
};
}
function update() {
dlg.setProps(body_props());
}
function validate() {
errors = { };
if (!values.realm)
errors.realm = "Realm can't be empty";
function validate_password(field, field2) {
if (!values[field])
errors[field] = "Password can't be empty";
if (values[field].length < 8)
errors[field] = "Password must be at least 8 characters";
if (values[field] && values[field2] != values[field])
errors[field2] = "Passwords don't match";
}
validate_password('dirmanpw', 'dirmanpw2');
validate_password('adminpw', 'adminpw2');
if (Object.keys(errors).length === 0)
errors = null;
update();
return cockpit.resolve();
}
function apply(progress_cb) {
var dfd = cockpit.defer();
var promise = dfd.promise();
var setup_promise;
var cancelled = false;
validate().
done(function () {
if (cancelled) {
cockpit.reject();
} else {
setup_promise = setup(values, progress_cb);
setup_promise.
done(function () {
dfd.resolve();
}).
fail(function (error) {
dfd.reject(error);
});
}
}).
fail(function(error) {
dfd.reject(error);
});
promise.cancel = function() {
if (setup_promise)
setup_promise.close("terminated");
cancelled = true;
}
return promise;
}
dlg = dialog.show_modal_dialog(
body_props(),
{
actions: [ { 'clicked': apply,
'caption': "Setup",
'style': 'primary' } ],
dialog_done: done_callback
}
);
}
/* MAIN */
class App extends React.Component {
render() {
return (
<div className="container-fluid">
<Status/>
</div>
);
}
}
$(function () {
React.render(<App/>, $('#app')[0]);
$('body').show();
});