secubox-openwrt/package/secubox/luci-app-crowdsec-dashboard/htdocs/luci-static/resources/view/crowdsec-dashboard/settings.js
CyberMind-FR 8f6eeede06 feat(crowdsec): Add console enrollment & integrate SecuBox theme
- Add Console Enrollment section to settings with persistent key storage
- Integrate CrowdSec CSS with SecuBox global theme variables (--cyber-*)
- Fix modules.js install button and add installModule function
- Map cs-*, soc-*, sh-* CSS variables to SecuBox theme for consistency

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 05:34:31 +01:00

449 lines
16 KiB
JavaScript

'use strict';
'require view';
'require dom';
'require ui';
'require uci';
'require fs';
'require crowdsec-dashboard.api as api';
/**
* CrowdSec SOC - Settings View
* System configuration and management
*/
return view.extend({
title: _('Settings'),
status: {},
machines: [],
collections: [],
settings: {},
load: function() {
return Promise.all([
api.getStatus(),
api.getMachines(),
api.getCollections(),
api.getAcquisitionConfig(),
api.getSettings(),
uci.load('crowdsec-dashboard')
]);
},
render: function(data) {
var self = this;
this.status = data[0] || {};
var machinesData = data[1] || {};
this.machines = Array.isArray(machinesData) ? machinesData : (machinesData.machines || []);
var collectionsData = data[2] || {};
this.collections = collectionsData.collections || [];
if (this.collections.collections) this.collections = this.collections.collections;
this.acquisition = data[3] || {};
this.settings = data[4] || {};
document.body.classList.add('cs-fullwidth');
return E('div', { 'class': 'cs-dashboard cs-theme-classic' }, [
this.renderHeader(),
this.renderNav('settings'),
E('div', { 'class': 'cs-stats' }, this.renderServiceStats()),
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, [
'Service Control',
E('span', { 'class': 'cs-severity ' + (this.status.crowdsec === 'running' ? 'low' : 'critical') },
this.status.crowdsec === 'running' ? 'RUNNING' : 'STOPPED')
]),
E('div', { 'class': 'cs-card-body' }, this.renderServiceControl())
]),
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, [
'Console Enrollment',
E('span', { 'class': 'cs-severity ' + (this.status.capi_enrolled ? 'low' : 'medium') },
this.status.capi_enrolled ? 'ENROLLED' : 'NOT ENROLLED')
]),
E('div', { 'class': 'cs-card-body', 'id': 'console-enrollment' }, this.renderConsoleEnrollment())
]),
E('div', { 'class': 'cs-grid-2' }, [
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, 'Acquisition Sources'),
E('div', { 'class': 'cs-card-body' }, this.renderAcquisition())
]),
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, 'Registered Machines'),
E('div', { 'class': 'cs-card-body' }, this.renderMachines())
])
]),
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, [
'Installed Collections (' + this.collections.filter(function(c) { return c.status === 'enabled' || c.installed; }).length + ')',
E('button', { 'class': 'cs-btn cs-btn-sm', 'click': L.bind(this.updateHub, this) }, 'Update Hub')
]),
E('div', { 'class': 'cs-card-body', 'id': 'collections-list' }, this.renderCollections())
]),
E('div', { 'class': 'cs-card' }, [
E('div', { 'class': 'cs-card-header' }, 'Configuration Files'),
E('div', { 'class': 'cs-card-body' }, this.renderConfigFiles())
])
]);
},
renderHeader: function() {
return E('div', { 'class': 'cs-header' }, [
E('div', { 'class': 'cs-title' }, [
E('svg', { 'viewBox': '0 0 24 24' }, [E('path', { 'd': 'M12 2L2 7v10l10 5 10-5V7L12 2z' })]),
'CrowdSec Security Operations'
]),
E('div', { 'class': 'cs-status' }, [E('span', { 'class': 'cs-status-dot online' }), 'SETTINGS'])
]);
},
renderNav: function(active) {
var tabs = ['overview', 'alerts', 'decisions', 'bouncers', 'settings'];
return E('div', { 'class': 'cs-nav' }, tabs.map(function(t) {
return E('a', {
'href': L.url('admin/secubox/security/crowdsec/' + t),
'class': active === t ? 'active' : ''
}, t.charAt(0).toUpperCase() + t.slice(1));
}));
},
renderServiceStats: function() {
var s = this.status;
return [
E('div', { 'class': 'cs-stat ' + (s.crowdsec === 'running' ? 'success' : 'danger') }, [
E('div', { 'class': 'cs-stat-value' }, s.crowdsec === 'running' ? 'ON' : 'OFF'),
E('div', { 'class': 'cs-stat-label' }, 'CrowdSec Agent')
]),
E('div', { 'class': 'cs-stat ' + (s.lapi_status === 'available' ? 'success' : 'danger') }, [
E('div', { 'class': 'cs-stat-value' }, s.lapi_status === 'available' ? 'OK' : 'DOWN'),
E('div', { 'class': 'cs-stat-label' }, 'Local API')
]),
E('div', { 'class': 'cs-stat' }, [
E('div', { 'class': 'cs-stat-value' }, s.version || 'N/A'),
E('div', { 'class': 'cs-stat-label' }, 'Version')
]),
E('div', { 'class': 'cs-stat' }, [
E('div', { 'class': 'cs-stat-value' }, String(this.machines.length)),
E('div', { 'class': 'cs-stat-label' }, 'Machines')
])
];
},
renderServiceControl: function() {
var self = this;
var running = this.status.crowdsec === 'running';
return E('div', {}, [
E('div', { 'style': 'display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px;' }, [
E('button', {
'class': 'cs-btn ' + (running ? '' : 'primary'),
'click': function() { self.serviceAction('start'); }
}, 'Start'),
E('button', {
'class': 'cs-btn ' + (running ? 'danger' : ''),
'click': function() { self.serviceAction('stop'); }
}, 'Stop'),
E('button', {
'class': 'cs-btn',
'click': function() { self.serviceAction('restart'); }
}, 'Restart'),
E('button', {
'class': 'cs-btn',
'click': function() { self.serviceAction('reload'); }
}, 'Reload')
]),
E('div', { 'class': 'cs-health' }, [
E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (running ? 'ok' : 'error') }, running ? '\u2713' : '\u2717'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, 'Agent'),
E('div', { 'class': 'cs-health-value' }, running ? 'Running' : 'Stopped')
])
]),
E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (this.status.lapi_status === 'available' ? 'ok' : 'error') },
this.status.lapi_status === 'available' ? '\u2713' : '\u2717'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, 'LAPI'),
E('div', { 'class': 'cs-health-value' }, this.status.lapi_status === 'available' ? 'Available' : 'Unavailable')
])
]),
E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (this.status.capi_enrolled ? 'ok' : 'warn') },
this.status.capi_enrolled ? '\u2713' : '!'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, 'CAPI'),
E('div', { 'class': 'cs-health-value' }, this.status.capi_enrolled ? 'Enrolled' : 'Not enrolled')
])
])
])
]);
},
renderAcquisition: function() {
var acq = this.acquisition;
var sources = [
{ name: 'Syslog', enabled: acq.syslog_enabled, path: acq.syslog_path },
{ name: 'SSH', enabled: acq.ssh_enabled },
{ name: 'Firewall', enabled: acq.firewall_enabled },
{ name: 'HTTP', enabled: acq.http_enabled }
];
return E('div', { 'class': 'cs-health' }, sources.map(function(src) {
return E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (src.enabled ? 'ok' : 'error') }, src.enabled ? '\u2713' : '\u2717'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, src.name),
E('div', { 'class': 'cs-health-value' }, src.enabled ? (src.path || 'Enabled') : 'Disabled')
])
]);
}));
},
renderCollections: function() {
var self = this;
var installed = this.collections.filter(function(c) {
return c.status === 'enabled' || c.installed === 'ok';
});
if (!installed.length) {
return E('div', { 'class': 'cs-empty' }, [
E('div', { 'class': 'cs-empty-icon' }, '\u26A0'),
'No collections installed. Click "Update Hub" to fetch available collections.'
]);
}
return E('table', { 'class': 'cs-table' }, [
E('thead', {}, E('tr', {}, [
E('th', {}, 'Collection'),
E('th', {}, 'Version'),
E('th', {}, 'Status'),
E('th', {}, 'Actions')
])),
E('tbody', {}, installed.map(function(c) {
return E('tr', {}, [
E('td', {}, E('span', { 'class': 'cs-scenario' }, c.name || 'Unknown')),
E('td', { 'class': 'cs-time' }, c.version || c.local_version || 'N/A'),
E('td', {}, E('span', { 'class': 'cs-severity low' }, 'INSTALLED')),
E('td', {}, E('button', {
'class': 'cs-btn cs-btn-sm danger',
'click': function() { self.removeCollection(c.name); }
}, 'Remove'))
]);
}))
]);
},
renderMachines: function() {
if (!this.machines.length) {
return E('div', { 'class': 'cs-empty' }, 'No machines registered');
}
return E('table', { 'class': 'cs-table' }, [
E('thead', {}, E('tr', {}, [
E('th', {}, 'Machine ID'),
E('th', {}, 'IP Address'),
E('th', {}, 'Last Update'),
E('th', {}, 'Status')
])),
E('tbody', {}, this.machines.map(function(m) {
var isActive = m.isValidated || m.is_validated;
return E('tr', {}, [
E('td', {}, E('strong', {}, m.machineId || m.machine_id || 'Unknown')),
E('td', {}, E('span', { 'class': 'cs-ip' }, m.ipAddress || m.ip_address || 'N/A')),
E('td', { 'class': 'cs-time' }, api.formatRelativeTime(m.updated_at || m.updatedAt)),
E('td', {}, E('span', { 'class': 'cs-severity ' + (isActive ? 'low' : 'medium') },
isActive ? 'ACTIVE' : 'PENDING'))
]);
}))
]);
},
renderConfigFiles: function() {
var configs = [
{ label: 'Main Config', path: '/etc/crowdsec/config.yaml' },
{ label: 'Acquisition', path: '/etc/crowdsec/acquis.yaml' },
{ label: 'Profiles', path: '/etc/crowdsec/profiles.yaml' },
{ label: 'Local API', path: '/etc/crowdsec/local_api_credentials.yaml' },
{ label: 'Firewall Bouncer', path: '/etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml' }
];
return E('div', { 'style': 'display: grid; gap: 8px;' }, configs.map(function(cfg) {
return E('div', { 'style': 'display: flex; justify-content: space-between; align-items: center; padding: 8px; background: var(--cs-bg); border-radius: 4px;' }, [
E('span', { 'style': 'color: var(--cs-text-muted);' }, cfg.label),
E('code', { 'class': 'cs-ip' }, cfg.path)
]);
}));
},
renderConsoleEnrollment: function() {
var self = this;
var enrolled = this.status.capi_enrolled;
var savedKey = this.settings.enrollment_key || '';
var machineName = this.settings.machine_name || '';
return E('div', {}, [
E('p', { 'style': 'color: var(--cs-text-muted); margin-bottom: 16px;' },
'Enroll in CrowdSec Console to receive community blocklists and share threat intelligence.'),
E('div', { 'style': 'display: grid; gap: 12px; margin-bottom: 16px;' }, [
E('div', {}, [
E('label', { 'style': 'display: block; margin-bottom: 4px; color: var(--cs-text-muted);' }, 'Enrollment Key'),
E('input', {
'type': 'text',
'id': 'enrollment-key',
'class': 'cs-input',
'value': savedKey,
'placeholder': 'Enter your enrollment key from app.crowdsec.net',
'style': 'width: 100%; padding: 8px 12px; border: 1px solid var(--cs-border); border-radius: 4px; background: var(--cs-bg); color: var(--cs-text);'
})
]),
E('div', {}, [
E('label', { 'style': 'display: block; margin-bottom: 4px; color: var(--cs-text-muted);' }, 'Machine Name (optional)'),
E('input', {
'type': 'text',
'id': 'machine-name',
'class': 'cs-input',
'value': machineName,
'placeholder': 'Custom name for this machine',
'style': 'width: 100%; padding: 8px 12px; border: 1px solid var(--cs-border); border-radius: 4px; background: var(--cs-bg); color: var(--cs-text);'
})
])
]),
E('div', { 'style': 'display: flex; gap: 8px; flex-wrap: wrap;' }, [
E('button', {
'class': 'cs-btn primary',
'click': function() { self.saveAndEnroll(); }
}, enrolled ? 'Re-enroll' : 'Save & Enroll'),
E('button', {
'class': 'cs-btn',
'click': function() { self.saveSettings(); }
}, 'Save Only'),
enrolled ? E('button', {
'class': 'cs-btn danger',
'click': function() { self.disableConsole(); }
}, 'Disable') : E('span')
]),
E('div', { 'class': 'cs-health', 'style': 'margin-top: 16px;' }, [
E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (enrolled ? 'ok' : 'error') }, enrolled ? '\u2713' : '\u2717'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, 'Console Status'),
E('div', { 'class': 'cs-health-value' }, enrolled ? 'Enrolled and active' : 'Not enrolled')
])
]),
E('div', { 'class': 'cs-health-item' }, [
E('div', { 'class': 'cs-health-icon ' + (savedKey ? 'ok' : 'warn') }, savedKey ? '\u2713' : '!'),
E('div', {}, [
E('div', { 'class': 'cs-health-label' }, 'Key Saved'),
E('div', { 'class': 'cs-health-value' }, savedKey ? 'Key stored in config' : 'No key saved')
])
])
])
]);
},
saveSettings: function() {
var self = this;
var key = document.getElementById('enrollment-key').value.trim();
var name = document.getElementById('machine-name').value.trim();
api.saveSettings(key, name, '0').then(function(r) {
if (r.success) {
self.showToast('Settings saved', 'success');
} else {
self.showToast('Failed to save: ' + (r.error || 'Unknown'), 'error');
}
}).catch(function(e) {
self.showToast('Error: ' + e.message, 'error');
});
},
saveAndEnroll: function() {
var self = this;
var key = document.getElementById('enrollment-key').value.trim();
var name = document.getElementById('machine-name').value.trim();
if (!key) {
self.showToast('Please enter an enrollment key', 'error');
return;
}
// First save the settings
api.saveSettings(key, name, '1').then(function(r) {
if (!r.success) {
self.showToast('Failed to save settings', 'error');
return;
}
// Then enroll
return api.consoleEnroll(key, name);
}).then(function(r) {
if (r && r.success) {
self.showToast('Enrolled successfully!', 'success');
setTimeout(function() { location.reload(); }, 2000);
} else if (r) {
self.showToast('Enrollment failed: ' + (r.error || 'Unknown'), 'error');
}
}).catch(function(e) {
self.showToast('Error: ' + e.message, 'error');
});
},
disableConsole: function() {
var self = this;
if (!confirm('Disable CrowdSec Console enrollment?')) return;
api.consoleDisable().then(function(r) {
if (r.success) {
self.showToast('Console disabled', 'success');
setTimeout(function() { location.reload(); }, 1500);
} else {
self.showToast('Failed: ' + (r.error || 'Unknown'), 'error');
}
});
},
serviceAction: function(action) {
var self = this;
api.serviceControl(action).then(function(r) {
if (r.success) {
self.showToast('Service ' + action + ' successful', 'success');
setTimeout(function() { location.reload(); }, 1500);
} else {
self.showToast('Failed: ' + (r.error || 'Unknown'), 'error');
}
});
},
updateHub: function() {
var self = this;
api.updateHub().then(function(r) {
if (r.success) {
self.showToast('Hub updated', 'success');
setTimeout(function() { location.reload(); }, 1500);
} else {
self.showToast('Failed: ' + (r.error || 'Unknown'), 'error');
}
});
},
removeCollection: function(name) {
var self = this;
if (!confirm('Remove collection "' + name + '"?')) return;
api.removeCollection(name).then(function(r) {
if (r.success) {
self.showToast('Collection removed', 'success');
setTimeout(function() { location.reload(); }, 1500);
} else {
self.showToast('Failed: ' + (r.error || 'Unknown'), 'error');
}
});
},
showToast: function(msg, type) {
var t = document.querySelector('.cs-toast');
if (t) t.remove();
t = E('div', { 'class': 'cs-toast ' + type }, msg);
document.body.appendChild(t);
setTimeout(function() { t.remove(); }, 4000);
},
handleSaveApply: null, handleSave: null, handleReset: null
});