From ab99386f54df1f32c9d2a5dbf13c056d7e3d9632 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:20:09 +0000 Subject: [PATCH 01/16] Local accounts sign in with a form and a session cookie; Sign out for everyone HTTP Basic is no longer accepted: browsers use the sign-in form, scripts use API tokens. Password changes, disabled and deleted accounts end the sessions of that account. Tests sign in through the form. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- public/app.css | 1 + public/app.js | 8 ++- server/server.js | 134 +++++++++++++++++++++------------------ server/sso.js | 38 +++++++---- tests/api.test.mjs | 19 ++++-- tests/e2e.mjs | 16 +++-- tests/hardening.test.mjs | 3 +- tests/modules.test.mjs | 3 +- tests/muster.test.mjs | 5 +- tests/readonly.test.mjs | 3 +- tests/secrets.test.mjs | 3 +- tests/session.mjs | 27 ++++++++ tests/signin.test.mjs | 25 ++++---- tests/sso.test.mjs | 40 ++++++++---- 14 files changed, 211 insertions(+), 114 deletions(-) create mode 100644 tests/session.mjs diff --git a/public/app.css b/public/app.css index ccf9818..3ef24ed 100644 --- a/public/app.css +++ b/public/app.css @@ -710,3 +710,4 @@ body.has-dash{overflow:hidden} #flatSvg .selhalo{animation:selhalo 1.6s ease-in-out infinite} @keyframes selhalo{0%,100%{opacity:.1}50%{opacity:.32}} @media (prefers-reduced-motion:reduce){#flatSvg .selhalo{animation:none}} +.ibtn.signout{border-color:var(--line);color:var(--fg);margin-left:6px;min-height:22px;padding:2px 8px} diff --git a/public/app.js b/public/app.js index d303df0..1e85e46 100644 --- a/public/app.js +++ b/public/app.js @@ -406,7 +406,7 @@ const Store={ const r=await fetch(url,{method,credentials:'same-origin',headers:h,body:body===undefined?undefined:JSON.stringify(body)}); let j=null;try{j=await r.json();}catch(e){} // a session that has ended (single sign-on): back to the sign-in page - if(r.status===401&&this.me&&this.me.via==='sso'){location.href='auth/login';} + if(r.status===401&&this.me&&this.me.signOut){location.href='auth/login';} if(!r.ok){const err=new Error((j&&j.error)||('HTTP '+r.status));err.status=r.status;err.body=j;throw err;} if(j&&j.rev)this.rev=j.rev; return j; @@ -465,6 +465,9 @@ const Store={ finally{this.bulk=false;this.batch=null;this.applyView(0);if(this.view==='draft'){clearTimeout(this._dt);this.saveDraft();}} } }; +// Sign out (local account or single sign-on), from the header or the account panel +async function signOut(){try{const j=await Store.api('POST','auth/logout',{});location.href=j.location||'auth/login?out=1';}catch(err){toast(err.message,true);}} +document.addEventListener('click',e=>{if(e.target.closest('[data-signout]')){e.preventDefault();signOut();}}); // Replace the whole map (demo, import, delete everything) in one request async function replaceAll(data,label){ const next={si:clone(data.si||{}),assets:clone(data.assets||{}),wan:clone(data.wan||[]),services:clone(data.services||[]),risk:clone(data.risk||{})}; @@ -632,7 +635,7 @@ function toast(msg,err,sticky){const t=$('#toast');t.textContent=msg;t.className /* ================= Title block and lists ================= */ const nums=arr=>`
${arr.map(([n,l,c])=>`
${n}${l}
`).join('')}
`; -function storeBadge(){return Store.mode==='api'?`Server${Store.user?' · '+esc(Store.user)+' ('+esc(Store.role)+')':''}`:`This browser only`;} +function storeBadge(){return Store.mode==='api'?`Server${Store.user?' · '+esc(Store.user)+' ('+esc(Store.role)+')':''}${Store.me&&Store.me.signOut?' ':''}`:`This browser only`;} function renderCart(){ const c=$('#cart'); if(state.view==='overview'){ @@ -3040,7 +3043,6 @@ det.addEventListener('change',async e=>{const s=e.target.closest('[data-urole]') det.addEventListener('click',async e=>{ const t=e.target,q=s=>t.closest(s); if(q('[data-new-prop]'))return proposalForm(); - if(q('[data-signout]')){try{const j=await Store.api('POST','auth/logout',{});location.href=j.location||'auth/login';}catch(err){toast(err.message,true);}return;} if(q('[data-endsessions]')){const b=q('[data-endsessions]');if(!b.classList.contains('armed')){b.classList.add('armed');b.textContent='Confirm: sign everyone out';return;} try{const j=await Store.api('DELETE','api/sessions',{});toast(`${j.ended} session(s) ended.`);showAccount();}catch(err){toast(err.message,true);}return;} if(q('[data-tdel]')){const b=q('[data-tdel]');if(!b.classList.contains('armed')){b.classList.add('armed');b.textContent='Confirm';return;} diff --git a/server/server.js b/server/server.js index 0f33822..9cf7e52 100644 --- a/server/server.js +++ b/server/server.js @@ -298,9 +298,9 @@ const hashPass = (pass, salt) => crypto.scryptSync(String(pass), salt, 32).toStr function loadUsers() { try { const st = fs.statSync(USERS_FILE); usersMtime = st.mtimeMs; const p = JSON.parse(fs.readFileSync(USERS_FILE, 'utf8')); users = isObj(p.users) ? p.users : {}; } catch { users = {}; } - authCache.clear(); + } -function saveUsers() { authCache.clear(); return writeJson(USERS_FILE, JSON.stringify({ users }, null, 2)).then(() => { try { usersMtime = fs.statSync(USERS_FILE).mtimeMs; } catch { } }); } +function saveUsers() { return writeJson(USERS_FILE, JSON.stringify({ users }, null, 2)).then(() => { try { usersMtime = fs.statSync(USERS_FILE).mtimeMs; } catch { } }); } function setUser(name, { role, password, disabled }) { const u = own(users, name) || { createdAt: new Date().toISOString() }; if (role) u.role = role; @@ -368,50 +368,38 @@ function recordFailure(kind, key, credential) { if (m.size > 10000) m.delete(m.keys().next().value); } -// Successful logins are cached for 10 minutes, so scrypt runs once per session, not per request -const authCache = new Map(); -// Returns the account and role, null when the credentials are missing or wrong, or {retryAfter} (ms) -// while sign-ins are locked -// Single sign-on and API tokens come first (sso.js); {denied} is a refusal to explain (403) +// Returns the account and role, or null when there is no valid sign-in. Local accounts sign in once +// with the form (/auth/login) and then carry a session cookie; scripts use API tokens. HTTP Basic is +// no longer accepted (0.11). {denied} is a refusal to explain (403) function authenticate(req) { const other = sso.fromSession(req) || sso.fromToken(req) || sso.fromHeaders(req); + if (other && other.via === 'local') { + // the account may have been disabled, deleted, given another role or a new password since + const u = own(users, other.user); + if (!u || u.disabled || !u.hash || (u.passwordChangedAt && Date.parse(u.passwordChangedAt) > other.since) + || sso.localLogin === 'off' || (sso.localLogin === 'admins' && u.role !== 'admin')) { sso.endUserSessions(other.user); return null; } + return { ...other, role: CONFIG.readOnly ? 'viewer' : (RANK[u.role] !== undefined ? u.role : 'viewer') }; + } if (other) return other.denied ? other : { ...other, role: CONFIG.readOnly ? 'viewer' : other.role }; if (!authEnabled() && !sso.enabled()) return { user: '', role: CONFIG.readOnly ? 'viewer' : 'admin', open: true, via: 'open' }; - // LOCAL_LOGIN=off: no local sign-in at all; admins: only the accounts whose stored role is admin. The - // others are not even checked, so the answer (401) says nothing of their password - if (sso.localLogin === 'off') return null; - if (sso.localLogin === 'admins' && own(users, basicName(req))?.role !== 'admin') return null; - return authenticateLocal(req); + return null; } -function basicName(req) { - const h = req.headers.authorization || ''; - if (!h.startsWith('Basic ')) return ''; - const raw = Buffer.from(h.slice(6), 'base64').toString('utf8'), i = raw.indexOf(':'); - return i < 1 ? '' : raw.slice(0, i); -} -function authenticateLocal(req) { - const h = req.headers.authorization || ''; - if (!h.startsWith('Basic ')) return null; - const raw = Buffer.from(h.slice(6), 'base64').toString('utf8'), i = raw.indexOf(':'); - const name = i < 1 ? '' : raw.slice(0, i), pass = raw.slice(i + 1); - const key = crypto.createHash('sha256').update(raw).digest('hex'), hit = authCache.get(key), addr = clientAddress(req); - const ok = hit && hit.until > Date.now() && hit.addrs.has(addr); - if (!ok) { - const wait = Math.max(lockedFor('address', addr), name ? lockedFor('account', name) : 0); - if (wait) return { retryAfter: wait }; - const u = name ? own(users, name) : undefined; - let good = !!(u && !u.disabled && u.hash); - if (good) { const a = Buffer.from(hashPass(pass, u.salt), 'hex'), b = Buffer.from(u.hash, 'hex'); good = a.length === b.length && crypto.timingSafeEqual(a, b); } - if (!good) { recordFailure('address', addr, key); if (name) recordFailure('account', name, key); return null; } - failures.account.delete(name); - const addrs = hit && hit.until > Date.now() ? hit.addrs : new Set(); - addrs.add(addr); - authCache.set(key, { until: Date.now() + 600000, addrs }); - } - const u = own(users, name); - if (!u || u.disabled) return null; - const role = CONFIG.readOnly ? 'viewer' : (RANK[u.role] !== undefined ? u.role : 'viewer'); - return { user: name, role, via: 'local' }; +// Checks a local account's password, with the limits on failed sign-ins: true, false, or {retryAfter} (ms) +function checkLocalPassword(name, pass, addr) { + name = String(name || ''); pass = String(pass || ''); + // LOCAL_LOGIN=off: no local sign-in at all; admins: only the accounts whose stored role is admin. The + // others are not even checked, so the answer says nothing of their password + if (sso.localLogin === 'off') return false; + if (sso.localLogin === 'admins' && own(users, name)?.role !== 'admin') return false; + const wait = Math.max(lockedFor('address', addr), name ? lockedFor('account', name) : 0); + if (wait) return { retryAfter: wait }; + const key = crypto.createHash('sha256').update(`${name}:${pass}`).digest('hex'); + const u = name ? own(users, name) : undefined; + let good = !!(u && !u.disabled && u.hash); + if (good) { const a = Buffer.from(hashPass(pass, u.salt), 'hex'), b = Buffer.from(u.hash, 'hex'); good = a.length === b.length && crypto.timingSafeEqual(a, b); } + if (!good) { recordFailure('address', addr, key); if (name) recordFailure('account', name, key); return false; } + failures.account.delete(name); + return true; } const can = (auth, role) => RANK[auth.role] >= RANK[role]; const need = (auth, role) => { if (!can(auth, role)) throw httpError(403, `This needs the ${role} role (you are ${auth.role}).`); }; @@ -430,7 +418,7 @@ const SEC_HEADERS = { // no other site's window or page may hold a reference to the app, or load its responses 'Cross-Origin-Opener-Policy': 'same-origin', 'Cross-Origin-Resource-Policy': 'same-origin', - 'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'none'", + 'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'", }; function send(res, status, body, headers = {}) { const isJson = typeof body !== 'string' && !Buffer.isBuffer(body); @@ -442,6 +430,15 @@ const fail = (res, status, error, extra) => send(res, status, { error, ...(extra // (closing the connection at once left it with a network error). Past the limit by more than // DISCARD_MAX, the connection is cut: nobody waits for a body that large. const DISCARD_MAX = 64 * 1024 * 1024; +// The raw body as text (a sign-in form), up to limit bytes +function readRaw(req, limit) { + return new Promise((resolve, reject) => { + let size = 0; const chunks = []; + req.on('data', c => { size += c.length; if (size > limit) { reject(httpError(413, 'Payload too large')); req.destroy(); } else chunks.push(c); }); + req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); + req.on('error', reject); + }); +} function readBody(req, limit) { return new Promise((resolve, reject) => { let size = 0, over = false; const chunks = []; @@ -494,7 +491,7 @@ async function api(req, res, parts, url, auth) { const ok = rev => send(res, 200, { ok: true, rev: rev ?? state.rev }); if (r1 === 'me' && !id && method === 'GET') return send(res, 200, { - user: auth.user, role: auth.role, auth: !auth.open, via: auth.via, signOut: auth.via === 'sso', sso: sso.oidc || sso.header, readOnly: CONFIG.readOnly, allowSelfApproval: CONFIG.allowSelfApproval, + user: auth.user, role: auth.role, auth: !auth.open, via: auth.via, signOut: auth.via === 'sso' || auth.via === 'local', sso: sso.oidc || sso.header, readOnly: CONFIG.readOnly, allowSelfApproval: CONFIG.allowSelfApproval, canWrite: can(auth, 'approver'), canPropose: can(auth, 'contributor'), canReview: can(auth, 'approver'), canAdmin: can(auth, 'admin'), connectors: !!connectors, sourceUrl: CONFIG.sourceUrl, muster: muster ? { url: muster.url } : null, }); if (r1 === 'me' && id === 'password' && method === 'PUT') { @@ -504,7 +501,12 @@ async function api(req, res, parts, url, auth) { const given = u ? Buffer.from(hashPass(b?.current || '', u.salt), 'hex') : null, stored = u ? Buffer.from(u.hash, 'hex') : null; if (!u || given.length !== stored.length || !crypto.timingSafeEqual(given, stored)) throw httpError(403, 'Current password is wrong.'); if (String(b?.next || '').length < 10) throw httpError(400, 'Use at least 10 characters.'); - setUser(auth.user, { password: b.next }); await saveUsers(); return send(res, 200, { ok: true }); + setUser(auth.user, { password: b.next }); await saveUsers(); + // other browsers signed in with the old password are signed out; this one gets a fresh session + sso.endUserSessions(auth.user, auth.sessionId); + const fresh = auth.sessionId ? sso.createSession({ user: auth.user, role: u.role, via: 'local' }) : null; + if (fresh) sso.endUserSessions(auth.user, fresh.sessionId); + return send(res, 200, { ok: true }, fresh ? { 'Set-Cookie': fresh.setCookie } : {}); } if (r1 === 'state' && method === 'GET') { const at = url.searchParams.get('at'); @@ -778,7 +780,7 @@ async function api(req, res, parts, url, auth) { } /* ---- API tokens (admin) ---- */ - if (r1 === 'sessions' && !id && method === 'DELETE') { need(auth, 'admin'); return send(res, 200, { ok: true, ended: sso.endSessions(auth.user || 'admin') }); } + if (r1 === 'sessions' && !id && method === 'DELETE') { need(auth, 'admin'); return send(res, 200, { ok: true, ended: sso.endSessions(auth.user || 'admin', auth.sessionId) }); } if (r1 === 'tokens') { need(auth, 'admin'); if (auth.open) throw httpError(400, 'Create an admin account first: with no account, a token would lock everyone else out.'); @@ -803,11 +805,12 @@ async function api(req, res, parts, url, auth) { if (b?.password && String(b.password).length < 10) throw httpError(400, 'Use at least 10 characters.'); if (id === auth.user && (b?.disabled || (b?.role && b.role !== 'admin'))) throw httpError(400, 'You cannot remove your own admin access.'); setUser(id, { role: b?.role || own(users, id)?.role || 'viewer', password: b?.password, disabled: b?.disabled }); + if (b?.password || b?.disabled) sso.endUserSessions(id); await saveUsers(); return send(res, 200, { ok: true }); } if (id && method === 'DELETE') { if (id === auth.user) throw httpError(400, 'You cannot delete your own account.'); - delete users[id]; await saveUsers(); return send(res, 200, { ok: true }); + delete users[id]; sso.endUserSessions(id); await saveUsers(); return send(res, 200, { ok: true }); } } return fail(res, 404, 'Unknown API route'); @@ -819,7 +822,27 @@ const localSignIn = () => sso.localLogin !== 'off' && authEnabled(); const html = (res, status, body, headers = {}) => send(res, status, body, { 'Content-Type': 'text/html; charset=utf-8', ...headers }); async function authRoute(req, res, url) { const p = url.pathname, page = (status, message, extra) => html(res, status, loginPage({ ssoName: sso.name, oidc: sso.oidc, local: localSignIn(), message, out: url.searchParams.has('out'), base: sso.publicUrl }), extra); - if (p === '/auth/login' && req.method === 'GET') return page(200); + if (p === '/auth/login' && req.method === 'GET') return authenticate(req) && !url.searchParams.has('out') ? send(res, 302, '', { Location: `${sso.publicUrl}/` }) : page(200); + // Local sign-in: the form of the sign-in page (urlencoded), or JSON from a script or a test + if (p === '/auth/login' && req.method === 'POST') { + const json = (req.headers['content-type'] || '').startsWith('application/json'); + // a page of another site must not sign a visitor in to an account of its choosing + const origin = req.headers.origin; + if (origin && origin !== 'null' && new URL(origin).host !== req.headers.host && (!sso.publicUrl || origin !== new URL(sso.publicUrl).origin)) return fail(res, 403, 'Sign-in from another site refused'); + if (!localSignIn()) return json ? fail(res, 404, 'Local sign-in is off') : page(404, 'Local sign-in is off on this server.'); + let b; + try { b = json ? await readBody(req, 4096) : Object.fromEntries(new URLSearchParams(await readRaw(req, 4096))); } + catch (e) { return fail(res, e.status || 400, e.message); } + const name = String(b?.user || '').slice(0, 64), r = checkLocalPassword(name, b?.password, clientAddress(req)); + if (r && r.retryAfter) { + const sec = Math.ceil(r.retryAfter / 1000), msg = `Too many failed sign-ins. Try again in ${sec >= 120 ? Math.ceil(sec / 60) + ' minutes' : sec + ' seconds'}.`; + return json ? send(res, 429, { error: msg }, { 'Retry-After': String(sec) }) : html(res, 429, loginPage({ ssoName: sso.name, oidc: sso.oidc, local: true, message: msg, base: sso.publicUrl, user: name }), { 'Retry-After': String(sec) }); + } + if (r !== true) return json ? fail(res, 401, 'Wrong user name or password') : html(res, 401, loginPage({ ssoName: sso.name, oidc: sso.oidc, local: true, message: 'Wrong user name or password.', base: sso.publicUrl, user: name })); + const u = own(users, name), s = sso.createSession({ user: name, role: u.role, via: 'local' }); + log(`Sign-in: ${JSON.stringify(name)} (local account)`); + return json ? send(res, 200, { ok: true, user: name, role: u.role }, { 'Set-Cookie': s.setCookie }) : send(res, 303, '', { Location: `${sso.publicUrl}/`, 'Set-Cookie': s.setCookie }); + } if (p === '/auth/oidc/start' && req.method === 'GET' && sso.oidc) { try { const s = await sso.start(); return send(res, 302, '', { Location: s.location, 'Set-Cookie': s.setCookie }); } catch (e) { log('SSO:', e.message); return page(502, `The identity provider cannot be reached: ${e.message}`); } @@ -847,20 +870,11 @@ const server = http.createServer(async (req, res) => { if (url.pathname.startsWith('/auth/')) return await authRoute(req, res, url); const auth = authenticate(req); if (auth && auth.denied) return send(res, 403, url.pathname.startsWith('/api/') ? { error: auth.denied } : loginPage({ ssoName: sso.name, oidc: sso.oidc, local: localSignIn(), message: auth.denied, base: sso.publicUrl }), url.pathname.startsWith('/api/') ? {} : { 'Content-Type': 'text/html; charset=utf-8' }); - if (auth && auth.retryAfter) { - const s = Math.ceil(auth.retryAfter / 1000); - res.writeHead(429, { ...SEC_HEADERS, 'Retry-After': String(s), 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' }); - return res.end(`Too many failed sign-ins. Try again in ${s >= 120 ? Math.ceil(s / 60) + ' minutes' : s + ' seconds'}.`); - } if (!auth) { - // with OpenID Connect, a page asks for the sign-in page, and the API answers 401 without a password prompt - // …except "/?local": the browser's password prompt for a local account, asked at the root so that - // the browser then sends the password with every request of the app - if (sso.oidc && url.pathname === '/' && url.searchParams.has('local') && localSignIn()) - return send(res, 401, loginPage({ ssoName: sso.name, oidc: sso.oidc, local: true, message: 'Local sign-in cancelled or refused.', base: sso.publicUrl }), { 'Content-Type': 'text/html; charset=utf-8', 'WWW-Authenticate': 'Basic realm="Strata", charset="UTF-8"' }); - if (sso.oidc && !url.pathname.startsWith('/api/') && (req.method === 'GET' || req.method === 'HEAD')) return send(res, 302, '', { Location: `${sso.publicUrl}/auth/login` }); - res.writeHead(401, { ...SEC_HEADERS, ...(sso.oidc || !localSignIn() ? {} : { 'WWW-Authenticate': 'Basic realm="Strata", charset="UTF-8"' }), 'Content-Type': 'text/plain; charset=utf-8' }); - return res.end('Authentication required'); + // a page goes to the sign-in page; the API answers 401 (and says why when a script still sends HTTP Basic) + if (!url.pathname.startsWith('/api/') && (req.method === 'GET' || req.method === 'HEAD')) return send(res, 302, '', { Location: `${sso.publicUrl}/auth/login` }); + const basic = String(req.headers.authorization || '').startsWith('Basic '); + return fail(res, 401, basic ? 'HTTP Basic sign-in was removed in Strata 0.11: use an API token (Authorization: Bearer stk_…), created in Account › API tokens' : 'Authentication required'); } if (url.pathname.startsWith('/api/')) await api(req, res, url.pathname.slice(5).split('/').filter(Boolean).map(decodeURIComponent), url, auth); else if (req.method === 'GET' || req.method === 'HEAD') await serveStatic(req, res, url.pathname); diff --git a/server/sso.js b/server/sso.js index 3c43149..6a118ea 100644 --- a/server/sso.js +++ b/server/sso.js @@ -107,7 +107,19 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU const id = readCookie(req, SESSION); const s = id && sessions.get(id); if (!s || s.exp < Date.now()) { if (s) sessions.delete(id); return null; } - return { user: s.user, role: s.role, via: 'sso', sessionId: id }; + return { user: s.user, role: s.role, via: s.via || 'sso', sessionId: id, since: s.since }; + } + // A session for a local account signed in with the form (server.js checks the password) + function createSession({ user, role, via }) { + const sid = rand(32); + sessions.set(sid, { user, role, via, since: Date.now(), exp: Date.now() + C.sessionMs }); + return { sessionId: sid, setCookie: cookie(SESSION, sid, Math.round(C.sessionMs / 1000)) }; + } + // Ends the sessions of one person (password changed, account disabled or deleted), but the one given + function endUserSessions(user, exceptId) { + let n = 0; + for (const [k, s] of sessions) if (s.user === user && k !== exceptId) { sessions.delete(k); n++; } + return n; } /* ---- trusted proxy headers ---- */ @@ -208,7 +220,7 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU const role = roleOf(groups); if (!role) throw Object.assign(new Error(`${user} is in no group that gives access to Strata. Ask an administrator.`), { denied: true }); const sid = rand(32); - sessions.set(sid, { user, role, exp: Date.now() + C.sessionMs, idToken: tok.id_token }); + sessions.set(sid, { user, role, via: 'sso', since: Date.now(), exp: Date.now() + C.sessionMs, idToken: tok.id_token }); log(`SSO sign-in: ${JSON.stringify(user)} as ${role}`); return { user, role, setCookie: [cookie(SESSION, sid, Math.round(C.sessionMs / 1000)), cookie(STATE, '', 0)] }; } @@ -216,7 +228,7 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU const s = sessions.get(sessionId); sessions.delete(sessionId); let location = `${C.publicUrl}/auth/login?out=1`; - if (s && oidc) { + if (s && s.via === 'sso' && oidc) { try { const d = await discovery(); if (d.end_session_endpoint) { const u = new URL(d.end_session_endpoint); u.searchParams.set('id_token_hint', s.idToken); u.searchParams.set('post_logout_redirect_uri', location); location = u.href; } @@ -224,8 +236,8 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU } return { location, setCookie: cookie(SESSION, '', 0) }; } - // An administrator ends every single sign-on session (after a group change, or someone leaving) - function endSessions(by) { const n = sessions.size; sessions.clear(); log(`${n} single sign-on session(s) ended by ${JSON.stringify(by)}`); return n; } + // An administrator ends every session (after a group change, or someone leaving), but their own + function endSessions(by, exceptId) { let n = 0; for (const k of [...sessions.keys()]) if (k !== exceptId) { sessions.delete(k); n++; } log(`${n} session(s) ended by ${JSON.stringify(by)}`); return n; } /* ---- API tokens (DATA_DIR/tokens.json, mode 600) ---- */ const FILE = path.join(dataDir, 'tokens.json'); @@ -271,7 +283,7 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU oidc, header, name: C.name, localLogin: C.localLogin, publicUrl: C.publicUrl, // anyone could sign in some way other than a local account enabled: () => oidc || header || Object.keys(tokens).length > 0, - fromSession, fromHeaders, fromToken, start, callback, logout, endSessions, sessionCount: () => sessions.size, + fromSession, fromHeaders, fromToken, start, callback, logout, endSessions, endUserSessions, createSession, sessionCount: () => sessions.size, listTokens: () => Object.entries(tokens).map(([id, t]) => publicToken(id, t)).sort((a, b) => a.name.localeCompare(b.name)), createToken, deleteToken, }; @@ -280,19 +292,23 @@ export function createSso({ config, dataDir, log, writeJson, peerAddress, localU /* ---------------- Sign-in page ---------------- */ const escHtml = s => String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); // base: PUBLIC_URL, or '' (links from the root) -export function loginPage({ ssoName, oidc, local, message, out, base = '' }) { +export function loginPage({ ssoName, oidc, local, message, out, base = '', user = '' }) { return `Sign in · Strata +a.btn,button.btn{display:block;width:100%;box-sizing:border-box;font:inherit;cursor:pointer;border:0;text-align:center;padding:11px 14px;border-radius:3px;text-decoration:none;font-weight:600;margin-top:10px} +a.primary,button.primary{background:var(--accent);color:var(--accent-fg)}a.second,button.second{background:transparent;border:1px solid var(--line);color:var(--fg)} +label{display:block;font-size:13px;font-weight:600;margin:12px 0 4px}input{display:block;width:100%;box-sizing:border-box;padding:9px 10px;font:inherit;color:var(--fg);background:transparent;border:1px solid var(--line);border-radius:3px} +.or{text-align:center;margin:16px 0 0;font-size:13px}.msg{color:var(--bad);border-left:3px solid var(--bad);padding:6px 10px;margin:0 0 16px}.ok{color:var(--muted)}

Strata

Map of the information systems

${message ? `` : out ? '

You are signed out.

' : ''} ${oidc ? `Sign in with ${escHtml(ssoName)}` : ''} -${local ? `Sign in with a local account` : ''} +${local ? `${oidc ? '

or with a local account

' : ''}
+ + +
` : ''}
`; } diff --git a/tests/api.test.mjs b/tests/api.test.mjs index fba0e86..7e579d0 100644 --- a/tests/api.test.mjs +++ b/tests/api.test.mjs @@ -8,6 +8,7 @@ import { request } from 'node:http'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const dataDir = mkdtempSync(join(tmpdir(), 'strata-api-')); @@ -34,10 +35,10 @@ after(async () => { }); class Client { - constructor(user, password = PASS[user]) { this.auth = user ? 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64') : ''; } + constructor(user, password = PASS[user]) { this.user = user; this.password = password; } // headers: extra headers; a null value removes a default one (used by the CSRF test) async req(method, path, body, headers = {}) { - const h = { ...(body !== undefined ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}), ...(this.auth ? { Authorization: this.auth } : {}), ...headers }; + const h = { ...(body !== undefined ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}), ...(await auth(base, this.user, this.password)), ...headers }; for (const k of Object.keys(h)) if (h[k] === null) delete h[k]; const res = await fetch(base + path, { method, @@ -73,7 +74,14 @@ test('a malformed percent-encoding in the path gets 400, not a server error', as test('auth: no credentials, wrong password and unknown user are refused', async () => { const none = await new Client().get('/api/state'); assert.equal(none.status, 401); - assert.match(none.headers.get('www-authenticate'), /^Basic /); + assert.equal(none.headers.get('www-authenticate'), null); + // a page sends the browser to the sign-in form; HTTP Basic is refused, with what to use instead + const page = await fetch(`${base}/`, { redirect: 'manual' }); + assert.equal(page.status, 302); + assert.equal(page.headers.get('location'), '/auth/login'); + const basic = await fetch(`${base}/api/state`, { headers: { Authorization: 'Basic ' + Buffer.from(`admin:${PASS.admin}`).toString('base64') } }); + assert.equal(basic.status, 401); + assert.match((await basic.json()).error, /API token/); assert.equal((await new Client('admin', 'wrong-password').get('/api/state')).status, 401); assert.equal((await new Client('nobody', 'whatever-123').get('/api/state')).status, 401); const me = await as('alice').get('/api/me'); @@ -242,8 +250,9 @@ test('import: replaces the map, validates ids, and enforces MAX_IMPORT_MB', asyn }); test('static files: path traversal cannot reach the server code', async () => { + const vicAuth = await auth(base, 'vic', PASS.vic); const raw = p => new Promise((res, rej) => { - request({ host: '127.0.0.1', port, path: p, headers: { Authorization: as('vic').auth } }, r => { let b = ''; r.on('data', c => (b += c)); r.on('end', () => res({ status: r.statusCode, body: b })); }).on('error', rej).end(); + request({ host: '127.0.0.1', port, path: p, headers: vicAuth }, r => { let b = ''; r.on('data', c => (b += c)); r.on('end', () => res({ status: r.statusCode, body: b })); }).on('error', rej).end(); }); for (const p of ['/../server/server.js', '/%2e%2e/server/server.js', '/..%2fserver%2fserver.js', '/%2e%2e%2f%2e%2e%2fpackage.json']) { const r = await raw(p); @@ -316,7 +325,7 @@ test('reserved keys: the users.mjs command line refuses them too', async () => { // (": ping") are skipped async function openEvents(user) { const ctrl = new AbortController(); - const res = await fetch(base + '/api/events', { headers: user ? { Authorization: as(user).auth } : {}, signal: ctrl.signal }); + const res = await fetch(base + '/api/events', { headers: await auth(base, user, PASS[user]), signal: ctrl.signal }); if (res.status !== 200) { await res.body.cancel(); return { status: res.status }; } const reader = res.body.getReader(), dec = new TextDecoder(); let buf = ''; diff --git a/tests/e2e.mjs b/tests/e2e.mjs index 45d083d..ce35198 100644 --- a/tests/e2e.mjs +++ b/tests/e2e.mjs @@ -10,6 +10,7 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; import { chromium } from 'playwright'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -31,7 +32,7 @@ before(async () => { assert.equal(r.status, 200); } // WebGL without a GPU: SwiftShader - browser = await chromium.launch({ args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] }); + browser = await chromium.launch({ ...(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}), args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] }); }); after(async () => { if (browser) await browser.close(); @@ -39,16 +40,23 @@ after(async () => { rmSync(dataDir, { recursive: true, force: true, maxRetries: 5 }); }); -function api(user, method, path, body) { +async function api(user, method, path, body) { return fetch(base + path, { method, - headers: { Authorization: 'Basic ' + Buffer.from(`${user}:${PASS[user]}`).toString('base64'), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, + headers: { ...(await auth(base, user, PASS[user])), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, body: body ? JSON.stringify(body) : undefined, }).then(async r => ({ status: r.status, data: await r.json().catch(() => null) })); } // A signed-in page; every page error and console error is recorded and fails the test async function signIn(user) { - const ctx = await browser.newContext({ viewport: { width: 1400, height: 900 }, httpCredentials: { username: user, password: PASS[user] } }); + const ctx = await browser.newContext({ viewport: { width: 1400, height: 900 } }); + // the sign-in form, as a person would fill it + const login = await ctx.newPage(); + await login.goto(base); + await login.fill('#u', user); + await login.fill('#p', PASS[user]); + await Promise.all([login.waitForURL(u => !String(u).includes('/auth/')), login.click('button[type=submit]')]); + await login.close(); const page = await ctx.newPage(); page.on('pageerror', e => errors.push(`${user}: ${e.message}`)); page.on('console', m => { if (m.type() === 'error') errors.push(`${user} console: ${m.text()}`); }); diff --git a/tests/hardening.test.mjs b/tests/hardening.test.mjs index 31bf2fd..94d4c6d 100644 --- a/tests/hardening.test.mjs +++ b/tests/hardening.test.mjs @@ -8,6 +8,7 @@ import { mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; import { parseXml } from '../server/connectors/sdk.js'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -35,7 +36,7 @@ test('data files are readable by the server user only, including those of older await new Promise((res, rej) => { proc.stdout.on('data', d => { if (String(d).includes('listening')) res(); }); proc.on('exit', c => rej(new Error('exit ' + c))); }); const r = await fetch(`http://127.0.0.1:${port}/api/si/s1`, { method: 'PUT', body: JSON.stringify({ name: 'IS one' }), - headers: { Authorization: 'Basic ' + Buffer.from(`admin:${pass}`).toString('base64'), 'Content-Type': 'application/json', 'X-Requested-With': 'strata' }, + headers: { ...(await auth(`http://127.0.0.1:${port}`, 'admin', pass)), 'Content-Type': 'application/json', 'X-Requested-With': 'strata' }, }); assert.equal(r.status, 200); await new Promise(res => setTimeout(res, 200)); diff --git a/tests/modules.test.mjs b/tests/modules.test.mjs index 8151989..e531be0 100644 --- a/tests/modules.test.mjs +++ b/tests/modules.test.mjs @@ -11,6 +11,7 @@ import zlib from 'node:zlib'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; import { readTgz, writeTgz, sha256 } from '../server/connectors/store.js'; import { checkManifest, compareVersions } from '../server/connectors/modules.js'; @@ -109,7 +110,7 @@ after(async () => { async function api(user, method, path, body) { const r = await fetch(base + path, { method, - headers: { Authorization: 'Basic ' + Buffer.from(`${user}:${PASS[user]}`).toString('base64'), ...(body !== undefined ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, + headers: { ...(await auth(base, user, PASS[user])), ...(body !== undefined ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, body: body !== undefined ? JSON.stringify(body) : undefined, }); return { status: r.status, data: await r.json().catch(() => null) }; diff --git a/tests/muster.test.mjs b/tests/muster.test.mjs index 821ccef..36d9598 100644 --- a/tests/muster.test.mjs +++ b/tests/muster.test.mjs @@ -8,6 +8,7 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const dataDir = mkdtempSync(join(tmpdir(), 'strata-muster-')); @@ -38,8 +39,8 @@ function fakeMuster(req, res) { json(404, { error: 'Not found' }); }); } -const as = user => (method, path, body) => fetch(base + path, { - method, headers: { Authorization: 'Basic ' + Buffer.from(`${user}:${PASS[user]}`).toString('base64'), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, +const as = user => async (method, path, body) => fetch(base + path, { + method, headers: { ...(await auth(base, user, PASS[user])), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, body: body ? JSON.stringify(body) : undefined, }).then(async r => ({ status: r.status, data: await r.json().catch(() => null) })); diff --git a/tests/readonly.test.mjs b/tests/readonly.test.mjs index 26d1d4b..6b4110c 100644 --- a/tests/readonly.test.mjs +++ b/tests/readonly.test.mjs @@ -8,6 +8,7 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -21,7 +22,7 @@ test('READ_ONLY=true: an admin can read, and every write is refused with 403', a const call = async (method, path, body) => { const r = await fetch(`http://127.0.0.1:${port}${path}`, { method, - headers: { Authorization: 'Basic ' + Buffer.from(`admin:${pass}`).toString('base64'), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, + headers: { ...(await auth(`http://127.0.0.1:${port}`, 'admin', pass)), ...(body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}) }, body: body ? JSON.stringify(body) : undefined, }); return { status: r.status, data: await r.json().catch(() => null) }; diff --git a/tests/secrets.test.mjs b/tests/secrets.test.mjs index 615b819..332c913 100644 --- a/tests/secrets.test.mjs +++ b/tests/secrets.test.mjs @@ -8,6 +8,7 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -27,7 +28,7 @@ function start(extra) { proc.on('exit', code => { rmSync(dataDir, { recursive: true, force: true, maxRetries: 5 }); rej(Object.assign(new Error(`exit ${code}`), { code, err })); }); }); } -const status = (port, user, pass) => fetch(`http://127.0.0.1:${port}/api/state`, { headers: { Authorization: 'Basic ' + Buffer.from(`${user}:${pass}`).toString('base64') } }).then(r => r.status); +const status = async (port, user, pass) => (await fetch(`http://127.0.0.1:${port}/api/state`, { headers: await auth(`http://127.0.0.1:${port}`, user, pass) })).status; test('EDIT_PASS_FILE and VIEW_PASS_FILE: the passwords come from the files, which win over the variables', async () => { const dir = mkdtempSync(join(tmpdir(), 'strata-cred-')); diff --git a/tests/session.mjs b/tests/session.mjs new file mode 100644 index 0000000..1eab594 --- /dev/null +++ b/tests/session.mjs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +// Signs a local account in with the sign-in form (POST /auth/login) and keeps its session cookie, per +// server, account and password: the tests send it as the browser does. HTTP Basic is gone since 0.11. +const cache = new Map(); +// base: http://127.0.0.1:port (with or without a trailing slash); extra: headers of the sign-in request +export async function login(base, user, pass, extra = {}) { + const r = await fetch(new URL('/auth/login', base), { + method: 'POST', redirect: 'manual', + headers: { 'Content-Type': 'application/json', ...extra }, + body: JSON.stringify({ user, password: pass }), + }); + await r.arrayBuffer(); + const set = r.headers.get('set-cookie') || ''; + return { status: r.status, retryAfter: r.headers.get('retry-after'), cookie: r.ok ? set.split(';')[0] : '' }; +} +// The headers that sign this account in ({} when the password is wrong: the request then gets a 401) +export async function auth(base, user, pass, extra) { + if (!user) return {}; + const key = `${base}|${user}|${pass}`; + if (!cache.has(key)) { + const r = await login(base, user, pass, extra); + if (!r.cookie) return {}; + cache.set(key, r.cookie); + } + return { Cookie: cache.get(key) }; +} +export const forget = () => cache.clear(); diff --git a/tests/signin.test.mjs b/tests/signin.test.mjs index 3ab084d..eb69ead 100644 --- a/tests/signin.test.mjs +++ b/tests/signin.test.mjs @@ -8,6 +8,7 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth, login } from './session.mjs'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const ADMIN = 'admin-pass-123'; @@ -28,18 +29,19 @@ function start(extra) { proc.on('exit', code => rej(new Error(`exit ${code}`))); }); } -// GET /api/state as user:pass, from a client address given by X-Forwarded-For (only believed behind a trusted proxy) +// Signs in with the form as user:pass, from a client address given by X-Forwarded-For (only believed +// behind a trusted proxy), then reads /api/state with the session: 200, 401 (wrong) or 429 (locked) async function get(port, user, pass, from) { - const headers = { Authorization: 'Basic ' + Buffer.from(`${user}:${pass}`).toString('base64') }; - if (from) headers['X-Forwarded-For'] = from; - const r = await fetch(`http://127.0.0.1:${port}/api/state`, { headers }); - await r.arrayBuffer(); - return { status: r.status, retryAfter: r.headers.get('retry-after') }; + const r = await login(`http://127.0.0.1:${port}`, user, pass, from ? { 'X-Forwarded-For': from } : {}); + if (!r.cookie) return { status: r.status, retryAfter: r.retryAfter }; + const s = await fetch(`http://127.0.0.1:${port}/api/state`, { headers: { Cookie: r.cookie } }); + await s.arrayBuffer(); + return { status: s.status }; } async function addUser(port, name, password) { const r = await fetch(`http://127.0.0.1:${port}/api/users/${name}`, { method: 'PUT', - headers: { Authorization: 'Basic ' + Buffer.from(`admin:${ADMIN}`).toString('base64'), 'Content-Type': 'application/json', 'X-Requested-With': 'strata' }, + headers: { ...(await auth(`http://127.0.0.1:${port}`, 'admin', ADMIN)), 'Content-Type': 'application/json', 'X-Requested-With': 'strata' }, body: JSON.stringify({ role: 'viewer', password }), }); assert.equal(r.status, 200); @@ -91,14 +93,15 @@ test('a client address is locked after LOGIN_MAX_FAILS_PER_ADDRESS, read from X- } finally { await t.stop(); } }); -test('a browser already signed in keeps working from its address while the account is locked', async () => { +test('a browser already signed in keeps its session while the account is locked', async () => { const s = await start({ LOGIN_MAX_FAILS: '3', LOGIN_MAX_FAILS_PER_ADDRESS: '100', TRUST_PROXY: '127.0.0.1' }); try { - assert.equal((await get(s.port, 'admin', ADMIN, '192.0.2.10')).status, 200); + const signed = await login(`http://127.0.0.1:${s.port}`, 'admin', ADMIN, { 'X-Forwarded-For': '192.0.2.10' }); + assert.equal(signed.status, 200); // someone else guesses the admin password from elsewhere for (const p of ['guess-1', 'guess-2', 'guess-3']) assert.equal((await get(s.port, 'admin', p, '192.0.2.66')).status, 401); - // the signed-in browser goes on; the right password from any other address waits - assert.equal((await get(s.port, 'admin', ADMIN, '192.0.2.10')).status, 200); + // the signed-in browser goes on with its session; a new sign-in, from any address, waits + assert.equal((await fetch(`http://127.0.0.1:${s.port}/api/state`, { headers: { Cookie: signed.cookie, 'X-Forwarded-For': '192.0.2.10' } })).status, 200); assert.equal((await get(s.port, 'admin', ADMIN, '192.0.2.66')).status, 429); assert.equal((await get(s.port, 'admin', ADMIN, '192.0.2.11')).status, 429); } finally { await s.stop(); } diff --git a/tests/sso.test.mjs b/tests/sso.test.mjs index 0394caf..4914690 100644 --- a/tests/sso.test.mjs +++ b/tests/sso.test.mjs @@ -10,6 +10,7 @@ import crypto from 'node:crypto'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { auth } from './session.mjs'; import { verifyJwt } from '../server/sso.js'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); @@ -98,7 +99,8 @@ async function signIn() { return { status: r.status, location: r.headers.get('location'), cookie: cookies(), text: r.status === 302 ? '' : await r.text() }; } const api = (path, cookie, init = {}) => fetch(base + path, { ...init, headers: { ...(cookie ? { Cookie: cookie } : {}), ...(init.body ? { 'Content-Type': 'application/json', 'X-Requested-With': 'strata' } : {}), ...init.headers } }); -const basic = (u, p) => ({ Authorization: 'Basic ' + Buffer.from(`${u}:${p}`).toString('base64') }); +// a local account, signed in with the sign-in form: its session cookie +const basic = (u, p) => auth(base, u, p); test('a page without a session goes to the sign-in page; the API answers 401 without a password prompt', async () => { const r = await fetch(`${base}/`, { redirect: 'manual' }); @@ -106,16 +108,26 @@ test('a page without a session goes to the sign-in page; the API answers 401 wit assert.equal(r.headers.get('location'), `${base}/auth/login`); const login = await (await fetch(`${base}/auth/login`)).text(); assert.match(login, /Sign in with Test IdP/); - assert.match(login, /Sign in with a local account/); + assert.match(login, /or with a local account/); const a = await fetch(`${base}/api/me`); assert.equal(a.status, 401); assert.equal(a.headers.get('www-authenticate'), null); - // "Sign in with a local account": the password prompt is asked at the root, so the browser then sends it everywhere - assert.ok(login.includes(`href="${base}/?local"`) && login.includes(`href="${base}/auth/oidc/start"`)); - const l = await fetch(`${base}/?local`, { redirect: 'manual' }); - assert.equal(l.status, 401); - assert.match(l.headers.get('www-authenticate'), /^Basic/); - assert.equal((await fetch(`${base}/?local`, { headers: basic('admin', 'admin-pass-123') })).status, 200); + // local accounts sign in with the form of the same page, which gives a session cookie + assert.ok(login.includes(`action="${base}/auth/login"`) && login.includes(`href="${base}/auth/oidc/start"`)); + const form = await fetch(`${base}/auth/login`, { method: 'POST', redirect: 'manual', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'user=admin&password=admin-pass-123' }); + assert.equal(form.status, 303); + assert.equal(form.headers.get('location'), `${base}/`); + const cookie = form.headers.get('set-cookie').split(';')[0]; + assert.equal((await fetch(`${base}/`, { headers: { Cookie: cookie } })).status, 200); + const bad = await fetch(`${base}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'user=admin&password=nope' }); + assert.equal(bad.status, 401); + assert.match(await bad.text(), /Wrong user name or password/); + // another site cannot sign a visitor in + assert.equal((await fetch(`${base}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Origin: 'https://evil.example' }, body: 'user=admin&password=admin-pass-123' })).status, 403); + // a local session signs out too + const out = await (await api('/auth/logout', cookie, { method: 'POST', body: '{}' })).json(); + assert.equal(out.location, `${base}/auth/login?out=1`); + assert.equal((await api('/api/me', cookie)).status, 401); }); test('OpenID Connect: groups give the role, the session cookie signs in, sign-out ends it', async () => { @@ -170,11 +182,11 @@ test('verifyJwt refuses "none" and unknown algorithms', () => { }); test('LOCAL_LOGIN=admins: the local admin keeps a way in, other local accounts do not', async () => { - assert.equal((await api('/api/users/bob', null, { method: 'PUT', headers: basic('admin', 'admin-pass-123'), body: JSON.stringify({ role: 'approver', password: 'bob-pass-12345' }) })).status, 200); - assert.equal((await api('/api/me', null, { headers: basic('admin', 'admin-pass-123') })).status, 200); + assert.equal((await api('/api/users/bob', null, { method: 'PUT', headers: await basic('admin', 'admin-pass-123'), body: JSON.stringify({ role: 'approver', password: 'bob-pass-12345' }) })).status, 200); + assert.equal((await api('/api/me', null, { headers: await basic('admin', 'admin-pass-123') })).status, 200); // refused like a wrong password: the answer says nothing of bob's password - assert.equal((await api('/api/me', null, { headers: basic('bob', 'bob-pass-12345') })).status, 401); - assert.equal((await api('/api/me', null, { headers: basic('bob', 'wrong-password') })).status, 401); + assert.equal((await api('/api/me', null, { headers: await basic('bob', 'bob-pass-12345') })).status, 401); + assert.equal((await api('/api/me', null, { headers: await basic('bob', 'wrong-password') })).status, 401); }); test('a provider name that is also a local account, or an unverified e-mail, is refused', async () => { @@ -200,7 +212,7 @@ test('a sign-in finishes once; an admin can end every session', async () => { assert.equal(again.status, 400); const s = await signIn(); assert.equal((await api('/api/me', s.cookie)).status, 200); - const end = await (await api('/api/sessions', null, { method: 'DELETE', headers: basic('admin', 'admin-pass-123'), body: '{}' })).json(); + const end = await (await api('/api/sessions', null, { method: 'DELETE', headers: await basic('admin', 'admin-pass-123'), body: '{}' })).json(); assert.ok(end.ended >= 1); assert.equal((await api('/api/me', s.cookie)).status, 401); }); @@ -214,7 +226,7 @@ test('headers need SSO_HEADER_TRUST with exact addresses', async () => { }); test('API tokens: created by an admin, shown once, used as Bearer, revoked', async () => { - const admin = basic('admin', 'admin-pass-123'); + const admin = await basic('admin', 'admin-pass-123'); const c = await (await api('/api/tokens', null, { method: 'POST', headers: admin, body: JSON.stringify({ name: 'ci', role: 'contributor', days: 7 }) })).json(); assert.match(c.token, /^stk_[0-9a-f]{16}_/); const bearer = { Authorization: `Bearer ${c.token}` }; From f9f89e010bc56a3a17ac4e108cbc1e59174a6b0f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:23:57 +0000 Subject: [PATCH 02/16] VLAN list: show all or none per zone, hide empty VLANs; loading bar; movable Internet; Arrange VLANs on a grid - All / None per zone and for the whole IS in the plate list - Hide empty VLANs: plates without an asset leave the 3D model and the 2D diagram - A loading bar for requests that last, with progress for batches of writes - The Internet cloud can be dragged (Arrange plates in 3D, the 2D diagram); its place is saved on the IS - Arrange VLANs puts each zone on rows and columns (a set number per row, or automatic), the VLANs that talk side by side Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- public/analysis.js | 32 +++++++++- public/app.css | 12 ++++ public/app.js | 134 ++++++++++++++++++++++++++++++++-------- public/index.html | 12 +++- tests/analysis.test.mjs | 31 ++++++++++ 5 files changed, 194 insertions(+), 27 deletions(-) diff --git a/public/analysis.js b/public/analysis.js index bcdb07e..c2d2d32 100644 --- a/public/analysis.js +++ b/public/analysis.js @@ -274,6 +274,36 @@ function chokePoints(site,byTarget){ return [...pts.values()].sort((x,y)=>y.every.length-x.every.length||y.paths-x.paths||byName(x.id,y.id)); } -root.StrataAnalysis={riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, +/* ---- Arranging the plates of a level on rows and columns ---- */ +// plates: [{id, w, D}] in their current order; weight(a, b): how much two VLANs talk (symmetric, >= 0); +// cols: the most plates per row (0 or less: about the square root of their number, a little wider than deep); +// gap: space between plates. Filled row by row from the back, each cell takes the plate with the most +// traffic to the plates already left of it and behind it, then the busiest plate, then the current order: +// VLANs that talk sit side by side, and VLANs with no flow end up last. Returns {id: [x, z]} (plate +// centres, the grid centred on 0) and the number of columns used. +function arrangeGrid(plates,weight,cols,gap=2){ + const n=plates.length;if(!n)return {places:{},cols:0,rows:0}; + const C=Math.max(1,Math.min(n,cols>0?Math.round(cols):Math.ceil(Math.sqrt(n*1.6)))); + const w=(a,b)=>{const v=+weight(a,b);return v>0&&isFinite(v)?v:0;}; + const total={};plates.forEach(p=>{total[p.id]=plates.reduce((t,q)=>t+(q.id!==p.id?w(p.id,q.id):0),0);}); + const left=plates.map((p,i)=>({p,i})),grid=[]; + for(let k=0;k0)nb.push(grid[k-1]);if(k>=C)nb.push(grid[k-C]); + let best=0,bs=null; + left.forEach((e,j)=>{const s=[nb.reduce((t,q)=>t+w(e.p.id,q.id),0),total[e.p.id],-e.i]; + if(!bs||s[0]>bs[0]||(s[0]===bs[0]&&(s[1]>bs[1]||(s[1]===bs[1]&&s[2]>bs[2])))){bs=s;best=j;}}); + grid.push(left.splice(best,1)[0].p); + } + const R=Math.ceil(n/C),colW=Array(C).fill(0),rowD=Array(R).fill(0); + grid.forEach((p,k)=>{colW[k%C]=Math.max(colW[k%C],p.w);rowD[Math.floor(k/C)]=Math.max(rowD[Math.floor(k/C)],p.D);}); + const sum=a=>a.reduce((t,x)=>t+x,0),W=sum(colW)+gap*(C-1),Dt=sum(rowD)+gap*(R-1); + const places={}; + grid.forEach((p,k)=>{const c=k%C,r=Math.floor(k/C); + const x=-W/2+sum(colW.slice(0,c))+gap*c+colW[c]/2,z=-Dt/2+sum(rowD.slice(0,r))+gap*r+rowD[r]/2; + places[p.id]=[Math.round(x*2)/2,Math.round(z*2)/2];}); + return {places,cols:C,rows:R,order:grid.map(p=>p.id)}; +} + +root.StrataAnalysis={arrangeGrid,riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, stepOdds,blastRadius,attackPaths,chokePoints,outside}; })(typeof globalThis!=='undefined'?globalThis:this); diff --git a/public/app.css b/public/app.css index 3ef24ed..a031a45 100644 --- a/public/app.css +++ b/public/app.css @@ -711,3 +711,15 @@ body.has-dash{overflow:hidden} @keyframes selhalo{0%,100%{opacity:.1}50%{opacity:.32}} @media (prefers-reduced-motion:reduce){#flatSvg .selhalo{animation:none}} .ibtn.signout{border-color:var(--line);color:var(--fg);margin-left:6px;min-height:22px;padding:2px 8px} +.vtools{display:flex;align-items:center;gap:4px;margin:0 0 4px}.vtools .fl{margin-right:auto} +.zone-h .zsel{float:right;display:inline-flex;gap:2px}.zone-h .zsel .ibtn{min-height:22px;padding:1px 6px} +.vrow.off{opacity:.45} +/* Loading bar for long operations */ +#busy{position:fixed;top:0;left:0;right:0;height:3px;z-index:1000;overflow:hidden;background:color-mix(in srgb,var(--fg) 10%,transparent);pointer-events:none} +#busy i{position:absolute;top:0;bottom:0;left:0;width:30%;background:var(--x-net,#2f6fdf);animation:busy 1.1s ease-in-out infinite} +#busy.det i{animation:none;transition:width .2s} +#busy span{position:fixed;top:6px;left:50%;transform:translateX(-50%);font-size:11px;padding:2px 8px;border-radius:3px;background:var(--panel,var(--bg));color:var(--fg);border:1px solid var(--line);white-space:nowrap} +#busy span:empty{display:none} +@keyframes busy{0%{left:-30%}100%{left:100%}} +@media (prefers-reduced-motion:reduce){#busy i{animation:none;width:100%;opacity:.5}} +.arrRow{display:flex;gap:6px;align-items:stretch}.arrRow select{width:auto;flex:0 0 76px;padding:6px 8px}.arrRow .btn{flex:1} diff --git a/public/app.js b/public/app.js index 1e85e46..02ec5de 100644 --- a/public/app.js +++ b/public/app.js @@ -382,9 +382,10 @@ const Store={ }, async pull(){ try{ - const r=await fetch('api/state',{cache:'no-store',credentials:'same-origin'}); - if(!r.ok)throw new Error('HTTP '+r.status); - const s=await r.json(); + Busy.start('Loading the map');let s; + try{const r=await fetch('api/state',{cache:'no-store',credentials:'same-origin'}); + if(!r.ok)throw new Error('HTTP '+r.status); + s=await r.json();}finally{Busy.end();} liveRaw={si:s.si||{},assets:s.assets||{},wan:Array.isArray(s.wan)?s.wan:[],services:Array.isArray(s.services)?s.services:[],risk:s.risk&&typeof s.risk==='object'?s.risk:{}};this.rev=s.rev||0; this.loaded=true;this.applyView(this.bulk?null:0); }catch(e){toast('Could not read the saved data. Check that the server is running, then reload.',true);} @@ -403,7 +404,8 @@ const Store={ async api(method,url,body){ const h={'content-type':'application/json','x-requested-with':'strata'}; if(this.batch){h['x-batch']=this.batch.id;h['x-batch-label']=encodeURIComponent(this.batch.label);} - const r=await fetch(url,{method,credentials:'same-origin',headers:h,body:body===undefined?undefined:JSON.stringify(body)}); + Busy.start();let r; + try{r=await fetch(url,{method,credentials:'same-origin',headers:h,body:body===undefined?undefined:JSON.stringify(body)});}finally{Busy.end();} let j=null;try{j=await r.json();}catch(e){} // a session that has ended (single sign-on): back to the sign-in page if(r.status===401&&this.me&&this.me.signOut){location.href='auth/login';} @@ -459,12 +461,25 @@ const Store={ putRisk(doc){return this.write('risk',null,doc);}, async many(tasks,label){ this.bulk=true;this.batch={id:'b'+Date.now().toString(36)+Math.random().toString(36).slice(2,6),label};let done=0;const total=tasks.length; - toast(`${label}: 0 / ${total}`,false,true); - try{for(const t of tasks){await t();done++;if(done%5===0||done===total)toast(`${label}: ${done} / ${total}`,false,true);}toast(`${label}: done.`);} + toast(`${label}: 0 / ${total}`,false,true);Busy.start(label);Busy.progress(0,total,label); + try{for(const t of tasks){await t();done++;Busy.progress(done,total);if(done%5===0||done===total)toast(`${label}: ${done} / ${total}`,false,true);}toast(`${label}: done.`);} catch(e){toast(`${label} stopped after ${done} items.`,true);} - finally{this.bulk=false;this.batch=null;this.applyView(0);if(this.view==='draft'){clearTimeout(this._dt);this.saveDraft();}} + finally{Busy.end();this.bulk=false;this.batch=null;this.applyView(0);if(this.view==='draft'){clearTimeout(this._dt);this.saveDraft();}} } }; +// The loading bar at the top: any request still running after 300 ms shows it (moving stripe); a batch +// of writes or an import shows how far it got. Busy.track(promise, label) wraps any long task. +const Busy={n:0,timer:null,label:'',done:0,total:0, + el:()=>document.getElementById('busy'), + start(label){this.n++;if(label)this.label=label;if(this.n===1&&!this.timer)this.timer=setTimeout(()=>{this.timer=null;if(this.n)this.paint();},300);}, + end(){this.n=Math.max(0,this.n-1);if(this.n)return;clearTimeout(this.timer);this.timer=null;this.label='';this.done=this.total=0;const e=this.el();if(e)e.hidden=true;}, + progress(done,total,label){this.done=done;this.total=total;if(label)this.label=label;if(this.n&&!this.timer)this.paint();}, + paint(){const e=this.el();if(!e)return;e.hidden=false;const det=this.total>0,pc=det?Math.round(100*this.done/this.total):0; + e.classList.toggle('det',det);e.firstElementChild.style.width=det?pc+'%':''; + if(det)e.setAttribute('aria-valuenow',pc);else e.removeAttribute('aria-valuenow'); + e.lastElementChild.textContent=this.label?(det?`${this.label} · ${this.done} / ${this.total}`:`${this.label}…`):'';}, + async track(p,label){this.start(label);try{return await (typeof p==='function'?p():p);}finally{this.end();}} +}; // Sign out (local account or single sign-on), from the header or the account panel async function signOut(){try{const j=await Store.api('POST','auth/logout',{});location.href=j.location||'auth/login?out=1';}catch(err){toast(err.message,true);}} document.addEventListener('click',e=>{if(e.target.closest('[data-signout]')){e.preventDefault();signOut();}}); @@ -476,9 +491,10 @@ async function replaceAll(data,label){ if(Store.mode==='local'){liveRaw=next;Store.saveLocal();Store.applyView(0);toast(`${label}: done.`);return;} toast(`${label}…`,false,true); Store.batch={id:'imp'+Date.now().toString(36),label}; + Busy.start(label); try{await Store.api('POST','api/import',next);liveRaw=next;Store.applyView(0);toast(`${label}: done.`);} catch(e){toast(`${label} failed: ${e.message||'error'}.`,true);} - finally{Store.batch=null;} + finally{Store.batch=null;Busy.end();} } /* ================= Compiling the data ================= */ @@ -615,7 +631,11 @@ const nameOf=id=>id==='internet'?'Internet':SVC[id]?SVC[id].name:(byId[id]&&!byI const siteServices=()=>SITE?SITE.services.filter(x=>x.scope==='site'||SITE.usedSvc.has(x.id)):[]; const vnum=a=>{const v=vlanById[a.vlan];return v?v.num:'?';}; -const state={attack:{from:'internet'},attackHl:null,hoverFlow:null,lens:'physical',biz:null,view:'overview',siteId:null,ovSelId:null,selId:null,mode:'zones',hidden:new Set(),minSev:0,minExp:0,dir:'all',color:'risk',flows:true,riskOnly:false,labels:true,ports:false,rotate:false,gap:8,path:false,panel:null,posts:'off',compact:true,arrange:false,flat:false,tool:'move'}; +const state={attack:{from:'internet'},attackHl:null,hoverFlow:null,lens:'physical',biz:null,view:'overview',siteId:null,ovSelId:null,selId:null,mode:'zones',hidden:new Set(),minSev:0,minExp:0,dir:'all',color:'risk',flows:true,riskOnly:false,labels:true,ports:false,rotate:false,gap:8,path:false,panel:null,posts:'off',compact:true,arrange:false,flat:false,tool:'move',hideEmpty:false}; +try{state.hideEmpty=localStorage.getItem('strata-hide-empty')==='1';}catch(e){} +// VLANs left out of the model: those without an asset, when "Hide empty VLANs" is on +const vlanEmpty=id=>!ASSETS.some(a=>a.vlan===id); +const vlanShownInLayout=id=>!(state.hideEmpty&&vlanEmpty(id)); const reduceMotion=matchMedia('(prefers-reduced-motion: reduce)').matches; // saved theme applied before the first frame, so the page does not flash try{const t=localStorage.getItem('strata-theme');if(t==='light'||t==='dark')document.documentElement.dataset.theme=t;}catch(e){} @@ -664,12 +684,14 @@ function renderSiteList(){ function renderVlanList(){ // By zone: listed left to right as on the level. One VLAN per floor: listed top to bottom. const zm=state.mode==='zones',L=zm?['←','→','Move left','Move right']:['↑','↓','Move up','Move down']; - $('#vlans').innerHTML=ZONES.slice().reverse().filter(z=>z.vlans.length).map(z=>{const vs=zm?z.vlans:z.vlans.slice().reverse();return `
${esc(z.name.toUpperCase())}${zm&&z.vlans.some(v=>v.place)?` `:zm&&vs.length>1?' left → right':''}
`+vs.map((v,i)=>{ + $('#vlans').innerHTML=ZONES.slice().reverse().filter(z=>z.vlans.length).map(z=>{const vs=zm?z.vlans:z.vlans.slice().reverse();return `
${esc(z.name.toUpperCase())}${zm&&z.vlans.some(v=>v.place)?` `:zm&&vs.length>1?' left → right':''}
`+vs.map((v,i)=>{ const first=i===0,last=i===vs.length-1,dA=zm?-1:1,dB=zm?1:-1; - return `
+
@@ -145,8 +146,15 @@

Layout

-

Drag a plate anywhere on its level (left, right, front, back), or use the arrows that appear in the VLAN list. “Line up” puts a level back in a row.

+

Drag a plate anywhere on its level (left, right, front, back), or use the arrows that appear in the VLAN list. “Line up” puts a level back in a row. The Internet cloud can be dragged too.

+
+ +
+
+

Each zone on rows and columns: VLANs with flows between them side by side, VLANs without flows last. Saved for everyone; “Line up” undoes it for a zone.

+
+

Internet services

@@ -155,6 +163,8 @@

Internet services

Plates (VLANs)

+
Show
+
diff --git a/tests/analysis.test.mjs b/tests/analysis.test.mjs index aa32dae..6e09083 100644 --- a/tests/analysis.test.mjs +++ b/tests/analysis.test.mjs @@ -233,3 +233,34 @@ test('attack paths: unlikely and long paths are left out, and a cut-short search // a target nothing leads to has no path assert.deepEqual(J(A.attackPaths(s, A.outside(s), ['bastion', 'fw']).byTarget.fw), []); }); + +test('arrange: plates of a level on rows and columns, the VLANs that talk side by side', () => { + const plates = 'abcdefghij'.split('').map((id, i) => ({ id, w: 8 + (i % 3), D: 6 + (i % 2) })); + const W = { 'a|h': 9, 'h|c': 7, 'c|j': 5 }; + const weight = (x, y) => W[`${x}|${y}`] || W[`${y}|${x}`] || 0; + const r = J(A.arrangeGrid(plates, weight, 4, 2)); + assert.equal(r.cols, 4); + assert.equal(r.rows, 3); + // the busiest VLAN first, then the one it talks to most beside it, and so on; VLANs without flows last + assert.deepEqual(r.order.slice(0, 4), ['h', 'a', 'c', 'j']); + assert.deepEqual(r.order.slice(4), ['b', 'd', 'e', 'f', 'g', 'i']); + // no two plates overlap, and the grid is centred on 0 + const box = id => { const p = plates.find(q => q.id === id), [x, z] = r.places[id]; return { x0: x - p.w / 2, x1: x + p.w / 2, z0: z - p.D / 2, z1: z + p.D / 2 }; }; + const ids = Object.keys(r.places); + for (const i of ids) for (const j of ids) if (i < j) { + const a = box(i), b = box(j); + assert.ok(a.x1 <= b.x0 || b.x1 <= a.x0 || a.z1 <= b.z0 || b.z1 <= a.z0, `${i} and ${j} overlap`); + } + const xs = ids.flatMap(id => [box(id).x0, box(id).x1]); + assert.ok(Math.abs(Math.min(...xs) + Math.max(...xs)) <= 1); +}); + +test('arrange: automatic columns, a single plate, and none', () => { + const plates = Array.from({ length: 40 }, (_, i) => ({ id: 'v' + i, w: 8, D: 6 })); + const r = J(A.arrangeGrid(plates, () => 0, 0, 2)); + assert.equal(r.cols, 8, 'about the square root, a little wider than deep: no more 40 plates on one row'); + assert.equal(r.rows, 5); + assert.deepEqual(r.order, plates.map(p => p.id), 'with no flow, the current order is kept'); + assert.deepEqual(J(A.arrangeGrid([{ id: 'x', w: 8, D: 6 }], () => 0, 6, 2)).places, { x: [0, 0] }); + assert.deepEqual(J(A.arrangeGrid([], () => 0, 6, 2)).places, {}); +}); From 4494190c50acbe6ed554782affd58a811b759215 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:29:03 +0000 Subject: [PATCH 03/16] Import VLANs: pick the VLANs of a CSV, a JSON list or a Strata export and the zone of each A Strata export also brings the assets of the chosen VLANs and the flows between them. Zone headers of the VLAN list laid out with flex, so All / None never cover the first VLAN. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- public/analysis.js | 60 +++++++++++++++++++++++++- public/app.css | 14 +++++- public/app.js | 95 +++++++++++++++++++++++++++++++++++++++++ public/index.html | 2 + tests/analysis.test.mjs | 24 +++++++++++ 5 files changed, 193 insertions(+), 2 deletions(-) diff --git a/public/analysis.js b/public/analysis.js index c2d2d32..f8c8ffb 100644 --- a/public/analysis.js +++ b/public/analysis.js @@ -274,6 +274,64 @@ function chokePoints(site,byTarget){ return [...pts.values()].sort((x,y)=>y.every.length-x.every.length||y.paths-x.paths||byName(x.id,y.id)); } +/* ---- VLANs read from a file, to import some of them ---- */ +// A CSV (header in English or French, separator , ; or tab), a JSON list ([{num, name, cidr, zone}] or +// {vlans: [...]}) or a Strata export (format "strata": every VLAN of every IS, with its zone and its IS). +// Returns {kind: 'csv'|'json'|'strata', items: [{num, name, cidr, zone, site, siteName, vlanId}], skipped} +// where skipped counts the lines without a valid VLAN number (1 to 4094). Throws when nothing is readable. +const VCOL={num:['vlan','vlan id','vlan_id','vlanid','vid','vlan number','num','number','numero','numéro','n°','tag'], + name:['name','nom','vlan name','description','desc','label','libellé','libelle'], + cidr:['cidr','subnet','network','prefix','range','réseau','reseau','sous-réseau','sous-reseau','plage','ip range','address range','prefixe','préfixe'], + zone:['zone','group','groupe','vlan group','site','location','emplacement','tenant']}; +function csvRows(text){ + const lines=String(text).replace(/^/,'').split(/\r?\n/).filter(l=>l.trim()); + if(!lines.length)return []; + const head=lines[0],sep=[';','\t',','].map(c=>[c,head.split(c).length]).sort((a,b)=>b[1]-a[1])[0][0]; + return lines.map(l=>{const out=[];let cur='',q=false; + for(let i=0;i{const m=/\d+/.exec(String(v==null?'':v));const n=m?parseInt(m[0],10):NaN;return n>=1&&n<=4094?n:null;}; +function vlansFromFile(text,fileName=''){ + const t=String(text||'').replace(/^/,'').trim(); + if(!t)throw new Error('the file is empty'); + const items=[];let skipped=0,kind; + const push=(o,extra={})=>{const num=vlanNum(o.num);if(num===null){skipped++;return;} + items.push(Object.assign({num,name:String(o.name||'').trim().slice(0,50)||`VLAN ${num}`,cidr:String(o.cidr||'').trim().slice(0,60),zone:String(o.zone||'').trim().slice(0,40)},extra));}; + if(/^[[{]/.test(t)&&!/\.csv$/i.test(fileName)){ + let o;try{o=JSON.parse(t);}catch(e){throw new Error('this is not valid JSON');} + if(o&&typeof o.si==='object'&&o.si&&!Array.isArray(o.si)){ + kind='strata'; + Object.entries(o.si).forEach(([sid,d])=>{if(!d||typeof d!=='object')return; + const zones=Array.isArray(d.zones)?d.zones:[]; + (Array.isArray(d.vlans)?d.vlans:[]).forEach(v=>{if(!v||typeof v!=='object')return;const z=zones.find(x=>x&&x.id===v.zone); + push({num:v.num,name:v.name,cidr:v.cidr,zone:z?z.name:''},{site:sid,siteName:String(d.name||sid),vlanId:String(v.id||'')});});}); + }else{ + kind='json'; + const list=Array.isArray(o)?o:Array.isArray(o&&o.vlans)?o.vlans:Array.isArray(o&&o.results)?o.results:null; + if(!list)throw new Error('expected a list of VLANs, {"vlans": [...]}, or a Strata export'); + const pick=(x,keys)=>{for(const k of keys){const hit=Object.keys(x).find(y=>y.toLowerCase()===k);if(hit!==undefined&&x[hit]!=null&&typeof x[hit]!=='object')return x[hit];} + // NetBox and others: {group: {name}}, {site: {name}} + for(const k of keys){const hit=Object.keys(x).find(y=>y.toLowerCase()===k);if(hit!==undefined&&x[hit]&&typeof x[hit]==='object'&&x[hit].name)return x[hit].name;}return '';}; + list.forEach(x=>{if(!x||typeof x!=='object'){skipped++;return;}push({num:pick(x,VCOL.num),name:pick(x,VCOL.name),cidr:pick(x,VCOL.cidr),zone:pick(x,VCOL.zone)});}); + } + }else{ + kind='csv'; + const rows=csvRows(t); + const norm=h=>h.toLowerCase().replace(/^["']|["']$/g,'').trim(); + const head=rows[0].map(norm),col={}; + Object.entries(VCOL).forEach(([k,names])=>{const i=head.findIndex(h=>names.includes(h));if(i>=0)col[k]=i;}); + let body=rows.slice(1); + // no header: number, name, range, zone in that order + if(col.num===undefined){if(vlanNum(rows[0][0])===null)throw new Error('no VLAN number column (vlan, vid, num, numéro…)');Object.assign(col,{num:0,name:1,cidr:2,zone:3});body=rows;} + body.forEach(r=>push({num:r[col.num],name:col.name!==undefined?r[col.name]:'',cidr:col.cidr!==undefined?r[col.cidr]:'',zone:col.zone!==undefined?r[col.zone]:''})); + } + if(!items.length)throw new Error('no VLAN with a number from 1 to 4094 in this file'); + return {kind,items,skipped}; +} + /* ---- Arranging the plates of a level on rows and columns ---- */ // plates: [{id, w, D}] in their current order; weight(a, b): how much two VLANs talk (symmetric, >= 0); // cols: the most plates per row (0 or less: about the square root of their number, a little wider than deep); @@ -304,6 +362,6 @@ function arrangeGrid(plates,weight,cols,gap=2){ return {places,cols:C,rows:R,order:grid.map(p=>p.id)}; } -root.StrataAnalysis={arrangeGrid,riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, +root.StrataAnalysis={arrangeGrid,vlansFromFile,riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, stepOdds,blastRadius,attackPaths,chokePoints,outside}; })(typeof globalThis!=='undefined'?globalThis:this); diff --git a/public/app.css b/public/app.css index a031a45..6f46034 100644 --- a/public/app.css +++ b/public/app.css @@ -712,7 +712,7 @@ body.has-dash{overflow:hidden} @media (prefers-reduced-motion:reduce){#flatSvg .selhalo{animation:none}} .ibtn.signout{border-color:var(--line);color:var(--fg);margin-left:6px;min-height:22px;padding:2px 8px} .vtools{display:flex;align-items:center;gap:4px;margin:0 0 4px}.vtools .fl{margin-right:auto} -.zone-h .zsel{float:right;display:inline-flex;gap:2px}.zone-h .zsel .ibtn{min-height:22px;padding:1px 6px} +.zone-h{display:flex;align-items:center;gap:4px;min-height:24px}.zone-h>span,.zone-h>.ibtn{float:none}.zone-h>:first-child{margin-left:auto}.zone-h .zsel{display:inline-flex;gap:2px;opacity:1}.zone-h .zsel .ibtn{min-height:22px;padding:1px 6px} .vrow.off{opacity:.45} /* Loading bar for long operations */ #busy{position:fixed;top:0;left:0;right:0;height:3px;z-index:1000;overflow:hidden;background:color-mix(in srgb,var(--fg) 10%,transparent);pointer-events:none} @@ -723,3 +723,15 @@ body.has-dash{overflow:hidden} @keyframes busy{0%{left:-30%}100%{left:100%}} @media (prefers-reduced-motion:reduce){#busy i{animation:none;width:100%;opacity:.5}} .arrRow{display:flex;gap:6px;align-items:stretch}.arrRow select{width:auto;flex:0 0 76px;padding:6px 8px}.arrRow .btn{flex:1} +/* Import VLANs */ +.vi-tools{display:flex;gap:4px;align-items:center;margin:6px 0}.vi-tools .search{flex:1;padding:6px 8px;margin-left:4px} +.vi-bulk{margin:4px 0 8px}.vi-bulk>label{display:block;font-size:11px;color:var(--muted);margin-bottom:3px} +.vi-list{max-height:min(48vh,460px);overflow:auto;border:1px solid var(--line);border-radius:3px} +table.vi{width:100%;border-collapse:collapse;font-size:12px} +table.vi th{position:sticky;top:0;background:var(--panel-2);text-align:left;font-weight:600;padding:5px 6px;font-size:11px;color:var(--muted)} +table.vi td{padding:5px 6px;border-top:1px solid var(--line);vertical-align:top} +table.vi td small{display:block;color:var(--muted)}table.vi td.m,table.vi .m{font-family:var(--f-mono)} +table.vi tr:not(.on) td:not(:first-child){opacity:.55} +table.vi select{max-width:140px;font-size:11px;background:var(--panel-2);color:var(--fg);border:1px solid var(--line);border-radius:3px;padding:3px} +table.vi .tag{display:inline-block;font-size:10px;padding:0 5px;border:1px solid var(--line);border-radius:8px;margin:2px 3px 0 0;color:var(--muted)} +table.vi .tag.warn{border-color:var(--high);color:var(--high)} diff --git a/public/app.js b/public/app.js index 02ec5de..d383c69 100644 --- a/public/app.js +++ b/public/app.js @@ -1999,6 +1999,7 @@ async function act(name){ if(name==='wipe'){if(!confirmArm('wipe'))return;await replaceAll({si:{},assets:{},wan:[]},'Deleting');state.ovSelId=null;closeDet();return;} if(name==='export')return exportJson(); if(name==='import'){$('#io').value='';$('#io').click();return;} + if(name==='import-vlans'){if(!SITE)return;$('#vlanFile').value='';$('#vlanFile').click();return;} } // Destructive actions need a second click on the same button const armed={}; @@ -2015,6 +2016,100 @@ async function exportJson(){ const data=JSON.stringify({format:'strata',version:3,exportedAt:new Date().toISOString(),si:raw.si,assets:raw.assets,wan:raw.wan,services:raw.services||[],risk:raw.risk||{}},null,2); await saveFile(`strata-${today()}.json`,'application/json',data); } +/* ---- Import VLANs: pick some VLANs of a file and the zone each one goes to ---- */ +$('#vlanFile').addEventListener('change',e=>{ + const f=e.target.files&&e.target.files[0];if(!f||!SITE)return; + if(f.size>20*1024*1024){toast('This file is too large (20 MB at most).',true);return;} + const r=new FileReader(); + r.onload=()=>{let P,src=null; + try{P=SA.vlansFromFile(r.result,f.name);if(P.kind==='strata')src=JSON.parse(r.result);} + catch(err){toast(`Could not read ${f.name}: ${err.message}.`,true);return;} + vlanImportForm(P,f.name,src);}; + r.readAsText(f); +}); +function vlanImportForm(P,fileName,src){ + const zones=SITE.raw.zones&&SITE.raw.zones.length?clone(SITE.raw.zones):[{id:'z-def',name:'Main zone'}]; + const have=new Map((SITE.raw.vlans||[]).map(v=>[+v.num,v])); + const zByName=n=>zones.find(z=>z.name.toLowerCase()===String(n).trim().toLowerCase()); + let extraZones=[...new Set(P.items.map(i=>i.zone).filter(z=>z&&!zByName(z)))]; + const seen=new Set(),srcAssets=src&&src.assets&&typeof src.assets==='object'?src.assets:{}; + const rows=P.items.map((it,i)=>{const dup=seen.has(it.num);seen.add(it.num);const ex=have.get(it.num)||null; + const n=src?Object.values(srcAssets).filter(a=>a&&a.site===it.site&&a.vlan===it.vlanId).length:0; + return Object.assign({},it,{i,dup,ex,assets:n,zoneSel:zByName(it.zone)?zByName(it.zone).id:it.zone?'new:'+it.zone:zones[zones.length-1].id,on:!dup&&!ex});}); + const zOpts=(sel,bulk)=>[...zones.slice().reverse().map(z=>[z.id,z.name]),...extraZones.map(n=>['new:'+n,`New zone: ${n}`]),...(bulk?[['__new','New zone…']]:[])] + .map(([v,t])=>``).join(''); + const sites=src?[...new Set(P.items.map(i=>i.siteName))]:[]; + const row=r=>`${r.num} + ${esc(r.name)}${r.cidr?`${esc(r.cidr)}`:''}${sites.length>1?`IS ${esc(r.siteName)}`:''} + ${r.ex?`exists`:''}${r.dup?'twice in the file':''}${r.assets?`${plural(r.assets,'asset')}`:''} + `; + const kindTxt={csv:'CSV',json:'JSON list',strata:'Strata export'}[P.kind]; + openForm('Import VLANs',`IS ${SITE.name}`,` +

${esc(fileName)} · ${kindTxt} · ${plural(P.items.length,'VLAN')}${P.skipped?` · ${plural(P.skipped,'line')} without a VLAN number left out`:''}

+
+
+
+
+
${rows.map(row).join('')}
VLANName, rangeZone
+

+ ${src?``:''} +

A ticked VLAN whose number is already in this IS is updated: name, range and zone.

`, + async()=>{ + const chosen=rows.filter(r=>r.on); + if(!chosen.length)return formErr('Tick at least one VLAN.'); + const nums=new Map();for(const r of chosen){if(nums.has(r.num))return formErr(`VLAN ${r.num} is ticked twice: keep one.`);nums.set(r.num,r);} + const d=clone(SITE.raw);d.zones=d.zones&&d.zones.length?d.zones:clone(zones);d.vlans=d.vlans||[];d.flows=d.flows||[]; + const zoneId=sel=>{if(!sel.startsWith('new:'))return sel;const n=sel.slice(4);let z=d.zones.find(x=>x.name.toLowerCase()===n.toLowerCase());if(!z){z={id:uid('z'),name:n};d.zones.push(z);}return z.id;}; + const vmap={};let added=0,updated=0; + chosen.forEach(r=>{const zone=zoneId(r.zoneSel); + if(r.ex){d.vlans=d.vlans.map(x=>{if(x.id!==r.ex.id)return x;const y=Object.assign({},x,{name:r.name,zone});if(r.cidr)y.cidr=r.cidr;if(x.zone!==zone)delete y.place;return y;});vmap[r.site+'|'+r.vlanId]=r.ex.id;updated++;} + else{const id=uid('v');d.vlans.push({id,num:r.num,name:r.name,cidr:r.cidr,zone});vmap[r.site+'|'+r.vlanId]=id;added++;}}); + const tasks=[()=>Store.putSite(SITE.id,d)];let nA=0,nF=0; + if(src&&$('#viAssets')&&$('#viAssets').checked){ + const amap={},taken=new Set(Object.keys(raw.assets)); + Object.entries(srcAssets).forEach(([aid,a])=>{if(!a||typeof a!=='object')return;const v=vmap[a.site+'|'+a.vlan];if(!v)return; + const nid=taken.has(aid)||!/^[\w.:-]{1,64}$/.test(aid)?uid('a-'):aid;taken.add(nid);amap[a.site+'|'+aid]=nid; + const doc=clone(a);doc.site=SITE.id;doc.vlan=v;tasks.push(()=>Store.putAsset(nid,doc));nA++;}); + const svcOk=id=>id==='internet'||(String(id).startsWith('svc:')&&SVC[id]); + const fids=new Set(d.flows.map(f=>f.id)),same=new Set(d.flows.map(f=>`${f.src}>${f.dst}>${f.port}`)); + [...new Set(chosen.map(r=>r.site))].forEach(sid=>{const sd=src.si&&src.si[sid]; + (sd&&Array.isArray(sd.flows)?sd.flows:[]).forEach(f=>{if(!f||typeof f!=='object')return; + const m=id=>amap[sid+'|'+id]||(svcOk(id)?id:null),s=m(f.src),t=m(f.dst); + if(!s||!t||s===t||(isNet(s)&&isNet(t))||same.has(`${s}>${t}>${f.port}`))return; + const doc=Object.assign(clone(f),{src:s,dst:t,id:f.id&&!fids.has(f.id)?f.id:uid('f')}); + if(Array.isArray(f.via))doc.via=f.via.map(x=>amap[sid+'|'+x]).filter(Boolean); + fids.add(doc.id);same.add(`${s}>${t}>${f.port}`);d.flows.push(doc);nF++;});}); + } + // the plates first, then the assets on them + if(tasks.length>1)await Store.many(tasks,'Importing the VLANs');else await Busy.track(()=>Store.putSite(SITE.id,d),'Importing the VLANs'); + toast(`VLANs: ${added} added, ${updated} updated${nA?`, ${plural(nA,'asset')}`:''}${nF?`, ${plural(nF,'flow')}`:''}.`); + afterSave(()=>restorePanel()); + }); + det.querySelector('.form-foot [type=submit]').textContent='Import'; + const body=$('#viBody'),frm=$('#frm'); + const count=()=>{const n=rows.filter(r=>r.on).length;$('#viCount').textContent=`${n} of ${rows.length} ticked`;}; + const visible=()=>[...body.querySelectorAll('tr[data-vi-row]')].filter(tr=>!tr.hidden).map(tr=>rows[+tr.dataset.viRow]); + const paint=()=>{const q=$('#viFilter').value.trim().toLowerCase(); + body.querySelectorAll('tr[data-vi-row]').forEach(tr=>{const r=rows[+tr.dataset.viRow];tr.hidden=!!q&&!`${r.num} ${r.name} ${r.cidr} ${r.zone} ${r.siteName||''}`.toLowerCase().includes(q); + tr.classList.toggle('on',r.on);tr.querySelector('[data-vi-on]').checked=r.on;});count();}; + frm.addEventListener('change',e=>{const tr=e.target.closest('tr[data-vi-row]');if(!tr)return;const r=rows[+tr.dataset.viRow]; + if(e.target.matches('[data-vi-on]')){r.on=e.target.checked;tr.classList.toggle('on',r.on);count();} + if(e.target.matches('[data-vi-zone]'))r.zoneSel=e.target.value;}); + $('#viZone').addEventListener('change',e=>{$('#viZoneNewBox').hidden=e.target.value!=='__new';}); + $('#viFilter').addEventListener('input',paint); + frm.addEventListener('click',e=>{const b=e.target.closest('[data-vi]');if(!b)return;const k=b.dataset.vi; + if(k==='all'||k==='none'||k==='new'){visible().forEach(r=>r.on=k==='all'?!r.dup:k==='new'?!r.ex&&!r.dup:false);paint();return;} + if(k==='apply'){let sel=$('#viZone').value; + if(sel==='__new'){const n=$('#viZoneNew').value.trim();if(!n)return formErr('Name the new zone.','viZoneNew'); + const z=zByName(n);sel=z?z.id:'new:'+n;if(!z&&!extraZones.includes(n))extraZones.push(n);} + const t=rows.filter(r=>r.on);if(!t.length)return formErr('Tick the VLANs to put in this zone first.'); + t.forEach(r=>r.zoneSel=sel);$('#ferr').hidden=true; + body.innerHTML=rows.map(row).join('');paint(); + $('#viZone').innerHTML=zOpts(sel,true);$('#viZoneNewBox').hidden=true;$('#viZoneNew').value=''; + toast(`${plural(t.length,'VLAN')} set to zone ${sel.startsWith('new:')?sel.slice(4):(zones.find(z=>z.id===sel)||{}).name}.`);} + }); + count(); +} $('#io').addEventListener('change',e=>{ const f=e.target.files&&e.target.files[0];if(!f)return; const r=new FileReader(); diff --git a/public/index.html b/public/index.html index cf39010..2291cee 100644 --- a/public/index.html +++ b/public/index.html @@ -88,6 +88,7 @@

Edit

+

@@ -224,6 +225,7 @@

Legend

+ diff --git a/tests/analysis.test.mjs b/tests/analysis.test.mjs index 6e09083..65faf54 100644 --- a/tests/analysis.test.mjs +++ b/tests/analysis.test.mjs @@ -264,3 +264,27 @@ test('arrange: automatic columns, a single plate, and none', () => { assert.deepEqual(J(A.arrangeGrid([{ id: 'x', w: 8, D: 6 }], () => 0, 6, 2)).places, { x: [0, 0] }); assert.deepEqual(J(A.arrangeGrid([], () => 0, 6, 2)).places, {}); }); + +test('VLAN files: a CSV with a French header, quotes and a semicolon separator', () => { + const r = J(A.vlansFromFile('Numéro;Nom;Réseau;Zone\n10;Serveurs;10.0.10.0/24;DC\n"20";"Postes; étage 1";10.0.20.0/24;Bureaux\nabc;x;;\n5000;trop grand;;\n')); + assert.equal(r.kind, 'csv'); + assert.deepEqual(r.items, [ + { num: 10, name: 'Serveurs', cidr: '10.0.10.0/24', zone: 'DC' }, + { num: 20, name: 'Postes; étage 1', cidr: '10.0.20.0/24', zone: 'Bureaux' }, + ]); + assert.equal(r.skipped, 2); + // without a header: number, name, range, zone + assert.deepEqual(J(A.vlansFromFile('30,Cameras,10.0.30.0/24\n40,Printers')).items.map(i => [i.num, i.name, i.cidr]), [[30, 'Cameras', '10.0.30.0/24'], [40, 'Printers', '']]); + assert.throws(() => A.vlansFromFile('name,range\nx,y'), /no VLAN number column/); + assert.throws(() => A.vlansFromFile(''), /empty/); +}); + +test('VLAN files: a JSON list (NetBox style) and a Strata export', () => { + const nb = J(A.vlansFromFile(JSON.stringify({ results: [{ id: 912, vid: 30, name: 'Cams', group: { name: 'OT' } }, { id: 913, name: 'no number' }] }))); + assert.equal(nb.kind, 'json'); + assert.deepEqual(nb.items, [{ num: 30, name: 'Cams', cidr: '', zone: 'OT' }]); + assert.equal(nb.skipped, 1); + const strata = J(A.vlansFromFile(JSON.stringify({ format: 'strata', si: { lyon: { name: 'Lyon', zones: [{ id: 'z1', name: 'DMZ' }], vlans: [{ id: 'v1', num: 100, name: 'Web', cidr: '192.0.2.0/24', zone: 'z1' }] } }, assets: {} }))); + assert.equal(strata.kind, 'strata'); + assert.deepEqual(strata.items, [{ num: 100, name: 'Web', cidr: '192.0.2.0/24', zone: 'DMZ', site: 'lyon', siteName: 'Lyon', vlanId: 'v1' }]); +}); From 77b1186895d219160fe781893ec2c594cb8dc615 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:51:17 +0000 Subject: [PATCH 04/16] Docs: memory and CPU needs, sign-in without HTTP Basic; e2e covers the new plate tools - README section 1: measured server memory and CPU from an empty map to 30 000 assets, browser needs - docker-compose.yml: resource limits, commented out - README, docs, .env.example, CHANGELOG: the sign-in form, sessions, API tokens for scripts - A malformed Origin on the sign-in form is refused (403), not an error Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- .env.example | 1 + CHANGELOG.md | 21 ++++++++++++ README.md | 39 +++++++++++++++++----- docker-compose.yml | 9 +++++ docs/api.html | 8 ++--- docs/docs.html | 6 ++-- public/app.js | 2 +- server/server.js | 4 ++- tests/e2e.mjs | 83 +++++++++++++++++++++++++++++++++++++++++++++- tests/sso.test.mjs | 1 + 10 files changed, 155 insertions(+), 19 deletions(-) diff --git a/.env.example b/.env.example index cd8149c..57a10ea 100644 --- a/.env.example +++ b/.env.example @@ -54,6 +54,7 @@ HOST=0.0.0.0 # SSO_HEADER_TRUST=127.0.0.1 # Local accounts once single sign-on is on: all (default), admins (break-glass only) or off # LOCAL_LOGIN=all +# How long a browser stays signed in, single sign-on and local accounts alike (hours) # SESSION_HOURS=8 # Connectors (Cyberwatch, GLPI, phpIPAM, Palo Alto, and modules). Set to false to turn them off. diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a6569e..f13fddf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,27 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://semver.org/). +## [Unreleased] +### Upgrading +- **HTTP Basic is gone.** Local accounts sign in with the form of the sign-in page and get a session cookie (`SESSION_HOURS`, 8 by default). Scripts that sent `curl -u name:password` get `401` with a message: give them an API token (*Account › API tokens*) and send `Authorization: Bearer stk_…`. `GET /?local` is gone too. +- Sessions are kept in memory: restarting Strata signs everyone out. + +### Added +- **Sign out** for every account, in the title block (it was in *Account*, for single sign-on only). +- **Import VLANs** (*Edit › Import VLANs*, in an IS): a CSV (header in English or French, `,` `;` or tab), a JSON list (`[{num, name, cidr, zone}]`, `{"vlans": […]}`, NetBox's `{"results": […]}`) or a Strata export. Tick the VLANs you want, filter, and choose the zone of each, or of all the ticked ones at once (an existing zone, a zone named in the file, or a new one). A VLAN whose number is already in the IS is updated when ticked. From a Strata export, the assets of the chosen VLANs and the flows between them come too. +- **Arrange VLANs** (*Layout*): each zone on rows and columns instead of one long row, the VLANs that talk to each other side by side (the most traffic to the plates left of and behind each one), VLANs without flows last. *VLANs per row*: automatic (about the square root of their number) or 3 to 12. Saved for everyone; *Line up* undoes it for a zone. +- **The Internet cloud can be moved**: dragged in *Arrange plates* (3D) and with *Move* in the 2D diagram. Its place is saved on the IS (`si.cloud`, `si.cloud2d`); the named Internet services and the flows follow; *Put Internet back* returns it to its default place. +- **Show all or no VLANs** of the IS, or of one zone (*All* / *None* next to each zone of the plate list). +- **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. +- **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. +- **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. +- **API**: `POST /auth/login` (the form, or JSON `{user, password}`); `analysis.js` exports `arrangeGrid` and `vlansFromFile`. + +### Changed +- Changing a password, or disabling or deleting an account, ends that account's other sessions. *End all sessions* (admin) ends the sessions of local accounts too, and keeps the admin's own. +- The Content-Security-Policy allows forms to post to Strata itself (`form-action 'self'`), for the sign-in form. +- `tests/e2e.mjs` takes `CHROMIUM_PATH`, a Chromium already installed. + ## [0.10.0] - 2026-10-28 ### Added - **Incident mode, with Muster** (*Incident* in the controls of an IS). Mark assets compromised, suspected or contained, from their sheet or from a playbook decision. The incident sheet then shows: diff --git a/README.md b/README.md index 4a8064f..924c97c 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,26 @@ The machine needs access to the npm registry **once**, during the build, to down Users need a browser with WebGL 2 and JavaScript modules: Chrome or Edge 80 and later, Firefox 114 and later, Safari 15 and later (three.js has needed WebGL 2 since 2024). +### Memory and CPU + +Measured on Node.js 22 (the Docker image), one server process, maps generated with 2 flows per asset and a vulnerability on one asset in four. *Peak* is during an import of the whole map, ten browsers opening it at once, 50 edits and the dashboard. + +| Map | Server RAM, at rest → peak | Opening the map (10 browsers at once) | One edit | Dashboard (12 months) | +|---|---|---|---|---| +| Empty | 57 → 73 MB | — | — | — | +| Demo size: 5 IS, ~150 assets | 83 MB | 13 ms | 2 ms | 4 ms | +| 10 IS, 200 VLANs, 5 000 assets (2 MB of JSON) | 125 MB | 0.14 s | 17 ms | 70 ms | +| 20 IS, 1 000 VLANs, 30 000 assets (11.5 MB of JSON) | 290 → 340 MB | 0.9 s | 90 ms | 0.35 s | + +| Size of the map | Server: vCPU | Server: RAM | Disk (`/data`) | +|---|---|---|---| +| Up to ~5 000 assets | 0.5 | 256 MB | 1 GB | +| Up to ~30 000 assets | 1 to 2 | 512 MB (1 GB with connectors running) | 5 GB | + +The server is idle between requests: CPU is spent on writes (each one saves the map and appends to the history) and on the dashboard. Each connector or module run starts a separate Node.js process, about 50 MB for as long as it runs. Disk: the map, the history (`HISTORY_KEEP` entries) and `BACKUP_KEEP` daily copies of the map, so roughly the size of the JSON export × (`BACKUP_KEEP` + 2), plus the history. + +**The browser does most of the work** (the 3D model, the 2D diagram, the analysis): a machine with 8 GB of RAM, 4 cores and hardware-accelerated WebGL 2 (any integrated GPU of the last ten years). An IS of 300 assets and 600 flows takes about 150 MB of browser memory. Without a GPU (a virtual desktop, a remote session, a VM with software rendering) the 3D view takes seconds per change from a few hundred assets on: use the 2D diagram, show fewer VLANs (*Plates › None*, then the VLANs you need; *Hide empty VLANs*), or a machine with a GPU. `docker-compose.yml` has the matching limits, commented out. + --- ## 2. Quick start with Docker @@ -268,7 +288,7 @@ SSO_ROLE_CONTRIBUTOR=it-ops SSO_DEFAULT_ROLE=viewer # everyone else; empty: no access ``` -Opening Strata then shows a sign-in page: *Sign in with Microsoft Entra ID*, and *Sign in with a local account* if local accounts are allowed. The groups come from the `groups` claim (`SSO_GROUPS_CLAIM`; for Entra ID, add the groups claim to the token, or use app roles with `SSO_GROUPS_CLAIM=roles`), the user name from `preferred_username` (`SSO_USER_CLAIM`). Strata checks the ID token's signature against the provider's keys, its issuer, audience, expiry and nonce, and uses PKCE. *Account › Sign out* ends the session at the provider too. +Opening Strata then shows a sign-in page: *Sign in with Microsoft Entra ID*, and the form for a local account under it if local accounts are allowed. The groups come from the `groups` claim (`SSO_GROUPS_CLAIM`; for Entra ID, add the groups claim to the token, or use app roles with `SSO_GROUPS_CLAIM=roles`), the user name from `preferred_username` (`SSO_USER_CLAIM`). Strata checks the ID token's signature against the provider's keys, its issuer, audience, expiry and nonce, and uses PKCE. *Sign out* (in the title block, or *Account*) ends the session at the provider too. **A sign-in proxy you already run** (Authelia, oauth2-proxy, Pomerium, Authentik outpost): `SSO_HEADER_USER=Remote-User`, `SSO_HEADER_GROUPS=Remote-Groups` and `SSO_HEADER_TRUST`, the address the proxy connects to Strata from (`127.0.0.1` when it runs on the same machine; several addresses comma-separated, no range: anything in a range could claim to be anyone). The same `SSO_ROLE_*` apply. The proxy must set the headers itself and drop any coming from the browser. `TRUST_PROXY` stays what it was, for the client address. @@ -276,13 +296,13 @@ Opening Strata then shows a sign-in page: *Sign in with Microsoft Entra ID*, and **Names.** The history and the proposals name people by the name the provider gives. A name that is also a local account is refused (rename one of them), and an e-mail address is used only once the provider has verified it. Prefer a name people cannot change themselves at the provider (`SSO_USER_CLAIM=oid` on Entra ID, or `sub`), or make sure they cannot. -**Sessions** last `SESSION_HOURS` (8 by default) with the role given at sign-in. *Account › End all single sign-on sessions* (admin) signs everyone out, after a group change or when someone leaves. +**Sessions** last `SESSION_HOURS` (8 by default), for single sign-on and for local accounts alike: a local account signs in once with the form of the sign-in page and gets a session cookie. *Sign out*, in the title block, ends it. Changing a password, or disabling or deleting an account, ends that account's other sessions. *Account › End all sessions* (admin) signs everyone else out, after a group change or when someone leaves. Sessions are kept in memory: restarting Strata signs everyone out. -**API tokens** for scripts and integrations: *Account › API tokens* (admin), with a name, a role and a validity of 1 to 730 days. The token (`stk_…`) is shown once and stored hashed; send it as `Authorization: Bearer stk_…`. Basic Auth with a local account keeps working for scripts. +**API tokens** for scripts and integrations: *Account › API tokens* (admin), with a name, a role and a validity of 1 to 730 days. The token (`stk_…`) is shown once and stored hashed; send it as `Authorization: Bearer stk_…`. Since 0.11, scripts can no longer send a local account's password with HTTP Basic: the API answers 401 and says to use a token. ## 5. Network access and HTTPS -Passwords travel with Basic Auth: **never expose the app over plain HTTP outside the machine**. +Passwords travel in the sign-in form and sessions in a cookie: **never expose the app over plain HTTP outside the machine**. Set `PUBLIC_URL=https://…` behind HTTPS, so the session cookie is `Secure` and `__Host-` prefixed. ### Option A — the bundled HTTPS proxy (Caddy) @@ -458,7 +478,7 @@ The data lives in the Docker volume and is not touched by the rebuild. Before up The full reference, with every route's role, request and answer, the data format, the module API and the compatibility policy, is the [API reference](https://solidgeeks.github.io/strata/api.html) ([`docs/api.html`](docs/api.html)). -Every route needs the same credentials as the interface (a local account with Basic Auth, an API token as `Authorization: Bearer stk_…`, or the session of a signed-in browser), with the rights of the account's role (direct writes need `approver`). Writes require the headers `Content-Type: application/json` and `X-Requested-With: strata`. A script can group several writes into a single history entry with the headers `X-Batch: ` and `X-Batch-Label: `. To have a tool propose instead of write, give it a `contributor` account and use `POST /api/proposals` with `{"title", "siteId", "changes": {"si": {...}, "assets": {...}}}` (a document set to `null` = deletion). +Every route needs the same credentials as the interface (an API token as `Authorization: Bearer stk_…`, or the session of a signed-in browser; HTTP Basic is no longer accepted since 0.11), with the rights of the account's role (direct writes need `approver`). Writes require the headers `Content-Type: application/json` and `X-Requested-With: strata`. A script can group several writes into a single history entry with the headers `X-Batch: ` and `X-Batch-Label: `. To have a tool propose instead of write, give it a `contributor` account and use `POST /api/proposals` with `{"title", "siteId", "changes": {"si": {...}, "assets": {...}}}` (a document set to `null` = deletion). | Method and route | What it does | |---|---| @@ -484,7 +504,7 @@ Every route needs the same credentials as the interface (a local account with Ba | `GET/POST /api/proposals`, `GET/PUT/DELETE /api/proposals/{id}` | Proposals; `POST …/{id}/submit`, `/withdraw`, `/comment`, `/approve` (`{"force": true}` on a conflict), `/reject` | | `GET /api/users`, `PUT/DELETE /api/users/{name}` | Accounts (admin); `PUT /api/me/password`: `{"current","next"}` (local accounts) | | `GET/POST /api/tokens`, `DELETE /api/tokens/{id}` | API tokens (admin): `POST` `{"name", "role", "days"}` answers the token once | -| `GET /auth/login`, `/auth/oidc/start`, `POST /auth/logout` | Sign-in page, single sign-on, sign-out (`{"location"}` to go to) | +| `GET /auth/login`, `POST /auth/login`, `/auth/oidc/start`, `POST /auth/logout` | Sign-in page; a local account's sign-in (the form, or JSON `{"user", "password"}`: sets the session cookie; 401 wrong, 429 locked); single sign-on; sign-out (`{"location"}` to go to) | | `GET /api/connectors` | Connectors, settings (secrets masked) and latest syncs | | `PUT /api/connectors/{id}` | A connector's settings (admin); `id`: `cyberwatch`, `glpi`, `phpipam`, `paloalto`, or a module's id | | `POST /api/connectors/{id}/test` | Tests access (approver) | @@ -499,7 +519,7 @@ Every route needs the same credentials as the interface (a local account with Ba Example: adding an asset from a script (inventory, NetBox, scanner export…): ```bash -curl -u admin:PASSWORD -H 'Content-Type: application/json' -H 'X-Requested-With: strata' \ +curl -H "Authorization: Bearer $STRATA_TOKEN" -H 'Content-Type: application/json' -H 'X-Requested-With: strata' \ -X PUT https://strata.example.lan/api/assets/srv-erp-01 \ -d '{"site":"lyon","name":"srv-erp-01","type":"server","vlan":"v-lyo-20","ip":"10.20.0.20", "os":"RHEL 8","role":"ERP","crit":3, @@ -569,7 +589,8 @@ This map describes your weak spots precisely: treat it as sensitive data. | "This browser only" badge at the top left | The page cannot reach the API: it is opened as a local file, or the proxy blocks `/api/`. | | Other users only see changes after a reload | The proxy buffers `/api/events` (see section 5, option B). | | "You have read-only access" | You are signed in with the viewer account, or `READ_ONLY=true`. | -| Switching accounts in the browser | The browser keeps Basic Auth credentials: open a private window or close the browser. | +| Switching accounts in the browser | *Sign out* in the title block, then sign in with the other account. | +| A script gets `401` with “HTTP Basic sign-in was removed” | Create an API token (*Account › API tokens*) and send `Authorization: Bearer stk_…`. | | Permission error on `/data` (Docker) | Keep the named volume from `docker-compose.yml`. If you mount a host folder, give it to UID 1000: `chown -R 1000:1000 ./data`. | --- @@ -577,7 +598,7 @@ This map describes your weak spots precisely: treat it as sensitive data. ## 13. Limits of v0.10 - Storage is a single JSON file, suited to a few thousand assets. Beyond that, a real database will be needed. -- Sessions of single sign-on are kept in memory: restarting Strata signs SSO users out. Roles come from the groups at sign-in: a group change applies at the next sign-in (`SESSION_HOURS`, 8 by default), or at once with *End all single sign-on sessions*. LDAP is not read directly: use an OpenID Connect provider in front of it (Keycloak, Authentik, Entra ID). +- Sessions are kept in memory: restarting Strata signs everyone out. Roles come from the groups at sign-in: a group change applies at the next sign-in (`SESSION_HOURS`, 8 by default), or at once with *End all single sign-on sessions*. LDAP is not read directly: use an OpenID Connect provider in front of it (Keycloak, Authentik, Entra ID). - Two approvers editing the same IS directly at the same time: the last save wins (the history lets you go back). Proposals, on the other hand, detect conflicts. - draw.io or Visio diagrams cannot be imported: only Strata JSON exports can become a proposal. - Opened without its server, the app keeps the map in the browser only: no accounts, history, proposals or connectors. diff --git a/docker-compose.yml b/docker-compose.yml index 965252f..fab538f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,15 @@ services: # To open it to your LAN without the HTTPS proxy, use "8080:8080". ports: - "127.0.0.1:8080:8080" + # Resources (README, section 1, "Memory and CPU"): measured 125 MB for 5 000 assets, 340 MB at peak + # for 30 000. Uncomment to cap the container; raise the memory if connectors run often. + # deploy: + # resources: + # limits: + # cpus: "1.0" + # memory: 512M + # reservations: + # memory: 128M # Optional HTTPS reverse proxy: docker compose --profile https up -d caddy: diff --git a/docs/api.html b/docs/api.html index 62fe7fc..e818284 100644 --- a/docs/api.html +++ b/docs/api.html @@ -71,7 +71,7 @@

Authentication

- + @@ -226,11 +226,11 @@

Sign-in pages

MethodHowvia
Local accountHTTP Basic: Authorization: Basic base64(name:password), as curl -u name:password. Refused when LOCAL_LOGIN=off, and for non-admins when LOCAL_LOGIN=admins.local
Local accountThe session cookie set by POST /auth/login (the sign-in form, or JSON {"user", "password"}). Meant for browsers; scripts use an API token. HTTP Basic is refused since 0.11. No local sign-in when LOCAL_LOGIN=off, nor for non-admins when LOCAL_LOGIN=admins.local
API tokenAuthorization: Bearer stk_…. Created by an admin (below) with its own role and expiry. Not available on a server with no accounts.token
Single sign-on sessionThe strata_session cookie (__Host-strata_session over HTTPS) set by /auth/oidc/callback. Meant for browsers.sso
Trusted proxy headerSSO_HEADER_USER (and SSO_HEADER_GROUPS), believed only from the addresses in SSO_HEADER_TRUST.header
- + + -
RouteDoes
GET /auth/loginThe sign-in page (with single sign-on)
GET /auth/loginThe sign-in page: the form for local accounts, and single sign-on
POST /auth/loginSigns a local account in: form fields or JSON {user, password} → session cookie (303 to the app, or {ok, user, role}); 401 wrong, 429 locked, 403 from another site
GET /auth/oidc/startStarts an OpenID Connect sign-in
GET /auth/oidc/callbackWhere the provider sends the browser back; sets the session cookie
POST /auth/logoutEnds the session → {ok, location}: where to go next (the provider’s sign-out)
GET /?localAsks for a local account’s password when single sign-on is on
@@ -317,7 +317,7 @@

Compatibility

What stays stable in 1.x

  • The routes on this page under /api/, their methods, the roles they need, the request fields they accept and the answer fields they return.
  • -
  • Authentication: Basic for local accounts, Bearer stk_… tokens, the write headers Content-Type: application/json and X-Requested-With: strata, X-Batch and X-Batch-Label.
  • +
  • Authentication: session cookies for browsers, Bearer stk_… tokens for scripts, the write headers Content-Type: application/json and X-Requested-With: strata, X-Batch and X-Batch-Label.
  • Status codes and the error key of refusals (not its wording).
  • The data format: the fields listed in Data format keep their meaning. Every export file written by an earlier version (version 3 and below) keeps importing; a 1.x version may write a higher version number, and still reads the lower ones.
  • The module API: a module written for "strataModule": 1 keeps running, with the same ctx and SDK helpers; a store index with "strataRegistry": 1 keeps being read.
  • diff --git a/docs/docs.html b/docs/docs.html index 0d7c81e..9496f56 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -106,7 +106,7 @@

    Configuration

HTTPS and network

-

Passwords travel as HTTP Basic credentials. Never expose Strata over plain HTTP beyond the machine it runs on.

+

Passwords travel in the sign-in form, and sessions in a cookie. Never expose Strata over plain HTTP beyond the machine it runs on; set PUBLIC_URL=https://… so the cookie is Secure.

Bundled proxy (Caddy)

  1. In .env, set SITE_ADDRESS=strata.example.lan, the name your users will type.
  2. @@ -339,7 +339,7 @@

    API

    GET /healthzHealth check, no authentication -
    curl -u admin:PASSWORD -H 'Content-Type: application/json' -H 'X-Requested-With: strata' \
    +    
    curl -H "Authorization: Bearer $STRATA_TOKEN" -H 'Content-Type: application/json' -H 'X-Requested-With: strata' \
       -X PUT https://strata.example.lan/api/assets/srv-erp-01 \
       -d '{"site":"lyon","name":"srv-erp-01","type":"server","vlan":"v-lyo-20","ip":"10.20.0.20",
            "os":"RHEL 8","role":"ERP","crit":3,
    @@ -369,7 +369,7 @@ 

    Troubleshooting

    “This browser only” badgeThe page cannot reach the API: it was opened as a local file, or the proxy blocks /api/. Others see changes only after reloadingThe proxy buffers /api/events: see your own reverse proxy. “You have read-only access”You are signed in as a viewer, or READ_ONLY=true. - Switching accountsBrowsers keep Basic credentials: use a private window, or close the browser. + Switching accountsSign out in the title block, then sign in again. Permission error on /dataKeep the named volume. A host folder must belong to UID 1000: chown -R 1000:1000 ./data. A connector cannot connectTest the connection names the cause: DNS, refused port, untrusted certificate, refused credentials. diff --git a/public/app.js b/public/app.js index d383c69..7613671 100644 --- a/public/app.js +++ b/public/app.js @@ -3153,7 +3153,7 @@ async function showAccount(){

    New account

    - ${me.sso?`

    Single sign-on

    Sessions keep the role given at sign-in until they expire. After a group change, or when someone leaves, end them all: everyone signs in again.

    `:''} + ${me.auth&&me.canAdmin?`

    Sessions

    A browser stays signed in until its session expires${me.sso?', with the role given at sign-in':''}. After a group change, or when someone leaves, end them all: everyone but you signs in again.

    `:''}

    API tokens · ${tokens.length}

    For scripts and integrations: Authorization: Bearer stk_…. A token is shown once, when it is created.

    ${tokens.length?`
      ${tokens.map(t=>`
    • ${esc(t.name)}${esc(t.role)}${Date.parse(t.expiresAt)
    • `).join('')}
    `:''} diff --git a/server/server.js b/server/server.js index 9cf7e52..c783c00 100644 --- a/server/server.js +++ b/server/server.js @@ -828,7 +828,9 @@ async function authRoute(req, res, url) { const json = (req.headers['content-type'] || '').startsWith('application/json'); // a page of another site must not sign a visitor in to an account of its choosing const origin = req.headers.origin; - if (origin && origin !== 'null' && new URL(origin).host !== req.headers.host && (!sso.publicUrl || origin !== new URL(sso.publicUrl).origin)) return fail(res, 403, 'Sign-in from another site refused'); + let fromHere = true; + if (origin && origin !== 'null') { try { fromHere = new URL(origin).host === req.headers.host || (!!sso.publicUrl && origin === new URL(sso.publicUrl).origin); } catch { fromHere = false; } } + if (!fromHere) return fail(res, 403, 'Sign-in from another site refused'); if (!localSignIn()) return json ? fail(res, 404, 'Local sign-in is off') : page(404, 'Local sign-in is off on this server.'); let b; try { b = json ? await readBody(req, 4096) : Object.fromEntries(new URLSearchParams(await readRaw(req, 4096))); } diff --git a/tests/e2e.mjs b/tests/e2e.mjs index ce35198..aa3c04b 100644 --- a/tests/e2e.mjs +++ b/tests/e2e.mjs @@ -6,7 +6,7 @@ import { test, before, after } from 'node:test'; import assert from 'node:assert/strict'; import { spawn } from 'node:child_process'; -import { mkdtempSync, rmSync } from 'node:fs'; +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -303,3 +303,84 @@ test('risk register: threats per asset on the matrix, requirements applied, sett await bob.click('#dash [data-dash="close"]'); assert.deepEqual(errors, [], 'JavaScript errors in the pages'); }); + +test('plates: all or none, empty VLANs hidden, VLANs imported into chosen zones, arranged on a grid, Internet moved, sign-out', async () => { + const admin = await signIn('admin'); + if (!(await admin.locator('#siteList [data-site="lyon"]').count())) { + await admin.click('#empty [data-act="demo"]'); + await admin.waitForFunction(() => document.querySelectorAll('#siteList [data-site]').length === 5); + } + await openIs(admin, 'lyon'); + await admin.waitForSelector('#vlans .vrow'); + const boxes = () => admin.locator('#vlans input[type=checkbox]:checked').count(); + const total = await admin.locator('#vlans input[type=checkbox]').count(); + + // all / none, for the IS and for one zone + await admin.click('.vtools [data-vsel="none"]'); + assert.equal(await boxes(), 0); + await admin.click('.vtools [data-vsel="all"]'); + assert.equal(await boxes(), total); + const zone = admin.locator('#vlans .zone-h [data-vsel="none"]').first(); + const zoneId = await zone.getAttribute('data-zone'); + const inZone = (await api('admin', 'GET', 'api/state')).data.si.lyon.vlans.filter(v => v.zone === zoneId).length; + await zone.click(); + assert.equal(await boxes(), total - inZone); + await admin.click('.vtools [data-vsel="all"]'); + + // a CSV of VLANs: two ticked, put in a new zone at once, one in a zone named by the file + const csv = join(dataDir, 'vlans.csv'); + writeFileSync(csv, 'Numéro;Nom;Réseau;Zone\n300;Lab A;10.30.0.0/24;Labo\n301;Lab B;10.30.1.0/24;Labo\n302;Printers R&D;10.30.2.0/24;\n10;DMZ renamed;;\n'); + await admin.locator('#vlanFile').setInputFiles(csv); + await admin.waitForSelector('#viBody tr'); + assert.equal(await admin.locator('#viBody tr').count(), 4); + assert.equal(await admin.locator('#viBody tr').nth(3).locator('[data-vi-on]').isChecked(), false, 'a VLAN already in the IS starts unticked'); + await admin.click('[data-vi="none"]'); + await admin.locator('#viBody tr').nth(1).locator('[data-vi-on]').check(); + await admin.locator('#viBody tr').nth(2).locator('[data-vi-on]').check(); + await admin.selectOption('#viZone', '__new'); + await admin.fill('#viZoneNew', 'Peripherals'); + await admin.click('[data-vi="apply"]'); + await admin.locator('#viBody tr').nth(0).locator('[data-vi-on]').check(); + await admin.click('#frm button[type=submit]'); + await admin.waitForFunction(() => /Lab A/.test(document.querySelector('#vlans')?.textContent || '')); + const si = (await api('admin', 'GET', 'api/state')).data.si.lyon; + const zoneOf = num => si.zones.find(z => z.id === si.vlans.find(v => v.num === num).zone).name; + assert.equal(zoneOf(300), 'Labo'); + assert.equal(zoneOf(301), 'Peripherals'); + assert.equal(zoneOf(302), 'Peripherals'); + assert.equal(si.vlans.find(v => v.num === 10).name, 'DMZ', 'an unticked VLAN is left alone'); + + // the three new VLANs are empty: hidden from the model when asked + await admin.check('#optEmpty'); + await admin.waitForFunction(() => document.querySelectorAll('#vlans .vrow.off').length >= 3); + await admin.uncheck('#optEmpty'); + + // Arrange VLANs: 3 per row at most, saved as plate positions + await admin.selectOption('#arrCols', '3'); + await admin.click('#arrangeGridBtn'); + await admin.waitForFunction(async () => (await (await fetch('api/state')).json()).si.lyon.vlans.every(v => Array.isArray(v.place))); + const placed = (await api('admin', 'GET', 'api/state')).data.si.lyon; + for (const z of placed.zones) { + const rows = new Set(placed.vlans.filter(v => v.zone === z.id).map(v => v.place[1])); + const n = placed.vlans.filter(v => v.zone === z.id).length; + assert.equal(rows.size, Math.ceil(n / 3), `zone ${z.name}: ${n} VLANs on ${rows.size} rows`); + } + + // the Internet node, dragged in the 2D diagram, keeps its place + await admin.click('#viewSeg [data-v="2d"]'); + await admin.waitForSelector('#flatSvg [data-internet="internet"] rect'); + const b = await admin.locator('#flatSvg [data-internet="internet"] rect').boundingBox(); + await admin.mouse.move(b.x + b.width / 2, b.y + b.height / 2); + await admin.mouse.down(); + await admin.mouse.move(b.x + 300, b.y + 200, { steps: 8 }); + await admin.mouse.up(); + await admin.waitForFunction(async () => Array.isArray((await (await fetch('api/state')).json()).si.lyon.cloud2d)); + await admin.click('#cloudReset'); + await admin.waitForFunction(async () => !(await (await fetch('api/state')).json()).si.lyon.cloud2d); + + // sign out: back to the sign-in page, and the session no longer opens the API + await admin.click('#cart [data-signout]'); + await admin.waitForURL(/\/auth\/login/); + assert.equal((await admin.request.get(base + 'api/me')).status(), 401); + assert.deepEqual(errors, [], 'JavaScript errors in the pages'); +}); diff --git a/tests/sso.test.mjs b/tests/sso.test.mjs index 4914690..0883673 100644 --- a/tests/sso.test.mjs +++ b/tests/sso.test.mjs @@ -124,6 +124,7 @@ test('a page without a session goes to the sign-in page; the API answers 401 wit assert.match(await bad.text(), /Wrong user name or password/); // another site cannot sign a visitor in assert.equal((await fetch(`${base}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Origin: 'https://evil.example' }, body: 'user=admin&password=admin-pass-123' })).status, 403); + assert.equal((await fetch(`${base}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', Origin: 'not a url' }, body: '{}' })).status, 403); // a local session signs out too const out = await (await api('/auth/logout', cookie, { method: 'POST', body: '{}' })).json(); assert.equal(out.location, `${base}/auth/login?out=1`); From 36b4363569e780b0c6bf2a84e53979bb63724f05 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 21:38:11 +0000 Subject: [PATCH 05/16] A lighter 3D view: drawn on demand, Auto / Quality / Economy graphics, far fewer draw calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Drawn only when something changes; shadows redrawn only when the model changes; a lower resolution while the view moves, sharp again when it stops; dots animated at 30 frames a second - Display › Graphics: Auto (Economy on a software renderer, or once moving the view is slow here), Quality, Economy (screen resolution, no shadows, no moving dots, fewer labels from afar); Animated flows on or off - Each equipment type merged into shared geometries; every flow of an IS in one geometry with per-vertex colour and opacity; moving dots instanced; click tubes on a layer never drawn; shared label textures; unreadable labels not drawn; placards in two draw calls - What the GPU held for a rebuilt model is freed - Lyon demo: 1 569 draw calls a frame before, 208 now (Quality), 173 (Economy) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 4 +- README.md | 15 ++- public/app.js | 301 +++++++++++++++++++++++++++++++++++++--------- public/index.html | 6 + tests/e2e.mjs | 22 ++++ 5 files changed, 286 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f13fddf..8bd64fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,9 +16,11 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. -- **API**: `POST /auth/login` (the form, or JSON `{user, password}`); `analysis.js` exports `arrangeGrid` and `vlansFromFile`. +- **Graphics for small laptops** (*Display › Graphics*: Auto, Quality, Economy, and *Animated flows*). Economy: screen resolution, no shadows, no moving dots, labels hidden sooner from afar. Auto, the default, is Quality until moving the view turns out slow on this computer (then Economy, remembered by the browser), and Economy when the browser draws without a graphics card. +- **API**: `POST /auth/login` (the form, or JSON `{user, password}`); `analysis.js` exports `arrangeGrid` and `vlansFromFile`; `window.strataRenderStats()` in the page gives what the last frame cost. ### Changed +- **The 3D view is much lighter to draw**, and looks the same: 8 to 15 times fewer draw calls (1 569 → 208 for the Lyon demo) and half the triangles. It is drawn only when something changes, not 60 times a second; shadows are drawn again only when the model changes; the view is a little less sharp while it moves. Each type of equipment is one shared geometry; every flow of an IS is in one geometry with its own colour and opacity, and the moving dots are one instanced mesh; label textures are shared; labels too small to read are not drawn; flows no longer cast shadows; a rebuilt model frees what the GPU held for the previous one. - Changing a password, or disabling or deleting an account, ends that account's other sessions. *End all sessions* (admin) ends the sessions of local accounts too, and keeps the admin's own. - The Content-Security-Policy allows forms to post to Strata itself (`form-action 'self'`), for the sign-in form. - `tests/e2e.mjs` takes `CHROMIUM_PATH`, a Chromium already installed. diff --git a/README.md b/README.md index 924c97c..55d8c59 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,20 @@ Measured on Node.js 22 (the Docker image), one server process, maps generated wi The server is idle between requests: CPU is spent on writes (each one saves the map and appends to the history) and on the dashboard. Each connector or module run starts a separate Node.js process, about 50 MB for as long as it runs. Disk: the map, the history (`HISTORY_KEEP` entries) and `BACKUP_KEEP` daily copies of the map, so roughly the size of the JSON export × (`BACKUP_KEEP` + 2), plus the history. -**The browser does most of the work** (the 3D model, the 2D diagram, the analysis): a machine with 8 GB of RAM, 4 cores and hardware-accelerated WebGL 2 (any integrated GPU of the last ten years). An IS of 300 assets and 600 flows takes about 150 MB of browser memory. Without a GPU (a virtual desktop, a remote session, a VM with software rendering) the 3D view takes seconds per change from a few hundred assets on: use the 2D diagram, show fewer VLANs (*Plates › None*, then the VLANs you need; *Hide empty VLANs*), or a machine with a GPU. `docker-compose.yml` has the matching limits, commented out. +**The browser does most of the work** (the 3D model, the 2D diagram, the analysis): 8 GB of RAM and WebGL 2 are enough, an integrated GPU included. An IS of 300 assets and 600 flows takes about 150 MB of browser memory. `docker-compose.yml` has the server limits, commented out. + +**Small laptops.** The 3D view is drawn only when something changes (the view, the data, a filter), sharper once the view stops moving, and *Display › Graphics* has three modes: *Quality*, *Economy* (screen resolution, no shadows, no moving dots on the flows, labels hidden sooner from afar) and *Auto*, the default, which is Quality until the view turns out slow on this computer, or when the browser draws without a graphics card. *Animated flows* turns the moving dots on or off in any mode. Measured on the same machine, without a GPU: + +| | Draw calls per frame | Triangles | Frames per second while turning the view | Frames drawn in 3 s, idle | +|---|---|---|---|---| +| Demo Lyon (34 assets), before 0.11 | 1 569 | 187 000 | 5 | 15 | +| … 0.11, Quality | 208 | 85 000 | 8 | only those of the moving dots (30 a second) | +| … 0.11, Economy | 173 | 30 000 | 35 | 2 | +| 120 assets, 260 flows, before 0.11 | 5 731 | 929 000 | 1.7 | 6 | +| … 0.11, Quality | 397 | 400 000 | 2.8 | only those of the moving dots | +| … 0.11, Economy | 365 | 134 000 | 21 | 2 | + +Without a GPU (a virtual desktop, a remote session), Auto chooses Economy; for very large IS, the 2D diagram and *Plates › None*, then the VLANs you need, stay the lightest. --- diff --git a/public/app.js b/public/app.js index 7613671..7ec00be 100644 --- a/public/app.js +++ b/public/app.js @@ -631,7 +631,8 @@ const nameOf=id=>id==='internet'?'Internet':SVC[id]?SVC[id].name:(byId[id]&&!byI const siteServices=()=>SITE?SITE.services.filter(x=>x.scope==='site'||SITE.usedSvc.has(x.id)):[]; const vnum=a=>{const v=vlanById[a.vlan];return v?v.num:'?';}; -const state={attack:{from:'internet'},attackHl:null,hoverFlow:null,lens:'physical',biz:null,view:'overview',siteId:null,ovSelId:null,selId:null,mode:'zones',hidden:new Set(),minSev:0,minExp:0,dir:'all',color:'risk',flows:true,riskOnly:false,labels:true,ports:false,rotate:false,gap:8,path:false,panel:null,posts:'off',compact:true,arrange:false,flat:false,tool:'move',hideEmpty:false}; +const state={attack:{from:'internet'},attackHl:null,hoverFlow:null,lens:'physical',biz:null,view:'overview',siteId:null,ovSelId:null,selId:null,mode:'zones',hidden:new Set(),minSev:0,minExp:0,dir:'all',color:'risk',flows:true,riskOnly:false,labels:true,ports:false,rotate:false,gap:8,path:false,panel:null,posts:'off',compact:true,arrange:false,flat:false,tool:'move',hideEmpty:false,anim:null}; +try{const v=localStorage.getItem('strata-anim');if(v==='1'||v==='0')state.anim=v==='1';}catch(e){} try{state.hideEmpty=localStorage.getItem('strata-hide-empty')==='1';}catch(e){} // VLANs left out of the model: those without an asset, when "Hide empty VLANs" is on const vlanEmpty=id=>!ASSETS.some(a=>a.vlan===id); @@ -747,15 +748,31 @@ function renderEmpty(){ $('#sevSeg').innerHTML=SEV_KEYS.map((k,i)=>``).join(''); /* ================= Three.js ================= */ -const renderer=new THREE.WebGLRenderer({antialias:true}); -renderer.setPixelRatio(Math.min(devicePixelRatio,2)); -renderer.shadowMap.enabled=true;renderer.shadowMap.type=THREE.PCFShadowMap; +// Graphics: Quality, Economy, or Auto (Economy on a software renderer, or once moving the view turns out +// slow on this computer). Remembered in this browser. Economy: screen resolution, no shadows, no moving +// dots on the flows, labels hidden sooner from afar, and no edge smoothing after a reload. +const GFX_KEY='strata-gfx',GFX_SLOW_KEY='strata-gfx-slow',GFX={pref:'auto',slow:false,soft:false,gpu:''}; +try{const p=localStorage.getItem(GFX_KEY);if(p==='quality'||p==='eco')GFX.pref=p;GFX.slow=localStorage.getItem(GFX_SLOW_KEY)==='1';}catch(e){} +const eco=()=>GFX.pref==='eco'||(GFX.pref==='auto'&&(GFX.slow||GFX.soft)); +const renderer=new THREE.WebGLRenderer({antialias:!eco()}); +try{const gl=renderer.getContext(),x=gl.getExtension('WEBGL_debug_renderer_info');GFX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):'');GFX.soft=/swiftshader|llvmpipe|softpipe|software|basic render/i.test(GFX.gpu);}catch(e){} +// Resolution at rest, and while the view moves (a little lower, sharp again as soon as it stops) +const basePR=()=>eco()?1:Math.min(devicePixelRatio,2),movePR=()=>eco()?0.75:Math.min(devicePixelRatio,1.25); +let curPR=basePR();renderer.setPixelRatio(curPR); +// Shadows are drawn again only when something in the model changes, not when the view moves +renderer.shadowMap.enabled=!eco();renderer.shadowMap.type=THREE.PCFShadowMap;renderer.shadowMap.autoUpdate=false; +// The model is drawn only when something changed: the view, the data, a filter, an animation +let needRender=true,shadowDirty=true,renderUntil=0,lastRender=0,lastMove=0,dragging=false,slowLog=[]; +function invalidate(ms,cameraOnly){needRender=true;if(!cameraOnly)shadowDirty=true;if(ms)renderUntil=Math.max(renderUntil,performance.now()+ms);} // Colours as three.js 0.128 rendered them: the palette's values go to the screen unconverted THREE.ColorManagement.enabled=false;renderer.outputColorSpace=THREE.LinearSRGBColorSpace; $('#scene').appendChild(renderer.domElement); const scene=new THREE.Scene(); const camera=new THREE.PerspectiveCamera(42,1,0.1,700); const controls=new OrbitControls(camera,renderer.domElement); +controls.addEventListener('change',()=>{invalidate(0,true);lastMove=performance.now();const m=movePR();if(curPR!==m&&dragging){curPR=m;renderer.setPixelRatio(m);}}); +controls.addEventListener('start',()=>{dragging=true;slowLog=[];}); +controls.addEventListener('end',()=>{dragging=false;}); controls.enableDamping=true;controls.dampingFactor=0.08;controls.maxPolarAngle=Math.PI*0.47;controls.minDistance=8;controls.maxDistance=260;controls.autoRotateSpeed=0.45; // Left button orbits; the middle button (or the right one) moves the view across the screen, left, right, // up and down, so the model need not stay centred on one point. The wheel zooms; on a touch screen, @@ -770,10 +787,41 @@ const W=34,D=21,SLAB=0.3,TABLE_Y=-2; // the colour the selected asset glows in const HL_COLOR=new THREE.Color(); let floors={},pickables=[],cloud=null,selRing=null,sun=null,levelTags=[],extent=W,nLevels=0,ANIM=[],OV={spanX:60,spanZ:40,ex:14,sites:{}}; -const pGeo=new THREE.SphereGeometry(0.12,10,8); +const pGeo=new THREE.SphereGeometry(0.12,10,8);pGeo.userData.cached=true; +// Geometries and textures made once and shared (models, labels): never disposed with the scene +const cachedGeo=g=>{g.userData.cached=true;return g;}; +// Several geometries, each with its own transform, in one: one draw call instead of one per part +function mergeGeos(list){ + if(!list.length)return null; + let nv=0,ni=0;list.forEach(([g])=>{nv+=g.attributes.position.count;ni+=g.index?g.index.count:g.attributes.position.count;}); + const pos=new Float32Array(nv*3),nor=new Float32Array(nv*3),idx=new Uint32Array(ni),v=new THREE.Vector3(),nm=new THREE.Matrix3(); + let vo=0,io=0; + list.forEach(([g,m])=>{const P=g.attributes.position,N=g.attributes.normal;nm.getNormalMatrix(m); + for(let i=0;i{if(x.geometry&&!x.geometry.userData.cached)x.geometry.dispose(); + (x.material?[].concat(x.material):[]).forEach(m=>{if(m.map&&!m.map.userData.cached)m.map.dispose();m.dispose();});});} function roundRect(ctx,x,y,w,h,r){ctx.beginPath();ctx.moveTo(x+r,y);ctx.arcTo(x+w,y,x+w,y+h,r);ctx.arcTo(x+w,y+h,x,y+h,r);ctx.arcTo(x,y+h,x,y,r);ctx.arcTo(x,y,x+w,y,r);ctx.closePath();} +// Label textures are drawn once per text and colours, and shared by every label that shows the same +const LABEL_TEX=new Map(); function tagLabel(text,dot,size=0.58,weight=500,border){ + const key=[text,dot,weight,border,css('--panel'),css('--fg'),css('--line')].join('\u0001'); + let hit=LABEL_TEX.get(key); + if(!hit){ + if(LABEL_TEX.size>4000){LABEL_TEX.forEach(x=>x.tex.dispose());LABEL_TEX.clear();} + hit=drawLabel(text,dot,weight,border);LABEL_TEX.set(key,hit); + } + const s=new THREE.Sprite(new THREE.SpriteMaterial({map:hit.tex,transparent:true,depthTest:false,depthWrite:false})); + s.scale.set(size*hit.w/hit.h,size,1);s.renderOrder=20;return s; +} +function drawLabel(text,dot,weight,border){ const px=40,pd=14,dotW=dot?28:0,c=document.createElement('canvas'),ctx=c.getContext('2d'),font=`${weight} ${px}px "IBM Plex Mono", ui-monospace, monospace`; ctx.font=font;const w=Math.ceil(ctx.measureText(text).width)+pd*2+dotW,h=Math.round(px*1.5); c.width=w;c.height=h;ctx.font=font; @@ -781,10 +829,10 @@ function tagLabel(text,dot,size=0.58,weight=500,border){ ctx.strokeStyle=border||css('--line');ctx.lineWidth=border?5:3;roundRect(ctx,2,2,w-4,h-4,5);ctx.stroke(); if(dot){ctx.fillStyle=dot;ctx.beginPath();ctx.arc(pd+9,h/2,9,0,Math.PI*2);ctx.fill();} ctx.fillStyle=css('--fg');ctx.textBaseline='middle';ctx.fillText(text,pd+dotW,h/2+2); - const tex=new THREE.CanvasTexture(c);tex.minFilter=THREE.LinearFilter; - const s=new THREE.Sprite(new THREE.SpriteMaterial({map:tex,transparent:true,depthTest:false,depthWrite:false})); - s.scale.set(size*w/h,size,1);s.renderOrder=20;return s; + const tex=new THREE.CanvasTexture(c);tex.minFilter=THREE.LinearFilter;tex.userData.cached=true; + return {tex,w,h}; } +const PLACARD_BOX=cachedGeo(new THREE.BoxGeometry(8,2,0.1)),PLACARD_FACE=cachedGeo(new THREE.PlaneGeometry(8,2)); function placard(v,col){ const c=document.createElement('canvas');c.width=1024;c.height=256;const ctx=c.getContext('2d'); ctx.fillStyle=css('--card');ctx.fillRect(0,0,1024,256);ctx.fillStyle=col;ctx.fillRect(0,0,22,256); @@ -797,7 +845,8 @@ function placard(v,col){ const tex=new THREE.CanvasTexture(c);tex.anisotropy=4; const side=new THREE.MeshStandardMaterial({color:new THREE.Color(css('--card')),roughness:0.9}); const face=new THREE.MeshStandardMaterial({map:tex,roughness:0.9}); - const m=new THREE.Mesh(new THREE.BoxGeometry(8,2,0.1),[side,side,side,side,face,side]);m.castShadow=true;return m; + // the box in one material and its printed face on top: two draw calls instead of six + const m=new THREE.Mesh(PLACARD_BOX,side),f=new THREE.Mesh(PLACARD_FACE,face);f.position.z=0.0505;m.add(f);m.castShadow=true;return m; } /* ---- Low-poly models ---- */ @@ -824,16 +873,39 @@ const MODELS={ ap:(g,c,k)=>{C(g,0.05,0.05,0.9,6,k,0,0.45,0);C(g,0.55,0.62,0.16,14,c,0,0.98,0);C(g,0.06,0.06,0.02,8,k,0,1.07,0.3);return 1.06;}, phone:(g,c,k)=>{B(g,0.95,0.1,0.75,c,0,0.05,0);const b=B(g,0.95,0.32,0.6,c,0,0.26,0.02);b.rotation.x=-0.35;const s=B(g,0.5,0.02,0.24,k,0.12,0.43,0.05);s.rotation.x=-0.35;B(g,0.2,0.14,0.72,c,-0.33,0.5,0);return 0.6;} }; +// Each type of equipment is a stack of small boxes and cylinders: merged once per type into two +// geometries (body, details), shared by every asset of that type +const MODEL_GEO=new Map(),SEV_GEO=new Map(); +function modelGeo(type){ + let m=MODEL_GEO.get(type);if(m)return m; + const c=new THREE.MeshBasicMaterial(),k=new THREE.MeshBasicMaterial(),g=new THREE.Group(); + const h=(MODELS[type]||MODELS.server)(g,c,k);g.updateMatrixWorld(true); + const box=new THREE.Box3().setFromObject(g),lc=[],lk=[]; + g.traverse(o=>{if(o.isMesh)(o.material===c?lc:lk).push([o.geometry,o.matrixWorld]);}); + m={c:mergeGeos(lc),k:mergeGeos(lk),h,box,size:box.getSize(new THREE.Vector3())}; + g.traverse(o=>{if(o.isMesh)o.geometry.dispose();});c.dispose();k.dispose(); + if(m.c)cachedGeo(m.c);if(m.k)cachedGeo(m.k);MODEL_GEO.set(type,m);return m; +} +// The vulnerability marks of a model: tape, pin head and flag (one geometry), and the needle +function sevGeo(type,sev){ + const key=type+'|'+sev;let m=SEV_GEO.get(key);if(m)return m; + const G=modelGeo(type),sz=G.size,ph=G.h+0.55+SEV[sev].rank*0.28,px=G.box.max.x+0.35,pz=-0.2,T=(x,y,z)=>new THREE.Matrix4().makeTranslation(x,y,z); + const parts=[[new THREE.BoxGeometry(sz.x+0.5,0.04,sz.z+0.5),T(0,0.02,0)],[new THREE.SphereGeometry(0.21,14,10),T(px,ph,pz)]]; + if(sev==='crit')parts.push([new THREE.BoxGeometry(0.6,0.36,0.025),T(px+0.32,ph-0.42,pz)]); + const needle=new THREE.CylinderGeometry(0.03,0.03,ph,6);needle.translate(px,ph/2,pz); + m={marks:cachedGeo(mergeGeos(parts)),needle:cachedGeo(needle),top:ph};parts.forEach(([g])=>g.dispose()); + SEV_GEO.set(key,m);return m; +} const MINI_H={dc:1.2,server:0.95,hypervisor:0.8,db:0.8,firewall:0.6,gateway:0.55,switch:0.3,workstation:0.55,printer:0.45,plc:0.6,camera:0.6,iot:0.7,ap:0.55,phone:0.3}; function buildBase(spanX,spanZ){ - while(scene.children.length)scene.remove(scene.children[0]); - pickables=[];ANIM=[]; + while(scene.children.length){const o=scene.children[0];scene.remove(o);disposeTree(o);} + pickables=[];ANIM=[];FLOWMESH=null;invalidate(); const BG=css('--bg');scene.background=new THREE.Color(BG);scene.fog=new THREE.Fog(BG,Math.max(90,spanX*1.3),Math.max(200,spanX*3)); // three.js now uses physical light units: ×π gives the brightness the theme values were tuned for scene.add(new THREE.HemisphereLight(0xffffff,0x8a8f8c,(parseFloat(css('--sky'))||0.6)*Math.PI)); sun=new THREE.DirectionalLight(0xffffff,(parseFloat(css('--sun'))||1)*Math.PI); - sun.position.set(-26,80,34);sun.castShadow=true;sun.shadow.mapSize.set(2048,2048); + sun.position.set(-26,80,34);sun.castShadow=!eco();sun.shadow.mapSize.set(2048,2048); const sb=Math.max(42,Math.max(spanX,spanZ)/2+12); Object.assign(sun.shadow.camera,{left:-sb,right:sb,top:sb,bottom:-sb,near:1,far:280});sun.shadow.bias=-0.0006;sun.shadow.normalBias=0.02; scene.add(sun);scene.add(sun.target); @@ -855,7 +927,7 @@ function buildBase(spanX,spanZ){ function thread(curve,vol,colorVar,nseg=1){ const col=new THREE.Color(css(colorVar)); const mat=new THREE.MeshStandardMaterial({color:col,roughness:0.8,transparent:true}); - const tube=new THREE.Mesh(new THREE.TubeGeometry(curve,48*nseg,0.022+Math.log10(1+vol)*0.022,5,false),mat);tube.castShadow=true;scene.add(tube); + const tube=new THREE.Mesh(new THREE.TubeGeometry(curve,(eco()?16:32)*nseg,0.022+Math.log10(1+vol)*0.022,5,false),mat);scene.add(tube); const pm=new THREE.MeshStandardMaterial({color:col,roughness:0.4}),n=Math.min(4,1+Math.round(Math.log10(1+vol)))*nseg,parts=[]; for(let i=0;i{const o=OV.sites[s.id];if(!o)return;const op=sel&&sel!==s?0.22:1;o.mats.forEach(m=>{m.opacity=op;m.depthWrite=op>0.5;});o.label.material.opacity=op>0.5?1:0.45;}); WAN.forEach(w=>{if(!w.tube)return;const vis=state.flows&&(!state.riskOnly||w.risk);let op=w.risk?0.95:0.85;if(sel)op=(w.a===sel.id||w.b===sel.id)?1:0.08; @@ -984,6 +1056,7 @@ function computeLayout(){ }); return {zm,compact,levels,plan,extent:ext,maxD}; } +const POST_GEO=cachedGeo(new THREE.CylinderGeometry(0.22,0.22,1,10)); function buildScene(){ const L=computeLayout(),zm=L.zm,compact=L.compact,plan=L.plan; nLevels=L.levels.length;extent=L.extent;floors={}; @@ -998,25 +1071,20 @@ function buildScene(){ slab.castShadow=slab.receiveShadow=true;slab.userData.plate=v.id;g.add(slab);pickables.push(slab); const pl=placard(v,vc),pk0=compact?0.72:1;pl.scale.setScalar(pk0);pl.position.set(-Wv/2+0.6+4*pk0,SLAB/2+pk0,Dv/2-0.9);g.add(pl); const posts=[]; - [[-1,-1],[1,-1],[-1,1],[1,1]].forEach(([sx,sz])=>{const p=new THREE.Mesh(new THREE.CylinderGeometry(0.22,0.22,1,10),postMat);p.position.set(sx*(Wv/2-0.9),0,sz*(Dv/2-0.9));p.castShadow=true;g.add(p);posts.push(p);}); + [[-1,-1],[1,-1],[-1,1],[1,1]].forEach(([sx,sz])=>{const p=new THREE.Mesh(POST_GEO,postMat);p.position.set(sx*(Wv/2-0.9),0,sz*(Dv/2-0.9));p.castShadow=true;g.add(p);posts.push(p);}); scene.add(g);floors[v.id]={group:g,posts,postMat,slab,level:inf.level,x0:inf.x0,z0:inf.z0||0,w:Wv,D:Dv}; inf.list.forEach(a=>{ const sp=inf.spots[a.id],x=sp.x,z=sp.z; const ag=new THREE.Group();ag.position.set(x,SLAB/2,z); const c=new THREE.MeshStandardMaterial({color:base.card,roughness:0.85,flatShading:true,transparent:true}); const k=new THREE.MeshStandardMaterial({color:base.detail,roughness:0.6,flatShading:true,transparent:true}); - const model=new THREE.Group();const h=(MODELS[a.type]||MODELS.server)(model,c,k);ag.add(model); - const box=new THREE.Box3().setFromObject(model),sz=box.getSize(new THREE.Vector3()); + const G=modelGeo(a.type),h=G.h;ag.add(new THREE.Mesh(G.c,c));if(G.k)ag.add(new THREE.Mesh(G.k,k)); const mats=[c,k];let pinTop=h; if(a.sev!=='none'){ - const sc=new THREE.Color(sevColor(a.sev)); + const sc=new THREE.Color(sevColor(a.sev)),S=sevGeo(a.type,a.sev); const sm=new THREE.MeshStandardMaterial({color:sc,roughness:0.5,transparent:true,emissive:sc,emissiveIntensity:0.12});mats.push(sm); - const tape=new THREE.Mesh(new THREE.BoxGeometry(sz.x+0.5,0.04,sz.z+0.5),sm);tape.position.y=0.02;ag.add(tape); - const ph=h+0.55+SEV[a.sev].rank*0.28,px=box.max.x+0.35,pz=-0.2; - const needle=new THREE.Mesh(new THREE.CylinderGeometry(0.03,0.03,ph,6),steel);needle.position.set(px,ph/2,pz);ag.add(needle); - const head=new THREE.Mesh(new THREE.SphereGeometry(0.21,14,10),sm);head.position.set(px,ph,pz);ag.add(head); - if(a.sev==='crit'){const fl=new THREE.Mesh(new THREE.BoxGeometry(0.6,0.36,0.025),sm);fl.position.set(px+0.32,ph-0.42,pz);ag.add(fl);} - pinTop=ph; + ag.add(new THREE.Mesh(S.marks,sm));ag.add(new THREE.Mesh(S.needle,steel)); + pinTop=S.top; } ag.traverse(o=>{if(o.isMesh){o.castShadow=true;o.receiveShadow=true;o.userData.asset=a;pickables.push(o);}}); if(DIFF&&DIFF.A[a.id]){const r=new THREE.Mesh(new THREE.RingGeometry(1.2,1.42,40).rotateX(-Math.PI/2),new THREE.MeshBasicMaterial({color:new THREE.Color(css(DIFF_COL[DIFF.A[a.id]])),depthWrite:false}));r.position.y=0.05;ag.add(r);} @@ -1062,20 +1130,68 @@ function flowCurve(f){ } return {path,segs}; } +// Every flow of the IS in one geometry (tubes and arrowheads), coloured per vertex with its own opacity, +// drawn as two meshes over the same vertices: the flows clearly shown (they hide what is behind them) and +// the faded ones (they do not). A few draw calls instead of six or more per flow. The moving dots are one +// instanced mesh; the wide invisible tubes that make a flow easy to click are on a layer never drawn. +let FLOWMESH=null; +const PICK_LAYER=1; +function disposeFlows(){ + const F=FLOWMESH;if(!F)return;FLOWMESH=null; + [F.strong,F.faint,F.dots].forEach(m=>{if(m){scene.remove(m);if(m.geometry!==pGeo)m.geometry.dispose();}}); + F.mat.dispose();if(F.dots)F.dots.material.dispose(); + F.picks.forEach(x=>{scene.remove(x);x.geometry.dispose();}); + const gone=new Set(F.picks);pickables=pickables.filter(o=>!gone.has(o)); +} function buildFlows(){ scene.updateMatrixWorld(true); - ANIM=ANIM.filter(o=>!o.owner); + disposeFlows(); + const seg=eco()?16:32,tmp=new THREE.Object3D(),I=new THREE.Matrix4(),items=[],picks=[]; + let nv=0,ni=0,nd=0; FLOWS.forEach(f=>{ - if(f.tube){scene.remove(f.tube);f.tube.geometry.dispose();f.parts.forEach(p=>scene.remove(p));(f.extras||[]).forEach(x=>{scene.remove(x);if(x.geometry!==coneGeo)x.geometry.dispose();});pickables=pickables.filter(o=>o!==f.pick);} - const {path,segs}=flowCurve(f); - const o=thread(path,f.vol,flowColorVar(f),segs.length);o.owner=f; - const extras=segs.map(c=>{const m=new THREE.Mesh(coneGeo,o.mat);const t=Math.max(0.5,1-1.1/Math.max(1,c.getLength()));m.position.copy(c.getPoint(t));m.lookAt(c.getPoint(t).add(c.getTangent(t))); - const k=0.75+Math.log10(1+f.vol)*0.18;m.scale.set(k,k,k);m.castShadow=true;scene.add(m);return m;}); - const pick=new THREE.Mesh(new THREE.TubeGeometry(path,24*segs.length,0.32,4,false),pickMat);pick.userData.flow=f;scene.add(pick);pickables.push(pick); - extras.push(pick); - Object.assign(f,{tube:o.tube,mat:o.mat,parts:o.parts,extras,cones:extras.slice(0,-1),pick}); + const {path,segs}=flowCurve(f),col=new THREE.Color(css(flowColorVar(f))); + const parts=[[new THREE.TubeGeometry(path,seg*segs.length,0.022+Math.log10(1+f.vol)*0.022,5,false),I]]; + const k=0.75+Math.log10(1+f.vol)*0.18; + segs.forEach(c=>{const t=Math.max(0.5,1-1.1/Math.max(1,c.getLength())),pt=c.getPoint(t);tmp.position.copy(pt);tmp.lookAt(pt.clone().add(c.getTangent(t)));tmp.scale.set(k,k,k);tmp.updateMatrix();parts.push([coneGeo,tmp.matrix.clone()]);}); + const g=mergeGeos(parts);parts[0][0].dispose(); + const pick=new THREE.Mesh(new THREE.TubeGeometry(path,12*segs.length,0.32,4,false),pickMat);pick.layers.set(PICK_LAYER);pick.userData.flow=f;scene.add(pick);pickables.push(pick);picks.push(pick); + const n=Math.min(4,1+Math.round(Math.log10(1+f.vol)))*segs.length; + f.fx={v0:nv,v1:nv+g.attributes.position.count,i0:ni,i1:ni+g.index.count,d0:nd,dn:n,curve:path,speed:(0.06+Math.log10(1+f.vol)*0.04)/Math.sqrt(segs.length),t:Array.from({length:n},(_,i)=>i/n),col,op:-1,on:false}; + f.pick=pick; + items.push([f,g]);nv+=g.attributes.position.count;ni+=g.index.count;nd+=n; }); -} + const pos=new Float32Array(nv*3),nor=new Float32Array(nv*3),colA=new Float32Array(nv*4),all=new Uint32Array(ni); + items.forEach(([f,g])=>{const x=f.fx;pos.set(g.attributes.position.array,x.v0*3);nor.set(g.attributes.normal.array,x.v0*3); + const ix=g.index.array;for(let i=0;i{const g=new THREE.BufferGeometry();g.setAttribute('position',P);g.setAttribute('normal',N);g.setAttribute('color',C); + const ix=new THREE.BufferAttribute(new Uint32Array(Math.max(1,ni)),1);ix.setUsage(THREE.DynamicDrawUsage);g.setIndex(ix);g.setDrawRange(0,0); + const m=new THREE.Mesh(g,depth?mat:mat.clone());if(!depth)m.material.depthWrite=false;m.renderOrder=depth?0:1;scene.add(m);return m;}; + const strong=mk(true),faint=mk(false); + if(nv){strong.geometry.computeBoundingSphere();faint.geometry.boundingSphere=strong.geometry.boundingSphere.clone();} + let dots=null; + if(nd){dots=new THREE.InstancedMesh(pGeo,new THREE.MeshStandardMaterial({roughness:0.4}),nd);dots.instanceMatrix.setUsage(THREE.DynamicDrawUsage); + const z=new THREE.Matrix4().makeScale(0,0,0);items.forEach(([f])=>{for(let i=0;i
    diff --git a/tests/e2e.mjs b/tests/e2e.mjs index aa3c04b..8ce8983 100644 --- a/tests/e2e.mjs +++ b/tests/e2e.mjs @@ -378,6 +378,28 @@ test('plates: all or none, empty VLANs hidden, VLANs imported into chosen zones, await admin.click('#cloudReset'); await admin.waitForFunction(async () => !(await (await fetch('api/state')).json()).si.lyon.cloud2d); + // the 3D view is cheap to draw: a few hundred draw calls, nothing drawn while idle, nothing leaked on a rebuild + await admin.click('#viewSeg [data-v="3d"]'); + await admin.click('.vtools [data-vsel="all"]'); + await admin.waitForTimeout(1500); + const rs = () => admin.evaluate(() => window.strataRenderStats()); + const r0 = await rs(); + assert.ok(r0.calls > 20 && r0.calls < 400, `${r0.calls} draw calls for the Lyon model`); + await admin.click('#gfxSeg [data-gfx="eco"]'); + await admin.waitForTimeout(800); + assert.equal((await rs()).mode, 'eco'); + const idle0 = (await rs()).frames; + await admin.waitForTimeout(1500); + assert.ok((await rs()).frames - idle0 <= 1, 'the model is drawn again while nothing changes'); + const geo0 = (await rs()).geometries; + for (const sp of ['wide', 'compact', 'wide', 'compact']) { await admin.click(`#spaceSeg [data-space="${sp}"]`); await admin.waitForTimeout(400); } + await admin.waitForTimeout(800); + assert.ok((await rs()).geometries <= geo0 + 5, `GPU geometries grow on each rebuild (${geo0} → ${(await rs()).geometries})`); + await admin.click('#gfxSeg [data-gfx="quality"]'); + await admin.waitForTimeout(800); + assert.equal((await rs()).mode, 'quality'); + await admin.click('#gfxSeg [data-gfx="auto"]'); + // sign out: back to the sign-in page, and the session no longer opens the API await admin.click('#cart [data-signout]'); await admin.waitForURL(/\/auth\/login/); From 54066d4cd54e10d689a9bb3719e77b052da897c2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 22:00:32 +0000 Subject: [PATCH 06/16] VLAN names no longer flicker on the plates The printed face of a plate's name sign sat half a millimetre in front of its box: on a GPU with a 24-bit depth buffer the two fought for every pixel from the usual viewing distance. The face now always wins the depth test (polygon offset). The view no longer drops below the screen's own resolution while it moves, where text shimmered. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- public/app.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/public/app.js b/public/app.js index 7ec00be..ec865eb 100644 --- a/public/app.js +++ b/public/app.js @@ -757,7 +757,8 @@ const eco=()=>GFX.pref==='eco'||(GFX.pref==='auto'&&(GFX.slow||GFX.soft)); const renderer=new THREE.WebGLRenderer({antialias:!eco()}); try{const gl=renderer.getContext(),x=gl.getExtension('WEBGL_debug_renderer_info');GFX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):'');GFX.soft=/swiftshader|llvmpipe|softpipe|software|basic render/i.test(GFX.gpu);}catch(e){} // Resolution at rest, and while the view moves (a little lower, sharp again as soon as it stops) -const basePR=()=>eco()?1:Math.min(devicePixelRatio,2),movePR=()=>eco()?0.75:Math.min(devicePixelRatio,1.25); +// (never below the screen's own resolution: text drawn below it shimmers while the view moves) +const basePR=()=>eco()?1:Math.min(devicePixelRatio,2),movePR=()=>eco()?1:Math.min(devicePixelRatio,1.5); let curPR=basePR();renderer.setPixelRatio(curPR); // Shadows are drawn again only when something in the model changes, not when the view moves renderer.shadowMap.enabled=!eco();renderer.shadowMap.type=THREE.PCFShadowMap;renderer.shadowMap.autoUpdate=false; @@ -844,9 +845,11 @@ function placard(v,col){ ctx.font='500 58px "IBM Plex Mono", monospace';ctx.fillStyle='#4a5359';ctx.fillText(v.cidr||'',60,210); const tex=new THREE.CanvasTexture(c);tex.anisotropy=4; const side=new THREE.MeshStandardMaterial({color:new THREE.Color(css('--card')),roughness:0.9}); - const face=new THREE.MeshStandardMaterial({map:tex,roughness:0.9}); + // the printed face sits just in front of the box and always wins the depth test against it: drawn + // half a millimetre away it fought the box face for each pixel, and the names flickered from afar + const face=new THREE.MeshStandardMaterial({map:tex,roughness:0.9,polygonOffset:true,polygonOffsetFactor:-4,polygonOffsetUnits:-4}); // the box in one material and its printed face on top: two draw calls instead of six - const m=new THREE.Mesh(PLACARD_BOX,side),f=new THREE.Mesh(PLACARD_FACE,face);f.position.z=0.0505;m.add(f);m.castShadow=true;return m; + const m=new THREE.Mesh(PLACARD_BOX,side),f=new THREE.Mesh(PLACARD_FACE,face);f.position.z=0.06;m.add(f);m.castShadow=true;return m; } /* ---- Low-poly models ---- */ From 53167c7505c9fabe8b97a2f8de2e4684d6bd3f48 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 22:15:53 +0000 Subject: [PATCH 07/16] CSV VLANs module, and modules that ask for a choice before an import - Module API (backward compatible): file.pick in the manifest, an optional preview(cfg, ctx) that lists what a file holds, ctx.picks in run() with what people chose and the zone of each, POST /api/connectors/{id}/preview, picks on POST /api/connectors/{id}/run - The import window of such a module: tick, filter, choose a zone per item or for all the ticked ones (a zone of the IS, one named by the file, or a new one); the result is a proposal - modules/csv-vlans in the store: VLANs from a CSV (number, name, range, any order, header or not), new ones ticked, those already in the IS updated when ticked - MODULES.md, README, API reference, CHANGELOG; tests for the module and the manifest checks Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 2 + MODULES.md | 28 +++++ README.md | 3 +- docs/api.html | 3 +- docs/modules/csv-vlans-1.0.0.tgz | Bin 0 -> 5229 bytes docs/modules/index.json | 16 +++ modules/csv-vlans/README.md | 38 +++++++ modules/csv-vlans/index.mjs | 151 +++++++++++++++++++++++++++ modules/csv-vlans/strata-module.json | 31 ++++++ public/app.js | 68 +++++++++++- server/connectors/host.js | 6 +- server/connectors/index.js | 48 ++++++++- server/connectors/modules.js | 14 ++- server/server.js | 15 ++- tests/modules.test.mjs | 53 ++++++++++ 15 files changed, 465 insertions(+), 11 deletions(-) create mode 100644 docs/modules/csv-vlans-1.0.0.tgz create mode 100644 modules/csv-vlans/README.md create mode 100644 modules/csv-vlans/index.mjs create mode 100644 modules/csv-vlans/strata-module.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd64fd..6e1ab18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. +- **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. +- **Modules can ask for a choice before an import** (`file.pick` in the manifest, a `preview(cfg, ctx)` function, `ctx.picks` in `run`, `POST /api/connectors/{id}/preview`): Strata shows what the file holds, people tick what to bring and, with `pick.zones`, the zone of each. Backward compatible: modules without `pick` work as before. See MODULES.md. - **Graphics for small laptops** (*Display › Graphics*: Auto, Quality, Economy, and *Animated flows*). Economy: screen resolution, no shadows, no moving dots, labels hidden sooner from afar. Auto, the default, is Quality until moving the view turns out slow on this computer (then Economy, remembered by the browser), and Economy when the browser draws without a graphics card. - **API**: `POST /auth/login` (the form, or JSON `{user, password}`); `analysis.js` exports `arrangeGrid` and `vlansFromFile`; `window.strataRenderStats()` in the page gives what the last frame cost. diff --git a/MODULES.md b/MODULES.md index a0ed480..f703f0c 100644 --- a/MODULES.md +++ b/MODULES.md @@ -30,6 +30,7 @@ has; administrators also see a *Module store* tab. | phpIPAM | Built-in | Sections, subnets, VLANs, addresses | Approvers | | Palo Alto | Built-in | Allow rules, as flows: from the XML API, or a configuration file | Approvers; any contributor for a file | | CSV inventory | Store | Assets of an IS from a spreadsheet saved as CSV | Any contributor (file import) | +| CSV VLANs | Store | VLANs of an IS (number, name, range): you choose the VLANs and the zone of each | Any contributor (file import) | | Nmap scan | Store | Hosts, systems and open ports from `nmap -oX` | Any contributor (file import) | | NetBox | Store | Devices, virtual machines, VLANs and prefixes | Approvers | @@ -378,6 +379,32 @@ export async function run(cfg, ctx) { } ``` +### Letting people choose before an import: `file.pick` and `preview()` + +Some files hold more than people want to bring: a list of every VLAN of the company, a whole CMDB +export. Add `pick` to `file` in the manifest, and export `preview(cfg, ctx)`: + +```json +"file": { "label": "Import VLANs from a CSV", "fields": ["site"], "role": "contributor", + "pick": { "title": "Choose the VLANs and their zone", "zones": true, "help": "Tick the VLANs to bring…" } } +``` + +1. People choose the file and the settings of `file.fields`, then *Next*: Strata calls + `preview(cfg, ctx)` (with `ctx.file`, and the map if the module reads it). Nothing is recorded. +2. `preview` returns `{items, summary, skipped}`. Each item: `key` (unique, given back in `ctx.picks`), + `label`, `detail`, `tags` and `warn` (short notes, shown as badges), `on` (ticked at first; default + `true`), `zone` (with `pick.zones`: the name of the zone suggested for it, an existing one or one to + create). At most 5000 items. +3. Strata shows the items: tick, filter, and with `pick.zones` choose a zone of the IS for each, or for + all the ticked ones at once (an existing zone, a suggested one, or a new one). `pick.zones` needs a + setting of type `site` in `file.fields`: the IS whose zones are offered. +4. *Import* runs `run(cfg, ctx)` with `ctx.picks`, and the result is a proposal, as usual. + +From a script: `POST /api/connectors/{id}/preview` with `{file, overrides}` answers `{items, summary, +skipped, pick}`; `POST /api/connectors/{id}/run` takes `picks` next to `file`. A Strata older than 0.11 +ignores `pick` and runs the import with `ctx.picks` undefined: handle that case as "no choice". +[`modules/csv-vlans`](modules/csv-vlans) is the example. + `test(cfg, ctx)` checks the access and returns a sentence for the person who clicked *Test the connection*. `run(cfg, ctx)` reads and returns `{proposals, summary, report}`: @@ -398,6 +425,7 @@ connection*. `run(cfg, ctx)` reads and returns `{proposals, summary, report}`: |---|---| | `ctx.state` | The map, `{si, assets, wan, services}`, when the manifest asks for it (`permissions.map`), `null` otherwise. A copy: changing it changes nothing. | | `ctx.file` | `{name, text}` for a file import, `null` for a sync. | +| `ctx.picks` | For a module with `file.pick`: what people chose after the preview, `{key: {zone}}` (the keys of the items chosen; `zone` with `pick.zones`: a zone id of the IS, or `"new:"` for a zone to create). `null` when no choice was made (a script that left `picks` out, or a sync): then do what the settings say. | | `ctx.sdk` | The helpers below. | | `ctx.log(...)` | A line in the server log, prefixed with the module id. | diff --git a/README.md b/README.md index 55d8c59..d3764f2 100644 --- a/README.md +++ b/README.md @@ -521,7 +521,8 @@ Every route needs the same credentials as the interface (an API token as `Author | `GET /api/connectors` | Connectors, settings (secrets masked) and latest syncs | | `PUT /api/connectors/{id}` | A connector's settings (admin); `id`: `cyberwatch`, `glpi`, `phpipam`, `paloalto`, or a module's id | | `POST /api/connectors/{id}/test` | Tests access (approver) | -| `POST /api/connectors/{id}/run` | Runs a sync (approver); `{"wait": true}` waits for the result. `{"file": {"name", "text"}, "overrides": {…}}` imports a file, for the modules that do (Palo Alto, CSV inventory: contributor); `overrides`: the settings the module lets one import change. The older Palo Alto form `{"xml", "fileName"}` still works | +| `POST /api/connectors/{id}/preview` | For a module that asks for a choice before its import (CSV VLANs): `{"file", "overrides"}` → the items of the file to choose from (`{items, summary, skipped, pick}`); nothing is recorded | +| `POST /api/connectors/{id}/run` | Runs a sync (approver); `{"wait": true}` waits for the result. With a choice: `"picks": {"": {"zone": ""}}` next to `file`. `{"file": {"name", "text"}, "overrides": {…}}` imports a file, for the modules that do (Palo Alto, CSV inventory: contributor); `overrides`: the settings the module lets one import change. The older Palo Alto form `{"xml", "fileName"}` still works | | `GET /api/modules` | The module store: what the registry offers, and what is installed (admin; `?fresh=1` reads the registry again) | | `POST /api/modules/{id}/install` | Installs or updates a module from the store (admin) | | `POST /api/modules/upload` | Installs a package: `{"data": "<.tgz in base64>"}` (admin, with `MODULE_UPLOAD=true`) | diff --git a/docs/api.html b/docs/api.html index e818284..2e106ae 100644 --- a/docs/api.html +++ b/docs/api.html @@ -178,7 +178,8 @@

    Connectors

    GET /api/connectorscontributor→ {connectors, canConfigure, canRun, store, problems?}: each connector with its fields, settings (secrets masked) and last run. A contributor sees only the file imports open to contributors. PUT /api/connectors/{id}adminthe settings → {ok, connector}. A masked secret sent back unchanged keeps the stored one. POST /api/connectors/{id}/testapprover→ {ok, message}. A failed test is 200 with ok: false and what to check. - POST /api/connectors/{id}/runapprover; contributor for a file import that allows it{wait?, file?: {name, text}, overrides?} → 202 {ok, started}, or with wait: true 200 {ok, run}. file imports a file instead of calling the tool; overrides: the settings the module lets one import change. 409 if already running. The older Palo Alto form {xml, fileName} still works. + POST /api/connectors/{id}/previewas its file importFor a module whose manifest has file.pick: {file: {name, text}, overrides?} → {ok, items, summary, skipped, pick}, the items of the file to choose from (key, label, detail, tags, warn, on, zone). Nothing is recorded. 400 for a module without file.pick. + POST /api/connectors/{id}/runapprover; contributor for a file import that allows it{wait?, file?: {name, text}, overrides?, picks?}. picks, after a preview: {key: {zone?}}, the items chosen and their zone (a zone id of the IS, or "new:Name"); the module reads it as ctx.picks. → 202 {ok, started}, or with wait: true 200 {ok, run}. file imports a file instead of calling the tool; overrides: the settings the module lets one import change. 409 if already running. The older Palo Alto form {xml, fileName} still works.

    A run never writes the map: it opens proposals (one per IS), which an approver reviews. run is {id, at, by, file, ok, summary, error, proposals: [{id, title, siteId}], ms}.

    diff --git a/docs/modules/csv-vlans-1.0.0.tgz b/docs/modules/csv-vlans-1.0.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..880ce6f3f829b286da02c333a9190b59b32f628f GIT binary patch literal 5229 zcmV-z6q4&7iwFP!000021MNHQjvKdj`K_loRu|UHdNh*kO^S}9Z0(J6>(*WeS>N0s zyPJwTLrKhzNO?$UC2IxbLD~nnPf(!!QK0ut@&bK@;vACmu^)CWdSSE|7O*=r9P*sQ z!}Co}s#o1r&ZO>r_x$PCub#*A?6bf0uix+Yzc@IcYro&Wc|G`iF!1-beJ~gt3_gSY zXCL9W)P@zG^*;)(|AW83fuob};ro|Qk97hJPE5fJgH%b$Q=^ zAf!>CB!?F#Fe%g=80ba8S*B;48_;aUGhiA)N3g%2&Xm$ZPT^`M(wT$7WCmu&;T^(J z6W}bJ?eE8}H!}_e*UQ|1(7<3(sD;ujPe3X&Ln!Ht$tl;MBmtPS1uUfzIWP%qu_)Av z7r@e#FN_9Z;#O-A!~T)Vb9SK$W`vUP>o5NfM}CspK6*aT(~J2+6~;BVIUb#Szn>7! z7bhkC6Or?{^*Hvd%Y`p`6*5!(JySs39U z!;6S;5fga};1<^+PzBzzJg0YdxC3WE$&};L`XQG7$hSx~=NSlDQc2(#B638;xyX=jF><&?@KC8hkourmoH8L{DpuU;4R!JQU{T<&Y4MP z9H;P&v$`a95ow;~e>zhlMZsH$^iq!@=Mw{JX~GEjiWeI@^_o#a`MxOBaym1SX9Z2h zcW4o<5%65w#{G;>7}{s-w(ntSZVbAJT1TT6)Mx07!fl2l!-ztbE6H0+nWJd$TOQVo z=&h0aNVbwOpyZX)PFk(;cznV1to8F-=t}5z3wA}bTmwZpajI9)*D zaX}Mkm%Z8%7``?M?5%eZpam;L#&rT)Ee!U|H%hj5Q44@roxryJq~C8Ru*Eqi(3bov zdC4x?y9nA(`h$DkJf6@qj{O0vPJwI+^$Ig+GhD+0Zvss|6Uo3Kw(xBeYTFDOe`JwwAf4d}A?;_U{(B!c? zaE8VnxIALG~S!O6E@|ImFYQZ6;`zR0*VVj_5vz|-G-`?C8a?srwu zB_(O~dcP@MfIxE&uU>!s`Z`bkq+7dAHn1s z7rurO=_XW>e0>WIc3u5i#PzI2r`dCo0B41*CMUh$<3hY7TuG2Si;J`roYBaxsRjsY@_P)iKd{o^8Mwue|i*}EZ8 z6-4(AUpaEDpFZv26<<;WUEc6=uD1O=YVC$CT-uXGj=>=cH3kn>=n8eEZHNYMmx>~7 z!7*(L+H43HZ4#z-n>-M+UBXgEHD9oTp=wQ+NMD9%C?B=1r_hFi?G|5?q)wJHr3`n$ z3eAu7DnLR)5)UG2gi0&q)V9Y3U*s(1LGP#k{*V9cMbPPlLtGD%E)3>jMB`$OQww@$ z#o?LkMKB0Y`{z|p%@;6&)6NiCb4Cjnb~wIBC>mIhlZfVgh+2=2^b|qL^BhLd=|u1rMlfNy<~R&l8emre zBj^u7z?XJ57W)vyqeqrE9!2WrdnyPofS5q=HiRwRq%azdpwsU72HO=L!Qj09is*Sn zkA!0gI~x{P)cF?QH60=aM#v?;D^Zsi;o(C!O?>Wz1PUWqOD4gp257!OItq0aFP3^1 z;K*=T@yVmOF|2bYJ8*ps{;BBPnE6>jgxs3;ne2piqBl|En%RazUD;>1gm)8D@uLOQ zPS~;W0ZkmqLmYMm@0#RIj0mLQW?4x43Wc&jQ7hG3W+-0Q`LYf}++eT{^9&R7fg=I+AQ=~gc1 z&UceBkI05bt`Km=3b{MCZj;jfE2hiT^p@$uK;e6gNlCpT(AlL`(uNgi8n~g8ln1oL zUO3DQ#SQCNI=hU^g5UF^P({#rQ%ixVCKKOb{1D!0-+gLSjYC z+QMEBgAG^939!6~6$uWR!YV9WN_7<`HfXZ-9b_R@cYIw(7+V`GYYMh>Q&t^jOxLWg zrW~_Lbw7g7`yo2c%*L}0sa7E+HLWiL5<|oz<)tcz@D=pqFKE@> zAP_?btTN(%~eC1il%OjwEK;Bi0Pjl(KF z@N;v6P94qXb>434ia9LxabOj9SDGD4#$8yWFU4ZPGylXn`Uafy2{!@ZN0{+M70+2Z z3xXm7K|)@SXmCczSa@WSQh^oSf+xBnx0;dXxToDwV%_TaC4$`BBIF(J%hXwHgVS0i zeQ)a=N`=U|yo#ewu&{m&9kRlmT^Nr;@AK-x5ON@%F^%MfA0mrr+C#szvqjga^f-3U z#V!F_Hlg8?FU(zN2_?%iLRmL1P610@e9+K`LugDIdz?4@DI^rGqGz=6P$lsQ?%_jt zfRasK?mNs^s67m6gq^TWgUBKy=0Y2#vW}zD7BZ>d7E0%w)HL|L6l0IOC=MIe89R(? zj2=fWlf(a2&o9u9+nkEfQ{w$z^w{GA=noKR6<)xOCcdj80n35k7783Y8NRaR`r^dz z7-=83F};y#E%CHPlpviUTE(P?tMjlnUKl>t&K&Eicin>XHHL?5#p5YA0YSm-ob`{2EIVJL z*hQ1cG{>)-4>n{zZ888=Mii31)vaE48Ff094)br8p#BRSVHp~chErk=ecjt3fY!VpnZR_@IM*<#b3S#!QrPvZfBecdl z@(UYIyX`MdU~j91B^UF1r(?T#>`c|&x?C*fpQXnbs(9Ep^;(PUFRju9%Fq47*`Cn8 zKJ7Pis`_){Ysa{%|2u5`@7c+P;H!# z7@KaTdKJQ9y<_5n4ND>wwCw`g;&ucDCs7zx@s;(kxVwvkVHY8je{|$pXjree9^+y| z8{{IRgCy?Bo`#@}&oA-*tF}F6VqetinU$yU8_HqQJc}?F8V?hdGeD%zHpkYYSwrT{ zCnjOf?7^+&|mf;xL^rr*_zG?9--3 zD)@LRK#I>baORQRr64#tj<_K;cbb_fGAW`j1rpNtZ>zrKu$)6yZ|88<{Y*#rDtz?oS6D zl$H*KMgApC=b$V}l|+>y5Zu#qSi^L45t{DO(N@BC`@wF!)s5JzgG)TfC+ux)*ByEp zw7qo~R6DYDQ_lTq3YUm1HP7O4cry+5EWK;a<-1xA;rZ@oVV`RqCezqqz7OopE)ADo zG4ZsY6==JV6o#RjL z;7;Owhmq)jOTH<&RUQcPh7mN|fL7%+Z8Z0wnk_D%9sPVQG>)O1naw#C>Tm8gRLbl^ z-PaT19D>4YYzw+8cP(SbcTLIT>q@U(2xPaNwh+fL236Kh+2GHHSe0p_YD!#Yz&0Q& zb2xL(Qf;Z)lEyUMMj3+hRe78$z{!-Sc$#QpIlb{49cGuu zG}qDQ{fz~~cpZas7uCC^4x4M;Sm3+OYa?}V=LF%`U;YJ~5c2NVU;gz*6~xtgY*l|; zq9h|!u12Ho<|1w6(|w^fYUtk9o1QU-*{-sG$v4`XnXnv{;zou$1=rK;}U0ZA)KWvUyAnoERKnz*Or~6)<-WKul|l0bj1B zftznMUJ|%0Re6zQ7lcT~g+cUL>fz0{>GUlGxCEheyUVH~4x7JJ{8e zLPtZ?s{DRr9onw-*K_`-I{#(Q`*m%G6yv$UkM#Y=lm3&N=f9pjdGfcP&VPN3AL?=2 zF8wQ7`E~*Wl=HU8+6lBv`DjxkYAZIEr@(A8p1&4K;+p{;%eJjGmBu&KsV`aka0DSW z&we!zeuZUbUey4~LtS@$L}GC>Iy#GJPgE{4_#Nk$j@aBDb;EbJPPo|~Gd1T6Hl;59 zIP6rI+43Sz)x1X#37}J&o+&u*&6$urt3I_8t`_12%PcsYfNjF=*ixRmw9N}%P=>3d z#)HAwZ_)N=mD}z#R6DV!NMPq?wBo*r8ql)hXtG^xD#0PDdp>GuTN~H5$Iq}M9)ONw zvo;ZyHF|U$+V&|FL2hFNREUeEQH6hHPc^j@IBlc-Z%5E>MxWSHODNjT(6(nh$0@d7 zOS{0Za9d3g!(jh>}%><34;6o(F`)xKn^so0s8mm+$xg*Ni1l&DIUZxsdcwb?9qy}SR^$MMJFsG#1K&l3J0ZY6D#`ZpJ00xZ`) zZhPOS3U#tBl*(&b&{X4!th$oaY5KAJFhsg)fekW2;`u>mP8wfj nRNsPyO_JJeT8{I-syOG<@6+$o@6+$oulf5Qar#4006+i$FSipZ literal 0 HcmV?d00001 diff --git a/docs/modules/index.json b/docs/modules/index.json index db4e5d5..a36c76e 100644 --- a/docs/modules/index.json +++ b/docs/modules/index.json @@ -17,6 +17,22 @@ "url": "csv-inventory-1.0.0.tgz", "sha256": "a2c1bb73afd5102c7e91ce84046e4b9d96e1931bfdf2856af73f885708642fab" }, + { + "id": "csv-vlans", + "name": "CSV VLANs", + "version": "1.0.0", + "description": "VLANs of an IS from a CSV (number, name, address range): choose the VLANs to bring and the zone of each", + "author": "Solid Geeks", + "license": "AGPL-3.0-or-later", + "homepage": "https://github.com/SolidGeeks/strata/tree/main/modules/csv-vlans", + "badge": "VLAN", + "permissions": { + "map": true, + "network": false + }, + "url": "csv-vlans-1.0.0.tgz", + "sha256": "166097ebd979206b10d22831e0b70045b2861f101b1d0975e9d2ec736be783ef" + }, { "id": "netbox", "name": "NetBox", diff --git a/modules/csv-vlans/README.md b/modules/csv-vlans/README.md new file mode 100644 index 0000000..2d84f69 --- /dev/null +++ b/modules/csv-vlans/README.md @@ -0,0 +1,38 @@ +# CSV VLANs: a Strata connector module + +Brings VLANs into one IS from a spreadsheet saved as CSV, **choosing which VLANs and the zone of each**. +The result is a proposal: nothing changes on the map until an approver accepts it. + +1. *Collaboration › Connectors › CSV VLANs › Import VLANs from a CSV*: the IS and the file. +2. Strata lists the VLANs of the file: tick those to bring (the new ones are ticked), filter, and choose + the zone of each, or of all the ticked ones at once: a zone of the IS, a zone named in the file, or a + new one. +3. *Import*: the proposal holds the IS with those VLANs; an approver reviews it in *Proposals*. + +| Column (any order, any case, English or French) | Meaning | +|---|---| +| `number`, `vlan`, `vid`, `numéro` (required) | VLAN number, 1 to 4094 (`VLAN 10` is read as 10) | +| `name`, `nom`, `description` | VLAN name (`VLAN ` when empty) | +| `cidr`, `subnet`, `network`, `réseau`, `range` | Address range, such as `10.20.0.0/24` | +| `zone`, `group` (optional) | The zone suggested for the VLAN; created when the IS has none of that name | + +Without a header line, the columns are told apart by what they hold: ranges, numbers, and names, in any +order (`10.20.0.0/24;Servers;20` works). Commas, semicolons or tabs. + +A VLAN already in the IS (same number) starts unticked; ticked, its name, range and zone are updated. The +same file imported again gives the same proposal, and none when the IS already matches. + +From a script, without a choice (`picks` left out), every VLAN of the file is brought, in the zone the file +gives, else the *default zone* of the settings, else the top zone; those already in the IS are left alone +unless *update the VLANs already in the IS* is on. With a choice: + +```bash +jq -n --rawfile x vlans.csv '{wait: true, file: {name: "vlans.csv", text: $x}, overrides: {site: "lyon"}, + picks: {"300": {zone: "new:Lab"}, "301": {zone: "new:Lab"}, "20": {}}}' | + curl -sS -H "Authorization: Bearer $STRATA_TOKEN" -H 'Content-Type: application/json' -H 'X-Requested-With: strata' \ + -X POST https://strata.example.lan/api/connectors/csv-vlans/run --data-binary @- +``` + +`POST /api/connectors/csv-vlans/preview` with the same `file` and `overrides` answers the VLANs of the +file, as the import window shows them. Needs Strata 0.11 or later for the choice; an older Strata +imports every VLAN of the file. diff --git a/modules/csv-vlans/index.mjs b/modules/csv-vlans/index.mjs new file mode 100644 index 0000000..c281f86 --- /dev/null +++ b/modules/csv-vlans/index.mjs @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +// CSV VLANs: a Strata connector module (see MODULES.md). It reads the VLANs of a CSV file (number, name, +// address range, and optionally a zone), lets people choose which ones to bring into an IS and the zone of +// each (preview, then the import with ctx.picks), and proposes the IS with those VLANs. Nothing changes on +// the map until an approver accepts the proposal. It needs no network. + +const COLUMNS = { + num: ['vlan', 'vlan id', 'vlanid', 'vid', 'vlan number', 'number', 'num', 'numero', 'n', 'no', 'tag'], + name: ['name', 'nom', 'vlan name', 'description', 'desc', 'label', 'libelle'], + cidr: ['cidr', 'subnet', 'network', 'prefix', 'range', 'reseau', 'sous reseau', 'plage', 'ip range', 'address range', 'prefixe', 'adresse'], + zone: ['zone', 'group', 'groupe', 'vlan group'], +}; + +// Splits CSV text into rows, with quoted fields ("a; b" and "" for a quote) and the separator guessed +// from the first line +export function parseCsv(text) { + text = String(text).replace(/^/, ''); + const head = text.split(/\r?\n/, 1)[0]; + const sep = [';', '\t', ','].map(c => [c, head.split(c).length]).sort((a, b) => b[1] - a[1])[0][0]; + const rows = []; let row = [], cell = '', q = false; + for (let i = 0; i < text.length; i++) { + const c = text[i]; + if (q) { if (c === '"') { if (text[i + 1] === '"') { cell += '"'; i++; } else q = false; } else cell += c; continue; } + if (c === '"' && cell === '') q = true; + else if (c === sep) { row.push(cell); cell = ''; } + else if (c === '\n' || c === '\r') { if (c === '\r' && text[i + 1] === '\n') i++; row.push(cell); rows.push(row); row = []; cell = ''; } + else cell += c; + } + if (cell !== '' || row.length) { row.push(cell); rows.push(row); } + return rows.map(r => r.map(x => x.trim())).filter(r => r.some(Boolean)); +} + +const vlanNum = v => { const m = /^\s*(?:vlan\s*)?(\d{1,4})\s*$/i.exec(String(v ?? '')); const n = m ? +m[1] : NaN; return n >= 1 && n <= 4094 ? n : null; }; +const looksCidr = v => /^\d{1,3}(\.\d{1,3}){3}(\/\d{1,2})?$/.test(String(v || '').trim()); + +// The VLANs of the file: [{num, name, cidr, zone, line, warn}], and what was left out +export function readVlans(text, sdk) { + const rows = parseCsv(text); + if (!rows.length) throw new sdk.ConnectorError('The file is empty.'); + const head = rows[0].map(h => sdk.norm(h)), col = {}; + for (const [k, names] of Object.entries(COLUMNS)) { const i = head.findIndex(h => names.includes(h)); if (i >= 0) col[k] = i; } + let body = rows.slice(1), first = 2; + if (col.num === undefined) { + // no header (or one we do not know): each column is told by what it holds, in the order the file has + const sample = rows.slice(0, 50), width = Math.max(...sample.map(r => r.length)); + const share = (i, test) => sample.filter(r => r[i] && test(r[i])).length / Math.max(1, sample.filter(r => r[i]).length); + for (let i = 0; i < width; i++) { + if (col.cidr === undefined && share(i, looksCidr) > 0.6) col.cidr = i; + else if (col.num === undefined && share(i, x => vlanNum(x) !== null) > 0.6) col.num = i; + else if (col.name === undefined && share(i, x => !looksCidr(x) && vlanNum(x) === null) > 0.6) col.name = i; + } + if (col.num === undefined) throw new sdk.ConnectorError(`No column of VLAN numbers (1 to 4094) in this file. First line: ${rows[0].join(', ').slice(0, 120)}`); + if (vlanNum(rows[0][col.num]) !== null) { body = rows; first = 1; } + } + const vlans = [], skipped = [], seen = new Set(); + body.forEach((r, i) => { + const line = i + first, num = vlanNum(r[col.num]); + if (num === null) { skipped.push(`Line ${line}: no VLAN number from 1 to 4094 (${String(r[col.num] ?? '').slice(0, 30) || 'empty'}).`); return; } + if (seen.has(num)) { skipped.push(`Line ${line}: VLAN ${num} again, left out (the first one is kept).`); return; } + seen.add(num); + const cidr = col.cidr !== undefined ? String(r[col.cidr] || '').trim() : '', warn = []; + if (cidr && !sdk.parseRange(cidr)) warn.push('range not understood'); + vlans.push({ num, name: (col.name !== undefined ? String(r[col.name] || '').trim() : '').slice(0, 50) || `VLAN ${num}`, cidr: cidr.slice(0, 60), zone: (col.zone !== undefined ? String(r[col.zone] || '').trim() : '').slice(0, 40), line, warn }); + }); + if (!vlans.length) throw new sdk.ConnectorError('No VLAN with a number from 1 to 4094 in this file.'); + return { vlans, skipped }; +} + +const zonesOf = si => (Array.isArray(si.zones) && si.zones.length ? si.zones : [{ id: 'z-def', name: 'Main zone' }]); + +// Before the import: the VLANs of the file, ticked when new, with the zone the file or the IS suggests +export async function preview(cfg, ctx) { + const { state, sdk } = ctx, si = state.si[cfg.site]; + if (!si) throw new sdk.ConnectorError('Choose the IS the VLANs go to.'); + const { vlans, skipped } = readVlans(ctx.file.text, sdk), zones = zonesOf(si); + const have = new Map((si.vlans || []).map(v => [+v.num, v])); + const items = vlans.map(v => { + const ex = have.get(v.num), exZone = ex && zones.find(z => z.id === ex.zone); + const same = ex && ex.name === v.name && (!v.cidr || ex.cidr === v.cidr); + return { + key: String(v.num), label: `VLAN ${v.num} · ${v.name}`, detail: v.cidr, + tags: ex ? [same ? 'already in the IS, the same' : 'already in the IS: ticked, it is updated'] : [], + warn: v.warn, on: !ex, zone: v.zone || (exZone ? exZone.name : ''), + }; + }); + const n = items.filter(i => i.on).length; + return { items, skipped, summary: `${vlans.length} VLAN(s) in ${ctx.file.name}, ${n} not yet in IS ${si.name || cfg.site}.` }; +} + +export async function run(cfg, ctx) { + const { state, sdk } = ctx, sid = cfg.site, si = state.si[sid]; + if (!si) throw new sdk.ConnectorError('Choose the IS the VLANs go to.'); + if (!ctx.file) throw new sdk.ConnectorError('This module imports a CSV file: choose one.'); + const { vlans, skipped } = readVlans(ctx.file.text, sdk); + const work = JSON.parse(JSON.stringify(si)); + work.zones = JSON.parse(JSON.stringify(zonesOf(si))); + work.vlans = Array.isArray(work.vlans) ? work.vlans : []; + const picks = ctx.picks, report = { skipped, warnings: [] }, newZones = []; + // a zone by its id, or by its name ("new:", or a name from the file or the settings): created when missing + const zoneByName = name => { + let z = work.zones.find(x => sdk.norm(x.name) === sdk.norm(name)); + if (!z) { + let id = `z-csv-${sdk.slug(name)}`; + for (let i = 2; work.zones.some(x => x.id === id); i++) id = `z-csv-${sdk.slug(name)}-${i}`; + z = { id, name: String(name).slice(0, 40) }; + work.zones.push(z); newZones.push(z.name); + } + return z.id; + }; + const zoneOf = (choice, fileZone, current) => { + if (choice) { + if (choice.startsWith('new:') && choice.slice(4).trim()) return zoneByName(choice.slice(4).trim()); + if (work.zones.some(z => z.id === choice)) return choice; + report.warnings.push(`Zone ${choice} is not in the IS: the zone of the file or the default one is used.`); + } + if (fileZone) return zoneByName(fileZone); + if (current) return current; + if (cfg.defaultZone) return zoneByName(cfg.defaultZone); + return work.zones[work.zones.length - 1].id; + }; + let added = 0, updated = 0, same = 0; + for (const v of vlans) { + const pick = picks ? picks[String(v.num)] : null; + if (picks && !pick) continue; + const i = work.vlans.findIndex(x => +x.num === v.num), ex = i >= 0 ? work.vlans[i] : null; + // without a choice (a script), a VLAN already in the IS is only updated when the settings say so + if (ex && !picks && !cfg.updateExisting) { same++; continue; } + const zone = zoneOf(pick && pick.zone, v.zone, ex && ex.zone); + if (ex) { + const next = { ...ex, name: v.name, zone }; + if (v.cidr && !v.warn.length) next.cidr = v.cidr; + if (next.zone !== ex.zone) delete next.place; + if (JSON.stringify(next) === JSON.stringify(ex)) { same++; continue; } + work.vlans[i] = next; updated++; + } else { + let id = `v-csv-${sdk.slug(sid)}-${v.num}`; + for (let k = 2; work.vlans.some(x => x.id === id); k++) id = `v-csv-${sdk.slug(sid)}-${v.num}-${k}`; + work.vlans.push({ id, num: v.num, name: v.name, cidr: v.warn.length ? '' : v.cidr, zone }); + if (v.warn.length) report.warnings.push(`VLAN ${v.num}: range “${v.cidr}” not understood, left empty.`); + added++; + } + } + const summary = `${vlans.length} VLAN(s) read${picks ? `, ${Object.keys(picks).length} chosen` : ''}: ${added} to add, ${updated} to update${same ? `, ${same} unchanged` : ''}${newZones.length ? `; new zone(s): ${newZones.join(', ')}` : ''}.`; + ctx.log(summary); + const changed = added || updated; + return { + proposals: changed ? [{ siteId: sid, title: `VLANs from ${ctx.file.name}`, description: summary + (skipped.length ? ` ${skipped.length} line(s) left out.` : ''), changes: { si: { [sid]: work }, assets: {} } }] : [], + summary, + report, + }; +} diff --git a/modules/csv-vlans/strata-module.json b/modules/csv-vlans/strata-module.json new file mode 100644 index 0000000..f5c20d3 --- /dev/null +++ b/modules/csv-vlans/strata-module.json @@ -0,0 +1,31 @@ +{ + "strataModule": 1, + "id": "csv-vlans", + "name": "CSV VLANs", + "version": "1.0.0", + "description": "VLANs of an IS from a CSV (number, name, address range): choose the VLANs to bring and the zone of each", + "author": "Solid Geeks", + "license": "AGPL-3.0-or-later", + "homepage": "https://github.com/SolidGeeks/strata/tree/main/modules/csv-vlans", + "badge": "VLAN", + "main": "index.mjs", + "permissions": { "map": true, "network": false }, + "live": false, + "file": { + "label": "Import VLANs from a CSV", + "accept": ".csv,.txt,text/csv", + "role": "contributor", + "fields": ["site", "updateExisting", "defaultZone"], + "help": "One VLAN per line: its number, its name and its address range (CIDR), in any order, with or without a header (number, vlan or vid; name or nom; cidr, subnet, network or réseau; optionally zone). Separated by commas, semicolons or tabs.", + "pick": { + "title": "Choose the VLANs and their zone", + "zones": true, + "help": "Tick the VLANs to bring into the IS and choose the zone of each, or of all the ticked ones at once. A VLAN already in the IS (same number) is updated when ticked." + } + }, + "fields": [ + { "k": "site", "label": "IS the VLANs go to", "type": "site", "required": true }, + { "k": "updateExisting", "label": "Without a choice (from a script): also update the VLANs already in the IS", "type": "bool", "default": false }, + { "k": "defaultZone", "label": "Zone when none is chosen nor given by the file (its name; empty: the top zone of the IS)", "default": "" } + ] +} diff --git a/public/app.js b/public/app.js index ec865eb..e728b4c 100644 --- a/public/app.js +++ b/public/app.js @@ -3465,6 +3465,11 @@ function fileImportForm(c){ const overrides=connValues(),miss=ff.find(x=>x.required&&!String(overrides[x.k]||'').trim());if(miss)return formErr(`Fill in “${miss.label}”.`,'cf_'+miss.k); const text=await f.text(); const btn=det.querySelector('.form-foot [type=submit]');btn.disabled=true;btn.textContent='Reading the file…'; + // a module that asks for a choice first: what the file holds, then the choice, then the import + if(c.file.pick){ + try{const P=await Busy.track(()=>Store.api('POST',`api/connectors/${c.id}/preview`,{file:{name:f.name,text},overrides}),'Reading the file');onSave=null;return pickForm(c,P,{name:f.name,text},overrides);} + catch(e){btn.disabled=false;btn.textContent='Next';return formErr(e.message);} + } try{const j=await Store.api('POST',`api/connectors/${c.id}/run`,{file:{name:f.name,text},wait:true,overrides}); onSave=null;const r=j.run; if(!r.ok){btn.disabled=false;btn.textContent='Import';return formErr(r.error);} @@ -3472,9 +3477,70 @@ function fileImportForm(c){ if(r.proposals.length)openProposal(r.proposals[0].id);else showConnectors(); }catch(e){btn.disabled=false;btn.textContent='Import';formErr(e.message);} },{cancel:()=>showConnectors()}); - det.querySelector('.form-foot [type=submit]').textContent='Import'; + det.querySelector('.form-foot [type=submit]').textContent=c.file.pick?'Next':'Import'; connWireSite(c); } +// The choice a module asks for before its import (file.pick): tick the items of the file and, when the +// module offers zones, choose the zone of the IS each one goes to, one by one or all the ticked at once +function pickForm(c,P,file,overrides){ + const pk=P.pick||{},S=siteById[overrides.site],zoned=!!(pk.zones&&S); + const zones=zoned?S.zones.map(z=>({id:z.id,name:z.name})):[]; + const zByName=n=>zones.find(z=>z.name.toLowerCase()===String(n).trim().toLowerCase()); + const top=zones.length?zones[zones.length-1].id:''; + let extra=[...new Set(P.items.map(i=>i.zone).filter(z=>z&&!zByName(z)))]; + const rows=P.items.map((it,i)=>Object.assign({},it,{i,zoneSel:it.zone?(zByName(it.zone)?zByName(it.zone).id:'new:'+it.zone):top})); + const zOpts=(sel,bulk)=>[...zones.slice().reverse().map(z=>[z.id,z.name]),...extra.map(n=>['new:'+n,`New zone: ${n}`]),...(bulk?[['__new','New zone…']]:[])] + .map(([v,t])=>``).join(''); + const row=r=>` + ${esc(r.label)}${r.detail?`${esc(r.detail)}`:''}${r.tags.map(t=>`${esc(t)}`).join('')}${r.warn.map(t=>`${esc(t)}`).join('')} + ${zoned?``:''}`; + openForm(pk.title||'Choose what to import',c.name,` +

    ${esc(file.name)}${S?` · IS ${esc(S.name)}`:''}${P.summary?` · ${esc(P.summary)}`:''}

    + ${pk.help?`

    ${esc(pk.help)}

    `:''} + ${P.skipped&&P.skipped.length?`
    ${plural(P.skipped.length,'line')} left out
      ${P.skipped.map(x=>`
    • ${esc(x)}
    • `).join('')}
    `:''} +
    +
    + ${zoned?`
    +
    `:''} +
    ${zoned?'':''}${rows.map(row).join('')}
    ItemZone
    +

    +

    The result is a proposal: nothing changes on the map until it is approved.

    `, + async()=>{ + const chosen=rows.filter(r=>r.on);if(!chosen.length)return formErr('Tick at least one item.'); + const picks={};chosen.forEach(r=>{picks[r.key]=zoned?{zone:r.zoneSel}:{};}); + const btn=det.querySelector('.form-foot [type=submit]');btn.disabled=true;btn.textContent='Importing…'; + try{const j=await Busy.track(()=>Store.api('POST',`api/connectors/${c.id}/run`,{file,wait:true,overrides,picks}),'Importing'); + onSave=null;const r=j.run; + if(!r.ok){btn.disabled=false;btn.textContent='Import';return formErr(r.error);} + toast(r.proposals.length?'Imported: review the proposal.':'Nothing new: the map already matches your choice.'); + if(r.proposals.length)openProposal(r.proposals[0].id);else showConnectors(); + }catch(e){btn.disabled=false;btn.textContent='Import';formErr(e.message);} + },{cancel:()=>showConnectors()}); + det.querySelector('.form-foot [type=submit]').textContent='Import'; + const body=$('#viBody'),frm=$('#frm'),first=new Set(rows.filter(r=>r.on).map(r=>r.i)); + const count=()=>{$('#viCount').textContent=`${rows.filter(r=>r.on).length} of ${rows.length} ticked`;}; + const paint=()=>{const q=$('#viFilter').value.trim().toLowerCase(); + body.querySelectorAll('tr[data-vi-row]').forEach(tr=>{const r=rows[+tr.dataset.viRow];tr.hidden=!!q&&!`${r.label} ${r.detail} ${r.zone} ${r.tags.join(' ')}`.toLowerCase().includes(q); + tr.classList.toggle('on',r.on);tr.querySelector('[data-vi-on]').checked=r.on;});count();}; + frm.addEventListener('change',e=>{const tr=e.target.closest('tr[data-vi-row]');if(!tr)return;const r=rows[+tr.dataset.viRow]; + if(e.target.matches('[data-vi-on]')){r.on=e.target.checked;tr.classList.toggle('on',r.on);count();} + if(e.target.matches('[data-vi-zone]'))r.zoneSel=e.target.value;}); + if(zoned)$('#viZone').addEventListener('change',e=>{$('#viZoneNewBox').hidden=e.target.value!=='__new';}); + $('#viFilter').addEventListener('input',paint); + frm.addEventListener('click',e=>{const b=e.target.closest('[data-vi]');if(!b)return;const k=b.dataset.vi; + const vis=[...body.querySelectorAll('tr[data-vi-row]')].filter(tr=>!tr.hidden).map(tr=>rows[+tr.dataset.viRow]); + if(k==='all'||k==='none'||k==='new'){vis.forEach(r=>r.on=k==='all'?true:k==='new'?first.has(r.i):false);paint();return;} + if(k==='apply'){let sel=$('#viZone').value; + if(sel==='__new'){const n=$('#viZoneNew').value.trim();if(!n)return formErr('Name the new zone.','viZoneNew'); + const z=zByName(n);sel=z?z.id:'new:'+n;if(!z&&!extra.includes(n))extra.push(n);} + const t=rows.filter(r=>r.on);if(!t.length)return formErr('Tick the items to put in this zone first.'); + t.forEach(r=>r.zoneSel=sel);$('#ferr').hidden=true; + body.innerHTML=rows.map(row).join('');paint(); + $('#viZone').innerHTML=zOpts(sel,true);$('#viZoneNewBox').hidden=true;$('#viZoneNew').value=''; + toast(`${plural(t.length,'item')} set to zone ${sel.startsWith('new:')?sel.slice(4):(zones.find(z=>z.id===sel)||{}).name}.`);} + }); + count(); +} det.addEventListener('change',async e=>{ if(e.target.id!=='modFile'||!e.target.files[0])return; const f=e.target.files[0]; diff --git a/server/connectors/host.js b/server/connectors/host.js index 56920d3..842e842 100644 --- a/server/connectors/host.js +++ b/server/connectors/host.js @@ -2,20 +2,20 @@ // Runs one connector module operation in its own process, started by runner.js with the Node.js // permission model: this process reads only its module's folder and the SDK, cannot write files or start // other processes, and its environment holds none of the server's settings or passwords. -// Messages: in {op: 'test' | 'run' | 'exports', main, cfg, state, file}; out {log} while it works, then +// Messages: in {op: 'test' | 'preview' | 'run' | 'exports', main, cfg, state, file, picks}; out {log} while it works, then // {ok: true, result} or {ok: false, error, expected}. import { pathToFileURL } from 'node:url'; import * as sdk from './sdk.js'; const send = m => new Promise(res => process.send(m, () => res())); -process.once('message', async ({ op, main, cfg, state, file }) => { +process.once('message', async ({ op, main, cfg, state, file, picks }) => { try { const mod = await import(pathToFileURL(main).href); // the functions the module offers, to check a package (scripts/check-registry.mjs) if (op === 'exports') { await send({ ok: true, result: Object.keys(mod).filter(k => typeof mod[k] === 'function').sort() }); process.exit(0); } const fn = mod[op]; if (typeof fn !== 'function') throw new sdk.ConnectorError(`This module has no ${op}() function.`); - const ctx = { state, file: file || null, sdk, log: (...a) => { process.send({ log: a.map(String).join(' ').slice(0, 2000) }); } }; + const ctx = { state, file: file || null, picks: picks || null, sdk, log: (...a) => { process.send({ log: a.map(String).join(' ').slice(0, 2000) }); } }; const result = await fn(cfg, ctx); await send({ ok: true, result: result === undefined ? null : JSON.parse(JSON.stringify(result)) }); } catch (e) { diff --git a/server/connectors/index.js b/server/connectors/index.js index db4aef3..28d03b0 100644 --- a/server/connectors/index.js +++ b/server/connectors/index.js @@ -41,6 +41,37 @@ function ownSite(changes, siteId, state) { return out; } +// The choices made after a preview, as the module receives them in ctx.picks: {key: {zone}}, the key of +// each item chosen and, when the module offers zones, the zone id of the IS or "new:" +export function cleanPicks(p) { + if (p === undefined || p === null) return null; + const list = Array.isArray(p) ? p.map(k => [k, {}]) : p && typeof p === 'object' ? Object.entries(p) : null; + if (!list) throw new ConnectorError('picks must be a list of keys or {key: {zone}}'); + if (list.length > 5000) throw new ConnectorError('At most 5000 items can be chosen in one import.'); + const out = Object.create(null); + for (const [k, v] of list) { + if (typeof k !== 'string' || !k || k.length > 200 || ['__proto__', 'constructor', 'prototype'].includes(k)) continue; + const o = {}; + if (v && typeof v === 'object' && typeof v.zone === 'string' && v.zone.trim()) o.zone = v.zone.trim().slice(0, 120); + out[k] = o; + } + return { ...out }; +} +// What a preview may answer: the items of the file people choose from +function cleanPreview(r) { + if (!r || typeof r !== 'object' || !Array.isArray(r.items)) throw new Error('the module answered its preview without a list of items'); + const seen = new Set(), items = [], s = (v, n) => (typeof v === 'string' || typeof v === 'number' ? String(v).slice(0, n) : ''); + for (const x of r.items.slice(0, 5000)) { + if (!x || typeof x !== 'object') continue; + const key = s(x.key, 200); + if (!key || seen.has(key)) continue; + seen.add(key); + items.push({ key, label: s(x.label, 200) || key, detail: s(x.detail, 300), tags: Array.isArray(x.tags) ? x.tags.map(t => s(t, 40)).filter(Boolean).slice(0, 6) : [], + warn: Array.isArray(x.warn) ? x.warn.map(t => s(t, 80)).filter(Boolean).slice(0, 4) : [], on: x.on !== false, zone: s(x.zone, 80), group: s(x.group, 80) }); + } + return { items, summary: s(r.summary, 500), skipped: Array.isArray(r.skipped) ? r.skipped.map(t => s(t, 200)).filter(Boolean).slice(0, 40) : [] }; +} + // opts: dataDir, providedDir (MODULES_DIR), installedDir (DATA_DIR/modules), strataVersion, timeoutMs, // and the server's getState, upsertProposal, log, writeJson, broadcast export function createConnectors({ dataDir, providedDir, installedDir, strataVersion, timeoutMs, getState, upsertProposal, log, writeJson, broadcast }) { @@ -122,7 +153,19 @@ export function createConnectors({ dataDir, providedDir, installedDir, strataVer finally { testing.delete(id); } return typeof r === 'string' ? r : r && typeof r.message === 'string' ? r.message : 'Connected.'; } - // Starts a run; resolves when it is finished. opts: {by, file: {name, text}, overrides} + // Reads a file for a module whose import starts with a choice (file.pick): returns the items to choose + // from. Nothing is recorded: the import itself is the run that follows, with the picks. + async function preview(id, { file, overrides } = {}) { + if (!known(id)) throw new ConnectorError('Unknown connector'); + const m = mod(id); + if (!m.file || !m.file.pick) throw new ConnectorError(`${m.name} has no choice to make before an import.`); + if (!file) throw new ConnectorError('Choose the file.'); + const cfg = effective(id, fileOverrides(id, overrides)); + const state = m.permissions.map ? JSON.parse(JSON.stringify(getState())) : null; + const r = await runModule(m, 'preview', { cfg, state, file: { name: String(file.name || 'file').slice(0, 120), text: String(file.text) } }, { timeoutMs: Math.min(timeoutMs, 5 * 60000), log }); + return cleanPreview(r); + } + // Starts a run; resolves when it is finished. opts: {by, file: {name, text}, overrides, picks} async function run(id, opts = {}) { if (!known(id)) throw new ConnectorError('Unknown connector'); const m = mod(id); @@ -142,6 +185,7 @@ export function createConnectors({ dataDir, providedDir, installedDir, strataVer const res = await runModule(m, 'run', { cfg, state: m.permissions.map ? state : null, file: opts.file ? { name: fileName, text: String(opts.file.text) } : null, + picks: opts.file && m.file.pick ? cleanPicks(opts.picks) : null, }, { timeoutMs, log }) || {}; if (res.proposals !== undefined && !Array.isArray(res.proposals)) throw new Error('the module answered without a list of proposals'); const sites = Array.isArray(cfg.sites) && cfg.sites.length ? new Set(cfg.sites) : null, rejected = []; @@ -195,7 +239,7 @@ export function createConnectors({ dataDir, providedDir, installedDir, strataVer list: () => ({ connectors: [...MODULES.keys()].map(publicView), runs: store.runs.slice(0, 30), problems }), get: id => (known(id) ? publicView(id) : null), module: id => (known(id) ? mod(id) : null), - update, test, run, reload, forget, + update, test, preview, run, reload, forget, isRunning: id => running.has(id) || testing.has(id), exists: known, }; diff --git a/server/connectors/modules.js b/server/connectors/modules.js index 201b4ba..b51a347 100644 --- a/server/connectors/modules.js +++ b/server/connectors/modules.js @@ -96,7 +96,19 @@ export function checkManifest(m, { strataVersion } = {}) { if (f.help !== undefined && !str(f.help, 1200)) throw new Error('file.help must be text'); if (f.role !== undefined && !['contributor', 'approver'].includes(f.role)) throw new Error('file.role must be contributor or approver'); if (f.fields !== undefined && !(Array.isArray(f.fields) && f.fields.every(k => keys.has(k)))) throw new Error('file.fields must list some of its settings'); - file = { label: f.label, accept: f.accept || '', help: f.help || '', role: f.role || 'approver', fields: f.fields || [] }; + // pick: before the import, the module reads the file (preview) and people choose what to bring, + // and, with zones: true, the zone of the IS each chosen item goes to + let pick = null; + if (f.pick !== undefined) { + const p = f.pick; + if (!p || typeof p !== 'object' || Array.isArray(p)) throw new Error('file.pick must be an object'); + if (p.title !== undefined && !str(p.title, 80)) throw new Error('file.pick.title must be text (80 characters at most)'); + if (p.help !== undefined && !str(p.help, 600)) throw new Error('file.pick.help must be text'); + if (p.zones !== undefined && typeof p.zones !== 'boolean') throw new Error('file.pick.zones must be true or false'); + if (p.zones && !fields.some(x => x.type === 'site' && (f.fields || []).includes(x.k))) throw new Error('file.pick.zones needs a setting of type site in file.fields (the IS whose zones are offered)'); + pick = { title: p.title || 'Choose what to import', help: p.help || '', zones: !!p.zones }; + } + file = { label: f.label, accept: f.accept || '', help: f.help || '', role: f.role || 'approver', fields: f.fields || [], pick }; } if (live === false && !file) throw new Error('a module with live: false must import files'); return { diff --git a/server/server.js b/server/server.js index c783c00..2154bf4 100644 --- a/server/server.js +++ b/server/server.js @@ -9,7 +9,7 @@ import fsp from 'node:fs/promises'; import path from 'node:path'; import crypto from 'node:crypto'; import { fileURLToPath } from 'node:url'; -import { createConnectors } from './connectors/index.js'; +import { createConnectors, cleanPicks } from './connectors/index.js'; import { createStore } from './connectors/store.js'; import { ID_RE as MODULE_ID_RE } from './connectors/modules.js'; import { ssoConfig, createSso, loginPage } from './sso.js'; @@ -761,6 +761,15 @@ async function api(req, res, parts, url, auth) { try { return send(res, 200, { ok: true, message: await connectors.test(id) }); } catch (e) { return send(res, 200, { ok: false, message: e.constructor.name === 'ConnectorError' ? e.message : `Unexpected error: ${e.message}` }); } } + // A module whose file import starts with a choice: reads the file and answers what there is to choose + if (r3 === 'preview' && method === 'POST') { + const b = await readBody(req, CONFIG.maxImportBytes) || {}, m = connectors.module(id); + if (!m.file || !m.file.pick) throw httpError(400, `${m.name} has no choice to make before an import.`); + need(auth, m.file.role === 'contributor' ? 'contributor' : 'approver'); + if (!isObj(b.file) || typeof b.file.text !== 'string' || !b.file.text.length) throw httpError(400, 'Choose the file.'); + try { return send(res, 200, { ok: true, pick: m.file.pick, ...(await connectors.preview(id, { file: b.file, overrides: isObj(b.overrides) ? b.overrides : {} })) }); } + catch (e) { if (e.constructor.name === 'ConnectorError') throw httpError(400, e.message); log('ERROR connector preview', id, e.stack || e.message); throw httpError(500, `Unexpected error: ${e.message}`); } + } if (r3 === 'run' && method === 'POST') { const b = await readBody(req, CONFIG.maxImportBytes) || {}; // {file: {name, text}}; the Palo Alto import also took {xml, fileName}, kept for scripts written for it @@ -772,7 +781,9 @@ async function api(req, res, parts, url, auth) { need(auth, f && m.file.role === 'contributor' ? 'contributor' : 'approver'); if (connectors.isRunning(id)) throw httpError(409, 'This connector is already running.'); if (!f && !connectors.get(id).live) throw httpError(400, m.live === false ? `${m.name} only imports files.` : 'Fill in its settings first.'); - const p = connectors.run(id, { by: auth.user || 'admin', file: f || undefined, overrides: isObj(b.overrides) ? b.overrides : {} }); + let picks; + try { picks = cleanPicks(b.picks); } catch (e) { throw httpError(400, e.message); } + const p = connectors.run(id, { by: auth.user || 'admin', file: f || undefined, overrides: isObj(b.overrides) ? b.overrides : {}, picks }); if (b.wait) return send(res, 200, { ok: true, run: await p }); p.catch(() => { }); return send(res, 202, { ok: true, started: true }); diff --git a/tests/modules.test.mjs b/tests/modules.test.mjs index e531be0..39602b2 100644 --- a/tests/modules.test.mjs +++ b/tests/modules.test.mjs @@ -284,6 +284,54 @@ test('store modules: an Nmap scan imported by a contributor, NetBox synced by an for (const id of ['nmap', 'netbox']) assert.equal((await api('admin', 'DELETE', `/api/modules/${id}`, {})).status, 200); }); +test('CSV VLANs: the VLANs of a file, a choice of VLANs and zones, then a proposal', async () => { + assert.equal((await api('admin', 'POST', '/api/modules/csv-vlans/install', {})).status, 200); + const c = (await api('alice', 'GET', '/api/connectors')).data.connectors.find(x => x.id === 'csv-vlans'); + assert.deepEqual(c.file.pick, { title: 'Choose the VLANs and their zone', help: c.file.pick.help, zones: true }); + // no header, columns in the order range, name, number; VLAN 20 is already in Lyon; a line without number + const file = { name: 'vlans.csv', text: '10.30.0.0/24;Lab A;300\n10.30.1.0/24;Lab B;301\n10.20.0.0/24;Servers (renamed);20\nbad;line;x\n10.40.0.0/24;Cameras;400\n' }; + const pv = await api('alice', 'POST', '/api/connectors/csv-vlans/preview', { file, overrides: { site: 'lyon' } }); + assert.equal(pv.status, 200, JSON.stringify(pv.data)); + assert.deepEqual(pv.data.items.map(i => [i.key, i.label, i.detail, i.on]), [ + ['300', 'VLAN 300 · Lab A', '10.30.0.0/24', true], ['301', 'VLAN 301 · Lab B', '10.30.1.0/24', true], + ['20', 'VLAN 20 · Servers (renamed)', '10.20.0.0/24', false], ['400', 'VLAN 400 · Cameras', '10.40.0.0/24', true]]); + assert.match(pv.data.items[2].tags[0], /already in the IS/); + assert.equal(pv.data.items[2].zone, 'Main', 'a VLAN already in the IS suggests its own zone'); + assert.match(pv.data.skipped[0], /Line 4: no VLAN number/); + assert.equal(pv.data.pick.zones, true); + // a preview records no run and opens no proposal + assert.equal((await api('admin', 'GET', '/api/proposals')).data.proposals.filter(p => p.connector === 'csv-vlans').length, 0); + + // the choice: 300 and 301 in a new zone Lab, 20 updated where it is, 400 left out + const picks = { 300: { zone: 'new:Lab' }, 301: { zone: 'new:Lab' }, 20: { zone: 'z1' } }; + const r = await api('alice', 'POST', '/api/connectors/csv-vlans/run', { wait: true, file, overrides: { site: 'lyon' }, picks }); + assert.equal(r.data.run.ok, true, r.data.run.error); + assert.match(r.data.run.summary, /4 VLAN\(s\) read, 3 chosen: 2 to add, 1 to update; new zone\(s\): Lab/); + const p = (await api('admin', 'GET', `/api/proposals/${r.data.run.proposals[0].id}`)).data.proposal; + const si = p.changes.si.lyon, lab = si.zones.find(z => z.name === 'Lab'); + assert.ok(lab, 'the new zone is in the proposal'); + assert.deepEqual(si.vlans.filter(v => v.zone === lab.id).map(v => v.num).sort(), [300, 301]); + assert.equal(si.vlans.find(v => v.num === 20).name, 'Servers (renamed)'); + assert.equal(si.vlans.find(v => v.num === 20).id, 'v20', 'an updated VLAN keeps its id, and so its assets'); + assert.ok(!si.vlans.some(v => v.num === 400), 'a VLAN left unticked is not brought'); + assert.equal(p.uploadedBy, 'alice'); + + // the same choice again refreshes the same proposal; from a script, without a choice, every new VLAN comes + const again = await api('alice', 'POST', '/api/connectors/csv-vlans/run', { wait: true, file, overrides: { site: 'lyon' }, picks }); + assert.equal(again.data.run.proposals[0].id, p.id); + await api('admin', 'PUT', '/api/connectors/csv-vlans', { defaultZone: 'Imported' }); + const all = await api('alice', 'POST', '/api/connectors/csv-vlans/run', { wait: true, file, overrides: { site: 'lyon' } }); + assert.match(all.data.run.summary, /4 VLAN\(s\) read: 3 to add, 0 to update, 1 unchanged; new zone\(s\): Imported/); + + // what is refused + assert.equal((await api('alice', 'POST', '/api/connectors/csv-vlans/preview', { file: { name: 'x.csv', text: 'name;range\nx;10.0.0.0/8' }, overrides: { site: 'lyon' } })).status, 400); + assert.equal((await api('alice', 'POST', '/api/connectors/csv-vlans/preview', { overrides: { site: 'lyon' } })).status, 400); + assert.equal((await api('alice', 'POST', '/api/connectors/csv-vlans/run', { wait: true, file, overrides: { site: 'lyon' }, picks: 'all' })).status, 400); + assert.equal((await api('bob', 'POST', '/api/connectors/csv-inventory/preview', { file, overrides: { site: 'lyon' } })).status, 404, 'csv-inventory is not installed'); + for (const pr of (await api('admin', 'GET', '/api/proposals')).data.proposals.filter(x => x.connector === 'csv-vlans')) await api('admin', 'DELETE', `/api/proposals/${pr.id}`, {}); + assert.equal((await api('admin', 'DELETE', '/api/modules/csv-vlans', {})).status, 200); +}); + test('the store refuses a package that does not match its checksum', async () => { tamper = true; try { @@ -324,6 +372,11 @@ test('package reader and manifest checks', () => { assert.throws(() => checkManifest({ ...man, permissions: { files: true } }), /permissions/); assert.throws(() => checkManifest({ ...man, live: false }), /must import files/); assert.throws(() => checkManifest({ ...man, minStrata: '99.0.0' }, { strataVersion: '0.8.0' }), /needs Strata 99/); + const fileOf = pick => ({ ...man, live: false, fields: [{ k: 'site', label: 'IS', type: 'site' }], file: { label: 'Import', fields: ['site'], pick } }); + assert.deepEqual(checkManifest(fileOf({ zones: true })).file.pick, { title: 'Choose what to import', help: '', zones: true }); + assert.equal(checkManifest({ ...man, live: false, file: { label: 'Import' } }).file.pick, null); + assert.throws(() => checkManifest(fileOf('yes')), /file.pick must be an object/); + assert.throws(() => checkManifest({ ...fileOf({ zones: true }), file: { label: 'Import', pick: { zones: true } } }), /needs a setting of type site/); assert.ok(compareVersions('0.10.0', '0.9.1') > 0 && compareVersions('1.0.0-rc.1', '1.0.0') < 0 && compareVersions('1.2.3', '1.2.3') === 0); // the top folder goes, whatever its name; a link inside a package is refused const tgz = writeTgz([{ path: 'strata-module.json', data: Buffer.from('{}') }, { path: 'lib/a.mjs', data: Buffer.from('') }], 'anything'); From 4b3f1d64258515897ff9432a28ce2d6fb044146d Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:01:24 +0000 Subject: [PATCH 08/16] Suggest zones from the flows, the Internet on top MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Layout › Suggest zones: each VLAN on the floor of its distance from the Internet along the flows (exposed, reached from the exposed zone, back end), the VLANs the Internet never reaches below by the role of their assets (internal servers, users, industrial, network and administration), empty VLANs left where they are. Existing zones holding most of the same VLANs keep their name. A review window, top first: rename zones, move VLANs, then Apply (approvers) or Propose (contributors: a draft proposal). StrataAnalysis.suggestZones, with tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 1 + README.md | 2 ++ public/analysis.js | 71 ++++++++++++++++++++++++++++++++++++++++- public/app.css | 8 +++++ public/app.js | 43 +++++++++++++++++++++++++ public/index.html | 4 +++ tests/analysis.test.mjs | 42 ++++++++++++++++++++++++ tests/e2e.mjs | 17 +++++++--- 8 files changed, 183 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e1ab18..18e5c22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. +- **Suggest zones** (*Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). - **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. - **Modules can ask for a choice before an import** (`file.pick` in the manifest, a `preview(cfg, ctx)` function, `ctx.picks` in `run`, `POST /api/connectors/{id}/preview`): Strata shows what the file holds, people tick what to bring and, with `pick.zones`, the zone of each. Backward compatible: modules without `pick` work as before. See MODULES.md. - **Graphics for small laptops** (*Display › Graphics*: Auto, Quality, Economy, and *Animated flows*). Economy: screen resolution, no shadows, no moving dots, labels hidden sooner from afar. Auto, the default, is Quality until moving the view turns out slow on this computer (then Economy, remembered by the browser), and Economy when the browser draws without a graphics card. diff --git a/README.md b/README.md index d3764f2..f793588 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,8 @@ The server is idle between requests: CPU is spent on writes (each one saves the | … 0.11, Quality | 397 | 400 000 | 2.8 | only those of the moving dots | | … 0.11, Economy | 365 | 134 000 | 21 | 2 | +**Laying out a large IS.** *Layout › Suggest zones* proposes zones from the flows, the Internet on top (distance from the Internet along the flows, then the role of the assets for what the Internet never reaches); *Arrange VLANs* then puts each zone on rows and columns. Both are reviewed or can be undone (*History*). + Without a GPU (a virtual desktop, a remote session), Auto chooses Economy; for very large IS, the 2D diagram and *Plates › None*, then the VLANs you need, stay the lightest. --- diff --git a/public/analysis.js b/public/analysis.js index f8c8ffb..9d8f587 100644 --- a/public/analysis.js +++ b/public/analysis.js @@ -332,6 +332,75 @@ function vlansFromFile(text,fileName=''){ return {kind,items,skipped}; } +/* ---- Zones suggested from the flows ---- */ +// The floors of an IS, the Internet on top: each VLAN goes to the floor of its depth, the number of flow +// hops from the Internet in the direction of the traffic (1: reached straight from the Internet), and the +// VLANs never reached from it go below, by the role of their assets (servers, users, industrial, +// network and administration). Empty VLANs stay in their zone. Existing zones that hold most of the same +// VLANs are kept, with their name and id. +// input: {vlans: [{id, num, name, zone}], assets: [{id, vlan, type}], flows: [{src, dst}], +// zones: [{id, name}] (bottom first, as stored), sources: ids that stand for the Internet (default +// 'internet' and every 'svc:…')} +// returns {zones: [{id, name, key, vlans: [ids], existing}] bottom first, byVlan: {vlanId: {zone, depth, role, +// moved}}, depth: {vlanId: n|null}} +const ROLE_OF={server:'srv',dc:'srv',db:'srv',hypervisor:'srv',workstation:'users',phone:'users',printer:'users',ap:'users', + plc:'ot',camera:'ot',iot:'ot',switch:'net',router:'net',firewall:'net',gateway:'net'}; +const ROLE_NAME={srv:'Internal servers',users:'Users',ot:'Industrial (OT)',net:'Network and administration',other:'Other'}; +// bottom to top below the floors reached from the Internet +const ROLE_ORDER=['net','ot','users','other','srv']; +function suggestZones({vlans,assets,flows,zones,sources}){ + const isNet=id=>sources?sources.includes(id):(id==='internet'||String(id).startsWith('svc:')); + const vOf={};assets.forEach(a=>{vOf[a.id]=a.vlan;}); + // hops from the Internet, following the flows from source to destination + const out={};flows.forEach(f=>{(out[f.src]=out[f.src]||[]).push(f.dst);}); + const dist={},q=[]; + Object.keys(out).filter(isNet).forEach(id=>{dist[id]=0;q.push(id);}); + while(q.length){const n=q.shift();(out[n]||[]).forEach(m=>{if(dist[m]===undefined&&!isNet(m)){dist[m]=dist[n]+1;q.push(m);}});} + const depth={},role={},count={},types={}; + vlans.forEach(v=>{depth[v.id]=null;count[v.id]={};types[v.id]={};}); + assets.forEach(a=>{if(!(a.vlan in depth))return;const d=dist[a.id];if(d!==undefined&&(depth[a.vlan]===null||d{const C=count[v.id],c=Object.entries(C).sort((x,y)=>y[1]-x[1]||ROLE_ORDER.indexOf(y[0])-ROLE_ORDER.indexOf(x[0])); + const n=c.reduce((t,x)=>t+x[1],0),ot=C.ot||0,name=v.name||'',plcs=types[v.id].plc||0; + if(!n)role[v.id]=null; + else if(plcs||(ot&&OT_NAME.test(name)))role[v.id]='ot'; + else if(USER_NAME.test(name)&&((C.users||0)+ot)*2>=n)role[v.id]='users'; + else if(ot*3>=n)role[v.id]='ot'; + else role[v.id]=c[0][0];}); + // the floors, bottom first + const maxD=Math.max(0,...vlans.map(v=>depth[v.id]||0)),deep=Math.min(maxD,3); + const floors=[...ROLE_ORDER.map(r=>({key:'role:'+r,name:ROLE_NAME[r]})), + ...[3,2,1].filter(d=>d<=deep).map(d=>({key:'depth:'+d,name:d===1?'Exposed to the Internet':d===2?'Reached from the exposed zone':'Back end, 3 hops or more'}))]; + const keyOf=v=>{if(role[v.id]===null)return null;const d=depth[v.id];if(d!==null)return 'depth:'+Math.min(d,3);return 'role:'+role[v.id];}; + const want={};vlans.forEach(v=>{const k=keyOf(v);if(k)(want[k]=want[k]||[]).push(v.id);}); + // keep an existing zone for the floor that holds most of its VLANs (and at least half of the floor) + const cur={};zones.forEach(z=>{cur[z.id]=new Set(vlans.filter(v=>v.zone===z.id).map(v=>v.id));}); + // (and that floor holds at least half of the zone's own VLANs with assets: a catch-all zone is not reused) + const full=z=>[...cur[z]].filter(id=>role[id]!==null).length; + const pairs=[];floors.forEach(f=>{const ids=want[f.key]||[];zones.forEach(z=>{const n=ids.filter(id=>cur[z.id].has(id)).length;if(n&&n*2>=ids.length&&n*2>=full(z.id))pairs.push({f:f.key,z:z.id,n});});}); + pairs.sort((a,b)=>b.n-a.n); + const zoneFor={},taken=new Set();pairs.forEach(p=>{if(!zoneFor[p.f]&&!taken.has(p.z)){zoneFor[p.f]=p.z;taken.add(p.z);}}); + const usedIds=new Set(zones.map(z=>z.id)); + const newId=key=>{let id='z-'+key.replace(/[^a-z0-9]+/g,'-');for(let i=2;usedIds.has(id);i++)id='z-'+key.replace(/[^a-z0-9]+/g,'-')+'-'+i;usedIds.add(id);return id;}; + const outZones=[],byVlan={}; + floors.forEach(f=>{const ids=want[f.key]||[];if(!ids.length)return; + const z=zoneFor[f.key],ex=z?zones.find(x=>x.id===z):null; + outZones.push({id:ex?ex.id:newId(f.key),name:ex?ex.name:f.name,key:f.key,vlans:ids.slice(),existing:!!ex,suggested:f.name});}); + // empty VLANs keep their zone: kept zones stay where they were, below the floors + const stay=vlans.filter(v=>role[v.id]===null); + const kept=new Set(outZones.map(z=>z.id)); + zones.forEach(z=>{const ids=stay.filter(v=>v.zone===z.id).map(v=>v.id);if(!ids.length)return; + const hit=outZones.find(o=>o.id===z.id);if(hit){hit.vlans.push(...ids);return;} + outZones.unshift({id:z.id,name:z.name,key:'keep:'+z.id,vlans:ids,existing:true,suggested:z.name});kept.add(z.id);}); + stay.filter(v=>!zones.some(z=>z.id===v.zone)).forEach(v=>{const z=outZones[0];if(z)z.vlans.push(v.id);}); + outZones.forEach(z=>z.vlans.forEach(id=>{const v=vlans.find(x=>x.id===id);byVlan[id]={zone:z.id,depth:depth[id],role:role[id],moved:v.zone!==z.id};})); + return {zones:outZones,byVlan,depth}; +} + /* ---- Arranging the plates of a level on rows and columns ---- */ // plates: [{id, w, D}] in their current order; weight(a, b): how much two VLANs talk (symmetric, >= 0); // cols: the most plates per row (0 or less: about the square root of their number, a little wider than deep); @@ -362,6 +431,6 @@ function arrangeGrid(plates,weight,cols,gap=2){ return {places,cols:C,rows:R,order:grid.map(p=>p.id)}; } -root.StrataAnalysis={arrangeGrid,vlansFromFile,riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, +root.StrataAnalysis={suggestZones,arrangeGrid,vlansFromFile,riskScore,riskPriorities,vulnRisk,likelihoodOf,inCidr,nextFreeIp,lanes,flowColours,dependencies,businessImpact,appGraph, stepOdds,blastRadius,attackPaths,chokePoints,outside}; })(typeof globalThis!=='undefined'?globalThis:this); diff --git a/public/app.css b/public/app.css index 6f46034..21779aa 100644 --- a/public/app.css +++ b/public/app.css @@ -52,6 +52,7 @@ button:disabled{opacity:.5;cursor:default} button:focus-visible,input:focus-visible,select:focus-visible,textarea:focus-visible,label:focus-within{outline:2px solid var(--accent);outline-offset:2px} .cap{font-family:var(--f-display);font-weight:600;font-size:11px;letter-spacing:.11em;text-transform:uppercase;color:var(--muted)} body.readonly .edit-only{display:none!important} +body.noprop .propose-only{display:none!important} /* Title block */ #cart{position:fixed;top:calc(14px + env(safe-area-inset-top,0px));left:max(16px,env(safe-area-inset-left,0px));z-index:5;display:grid;grid-template-columns:1fr auto;width:min(440px,calc(100% - 32px));box-sizing:border-box;font-size:12px} @@ -735,3 +736,10 @@ table.vi tr:not(.on) td:not(:first-child){opacity:.55} table.vi select{max-width:140px;font-size:11px;background:var(--panel-2);color:var(--fg);border:1px solid var(--line);border-radius:3px;padding:3px} table.vi .tag{display:inline-block;font-size:10px;padding:0 5px;border:1px solid var(--line);border-radius:8px;margin:2px 3px 0 0;color:var(--muted)} table.vi .tag.warn{border-color:var(--high);color:var(--high)} +/* Suggested zones */ +.sz-list{max-height:min(56vh,560px);padding:6px} +.sz{border:1px solid var(--line);border-radius:3px;margin:0 0 8px;background:var(--panel)} +.sz-h{display:flex;flex-direction:column;gap:2px;padding:6px;background:var(--panel-2)} +.sz-h input{padding:5px 7px;font-weight:600} +.sz-h small{color:var(--muted);font-size:11px} +.sz table.vi td{padding:4px 6px} diff --git a/public/app.js b/public/app.js index e728b4c..2d6c389 100644 --- a/public/app.js +++ b/public/app.js @@ -719,6 +719,7 @@ function syncUI(){ $('#hint').textContent=flat?(state.tool==='flow'?'Draw a flow: press on the source asset and release on the destination':state.tool==='asset'?'Add an asset: click on a VLAN plate where it should go':'Drag the background to pan · scroll to zoom · drag an asset, or a plate by its header (anywhere on its level, or onto another level)'):state.view==='overview'?'Drag to orbit · middle or right button to move the view · click a plinth for its summary · click again to open it':state.arrange?'Arrange mode · drag a plate anywhere on its level (left, right, front, back) · click “Arrange plates” again to finish':'Drag to orbit · middle or right button to move the view · scroll to zoom · click an asset or a flow'; document.querySelectorAll('#siteList .site').forEach(b=>b.setAttribute('aria-pressed',String(b.dataset.site===state.ovSelId))); document.body.classList.toggle('readonly',!Store.canWrite); + document.body.classList.toggle('noprop',!(Store.canWrite||(Store.me&&Store.me.canPropose&&!Store.view))); $('#portsBtn').setAttribute('aria-pressed',String(state.panel==='ports')); $('#riskBtn').setAttribute('aria-pressed',String(state.panel==='risk')); $('#atkBtn').setAttribute('aria-pressed',String(state.panel==='attack')); @@ -4720,6 +4721,48 @@ $('#gfxSeg').addEventListener('click',e=>{const b=e.target.closest('[data-gfx]') try{if(GFX.pref==='auto')localStorage.removeItem(GFX_KEY);else localStorage.setItem(GFX_KEY,GFX.pref);}catch(err){} applyGfx();if(was&&!eco())toast('Quality graphics. Edges are smoothed again after a reload of the page.');}); $('#optAnim').addEventListener('change',e=>{state.anim=e.target.checked;try{localStorage.setItem('strata-anim',state.anim?'1':'0');}catch(err){}if(state.view==='site')refresh();else refreshOverview();invalidate();}); +// "Suggest zones": the floors of the IS from its flows (SA.suggestZones), reviewed before they are applied +const ROLE_LBL={srv:'servers',users:'users',ot:'industrial',net:'network',other:'other'}; +function suggestZonesForm(){ + if(!SITE)return; + const raw=SITE.raw,zones=raw.zones&&raw.zones.length?raw.zones:[{id:'z-def',name:'Main zone'}]; + const S=SA.suggestZones({zones,vlans:(raw.vlans||[]).map(v=>({id:v.id,num:v.num,name:v.name,zone:zones.some(z=>z.id===v.zone)?v.zone:zones[0].id})), + assets:ASSETS.map(a=>({id:a.id,vlan:a.vlan,type:a.type})),flows:FLOWS.map(f=>({src:f.src,dst:f.dst}))}); + if(!S.zones.length){toast('No VLAN in this IS yet.',true);return;} + const Z=S.zones.map(z=>({id:z.id,name:z.name,suggested:z.suggested,existing:z.existing,key:z.key})); + const at={};S.zones.forEach(z=>z.vlans.forEach(id=>{at[id]=z.id;})); + const was=Object.fromEntries((raw.vlans||[]).map(v=>[v.id,v.zone])); + const zName=id=>{const z=Z.find(x=>x.id===id)||zones.find(x=>x.id===id);return z?z.name:'?';}; + const dTxt=id=>{const v=S.byVlan[id];return v.role===null?'empty':v.depth===null?'not reached':v.depth===1?'1 hop':v.depth+' hops';}; + const reached=Object.values(S.byVlan).filter(x=>x.depth!==null).length; + const body=()=>Z.slice().reverse().map(z=>{const ids=Object.keys(at).filter(id=>at[id]===z.id).map(id=>vlanById[id]).filter(Boolean).sort((a,b)=>a.num-b.num); + return `
    ${z.existing?'kept':'new'}${z.key.startsWith('depth:')?' · '+esc(z.suggested):z.key.startsWith('keep:')?' · empty VLANs, unchanged':''} · ${plural(ids.length,'VLAN')}
    + ${ids.length?`${ids.map(v=>` + `).join('')}
    VLAN ${esc(v.num)} · ${esc(v.name)}${esc(dTxt(v.id))}${S.byVlan[v.id].role?' · '+ROLE_LBL[S.byVlan[v.id].role]:''}${at[v.id]!==was[v.id]?` · was ${esc(zName(was[v.id]))}`:''}
    `:'

    No VLAN left: this zone will not be created.

    '}
    `;}).join(''); + const moved=()=>Object.keys(at).filter(id=>at[id]!==was[id]).length; + const direct=Store.liveWrite||Store.view==='draft'; + openForm('Suggested zones',`IS ${SITE.name}`,` +

    Top first, the Internet above: ${plural(reached,'VLAN')} reached from the Internet along the flows, on the floor of their distance; the others by the role of their assets; empty VLANs stay where they are. Rename a zone, or move a VLAN, before applying.

    +

    +
    ${body()}
    +

    ${direct?'Applied to the map at once (History can undo it).':'Your role cannot change the map: this opens a proposal with these zones.'} Plates that change zone are lined up again on their new level.

    `, + async()=>{ + const d=clone(raw),keep=Z.filter(z=>Object.values(at).includes(z.id)); + d.zones=keep.map(z=>({id:z.id,name:(z.name||'').trim()||z.suggested})); + d.vlans=(d.vlans||[]).map(v=>{if(!at[v.id]||at[v.id]===v.zone)return v;const y=Object.assign({},v,{zone:at[v.id]});delete y.place;return y;}); + if(JSON.stringify(d.zones)===JSON.stringify(raw.zones||[])&&!moved()){toast('The zones already match the flows: nothing to change.');return closeDet();} + if(direct){await Busy.track(()=>Store.putSite(SITE.id,d),'Applying the zones');toast(`Zones applied: ${plural(moved(),'VLAN')} moved, ${plural(d.zones.length,'zone')}.`);afterSave(()=>restorePanel());return;} + try{const j=await Store.api('POST','api/proposals',{title:`Zones of ${SITE.name} from the flows`,description:`Suggested from the flows (the Internet on top): ${moved()} VLAN(s) moved, ${d.zones.length} zone(s).`,siteId:SITE.id,changes:{si:{[SITE.id]:d},assets:{}}}); + onSave=null;toast('Proposal opened with the suggested zones: check it, then submit it.');enterProp(j.proposal,'draft');} + catch(e){formErr(e.message||'Could not open the proposal.');} + }); + det.querySelector('.form-foot [type=submit]').textContent=direct?'Apply':'Propose'; + const count=()=>{$('#szCount').textContent=`${plural(moved(),'VLAN')} change zone · ${plural(Z.filter(z=>Object.values(at).includes(z.id)).length,'zone')}`;}; + $('#frm').addEventListener('change',e=>{const s=e.target.closest('[data-sz-vlan]');if(s){at[s.dataset.szVlan]=s.value;$('#szBody').innerHTML=body();count();}}); + $('#frm').addEventListener('input',e=>{const n=e.target.closest('[data-sz-name]');if(n){const z=Z.find(x=>x.id===n.dataset.szName);if(z)z.name=n.value;}}); + count(); +} +$('#suggestZonesBtn').addEventListener('click',()=>{if(state.view!=='site'||!SITE)return;suggestZonesForm();}); // "Arrange VLANs": the plates of every zone on a grid, the VLANs that talk side by side (SA.arrangeGrid) try{const c=localStorage.getItem('strata-arrange-cols');if(c!==null&&$('#arrCols').querySelector(`option[value="${CSS.escape(c)}"]`))$('#arrCols').value=c;}catch(e){} $('#arrCols').addEventListener('change',e=>{try{localStorage.setItem('strata-arrange-cols',e.target.value);}catch(err){}}); diff --git a/public/index.html b/public/index.html index 17953db..7460f3c 100644 --- a/public/index.html +++ b/public/index.html @@ -149,6 +149,10 @@

    Layout

    Drag a plate anywhere on its level (left, right, front, back), or use the arrows that appear in the VLAN list. “Line up” puts a level back in a row. The Internet cloud can be dragged too.

    +
    + +

    The Internet on top: each VLAN on the floor of its distance from the Internet along the flows, the others by role. You review it before anything changes.

    +
    diff --git a/tests/analysis.test.mjs b/tests/analysis.test.mjs index 65faf54..e54f2dd 100644 --- a/tests/analysis.test.mjs +++ b/tests/analysis.test.mjs @@ -288,3 +288,45 @@ test('VLAN files: a JSON list (NetBox style) and a Strata export', () => { assert.equal(strata.kind, 'strata'); assert.deepEqual(strata.items, [{ num: 100, name: 'Web', cidr: '192.0.2.0/24', zone: 'DMZ', site: 'lyon', siteName: 'Lyon', vlanId: 'v1' }]); }); + +test('zones from the flows: the Internet on top, by depth, then by role; matching zones kept, empty VLANs stay', () => { + const r = J(A.suggestZones({ + zones: [{ id: 'z1', name: 'Main' }, { id: 'zd', name: 'Our DMZ' }], + vlans: [{ id: 'dmz', zone: 'zd' }, { id: 'app', zone: 'z1' }, { id: 'db', zone: 'z1' }, { id: 'pc', zone: 'z1' }, { id: 'ot', zone: 'z1' }, { id: 'mg', zone: 'z1' }, { id: 'empty', zone: 'z1' }], + assets: [{ id: 'w', vlan: 'dmz', type: 'server' }, { id: 'a', vlan: 'app', type: 'server' }, { id: 'd', vlan: 'db', type: 'db' }, { id: 'd2', vlan: 'db', type: 'db' }, + { id: 'p', vlan: 'pc', type: 'workstation' }, { id: 'plc', vlan: 'ot', type: 'plc' }, { id: 'sw', vlan: 'mg', type: 'switch' }], + flows: [{ src: 'internet', dst: 'w' }, { src: 'w', dst: 'a' }, { src: 'a', dst: 'd' }, { src: 'p', dst: 'a' }, { src: 'p', dst: 'internet' }, { src: 'svc:x', dst: 'd2' }], + })); + // bottom first: empty VLANs keep their zone, then the roles of what the Internet never reaches, then depth, the exposed on top + // (db is reached by an Internet service: it is exposed too, on the floor of the DMZ) + assert.deepEqual(r.zones.map(z => [z.name, z.vlans]), [ + ['Main', ['empty']], ['Network and administration', ['mg']], ['Industrial (OT)', ['ot']], ['Users', ['pc']], + ['Reached from the exposed zone', ['app']], ['Our DMZ', ['dmz', 'db']], + ]); + assert.equal(r.zones.at(-1).id, 'zd', 'the DMZ zone is kept, with its name'); + assert.equal(r.byVlan.pc.depth, null, 'a VLAN that only calls the Internet is not reached from it'); + assert.equal(r.byVlan.db.depth, 1, 'an Internet service counts as the Internet'); + assert.equal(r.byVlan.empty.moved, false); + assert.equal(r.byVlan.app.moved, true); +}); + +test('zones from the flows: a VLAN with PLCs among its servers is industrial', () => { + const r = J(A.suggestZones({ zones: [{ id: 'z1', name: 'Main' }], + vlans: [{ id: 'v50', name: 'OT production', zone: 'z1' }, { id: 'v60', name: 'Lab', zone: 'z1' }], + assets: [{ id: 'plc', vlan: 'v50', type: 'plc' }, { id: 'hmi', vlan: 'v50', type: 'server' }, { id: 'hist', vlan: 'v50', type: 'server' }, + { id: 's1', vlan: 'v60', type: 'server' }, { id: 's2', vlan: 'v60', type: 'server' }, { id: 's3', vlan: 'v60', type: 'server' }, { id: 'cam', vlan: 'v60', type: 'camera' }], + flows: [] })); + assert.equal(r.byVlan.v50.role, 'ot'); + assert.equal(r.byVlan.v60.role, 'srv', 'one camera among four servers does not make it industrial'); +}); + +test('zones from the flows: meeting-room screens are the users\', PLCs make a VLAN industrial', () => { + const r = J(A.suggestZones({ zones: [{ id: 'z1', name: 'Main' }], + vlans: [{ id: 'room', name: 'Salles de réunion étage 1', zone: 'z1' }, { id: 'line', name: 'Line 1', zone: 'z1' }, { id: 'cams', name: 'Vidéosurveillance', zone: 'z1' }], + assets: [{ id: 's1', vlan: 'room', type: 'iot' }, { id: 's2', vlan: 'room', type: 'iot' }, { id: 'p', vlan: 'line', type: 'plc' }, { id: 'h', vlan: 'line', type: 'server' }, { id: 'h2', vlan: 'line', type: 'server' }, + { id: 'c1', vlan: 'cams', type: 'camera' }, { id: 'c2', vlan: 'cams', type: 'camera' }], + flows: [] })); + assert.equal(r.byVlan.room.role, 'users'); + assert.equal(r.byVlan.line.role, 'ot'); + assert.equal(r.byVlan.cams.role, 'ot'); +}); diff --git a/tests/e2e.mjs b/tests/e2e.mjs index 8ce8983..c75efd2 100644 --- a/tests/e2e.mjs +++ b/tests/e2e.mjs @@ -378,6 +378,14 @@ test('plates: all or none, empty VLANs hidden, VLANs imported into chosen zones, await admin.click('#cloudReset'); await admin.waitForFunction(async () => !(await (await fetch('api/state')).json()).si.lyon.cloud2d); + // Suggest zones: the Internet on top; applied by the admin + await admin.click('#viewSeg [data-v="3d"]'); + await admin.click('#suggestZonesBtn'); + await admin.waitForSelector('#szBody .sz'); + assert.equal(await admin.locator('#frm button[type=submit]').textContent(), 'Apply'); + await admin.click('#frm button[type=submit]'); + await admin.waitForFunction(async () => { const s = (await (await fetch('api/state')).json()).si.lyon; const top = s.zones[s.zones.length - 1]; return s.vlans.some(v => v.num === 10 && v.zone === top.id); }); + // the 3D view is cheap to draw: a few hundred draw calls, nothing drawn while idle, nothing leaked on a rebuild await admin.click('#viewSeg [data-v="3d"]'); await admin.click('.vtools [data-vsel="all"]'); @@ -391,10 +399,11 @@ test('plates: all or none, empty VLANs hidden, VLANs imported into chosen zones, const idle0 = (await rs()).frames; await admin.waitForTimeout(1500); assert.ok((await rs()).frames - idle0 <= 1, 'the model is drawn again while nothing changes'); - const geo0 = (await rs()).geometries; - for (const sp of ['wide', 'compact', 'wide', 'compact']) { await admin.click(`#spaceSeg [data-space="${sp}"]`); await admin.waitForTimeout(400); } - await admin.waitForTimeout(800); - assert.ok((await rs()).geometries <= geo0 + 5, `GPU geometries grow on each rebuild (${geo0} → ${(await rs()).geometries})`); + // shared geometries (equipment models) go to the GPU the first time they are seen: one cycle warms them up, + // then rebuilding the model again and again must not add any + const cycle = async () => { for (const sp of ['wide', 'compact']) { await admin.click(`#spaceSeg [data-space="${sp}"]`); await admin.waitForTimeout(500); } await admin.waitForTimeout(600); return (await rs()).geometries; }; + const geo0 = await cycle(), geos = [await cycle(), await cycle(), await cycle()]; + assert.ok(Math.max(...geos) <= geo0 + 2, `GPU geometries grow on each rebuild (${geo0} → ${geos.join(', ')})`); await admin.click('#gfxSeg [data-gfx="quality"]'); await admin.waitForTimeout(800); assert.equal((await rs()).mode, 'quality'); From dace7381b0f9f7bf123d8089007d1d45ed93dc23 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:08:18 +0000 Subject: [PATCH 09/16] The title block folds into one line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ▴ in its corner: the IS name, the four key figures, back to the overview, sign out and the incident band stay; the controls move up with it. Remembered by the browser; fits a phone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 1 + public/app.css | 12 ++++++++++++ public/app.js | 20 +++++++++++++++++--- tests/e2e.mjs | 11 +++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e5c22..c7ccdc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. +- **The title block folds into one line** (▴ in its corner): the name of the IS or *Strata*, the four key figures, back to the overview, sign out and the incident band stay; the controls move up with it. Remembered by the browser. - **Suggest zones** (*Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). - **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. - **Modules can ask for a choice before an import** (`file.pick` in the manifest, a `preview(cfg, ctx)` function, `ctx.picks` in `run`, `POST /api/connectors/{id}/preview`): Strata shows what the file holds, people tick what to bring and, with `pick.zones`, the zone of each. Backward compatible: modules without `pick` work as before. See MODULES.md. diff --git a/public/app.css b/public/app.css index 21779aa..7807946 100644 --- a/public/app.css +++ b/public/app.css @@ -743,3 +743,15 @@ table.vi .tag.warn{border-color:var(--high);color:var(--high)} .sz-h input{padding:5px 7px;font-weight:600} .sz-h small{color:var(--muted);font-size:11px} .sz table.vi td{padding:4px 6px} +/* Title block folded into one line */ +#cart .cart-tog{position:absolute;top:3px;right:3px;min-height:24px;padding:0 7px;font-size:12px;z-index:1} +#cart:not(.min) .pl{padding-right:34px} +#cart.min{display:block;width:auto;max-width:calc(100% - 32px)} +#cart .mini{display:flex;align-items:center;gap:10px;padding:5px 38px 5px 10px;border-bottom:0;flex-wrap:wrap} +#cart .mini .back{padding:2px 6px} +#cart .mini .ttl{font-family:var(--f-display);font-weight:700;letter-spacing:.06em;text-transform:uppercase;font-size:14px;white-space:nowrap} +#cart .mini .mn{display:flex;gap:10px;font-family:var(--f-mono);font-size:12px;font-variant-numeric:tabular-nums;color:var(--muted);flex-wrap:wrap} +#cart .mini .mn .net{color:var(--x-net)}#cart .mini .mn .alert{color:var(--crit)} +#cart .mini .signout{margin-left:0} +#cart.min .incband{display:flex} +@media (max-width:760px){#cart.min{max-width:calc(100% - 140px)}#cart .mini{gap:4px 8px}#cart .mini .mn{gap:2px 8px;font-size:11px}} diff --git a/public/app.js b/public/app.js index 2d6c389..1dd0ae8 100644 --- a/public/app.js +++ b/public/app.js @@ -657,16 +657,29 @@ function toast(msg,err,sticky){const t=$('#toast');t.textContent=msg;t.className /* ================= Title block and lists ================= */ const nums=arr=>`
    ${arr.map(([n,l,c])=>`
    ${n}${l}
    `).join('')}
    `; function storeBadge(){return Store.mode==='api'?`Server${Store.user?' · '+esc(Store.user)+' ('+esc(Store.role)+')':''}${Store.me&&Store.me.signOut?' ':''}`:`This browser only`;} +// The title block can be folded into one line (remembered in this browser): the name, the four key +// figures, back to the overview and sign out stay; the controls below move up with it +let cartMin=false;try{cartMin=localStorage.getItem('strata-cart-min')==='1';}catch(e){} +const cartTog=()=>``; function renderCart(){ const c=$('#cart'); + c.classList.toggle('min',cartMin); + if(cartMin){ + const ov=state.view==='overview',T=k=>SITES.reduce((n,s)=>n+s.stats[k],0); + const f=ov?{a:T('assets'),x:T('xNet'),c:T('crit'),r:T('risk')+WAN.filter(w=>w.risk).length}:{a:SITE.stats.assets,x:SITE.stats.xNet,c:SITE.stats.crit,r:SITE.stats.risk}; + c.innerHTML=`
    ${ov?'':''}${ov?'Strata':'IS '+esc(SITE.name)} + ${f.a} assets${f.x} exposed${f.c} crit.${f.r} risky + ${Store.me&&Store.me.signOut?'':''}${cartTog()}
    ${ov?'':incBand()}`; + document.body.classList.toggle('incident-on',incOn());placeCtl();return; + } if(state.view==='overview'){ const T=k=>SITES.reduce((n,s)=>n+s.stats[k],0); - c.innerHTML=`

    Strata

    Overview
    Sheet00
    + c.innerHTML=`${cartTog()}

    Strata

    Overview
    Sheet00
    Scope ${plural(SITES.length,'IS').replace('ISs','IS')} · ${plural(T('vlans'),'VLAN')}Storage ${storeBadge()}
    ${nums([[T('assets'),'assets'],[T('xNet'),'internet-facing','net'],[T('crit'),'critical vulns','alert'],[T('risk')+WAN.filter(w=>w.risk).length,'risky flows','alert']])}`; }else{ const s=SITE.stats; - c.innerHTML=`

    IS ${esc(SITE.name)}

    ${esc(SITE.kind||'Information system')}
    Sheet${SITE.planche}
    + c.innerHTML=`${cartTog()}

    IS ${esc(SITE.name)}

    ${esc(SITE.kind||'Information system')}
    Sheet${SITE.planche}
    Zones ${ZONES.length}Scale 1 plate = 1 VLANStorage ${storeBadge()}
    ${nums([[s.assets,'assets'],[s.xNet,'internet-facing','net'],[s.crit,'critical vulns','alert'],[s.risk,'risky flows','alert']])}${incBand()}`; } @@ -4699,7 +4712,8 @@ function stopRotate(){if(state.rotate){state.rotate=false;$('#optRotate').checke /* ================= Controls ================= */ const rerender=()=>state.view==='site'?refresh():refreshOverview(); const segPress=(sel,b)=>document.querySelectorAll(sel+' button').forEach(x=>x.setAttribute('aria-pressed',String(x===b))); -$('#cart').addEventListener('click',e=>{if(e.target.closest('[data-back]'))backToOverview();}); +$('#cart').addEventListener('click',e=>{if(e.target.closest('[data-back]'))backToOverview(); + if(e.target.closest('[data-cart-tog]')){cartMin=!cartMin;try{localStorage.setItem('strata-cart-min',cartMin?'1':'0');}catch(err){}renderCart();const t=$('#cart [data-cart-tog]');if(t)t.focus();}}); $('#siteList').addEventListener('click',e=>{const b=e.target.closest('[data-site]');if(b)selectSite(siteById[b.dataset.site],true);}); $('#vlans').addEventListener('change',e=>{const id=e.target.dataset.vlan;if(!id)return;if(state.focus){state.focus=null;renderFocusBar();}e.target.checked?state.hidden.delete(id):state.hidden.add(id); const a=selAsset();if(a&&state.hidden.has(a.vlan))select(null);else refresh();}); diff --git a/tests/e2e.mjs b/tests/e2e.mjs index c75efd2..872bcc4 100644 --- a/tests/e2e.mjs +++ b/tests/e2e.mjs @@ -315,6 +315,17 @@ test('plates: all or none, empty VLANs hidden, VLANs imported into chosen zones, const boxes = () => admin.locator('#vlans input[type=checkbox]:checked').count(); const total = await admin.locator('#vlans input[type=checkbox]').count(); + // the title block folds into one line, and the controls move up with it + const cartH = () => admin.evaluate(() => document.getElementById('cart').getBoundingClientRect().height); + const ctlTop = () => admin.evaluate(() => document.getElementById('ctl').getBoundingClientRect().top); + const [h0, t0] = [await cartH(), await ctlTop()]; + await admin.click('#cart [data-cart-tog]'); + assert.ok(await cartH() < h0 / 2, 'the title block did not fold'); + assert.ok(await ctlTop() < t0 - 50, 'the controls did not move up'); + assert.match(await admin.locator('#cart .mini').textContent(), /IS Lyon.*assets.*exposed.*crit.*risky/s); + await admin.click('#cart [data-cart-tog]'); + assert.ok(Math.abs(await cartH() - h0) < 2); + // all / none, for the IS and for one zone await admin.click('.vtools [data-vsel="none"]'); assert.equal(await boxes(), 0); From 6f83ce25f80f67b0356ab08eb5edfef4ebd1f007 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:14:11 +0000 Subject: [PATCH 10/16] Suggest zones from the flows, also in the Edit section The button was only in Layout, far down the controls; it is now next to Zones, contacts. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 2 +- public/app.js | 2 +- public/index.html | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7ccdc1..a4e3647 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. - **The title block folds into one line** (▴ in its corner): the name of the IS or *Strata*, the four key figures, back to the overview, sign out and the incident band stay; the controls move up with it. Remembered by the browser. -- **Suggest zones** (*Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). +- **Suggest zones from the flows** (*Edit*, and *Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). - **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. - **Modules can ask for a choice before an import** (`file.pick` in the manifest, a `preview(cfg, ctx)` function, `ctx.picks` in `run`, `POST /api/connectors/{id}/preview`): Strata shows what the file holds, people tick what to bring and, with `pick.zones`, the zone of each. Backward compatible: modules without `pick` work as before. See MODULES.md. - **Graphics for small laptops** (*Display › Graphics*: Auto, Quality, Economy, and *Animated flows*). Economy: screen resolution, no shadows, no moving dots, labels hidden sooner from afar. Auto, the default, is Quality until moving the view turns out slow on this computer (then Economy, remembered by the browser), and Economy when the browser draws without a graphics card. diff --git a/public/app.js b/public/app.js index 1dd0ae8..77a3aa4 100644 --- a/public/app.js +++ b/public/app.js @@ -4776,7 +4776,7 @@ function suggestZonesForm(){ $('#frm').addEventListener('input',e=>{const n=e.target.closest('[data-sz-name]');if(n){const z=Z.find(x=>x.id===n.dataset.szName);if(z)z.name=n.value;}}); count(); } -$('#suggestZonesBtn').addEventListener('click',()=>{if(state.view!=='site'||!SITE)return;suggestZonesForm();}); +document.addEventListener('click',e=>{if(e.target.closest('[data-suggest-zones]')&&state.view==='site'&&SITE)suggestZonesForm();}); // "Arrange VLANs": the plates of every zone on a grid, the VLANs that talk side by side (SA.arrangeGrid) try{const c=localStorage.getItem('strata-arrange-cols');if(c!==null&&$('#arrCols').querySelector(`option[value="${CSS.escape(c)}"]`))$('#arrCols').value=c;}catch(e){} $('#arrCols').addEventListener('change',e=>{try{localStorage.setItem('strata-arrange-cols',e.target.value);}catch(err){}}); diff --git a/public/index.html b/public/index.html index 7460f3c..6d3bdfb 100644 --- a/public/index.html +++ b/public/index.html @@ -88,6 +88,7 @@

    Edit

    +
    @@ -150,7 +151,7 @@

    Layout

    Drag a plate anywhere on its level (left, right, front, back), or use the arrows that appear in the VLAN list. “Line up” puts a level back in a row. The Internet cloud can be dragged too.

    - +

    The Internet on top: each VLAN on the floor of its distance from the Internet along the flows, the others by role. You review it before anything changes.

    From 1b4743161740340d6f2decd22b636ef6226c73a5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:22:33 +0000 Subject: [PATCH 11/16] A large IS can be saved again; the history stops at a size One document was limited to 512 KB, and an IS holds all its flows (about 300 bytes each): an IS of some 1 800 flows, imported through a proposal, could no longer be edited from the interface ("This item is too large to save"). The limit is now MAX_DOC_MB, 8 MB by default. Each change keeps the edited document before and after it, in the history file and in memory: about a megabyte a change for such an IS. The history now also stops at HISTORY_MAX_MB (200 MB by default), the oldest changes first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- .env.example | 4 ++++ CHANGELOG.md | 4 ++++ README.md | 4 +++- docs/docs.html | 2 ++ nix/module.nix | 2 +- public/app.js | 2 +- server/server.js | 28 ++++++++++++++++++++-------- tests/hardening.test.mjs | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 69 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 57a10ea..2747a42 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,10 @@ VIEW_PASS= ALLOW_SELF_APPROVAL=false HISTORY_KEEP=5000 +# The history also stops at this size (MB): each change keeps the edited document before and after it +# HISTORY_MAX_MB=200 +# Largest document saved at once (MB): an IS holds its VLANs and all its flows, about 300 bytes a flow +# MAX_DOC_MB=8 READ_ONLY=false BACKUP_KEEP=14 BACKUP_EVERY_HOURS=24 diff --git a/CHANGELOG.md b/CHANGELOG.md index a4e3647..c61aac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Graphics for small laptops** (*Display › Graphics*: Auto, Quality, Economy, and *Animated flows*). Economy: screen resolution, no shadows, no moving dots, labels hidden sooner from afar. Auto, the default, is Quality until moving the view turns out slow on this computer (then Economy, remembered by the browser), and Economy when the browser draws without a graphics card. - **API**: `POST /auth/login` (the form, or JSON `{user, password}`); `analysis.js` exports `arrangeGrid` and `vlansFromFile`; `window.strataRenderStats()` in the page gives what the last frame cost. +### Fixed +- **A large IS could no longer be saved** (“This item is too large to save”): one document was limited to 512 KB, and an IS holds all its flows, about 300 bytes each, so an IS of some 1 800 flows or more, imported through a proposal, could no longer be edited from the interface. The limit is now `MAX_DOC_MB`, 8 MB by default. +- **The history no longer grows without bound with a large IS**: each change keeps the edited document before and after it, about a megabyte a change for an IS of 2 000 flows, and the history is held in memory. It now also stops at `HISTORY_MAX_MB` (200 MB by default), the oldest changes first. + ### Changed - **The 3D view is much lighter to draw**, and looks the same: 8 to 15 times fewer draw calls (1 569 → 208 for the Lyon demo) and half the triangles. It is drawn only when something changes, not 60 times a second; shadows are drawn again only when the model changes; the view is a little less sharp while it moves. Each type of equipment is one shared geometry; every flow of an IS is in one geometry with its own colour and opacity, and the moving dots are one instanced mesh; label textures are shared; labels too small to read are not drawn; flows no longer cast shadows; a rebuilt model frees what the GPU held for the previous one. - Changing a password, or disabling or deleting an account, ends that account's other sessions. *End all sessions* (admin) ends the sessions of local accounts too, and keeps the admin's own. diff --git a/README.md b/README.md index f793588..8bfa8ca 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ Measured on Node.js 22 (the Docker image), one server process, maps generated wi | Up to ~5 000 assets | 0.5 | 256 MB | 1 GB | | Up to ~30 000 assets | 1 to 2 | 512 MB (1 GB with connectors running) | 5 GB | -The server is idle between requests: CPU is spent on writes (each one saves the map and appends to the history) and on the dashboard. Each connector or module run starts a separate Node.js process, about 50 MB for as long as it runs. Disk: the map, the history (`HISTORY_KEEP` entries) and `BACKUP_KEEP` daily copies of the map, so roughly the size of the JSON export × (`BACKUP_KEEP` + 2), plus the history. +The server is idle between requests: CPU is spent on writes (each one saves the map and appends to the history) and on the dashboard. The history is kept in memory too, up to `HISTORY_MAX_MB` (200 MB by default): count that much more RAM for an IS of thousands of flows edited often, or lower it. Each connector or module run starts a separate Node.js process, about 50 MB for as long as it runs. Disk: the map, the history (`HISTORY_KEEP` entries) and `BACKUP_KEEP` daily copies of the map, so roughly the size of the JSON export × (`BACKUP_KEEP` + 2), plus the history. **The browser does most of the work** (the 3D model, the 2D diagram, the analysis): 8 GB of RAM and WebGL 2 are enough, an integrated GPU included. An IS of 300 assets and 600 flows takes about 150 MB of browser memory. `docker-compose.yml` has the server limits, commented out. @@ -259,6 +259,8 @@ By default, the port only listens on the machine itself (`127.0.0.1:8080`). To o | `EDIT_PASS_FILE` / `VIEW_PASS_FILE` | — | Read the password from this file instead (Docker secrets, systemd credentials, sops-nix, agenix). The file wins over the variable; a trailing newline is ignored. If the file cannot be read, the server does not start. | | `ALLOW_SELF_APPROVAL` | `false` | `true` lets an approver approve their own proposals. | | `HISTORY_KEEP` | `5000` | Number of changes kept in the history. | +| `HISTORY_MAX_MB` | `200` | The history also stops at this size: each change keeps the whole edited document before and after it, so editing a large IS often (moving plates, importing flows) adds about twice its size each time. The oldest changes go first. | +| `MAX_DOC_MB` | `8` | Largest document saved at once. An IS holds its VLANs and all its flows (about 300 bytes a flow: 8 MB is some 25 000 flows). | | `READ_ONLY` | `false` | Freezes the map for everyone. | | `BACKUP_KEEP` | `14` | Number of automatic backups kept. | | `BACKUP_EVERY_HOURS` | `24` | How often automatic backups are made. | diff --git a/docs/docs.html b/docs/docs.html index 9496f56..1e50945 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -86,6 +86,8 @@

    Configuration

    EDIT_PASS_FILE / VIEW_PASS_FILEnoneRead the password from this file instead (Docker secrets, systemd credentials, sops-nix, agenix). The file wins; a trailing newline is ignored; an unreadable file stops the server. ALLOW_SELF_APPROVALfalsetrue lets an approver approve their own proposals. HISTORY_KEEP5000How many document changes the history keeps. + HISTORY_MAX_MB200The history also stops at this size, the oldest changes first (each change keeps the edited document before and after it). + MAX_DOC_MB8Largest document saved at once: an IS with all its VLANs and flows (about 300 bytes a flow). READ_ONLYfalseFreezes the map for everyone (an audit, a migration). BACKUP_KEEP / BACKUP_EVERY_HOURS14 / 24Automatic backups: how many to keep, how often. MAX_IMPORT_MB20Largest JSON or firewall configuration file accepted. diff --git a/nix/module.nix b/nix/module.nix index 0986022..f73bc61 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -88,7 +88,7 @@ in ''; description = '' Environment variables of Strata, as listed in section 3 of the README and in .env.example: - EDIT_USER, VIEW_USER, ALLOW_SELF_APPROVAL, HISTORY_KEEP, READ_ONLY, BACKUP_KEEP, + EDIT_USER, VIEW_USER, ALLOW_SELF_APPROVAL, HISTORY_KEEP, HISTORY_MAX_MB, MAX_DOC_MB, READ_ONLY, BACKUP_KEEP, BACKUP_EVERY_HOURS, MAX_IMPORT_MB, LOGIN_MAX_FAILS, LOGIN_MAX_FAILS_PER_ADDRESS, LOGIN_LOCK_MINUTES, TRUST_PROXY (set it to "loopback" behind a reverse proxy on this host), PUBLIC_URL, SSO_ISSUER, SSO_CLIENT_ID, SSO_NAME, SSO_ROLE_ADMIN, SSO_ROLE_APPROVER, diff --git a/public/app.js b/public/app.js index 77a3aa4..1848258 100644 --- a/public/app.js +++ b/public/app.js @@ -431,7 +431,7 @@ const Store={ local();this.applyView(this.bulk?null:0); }catch(e){ if(e.status===403)toast(e.message||'You cannot change this.',true); - else if(e.status===413)toast('This item is too large to save.',true); + else if(e.status===413)toast(`This ${kind==='si'?'IS':'item'} is too large to save: the server accepts documents up to MAX_DOC_MB (8 MB unless set otherwise).`,true); else toast('Could not save: '+(e.message||'error')+'.',true); throw e; } diff --git a/server/server.js b/server/server.js index 2154bf4..1de7072 100644 --- a/server/server.js +++ b/server/server.js @@ -44,8 +44,12 @@ const CONFIG = { backupKeep: Math.max(0, parseInt(env.BACKUP_KEEP || '14', 10)), backupEveryHours: Math.max(1, parseInt(env.BACKUP_EVERY_HOURS || '24', 10)), historyKeep: Math.max(50, parseInt(env.HISTORY_KEEP || '5000', 10)), + // the history also stops at this size: each change keeps the whole document before and after it, so a + // large IS edited often (plates moved, flows imported) would otherwise grow it by a megabyte a change + historyMaxBytes: Math.max(1, parseFloat(env.HISTORY_MAX_MB || '200') || 200) * 1024 * 1024, maxImportBytes: parseInt(env.MAX_IMPORT_MB || '20', 10) * 1024 * 1024, - maxDocBytes: 512 * 1024, + // one document: an IS holds its VLANs and all its flows (about 300 bytes a flow, 8 MB ≈ 25 000 flows) + maxDocBytes: Math.max(0.5, parseFloat(env.MAX_DOC_MB || '8') || 8) * 1024 * 1024, // Failed sign-ins: 0 turns a limit off loginMaxFails: Math.max(0, parseInt(env.LOGIN_MAX_FAILS ?? '10', 10) || 0), loginMaxFailsPerAddress: Math.max(0, parseInt(env.LOGIN_MAX_FAILS_PER_ADDRESS ?? '20', 10) || 0), @@ -134,7 +138,10 @@ async function backup(reason) { One line per changed document: {rev, at, user, batch, label, kind, id, before, after}. kind: si | assets | wan | services | risk | all (whole map, for imports). Rolling back replays the "before" of later lines, and is itself recorded, so a rollback can be undone too. */ -let history = []; +let history = [], historyBytes = 0; +// the size of each line in the history file, kept aside (the lines go to the file and the API as they are) +const LINE_BYTES = new WeakMap(); +const lineBytes = h => { let n = LINE_BYTES.get(h); if (n === undefined) { n = Buffer.byteLength(JSON.stringify(h)) + 1; LINE_BYTES.set(h, n); } return n; }; function loadHistory() { if (!fs.existsSync(HISTORY_FILE)) return; history = fs.readFileSync(HISTORY_FILE, 'utf8').split('\n').filter(Boolean).map(l => { try { return JSON.parse(l); } catch { return null; } }).filter(Boolean); @@ -145,12 +152,16 @@ function loadHistory() { log(`History: ${history.length} changes, back to rev ${history.length ? history[0].rev : state.rev}`); } function trimHistory(rewrite) { - if (history.length <= CONFIG.historyKeep && !rewrite) return; - if (history.length > CONFIG.historyKeep) { - // keep whole revisions: drop the oldest ones until under the limit - let cut = history.length - CONFIG.historyKeep; - while (cut < history.length && history[cut].rev === history[cut - 1]?.rev) cut++; - history = history.slice(cut); + if (rewrite) historyBytes = history.reduce((t, h) => t + lineBytes(h), 0); + if (history.length <= CONFIG.historyKeep && historyBytes <= CONFIG.historyMaxBytes && !rewrite) return; + if (history.length > CONFIG.historyKeep || historyBytes > CONFIG.historyMaxBytes) { + // keep whole revisions, and always the latest one: drop the oldest until under both limits + let cut = Math.max(0, history.length - CONFIG.historyKeep), bytes = historyBytes; + for (let i = 0; i < cut; i++) bytes -= lineBytes(history[i]); + while (cut < history.length - 1 && bytes > CONFIG.historyMaxBytes) bytes -= lineBytes(history[cut++]); + while (cut > 0 && cut < history.length && history[cut].rev === history[cut - 1].rev) bytes -= lineBytes(history[cut++]); + if (cut >= history.length) { cut = history.length - 1; while (cut > 0 && history[cut - 1].rev === history[cut].rev) cut--; bytes = history.slice(cut).reduce((t, h) => t + lineBytes(h), 0); } + if (cut > 0) { log(`History: ${cut} oldest change(s) dropped (limits: ${CONFIG.historyKeep} changes, ${Math.round(CONFIG.historyMaxBytes / 1048576)} MB)`); history = history.slice(cut); historyBytes = bytes; } } writeJson(HISTORY_FILE, history.map(h => JSON.stringify(h)).join('\n') + (history.length ? '\n' : '')); } @@ -163,6 +174,7 @@ async function commitChanges(changes, auth, meta = {}) { state.updatedAt = new Date().toISOString(); const lines = real.map(c => ({ rev: state.rev, at: state.updatedAt, user: auth.user || '', batch: meta.batch || `r${state.rev}`, label: meta.label || '', kind: c.kind, id: c.id ?? null, before: c.before ?? null, after: c.after ?? null })); history.push(...lines); + for (const l of lines) historyBytes += lineBytes(l); await writeJson(DB_FILE, state); const text = lines.map(l => JSON.stringify(l)).join('\n') + '\n'; writeChain = writeChain.then(() => fsp.appendFile(HISTORY_FILE, text, { mode: 0o600 })).catch(e => log('ERROR history', e.message)); diff --git a/tests/hardening.test.mjs b/tests/hardening.test.mjs index 94d4c6d..ce9b307 100644 --- a/tests/hardening.test.mjs +++ b/tests/hardening.test.mjs @@ -49,3 +49,37 @@ test('data files are readable by the server user only, including those of older rmSync(dataDir, { recursive: true, force: true, maxRetries: 5 }); } }); + +test('a large IS (3500 flows) can be saved, and the history stops at HISTORY_MAX_MB', async () => { + const dataDir = mkdtempSync(join(tmpdir(), 'strata-big-')); + const port = 9300 + Math.floor(Math.random() * 90) + 100, pass = 'admin-pass-123', base = `http://127.0.0.1:${port}`; + const proc = spawn(process.execPath, [join(root, 'server/server.js')], { + env: { ...process.env, PORT: String(port), HOST: '127.0.0.1', DATA_DIR: dataDir, EDIT_USER: 'admin', EDIT_PASS: pass, VIEW_PASS: '', CONNECTORS: 'false', HISTORY_MAX_MB: '1' }, + stdio: ['ignore', 'pipe', 'inherit'], + }); + let out = ''; + try { + await new Promise((res, rej) => { proc.stdout.on('data', d => { out += d; if (String(d).includes('listening')) res(); }); proc.on('exit', c => rej(new Error('exit ' + c))); }); + const h = { ...(await auth(base, 'admin', pass)), 'Content-Type': 'application/json', 'X-Requested-With': 'strata' }; + // an IS of 150 VLANs and 3500 flows with notes, about 700 KB: above the old 512 KB limit + const doc = n => ({ name: 'Big', zones: [{ id: 'z1', name: 'Main' }], vlans: Array.from({ length: 150 }, (_, i) => ({ id: 'v' + i, num: i + 1, name: 'VLAN ' + i, zone: 'z1' })), + flows: Array.from({ length: 3500 }, (_, i) => ({ id: `f${i}`, src: `a${i}`, dst: `b${i}`, port: 'tcp/443', vol: n, risk: false, note: `Palo Alto rule “R${i}” — a note long enough to look like a real one, edit ${n}` })) }); + assert.ok(JSON.stringify(doc(0)).length > 512 * 1024); + for (let n = 0; n < 4; n++) { + const r = await fetch(`${base}/api/si/big`, { method: 'PUT', headers: h, body: JSON.stringify(doc(n)) }); + assert.equal(r.status, 200, `save ${n}: ${await r.text()}`); + } + // each change keeps the IS before and after it (more than 1 MB): only the latest is kept + const hist = await (await fetch(`${base}/api/history?limit=50`, { headers: h })).json(); + assert.equal(hist.entries.length, 1, JSON.stringify(hist.entries.map(e => e.rev))); + assert.equal(hist.rev, 4); + assert.ok(statSync(join(dataDir, 'history.jsonl')).size < 2 * 1024 * 1024); + assert.match(out, /History: \d+ oldest change\(s\) dropped \(limits: 5000 changes, 1 MB\)/); + // and still more than MAX_DOC_MB is refused + const huge = { ...doc(9), notes: 'x'.repeat(9 * 1024 * 1024) }; + assert.equal((await fetch(`${base}/api/si/big`, { method: 'PUT', headers: h, body: JSON.stringify(huge) })).status, 413); + } finally { + await new Promise(res => { if (proc.exitCode !== null) return res(); proc.once('exit', res); proc.kill(); }); + rmSync(dataDir, { recursive: true, force: true, maxRetries: 5 }); + } +}); From 9c04ab2aff5ce803b530e4155422078b05f0109c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:33:02 +0000 Subject: [PATCH 12/16] The overview shows each IS in the compact form of its model Each zone of the small model follows the rows and order saved by Arrange VLANs (or by plates placed by hand); a zone never arranged keeps one row up to 8 VLANs, then wraps into a grid. The assets of a plate wrap into lines, floors come closer past 5 zones, and the small models share their geometries. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 1 + public/app.js | 74 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c61aac8..0e89913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Hide empty VLANs** (*Plates*): the plates without an asset leave the 3D model and the 2D diagram. Remembered by the browser. - **A loading bar** at the top of the screen for any request still running after 300 ms, with the progress of batches of writes and imports. - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. +- **The overview shows each IS in the compact form of its model**: each zone of the small model takes the rows and the order of *Arrange VLANs* (or of plates placed by hand); a zone never arranged keeps one row up to 8 VLANs, then wraps into a grid; the assets of a plate wrap into lines; past 5 zones the floors come closer. The small models share their geometries, lighter for a large IS. - **The title block folds into one line** (▴ in its corner): the name of the IS or *Strata*, the four key figures, back to the overview, sign out and the incident band stay; the controls move up with it. Remembered by the browser. - **Suggest zones from the flows** (*Edit*, and *Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). - **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. diff --git a/public/app.js b/public/app.js index 1848258..1dcaec3 100644 --- a/public/app.js +++ b/public/app.js @@ -952,16 +952,43 @@ function thread(curve,vol,colorVar,nseg=1){ /* ================= Overview ================= */ const MINI={slab:0.18,D:4.6,lg:3.1,sp:1.05,gap:0.8}; -function miniPlan(s){ - let ext=0;const lv=s.zones.filter(z=>z.vlans.length).map((z,li)=>{ - const vs=z.vlans.map(v=>{const n=s.assets.filter(a=>a.vlan===v.id).length;return {v,n,w:Math.max(3.4,n*MINI.sp+1.4)};}); - const tot=vs.reduce((t,x)=>t+x.w,0)+(vs.length-1)*MINI.gap;let cx=-tot/2;vs.forEach(x=>{x.x0=cx+x.w/2;cx+=x.w+MINI.gap;}); - ext=Math.max(ext,tot);return {li,vs};}); - return {lv,ext:Math.max(ext,8)}; +// The rows of a zone in the small model: those of "Arrange VLANs" (or of plates placed by hand), read from +// the saved places, front to back and left to right; a zone never arranged keeps one row up to 8 VLANs, +// and wraps into a grid past that +function miniRows(vs){ + if(vs.some(v=>v.place)){ + const placed=vs.filter(v=>v.place).sort((a,b)=>a.place[1]-b.place[1]),rows=[];let rz=null; + placed.forEach(v=>{if(rz===null||v.place[1]-rz>2.5){rows.push([]);rz=v.place[1];}rows[rows.length-1].push(v);}); + rows.forEach(r=>r.sort((a,b)=>a.place[0]-b.place[0])); + const rest=vs.filter(v=>!v.place);if(rest.length)rows.push(rest); + return rows; + } + if(vs.length<=8)return [vs]; + const C=Math.ceil(Math.sqrt(vs.length*1.6)),rows=[];for(let i=0;iz.vlans.length).map((z,li)=>{ + const rows=miniRows(z.vlans).map(r=>{ + const vs=r.map(v=>{const n=s.assets.filter(a=>a.vlan===v.id).length; + // the assets of a plate wrap into lines, a little wider than deep + const ac=n<=6?Math.max(1,n):Math.ceil(Math.sqrt(n*3)),ar=Math.max(1,Math.ceil(n/ac)); + return {v,n,ac,ar,w:Math.max(3.4,ac*MINI.sp+1.4),D:Math.max(MINI.D,ar*0.95+2.2)};}); + const tot=vs.reduce((t,x)=>t+x.w,0)+(vs.length-1)*MINI.gap;let cx=-tot/2;vs.forEach(x=>{x.x0=cx+x.w/2;cx+=x.w+MINI.gap;}); + return {vs,tot,D:Math.max(...vs.map(x=>x.D))};}); + const lvD=rows.reduce((t,r)=>t+r.D,0)+(rows.length-1)*MINI.gap;let cz=-lvD/2; + rows.forEach(r=>{const zc=cz+r.D/2;r.vs.forEach(x=>{x.z0=zc;});cz+=r.D+MINI.gap;}); + ext=Math.max(ext,...rows.map(r=>r.tot));depth=Math.max(depth,lvD); + return {li,vs:rows.flatMap(r=>r.vs)};}); + // past 5 zones, the floors come closer: the small model stays about as tall as wide + const lg=lv.length>5?Math.max(1.4,MINI.lg*5/lv.length):MINI.lg; + return {lv,ext:Math.max(ext,8),depth,lg}; +} +// Shared geometries of the small models (one per shape and size, whatever the number of assets) +const MINI_GEO=new Map(),miniGeo=(key,make)=>{let g=MINI_GEO.get(key);if(!g){g=cachedGeo(make());MINI_GEO.set(key,g);}return g;}; function buildOverview(){ const plans=SITES.map(miniPlan),ex=Math.max(14,...plans.map(p=>p.ext)); - const n=SITES.length,cols=Math.max(1,Math.ceil(Math.sqrt(n))),rows=Math.max(1,Math.ceil(n/cols)),cellW=ex+9,cellD=18; + const n=SITES.length,cols=Math.max(1,Math.ceil(Math.sqrt(n))),rows=Math.max(1,Math.ceil(n/cols)),cellW=ex+9,cellD=Math.max(18,...plans.map(p=>p.depth+11)); OV={spanX:cols*cellW,spanZ:rows*cellD,ex,sites:{}}; const base=buildBase(OV.spanX,OV.spanZ); SITES.forEach((s,i)=>{ @@ -970,29 +997,32 @@ function buildOverview(){ const p=plans[i],g=new THREE.Group();g.position.set(px,TABLE_Y,pz); const mats=[];const M=o=>{const m=new THREE.MeshStandardMaterial(Object.assign({roughness:0.88,transparent:true},o));mats.push(m);return m;}; const card=M({color:base.card,flatShading:true}),dark=M({color:base.detail}),steel=M({color:0xb9bec2,metalness:0.6,roughness:0.35}); - const plinth=new THREE.Mesh(new THREE.BoxGeometry(p.ext+2.4,0.5,MINI.D+2.6),card);plinth.position.y=0.25;g.add(plinth); - const edge=new THREE.Mesh(new THREE.BoxGeometry(p.ext+2.42,0.08,MINI.D+2.62),dark);edge.position.y=0.04;g.add(edge); + const plinth=new THREE.Mesh(new THREE.BoxGeometry(p.ext+2.4,0.5,p.depth+2.6),card);plinth.position.y=0.25;g.add(plinth); + const edge=new THREE.Mesh(new THREE.BoxGeometry(p.ext+2.42,0.08,p.depth+2.62),dark);edge.position.y=0.04;g.add(edge); p.lv.forEach(({li,vs})=>{ - const y=1.5+li*MINI.lg; - vs.forEach(({v,n:cnt,w,x0})=>{ - const slab=new THREE.Mesh(new THREE.BoxGeometry(w,MINI.slab,MINI.D),M({color:new THREE.Color(css('--c'+v.ci))}));slab.position.set(x0,y,0);g.add(slab); - [-1,1].forEach(sx=>[-1,1].forEach(sz=>{const len=y-MINI.slab/2-0.5;const post=new THREE.Mesh(new THREE.CylinderGeometry(0.07,0.07,len,6),steel);post.position.set(x0+sx*(w/2-0.3),0.5+len/2,sz*(MINI.D/2-0.3));post.visible=state.posts!=='off';if(state.posts==='thin')post.scale.set(0.5,1,0.5);g.add(post);})); + const y=1.5+li*p.lg; + vs.forEach(({v,ac,ar,w,D,x0,z0})=>{ + const slab=new THREE.Mesh(new THREE.BoxGeometry(w,MINI.slab,D),M({color:new THREE.Color(css('--c'+v.ci))}));slab.position.set(x0,y,z0);g.add(slab); + [-1,1].forEach(sx=>[-1,1].forEach(sz=>{const len=y-MINI.slab/2-0.5;const post=new THREE.Mesh(miniGeo('post|'+len.toFixed(2),()=>new THREE.CylinderGeometry(0.07,0.07,len,6)),steel);post.position.set(x0+sx*(w/2-0.3),0.5+len/2,z0+sz*(D/2-0.3));post.visible=state.posts!=='off';if(state.posts==='thin')post.scale.set(0.5,1,0.5);g.add(post);})); + // severity colours shared by the plates of this IS + const sevMat={}; s.assets.filter(a=>a.vlan===v.id).forEach((a,j)=>{ - const h=MINI_H[a.type]||0.6,ax=x0+(j-(cnt-1)/2)*MINI.sp,ay=y+MINI.slab/2; - const geo=a.type==='db'?new THREE.CylinderGeometry(0.32,0.32,h,10):new THREE.BoxGeometry(a.type==='firewall'||a.type==='switch'?0.9:0.6,h,0.6); - const m=new THREE.Mesh(geo,card);m.position.set(ax,ay+h/2,0);g.add(m); - if(a.sev!=='none'){const sc=new THREE.Color(sevColor(a.sev)),sm=M({color:sc,emissive:sc,emissiveIntensity:0.15}); + const h=MINI_H[a.type]||0.6,ax=x0+((j%ac)-(ac-1)/2)*MINI.sp,az=z0+(Math.floor(j/ac)-(ar-1)/2)*0.95,ay=y+MINI.slab/2; + const wide=a.type==='firewall'||a.type==='switch'; + const geo=miniGeo(`${a.type==='db'?'db':wide?'wide':'box'}|${h}`,()=>a.type==='db'?new THREE.CylinderGeometry(0.32,0.32,h,10):new THREE.BoxGeometry(wide?0.9:0.6,h,0.6)); + const m=new THREE.Mesh(geo,card);m.position.set(ax,ay+h/2,az);g.add(m); + if(a.sev!=='none'){const sm=sevMat[a.sev]||(sevMat[a.sev]=(c=>M({color:c,emissive:c,emissiveIntensity:0.15}))(new THREE.Color(sevColor(a.sev)))); const ph=h+0.25+SEV[a.sev].rank*0.12; - const nd=new THREE.Mesh(new THREE.CylinderGeometry(0.02,0.02,ph,4),steel);nd.position.set(ax+0.3,ay+ph/2,0.15);g.add(nd); - const hd=new THREE.Mesh(new THREE.SphereGeometry(0.14,10,8),sm);hd.position.set(ax+0.3,ay+ph,0.15);g.add(hd);} + const nd=new THREE.Mesh(miniGeo('needle|'+ph.toFixed(2),()=>new THREE.CylinderGeometry(0.02,0.02,ph,4)),steel);nd.position.set(ax+0.3,ay+ph/2,az+0.15);g.add(nd); + const hd=new THREE.Mesh(miniGeo('head',()=>new THREE.SphereGeometry(0.14,10,8)),sm);hd.position.set(ax+0.3,ay+ph,az+0.15);g.add(hd);} }); }); }); - const top=1.5+Math.max(0,p.lv.length-1)*MINI.lg+2.2; + const top=1.5+Math.max(0,p.lv.length-1)*p.lg+2.2; const lab=tagLabel(`${s.name.toUpperCase()} · ${s.stats.crit} CRIT.`,s.stats.crit?sevColor('crit'):null,0.95,600);lab.position.set(0,top+1.0,0);g.add(lab); g.traverse(o=>{if(o.isMesh){o.castShadow=true;o.receiveShadow=true;o.userData.site=s;pickables.push(o);}}); scene.add(g); - OV.sites[s.id]={group:g,mats,label:lab,top,ext:p.ext,anchor:new THREE.Vector3(px,TABLE_Y+top-0.6,pz)}; + OV.sites[s.id]={group:g,mats,label:lab,top,ext:p.ext,depth:p.depth,anchor:new THREE.Vector3(px,TABLE_Y+top-0.6,pz)}; }); WAN.forEach(w=>{ const a=OV.sites[w.a].anchor,b=OV.sites[w.b].anchor,mid=a.clone().add(b).multiplyScalar(0.5);mid.y=Math.max(a.y,b.y)+4+a.distanceTo(b)*0.18; @@ -1005,7 +1035,7 @@ function refreshOverview(){invalidate(); SITES.forEach(s=>{const o=OV.sites[s.id];if(!o)return;const op=sel&&sel!==s?0.22:1;o.mats.forEach(m=>{m.opacity=op;m.depthWrite=op>0.5;});o.label.material.opacity=op>0.5?1:0.45;}); WAN.forEach(w=>{if(!w.tube)return;const vis=state.flows&&(!state.riskOnly||w.risk);let op=w.risk?0.95:0.85;if(sel)op=(w.a===sel.id||w.b===sel.id)?1:0.08; w.mat.opacity=op;w.mat.depthWrite=op>0.5;w.tube.visible=vis;w.parts.forEach(p=>p.visible=vis&&op>0.5);}); - if(sel&&OV.sites[sel.id]){const o=OV.sites[sel.id];selRing.scale.set((o.ext+5)/2,1,(MINI.D+5)/2);selRing.position.set(o.group.position.x,TABLE_Y+0.02,o.group.position.z);selRing.visible=true;} + if(sel&&OV.sites[sel.id]){const o=OV.sites[sel.id];selRing.scale.set((o.ext+5)/2,1,(o.depth+5)/2);selRing.position.set(o.group.position.x,TABLE_Y+0.02,o.group.position.z);selRing.visible=true;} else selRing.visible=false; syncUI(); } From 25835e7968d516a76d9d637f90d4aa5249d796e1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:54:59 +0000 Subject: [PATCH 13/16] Palo Alto: build the IS from a configuration "Build the IS" on the file import: zones from the security zones, VLANs from the layer-3 (sub-)interfaces outside the Internet zones (tag, subnet, comment), assets from the host address objects of private ranges (in the VLAN of their IP; the type from their name or description, else the ports opened to them, else a workstation when they only open connections) and the firewall itself, crossed by the flows that change VLAN; then the rules as flows. What the map has is kept and the same file again changes nothing. An import that draws nothing no longer opens an empty proposal. Tests for both; docs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- CHANGELOG.md | 2 + MODULES.md | 2 +- docs/docs.html | 1 + package.json | 2 +- server/connectors/builtin/paloalto/index.js | 167 +++++++++++++++++- .../builtin/paloalto/strata-module.json | 11 +- tests/modules.test.mjs | 4 +- tests/paloalto.test.mjs | 94 ++++++++++ 8 files changed, 268 insertions(+), 15 deletions(-) create mode 100644 tests/paloalto.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e89913..50d227a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem - **Memory and CPU needs**, measured from an empty map to 30 000 assets (README, section 1), and resource limits, commented out, in `docker-compose.yml`. - **The overview shows each IS in the compact form of its model**: each zone of the small model takes the rows and the order of *Arrange VLANs* (or of plates placed by hand); a zone never arranged keeps one row up to 8 VLANs, then wraps into a grid; the assets of a plate wrap into lines; past 5 zones the floors come closer. The small models share their geometries, lighter for a large IS. - **The title block folds into one line** (▴ in its corner): the name of the IS or *Strata*, the four key figures, back to the overview, sign out and the incident band stay; the controls move up with it. Remembered by the browser. +- **Palo Alto: build the IS from a configuration** (*Build the IS*, a box of the file import): zones from the security zones, VLANs from the layer-3 sub-interfaces (tag, subnet, comment), assets from the host address objects (in the VLAN of their IP, the type from their name or description, else the ports opened to them, else a workstation when they only open connections) and the firewall itself, crossed by the flows that change VLAN; then the rules as flows, as before. What the map has is kept; the same file again changes nothing. On the 991 objects of the test configuration, 90% get the right type. +- Palo Alto: an import that draws nothing no longer opens an empty proposal. - **Suggest zones from the flows** (*Edit*, and *Layout*): the zones of an IS from its flows, the Internet on top. Each VLAN goes to the floor of its distance from the Internet along the flows (*Exposed to the Internet*, *Reached from the exposed zone*, *Back end, 3 hops or more*); the VLANs the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration); empty VLANs stay where they are. Existing zones that hold most of the same VLANs keep their name. A window shows the result top first: rename a zone, move a VLAN, then *Apply* (approvers) or *Propose* (contributors: a draft proposal opens). - **CSV VLANs**, a module in the store: the VLANs of an IS from a CSV (number, name, range, in any order, with or without a header). After the file, a window lists its VLANs: tick those to bring, filter, and choose the zone of each, or of all the ticked ones at once (a zone of the IS, a zone named in the file, or a new one). The result is a proposal, which any contributor can make; from a script, `picks` gives the same choice. - **Modules can ask for a choice before an import** (`file.pick` in the manifest, a `preview(cfg, ctx)` function, `ctx.picks` in `run`, `POST /api/connectors/{id}/preview`): Strata shows what the file holds, people tick what to bring and, with `pick.zones`, the zone of each. Backward compatible: modules without `pick` work as before. See MODULES.md. diff --git a/MODULES.md b/MODULES.md index f703f0c..c83c252 100644 --- a/MODULES.md +++ b/MODULES.md @@ -28,7 +28,7 @@ has; administrators also see a *Module store* tab. | Cyberwatch | Built-in | Servers and their CVEs (API v3) | Approvers | | GLPI | Built-in | Computers, network devices, printers, phones, software, people in charge | Approvers | | phpIPAM | Built-in | Sections, subnets, VLANs, addresses | Approvers | -| Palo Alto | Built-in | Allow rules, as flows: from the XML API, or a configuration file | Approvers; any contributor for a file | +| Palo Alto | Built-in | Allow rules, as flows: from the XML API, or a configuration file. *Build the IS*: also the zones, the VLANs (layer-3 sub-interfaces), the assets (host address objects) and the firewall | Approvers; any contributor for a file | | CSV inventory | Store | Assets of an IS from a spreadsheet saved as CSV | Any contributor (file import) | | CSV VLANs | Store | VLANs of an IS (number, name, range): you choose the VLANs and the zone of each | Any contributor (file import) | | Nmap scan | Store | Hosts, systems and open ports from `nmap -oX` | Any contributor (file import) | diff --git a/docs/docs.html b/docs/docs.html index 1e50945..b9cbe54 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -275,6 +275,7 @@

    Palo Alto

  3. any in an untrust, internet, outside or wan zone → the Internet;
  4. the chosen firewall as the device crossed; Internet-to-admin or database ports flagged as risky.
  5. +

    Build the IS (a box of the file import): on an empty or incomplete IS, the configuration also gives what the map lacks. A zone per security zone; a VLAN per layer-3 interface or sub-interface outside the Internet zones (its tag and subnet, named after its comment; an untagged interface gets a number from 4001 on, to change); an asset per host address object of a private range, in the VLAN whose range holds it, its type from its name or description (PC-, TEL-, SQL-, CAM-…), else from the ports opened to it, else a workstation if it only opens connections; and the firewall itself, from its host name and inside address, crossed by the flows that change VLAN. What the map has (a zone of the same name, a VLAN of the same number or range, an asset at the same IP) is kept; the same file again changes nothing. Systems, vulnerabilities and contacts still come from an inventory or a scanner.

    What cannot be drawn (internal any→any, dynamic groups, negations, addresses with no asset) is listed in the proposal. Flows from a rule that disappeared are removed; flows drawn by hand are never touched. A proposal from a file counts as the importer’s own for the four-eyes rule.

    Modules and the module store

    Each connector is a module: a folder with a manifest (its settings, whether it imports files, what it may reach) and its code. The four above ship with Strata; three more come from the store:

    diff --git a/package.json b/package.json index 1352fc7..b41cc26 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "scripts": { "vendor": "node scripts/vendor.mjs", "start": "node server/server.js", - "test": "node --test tests/api.test.mjs tests/analysis.test.mjs tests/incident.test.mjs tests/risk.test.mjs tests/kpi.test.mjs tests/muster.test.mjs tests/secrets.test.mjs tests/signin.test.mjs tests/hardening.test.mjs tests/readonly.test.mjs tests/modules.test.mjs tests/sso.test.mjs", + "test": "node --test tests/api.test.mjs tests/analysis.test.mjs tests/incident.test.mjs tests/risk.test.mjs tests/kpi.test.mjs tests/muster.test.mjs tests/secrets.test.mjs tests/signin.test.mjs tests/hardening.test.mjs tests/readonly.test.mjs tests/modules.test.mjs tests/sso.test.mjs tests/paloalto.test.mjs", "test:e2e": "node --test tests/e2e.mjs", "setup": "npm ci && npm run vendor", "build:online": "node scripts/build-online.mjs" diff --git a/server/connectors/builtin/paloalto/index.js b/server/connectors/builtin/paloalto/index.js index 96c8516..4aa6edd 100644 --- a/server/connectors/builtin/paloalto/index.js +++ b/server/connectors/builtin/paloalto/index.js @@ -5,7 +5,7 @@ // Each "allow" security rule becomes flows: source address objects → assets of the IS (by IP) or the // Internet / an Internet service (public addresses, FQDN), destination likewise, services → ports, and the // firewall as the device the flows cross. -import { request, expectOk, ConnectorError, stripTags, parseXml, kids, kid, path, txt, members, parseRange, isPrivateRange, inRange, isIp, slug, hash, norm } from '../../sdk.js'; +import { request, expectOk, ConnectorError, stripTags, parseXml, kids, kid, path, txt, members, parseRange, isPrivateRange, inRange, isIp, slug, hash, norm, intToIp, vlanForIp, holdingVlan } from '../../sdk.js'; // Ports of the most common App-IDs, for rules set to "application-default" const APP_PORTS = { @@ -51,12 +51,13 @@ function findConfig(root) { } function objectsOf(el) { // keys are object names from the configuration file: no prototype, so "__proto__" or "constructor" are just names - const o = { address: Object.create(null), group: Object.create(null), service: Object.create(null), svcGroup: Object.create(null) }; + const o = { address: Object.create(null), group: Object.create(null), service: Object.create(null), svcGroup: Object.create(null), description: Object.create(null) }; if (!el) return o; for (const e of path(el, 'address', 'entry')) { const name = e.attrs.name; if (!name) continue; const v = kid(e, 'ip-netmask') || kid(e, 'ip-range') || kid(e, 'fqdn') || kid(e, 'ip-wildcard'); o.address[name] = v ? { kind: v.name === 'fqdn' ? 'fqdn' : v.name === 'ip-wildcard' ? 'wildcard' : 'ip', value: txt(v) } : { kind: 'unknown', value: '' }; + const d = txt(kid(e, 'description')); if (d) o.description[name] = d.slice(0, 120); } for (const e of path(el, 'address-group', 'entry')) { const name = e.attrs.name; if (!name) continue; @@ -105,7 +106,149 @@ export function parseConfig(xml) { // Panorama order: shared pre, device-group pre, device-group post, shared post (the evaluation order of PAN-OS) const rank = r => r.where === 'pre-rulebase' ? (r.scope === 'shared' ? 0 : 1) : r.where === 'post-rulebase' ? (r.scope === 'shared' ? 3 : 2) : 1; rules.sort((a, b) => rank(a) - rank(b)); - return { kind: dgs.length ? 'panorama' : 'firewall', objects, rules }; + return { kind: dgs.length ? 'panorama' : 'firewall', objects, rules, network: networkOf(config) }; +} +// The network side of the configuration, for building an IS: the layer-3 interfaces and sub-interfaces with +// their address and VLAN tag, the zone of each, and the firewall's host name. On Panorama, from its templates. +function networkOf(config) { + const roots = [config, ...path(config, 'devices', 'entry', 'template', 'entry', 'config')]; + const interfaces = [], zoneOf = Object.create(null); + let hostname = ''; + for (const root of roots) for (const dev of path(root, 'devices', 'entry')) { + hostname = hostname || txt(path(dev, 'deviceconfig', 'system', 'hostname')[0]); + const itf = kid(kid(dev, 'network'), 'interface'); + const add = (e, tag) => { + const ips = path(e, 'ip', 'entry').map(x => x.attrs.name).filter(Boolean); + if (e.attrs.name) interfaces.push({ name: e.attrs.name, tag, ips, comment: txt(kid(e, 'comment')) }); + }; + for (const kind of ['ethernet', 'aggregate-ethernet']) for (const e of path(itf, kind, 'entry')) { + const l3 = kid(e, 'layer3'); + if (l3) add(l3 && { ...l3, attrs: e.attrs, children: [...l3.children, ...kids(e, 'comment')] }, null); + for (const u of path(l3, 'units', 'entry')) add(u, parseInt(txt(kid(u, 'tag')), 10) || null); + } + const vl = kid(itf, 'vlan'); + if (vl) for (const u of path(vl, 'units', 'entry')) add(u, parseInt(txt(kid(u, 'tag')), 10) || parseInt(String(u.attrs.name).split('.')[1], 10) || null); + for (const v of [...path(dev, 'vsys', 'entry')]) for (const z of path(v, 'zone', 'entry')) { + const net = kid(z, 'network'); + for (const k of ['layer3', 'layer2', 'virtual-wire', 'tap']) for (const m of members(kid(net, k))) zoneOf[m] = z.attrs.name; + } + } + return { interfaces, zoneOf, hostname }; +} + +/* ---------------- building the IS ---------------- */ +// What the ports opened to an address say about it (the first that matches); a server otherwise +const TYPE_BY_PORT = [ + [['tcp/502', 'tcp/102', 'tcp/44818', 'udp/47808', 'tcp/20000', 'tcp/2404'], 'plc'], + [['tcp/1433', 'tcp/1521', 'tcp/3306', 'tcp/5432', 'tcp/27017', 'tcp/5022'], 'db'], + [['tcp/88', 'udp/88', 'tcp/636', 'tcp/3268'], 'dc'], + [['tcp/554'], 'camera'], [['tcp/9100', 'tcp/631', 'tcp/515'], 'printer'], + [['tcp/902', 'tcp/3260', 'tcp/8000'], 'hypervisor'], +]; +// What the name or description of an address object says (naming conventions: PC-…, TEL-…, CAM-…, SQL-…) +const TYPE_BY_WORD = [ + [['pc', 'ws', 'wks', 'poste', 'postes', 'laptop', 'desktop', 'workstation', 'portable', 'vdi'], 'workstation'], + [['tel', 'phone', 'voip', 'ipphone', 'telephone', 'sip'], 'phone'], + [['cam', 'camera', 'cctv', 'video', 'videosurveillance'], 'camera'], + [['imp', 'prn', 'print', 'printer', 'imprimante', 'mfp'], 'printer'], + [['ap', 'wifi', 'borne', 'wap'], 'ap'], + [['plc', 'automate', 'automates', 'rtu'], 'plc'], + [['cap', 'sensor', 'capteur', 'iot', 'badge', 'badgeuse', 'compteur', 'meter'], 'iot'], + [['sql', 'db', 'database', 'ora', 'oracle', 'pg', 'postgres', 'mysql', 'mongo'], 'db'], + [['dc', 'ad', 'adds', 'domain'], 'dc'], + [['esx', 'esxi', 'hv', 'hyperv', 'hypervisor', 'nutanix', 'san', 'nas', 'storage', 'stockage'], 'hypervisor'], + [['fw', 'firewall', 'pare'], 'firewall'], + [['rtr', 'router', 'routeur'], 'router'], + [['sw', 'switch', 'commutateur', 'net', 'netdev'], 'switch'], + [['vpn', 'gateway', 'gw'], 'gateway'], +]; +// Adds to the IS what the configuration describes and the map lacks: a zone per security zone, a VLAN per +// layer-3 sub-interface (tag and subnet; untagged interfaces get a number from 4001 on, to change), an asset +// per host address object of a private range (in the VLAN whose range holds it), and the firewall. What is +// already there (same zone name, same VLAN number or range, an asset with the same IP) is kept as it is. +function buildIs(parsed, cfg, state, si, assets, netZones) { + const siteId = cfg.site, N = parsed.network || { interfaces: [], zoneOf: {}, hostname: '' }, stats = { zonesAdded: 0, vlansAdded: 0, assetsAdded: 0, firewallAdded: 0 }; + si.zones = si.zones && si.zones.length ? si.zones : []; + si.vlans = si.vlans || []; + const zoneId = name => { + let z = si.zones.find(x => norm(x.name) === norm(name)); + if (!z) { let id = 'z-pa-' + slug(name); for (let i = 2; si.zones.some(x => x.id === id); i++) id = `z-pa-${slug(name)}-${i}`; z = { id, name: String(name).slice(0, 40) }; si.zones.push(z); stats.zonesAdded++; } + return z.id; + }; + const addrOf = name => { for (const o of Object.values(parsed.objects)) { const a = o.address[name]; if (a && a.kind === 'ip') return a.value; } return name; }; + // VLANs from the interfaces of the zones that are not the Internet + const fwIps = []; + let untagged = 4001; + for (const itf of N.interfaces) { + const zone = N.zoneOf[itf.name]; + for (const raw of itf.ips) { + const v4 = addrOf(raw), m = /^(\d+\.\d+\.\d+\.\d+)\/(\d{1,2})$/.exec(v4); + if (!m || !isIp(m[1])) continue; + if (!zone || netZones.includes(norm(zone))) { fwIps.push({ ip: m[1], net: true }); continue; } + fwIps.push({ ip: m[1], net: false }); + const r = parseRange(`${m[1]}/${m[2]}`), cidr = `${intToIp(r.lo)}/${m[2]}`; + if (+m[2] >= 31) continue; + const num = itf.tag && itf.tag >= 1 && itf.tag <= 4094 ? itf.tag : null; + const same = si.vlans.find(v => v.cidr === cidr) || (num && si.vlans.find(v => +v.num === num)); + if (same) { if (!same.cidr) same.cidr = cidr; continue; } + let n = num; + if (!n) { while (si.vlans.some(v => +v.num === untagged)) untagged++; n = untagged++; } + si.vlans.push({ id: `v-pa-${slug(siteId)}-${n}`, num: n, name: (itf.comment || `${zone} ${itf.name}${num ? '' : ' (untagged)'}`).slice(0, 50), cidr, zone: zoneId(zone) }); + stats.vlansAdded++; + } + } + if (!si.zones.length) si.zones.push({ id: 'z-def', name: 'Main zone' }); + // the ports opened to each address, from the allow rules, to guess its type + const portsTo = Object.create(null); + for (const r of parsed.rules) { + if (r.action !== 'allow' || r.disabled) continue; + const ports = r.service.flatMap(sv => { const o = Object.values(parsed.objects).map(x => x.service[sv]).find(Boolean); const mm = /^(tcp|udp)[-_/ ]?(\d+)$/i.exec(sv); return o || (mm ? [`${mm[1].toLowerCase()}/${mm[2]}`] : []); }); + for (const d of r.destination) (portsTo[d] = portsTo[d] || new Set()) && ports.forEach(p => portsTo[d].add(p)); + } + const sources = new Set(parsed.rules.filter(r => r.action === 'allow' && !r.disabled).flatMap(r => r.source)); + // the type: its name or description first, then the ports opened to it, then an address that only opens + // connections is a workstation, and anything else a server + const typeOf = (name, desc) => { + const words = new Set(norm(`${name} ${desc || ''}`).split(' ')); + for (const [list, t] of TYPE_BY_WORD) if (list.some(w => words.has(w))) return t; + const p = portsTo[name]; + if (p) for (const [list, t] of TYPE_BY_PORT) if (list.some(x => p.has(x))) return t; + if (!p && sources.has(name)) return 'workstation'; + return 'server'; + }; + // assets from host objects of private ranges that no asset of the IS has yet + const byIp = new Map(assets.map(([id, a]) => [a.ip, id])), out = {}; + for (const [scope, o] of Object.entries(parsed.objects)) { + if (cfg.scope && scope !== cfg.scope && scope !== 'shared') continue; + for (const [name, a] of Object.entries(o.address)) { + if (a.kind !== 'ip') continue; + const ip = /^(\d+\.\d+\.\d+\.\d+)(?:\/32)?$/.exec(a.value); + if (!ip || !isIp(ip[1]) || !isPrivateRange(parseRange(ip[1])) || byIp.has(ip[1]) || fwIps.some(f => f.ip === ip[1])) continue; + if (Object.keys(out).length >= 5000) break; + const vlan = vlanForIp(si, ip[1]) || holdingVlan(si, siteId, 'To sort (Palo Alto)'); + let id = `pa-${slug(siteId)}-${slug(name)}`; + for (let i = 2; state.assets[id] || out[id]; i++) id = `pa-${slug(siteId)}-${slug(name)}-${i}`; + out[id] = { site: siteId, name: name.slice(0, 80), type: typeOf(name, o.description && o.description[name]), vlan: vlan.id, ip: ip[1], os: '', role: (o.description && o.description[name]) || '', crit: 2, vulns: [], contacts: [], apps: [], notes: 'Created from a Palo Alto address object.', ext: { paloalto: name } }; + byIp.set(ip[1], id); stats.assetsAdded++; + } + } + // the firewall itself: an existing firewall asset at one of its addresses, or a new one + let firewall = null; + const inside = fwIps.find(f => !f.net); + const existing = assets.find(([, a]) => a.type === 'firewall' && fwIps.some(f => f.ip === a.ip)) || (!inside && assets.find(([, a]) => a.type === 'firewall')); + if (existing) firewall = existing[0]; + else if (inside) { + const name = N.hostname || 'Palo Alto firewall', id = `pa-${slug(siteId)}-fw-${slug(name)}`; + if (!state.assets[id]) { + const vlan = vlanForIp(si, inside.ip) || holdingVlan(si, siteId, 'To sort (Palo Alto)'); + out[id] = { site: siteId, name, type: 'firewall', vlan: vlan.id, ip: inside.ip, os: 'PAN-OS', role: 'Firewall', crit: 3, vulns: [], contacts: [], apps: [], notes: 'Created from the Palo Alto configuration.', ext: { paloalto: 'firewall' } }; + stats.firewallAdded = 1; + } + firewall = id; + } + // the new assets take part in the rules like the others + for (const [id, a] of Object.entries(out)) assets.push([id, a]); + return { assets: out, firewall, stats }; } /* ---------------- turning rules into flows ---------------- */ @@ -116,8 +259,11 @@ export function rulesToChanges(parsed, cfg, state) { const si = JSON.parse(JSON.stringify(si0)); si.flows = si.flows || []; si.services = si.services || []; const assets = Object.entries(state.assets).filter(([, a]) => a.site === siteId); - const fw = cfg.firewall && state.assets[cfg.firewall] && state.assets[cfg.firewall].site === siteId ? cfg.firewall : null; const netZones = String(cfg.internetZones || 'untrust, internet, outside, wan').split(/[\s,;]+/).map(norm).filter(Boolean); + // "Build the IS": zones, VLANs, assets and the firewall from the configuration, before the flows + const built = cfg.build ? buildIs(parsed, cfg, state, si, assets, netZones) : null; + const newAssets = built ? built.assets : {}; + const fw = cfg.firewall && state.assets[cfg.firewall] && state.assets[cfg.firewall].site === siteId ? cfg.firewall : built && built.firewall ? built.firewall : null; const maxExpand = Math.max(1, parseInt(cfg.maxExpand, 10) || 12); const scopeFilter = String(cfg.scope || '').trim(); const warn = [], skipped = []; @@ -194,6 +340,8 @@ export function rulesToChanges(parsed, cfg, state) { return [...out]; }; + const vlanOfId = id => (newAssets[id] || state.assets[id] || {}).vlan; + const sameVlan = (a, b) => !a.startsWith('svc:') && a !== 'internet' && !b.startsWith('svc:') && b !== 'internet' && vlanOfId(a) && vlanOfId(a) === vlanOfId(b); const imported = new Map(); // key src>dst>port → flow let used = 0, ignored = 0; for (const r of parsed.rules) { @@ -217,7 +365,7 @@ export function rulesToChanges(parsed, cfg, state) { imported.set(key, { id: 'f-pa-' + hash(key), src: s, dst: d, port: p, vol: 1, risk, note: `Palo Alto rule “${r.name}”${r.description ? ` — ${r.description.slice(0, 120)}` : ''}${r.application.length && !r.application.includes('any') ? ` · App-ID: ${r.application.join(', ')}` : ''}${risk ? ' · administration or database port open from the Internet' : ''}`, - ...(fw && s !== fw && d !== fw ? { via: [fw] } : {}), + ...(fw && s !== fw && d !== fw && !(built && sameVlan(s, d)) ? { via: [fw] } : {}), pan: { rules: [r.name], scope: r.scope }, }); } @@ -241,10 +389,11 @@ export function rulesToChanges(parsed, cfg, state) { si.flows = si.flows.filter(f => !f.pan || imported.has(keyOf(f))); removed = before - si.flows.length; } - const changed = JSON.stringify(si) !== JSON.stringify(si0); + // the empty lists added above are no change: compare the IS as it was, with them + const changed = JSON.stringify(si) !== JSON.stringify({ ...si0, flows: si0.flows || [], services: si0.services || [] }) || Object.keys(newAssets).length > 0; return { - changes: changed ? { si: { [siteId]: si }, assets: {} } : null, - stats: { rules: parsed.rules.length, used, ignored, flows: imported.size, added, tagged, removed, services: si.services.length - (si0.services || []).length }, + changes: changed ? { si: { [siteId]: si }, assets: newAssets } : null, + stats: { rules: parsed.rules.length, used, ignored, flows: imported.size, added, tagged, removed, services: si.services.length - (si0.services || []).length, ...(built ? built.stats : {}) }, warnings: [...new Set(warn)].slice(0, 40), skipped: [...new Set(skipped)].slice(0, 60), }; } @@ -255,7 +404,7 @@ export async function run(cfg, ctx) { const res = rulesToChanges(parsed, cfg, ctx.state); const siName = ctx.state.si[cfg.site]?.name || cfg.site; const st = res.stats; - const summary = `${st.rules} rule(s) read (${parsed.kind}), ${st.used} allow rule(s) drawn, ${st.ignored} deny or disabled ignored; ${st.added} new flow(s), ${st.removed} removed, ${st.services} new Internet service(s).`; + const summary = `${cfg.build ? `IS built: ${st.zonesAdded} new zone(s), ${st.vlansAdded} new VLAN(s), ${st.assetsAdded} new asset(s)${st.firewallAdded ? ' and the firewall' : ''}; ` : ''}${st.rules} rule(s) read (${parsed.kind}), ${st.used} allow rule(s) drawn, ${st.ignored} deny or disabled ignored; ${st.added} new flow(s), ${st.removed} removed, ${st.services} new Internet service(s).`; return { proposals: res.changes ? [{ siteId: cfg.site, diff --git a/server/connectors/builtin/paloalto/strata-module.json b/server/connectors/builtin/paloalto/strata-module.json index c745fa7..f662dd6 100644 --- a/server/connectors/builtin/paloalto/strata-module.json +++ b/server/connectors/builtin/paloalto/strata-module.json @@ -23,9 +23,10 @@ "fields": [ "site", "firewall", - "scope" + "scope", + "build" ], - "help": "On the firewall: Device › Setup › Operations › Export named configuration snapshot (running-config.xml). On Panorama: Panorama › Setup › Operations, same export; then give the device group below. Allow rules become flows: address objects are matched to the assets of the IS by IP, public addresses and FQDNs to Internet services." + "help": "On the firewall: Device › Setup › Operations › Export named configuration snapshot (running-config.xml). On Panorama: Panorama › Setup › Operations, same export; then give the device group below. Allow rules become flows: address objects are matched to the assets of the IS by IP, public addresses and FQDNs to Internet services. Tick “Build the IS” on an empty or incomplete IS: the configuration also gives the zones, the VLANs (layer-3 sub-interfaces), the assets (host address objects) and the firewall." }, "fields": [ { @@ -57,6 +58,12 @@ "type": "site", "required": true }, + { + "k": "build", + "label": "Build the IS: zones, VLANs (interfaces), assets (address objects) and the firewall, when the map lacks them", + "type": "bool", + "default": false + }, { "k": "firewall", "label": "Firewall asset the flows cross", diff --git a/tests/modules.test.mjs b/tests/modules.test.mjs index 39602b2..474b309 100644 --- a/tests/modules.test.mjs +++ b/tests/modules.test.mjs @@ -122,7 +122,7 @@ test('the built-in modules, the server-provided ones, and the folders that are n const by = Object.fromEntries(r.data.connectors.map(c => [c.id, c])); for (const id of ['cyberwatch', 'glpi', 'phpipam', 'paloalto']) assert.equal(by[id].origin, 'builtin', id); assert.equal(by.paloalto.badge, 'FW'); - assert.deepEqual(by.paloalto.file.fields, ['site', 'firewall', 'scope']); + assert.deepEqual(by.paloalto.file.fields, ['site', 'firewall', 'scope', 'build']); assert.equal(by.spy.origin, 'provided'); assert.equal(by.glpi.name, 'GLPI', 'a provided module cannot take a built-in id'); const problems = r.data.problems.map(p => p.folder + ': ' + p.error).join('\n'); @@ -131,7 +131,7 @@ test('the built-in modules, the server-provided ones, and the folders that are n // a contributor only sees the file imports open to them, with their own settings const a = await api('alice', 'GET', '/api/connectors'); assert.deepEqual(a.data.connectors.map(c => c.id), ['paloalto']); - assert.deepEqual(Object.keys(a.data.connectors[0].settings).sort(), ['firewall', 'scope', 'site']); + assert.deepEqual(Object.keys(a.data.connectors[0].settings).sort(), ['build', 'firewall', 'scope', 'site']); assert.equal(a.data.problems, undefined); }); diff --git a/tests/paloalto.test.mjs b/tests/paloalto.test.mjs new file mode 100644 index 0000000..46e752f --- /dev/null +++ b/tests/paloalto.test.mjs @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +// Palo Alto module: building an IS from a configuration (zones, VLANs from the interfaces, assets from the +// address objects, the firewall), then the rules as flows. Run with: npm test +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { parseConfig, rulesToChanges } from '../server/connectors/builtin/paloalto/index.js'; + +const XML = ` + FW-PARIS + + + + + 20Servers + 30Users + + + + + ethernet1/1 + ethernet1/2ethernet1/2.20 + ethernet1/2.30 + +
    + 10.30.0.1/23 + 10.20.0.10ERP database + 10.20.0.11/32 + 10.0.0.50 + 10.30.0.77 + 192.168.99.9 + 10.30.0.0/23 + 203.0.113.10 +
    + 502 + + trusttrustapp-01SQL-01anytcp-1433allow + userstrusthost-77app-01anytcp-443allow + trusttrustapp-01plc-lineanytcp-502allow + untrusttrustanyapp-01anytcp-443allow + +
    +
    `; + +const build = state => rulesToChanges(parseConfig(XML), { site: 'p', build: true }, state); + +test('Palo Alto, build the IS: zones, VLANs from the interfaces, assets from the address objects, the firewall', () => { + const r = build({ si: { p: { name: 'Paris' } }, assets: {}, services: [] }); + const si = r.changes.si.p, A = r.changes.assets, by = n => Object.values(A).find(a => a.name === n); + // zones of the interfaces that are not the Internet (and the zone of "To sort", for the host outside them) + assert.deepEqual(si.zones.map(z => z.name), ['trust', 'users', 'Imported']); + // a VLAN per subnet: tagged sub-interfaces keep their tag, the untagged interface gets 4001; an interface + // address given as an object is read through it; the comment names the VLAN + assert.deepEqual(si.vlans.filter(v => !/unsorted$/.test(v.id)).map(v => [v.num, v.name, v.cidr, si.zones.find(z => z.id === v.zone).name]), + [[4001, 'trust ethernet1/2 (untagged)', '10.0.0.0/24', 'trust'], [20, 'Servers', '10.20.0.0/24', 'trust'], [30, 'Users', '10.30.0.0/23', 'users']]); + // the type: from the name or description, from the ports opened to it, a client only is a workstation + assert.equal(by('SQL-01').type, 'db'); + assert.equal(by('SQL-01').role, 'ERP database'); + assert.equal(by('plc-line').type, 'plc'); + assert.equal(by('host-77').type, 'workstation'); + assert.equal(by('app-01').type, 'server'); + assert.equal(by('SQL-01').vlan, si.vlans.find(v => v.num === 20).id); + // a host outside every VLAN waits in "To sort"; ranges, public addresses and the firewall's own are no assets + assert.match(by('lost').vlan, /unsorted$/); + for (const n of ['users-net', 'partner', 'gw-users']) assert.equal(by(n), undefined, n); + // the firewall: its host name, its inside address + const fw = Object.entries(A).find(([, a]) => a.type === 'firewall'); + assert.deepEqual([fw[1].name, fw[1].ip], ['FW-PARIS', '10.0.0.1']); + // flows: through the firewall when they change VLAN or come from the Internet, not inside a VLAN + const f = (s, d) => si.flows.find(x => x.src === s && x.dst === d); + const id = n => Object.entries(A).find(([, a]) => a.name === n)[0]; + assert.equal(f(id('app-01'), id('SQL-01')).via, undefined); + assert.deepEqual(f(id('host-77'), id('app-01')).via, [fw[0]]); + assert.deepEqual(f('internet', id('app-01')).via, [fw[0]]); + assert.equal(r.stats.vlansAdded, 3); + assert.equal(r.stats.assetsAdded, 5); +}); + +test('Palo Alto, build the IS: what the map has is kept, and the same file again changes nothing', () => { + const first = build({ si: { p: { name: 'Paris', zones: [{ id: 'z9', name: 'TRUST' }], vlans: [{ id: 'mine', num: 20, name: 'My servers', zone: 'z9' }] } }, + assets: { mydb: { site: 'p', name: 'erp-db', type: 'db', vlan: 'mine', ip: '10.20.0.10' } }, services: [] }); + const si = first.changes.si.p; + assert.equal(si.vlans.find(v => v.num === 20).id, 'mine', 'the VLAN of the map is kept, and gets its range'); + assert.equal(si.vlans.find(v => v.num === 20).cidr, '10.20.0.0/24'); + assert.equal(si.zones.filter(z => /trust/i.test(z.name)).length, 1, 'a zone of the same name is reused'); + assert.ok(!Object.values(first.changes.assets).some(a => a.ip === '10.20.0.10'), 'an asset already at that IP is not created again'); + const state = { si: { p: si }, assets: { mydb: { site: 'p', name: 'erp-db', type: 'db', vlan: 'mine', ip: '10.20.0.10' }, ...first.changes.assets }, services: [] }; + assert.equal(build(state).changes, null); +}); + +test('Palo Alto without "build": an empty IS gives nothing to draw, and no empty proposal', () => { + const r = rulesToChanges(parseConfig(XML), { site: 'p' }, { si: { p: { name: 'Paris' } }, assets: {}, services: [] }); + assert.equal(r.changes, null); + assert.ok(r.skipped.some(x => /no asset of the IS at 10\.20\.0\.10/.test(x))); +}); From 62747d2913da58cf53ec45bfc6ed60e7d899f614 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 23:57:40 +0000 Subject: [PATCH 14/16] tests: the large-IS test gets a port range of its own It shared 9400-9489 with the sign-in and Muster tests, which run at the same time. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh --- tests/hardening.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hardening.test.mjs b/tests/hardening.test.mjs index ce9b307..1f8d1c1 100644 --- a/tests/hardening.test.mjs +++ b/tests/hardening.test.mjs @@ -52,7 +52,7 @@ test('data files are readable by the server user only, including those of older test('a large IS (3500 flows) can be saved, and the history stops at HISTORY_MAX_MB', async () => { const dataDir = mkdtempSync(join(tmpdir(), 'strata-big-')); - const port = 9300 + Math.floor(Math.random() * 90) + 100, pass = 'admin-pass-123', base = `http://127.0.0.1:${port}`; + const port = 13100 + Math.floor(Math.random() * 400), pass = 'admin-pass-123', base = `http://127.0.0.1:${port}`; const proc = spawn(process.execPath, [join(root, 'server/server.js')], { env: { ...process.env, PORT: String(port), HOST: '127.0.0.1', DATA_DIR: dataDir, EDIT_USER: 'admin', EDIT_PASS: pass, VIEW_PASS: '', CONNECTORS: 'false', HISTORY_MAX_MB: '1' }, stdio: ['ignore', 'pipe', 'inherit'], From 5efa79ca265aa065d9164debb7bd289361a27607 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 19:24:25 +0000 Subject: [PATCH 15/16] Release 0.11.0 Version 0.11.0 in package.json, docker-compose.yml, the interface and the website; README section "New in v0.11" and roadmap row; CHANGELOG section dated; the documentation site covers sign-in and sign-out, Import VLANs, Arrange VLANs, Suggest zones, the Graphics setting, the folding title block and the CSV VLANs module. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh Signed-off-by: Claude --- CHANGELOG.md | 3 +++ README.md | 23 +++++++++++++++++------ docker-compose.yml | 2 +- docs/api.html | 2 +- docs/docs.html | 22 +++++++++++++++------- docs/index.html | 7 ++++--- package-lock.json | 4 ++-- package.json | 2 +- public/index.html | 2 +- 9 files changed, 45 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50d227a..045fea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://semver.org/). ## [Unreleased] + +## [0.11.0] - 2026-11-08 ### Upgrading - **HTTP Basic is gone.** Local accounts sign in with the form of the sign-in page and get a session cookie (`SESSION_HOURS`, 8 by default). Scripts that sent `curl -u name:password` get `401` with a message: give them an API token (*Account › API tokens*) and send `Authorization: Bearer stk_…`. `GET /?local` is gone too. - Sessions are kept in memory: restarting Strata signs everyone out. @@ -252,6 +254,7 @@ All notable changes to Strata. Versions follow [Semantic Versioning](https://sem ## [0.1.0] - First release: 3D map of IS, zones, VLANs, assets, flows, exposure, vulnerabilities and incident contacts. +[0.11.0]: https://github.com/SolidGeeks/strata/releases/tag/v0.11.0 [0.10.0]: https://github.com/SolidGeeks/strata/releases/tag/v0.10.0 [0.9.0]: https://github.com/SolidGeeks/strata/releases/tag/v0.9.0 [0.8.0]: https://github.com/SolidGeeks/strata/releases/tag/v0.8.0 diff --git a/README.md b/README.md index 8bfa8ca..23da65e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Strata — v0.10 +# Strata — v0.11 [![Release](https://img.shields.io/github/v/release/SolidGeeks/strata)](https://github.com/SolidGeeks/strata/releases) [![License AGPL-3.0](https://raw.githubusercontent.com/SolidGeeks/.github/main/assets/badges/license-agpl.svg)](LICENSE) [![Signed releases](https://raw.githubusercontent.com/SolidGeeks/.github/main/assets/badges/signed-releases.svg)](#2-quick-start-with-docker) [![Tests](https://github.com/SolidGeeks/strata/actions/workflows/ci.yml/badge.svg)](https://github.com/SolidGeeks/strata/actions/workflows/ci.yml) @@ -16,10 +16,21 @@ Strata is on its way to **1.0.0**: from then on, the API, the export format, the | [0.8.0](https://github.com/SolidGeeks/strata/milestone/2) ✅ | Connector modules and a module store, three.js 0.186. Released on 2026-10-26 | | [0.9.0](https://github.com/SolidGeeks/strata/milestone/3) ✅ | The last breaking changes: single sign-on, pre-releases, an API reference, the old product name removed. Released on 2026-10-26 | | 0.10.0 ✅ | Incident mode with Muster, the risk register with baselines, logs for the SIEM, the steering dashboard. Released on 2026-10-28 | +| 0.11.0 ✅ | Cookie sessions and sign-out, VLAN import with zones, Arrange VLANs and zones suggested from the flows, Palo Alto builds an IS, a lighter 3D view for small laptops. Released on 2026-11-08 | | [1.0.0](https://github.com/SolidGeeks/strata/milestone/4) | Release candidates (`1.0.0-rc.N`), then 1.0.0 | Follow it on the [milestones page](https://github.com/SolidGeeks/strata/milestones). Ideas for after 1.0 are discussed, and voted on, in [Discussions](https://github.com/orgs/SolidGeeks/discussions/categories/ideas). Want to help? Start with a [good first issue](https://github.com/SolidGeeks/strata/issues?q=is%3Aopen+label%3A%22good+first+issue%22). +## New in v0.11: sign-out, VLAN import, zones from the flows, a lighter 3D view + +- **Sessions and sign-out**: local accounts sign in with the form and get a session cookie; *Sign out* is in the title block for everyone. HTTP Basic is gone: scripts use API tokens (read *Upgrading* in the [CHANGELOG](CHANGELOG.md)). +- **Import VLANs** (*Edit › Import VLANs*): a CSV, a JSON list or a Strata export; tick the VLANs to bring and choose the zone of each. The **CSV VLANs** module of the store does the same as a proposal, and modules can now ask for such a choice before an import (`file.pick`, see [MODULES.md](MODULES.md)). +- **Arrange VLANs** lays each zone out on rows and columns by its flows; **Suggest zones from the flows** proposes the zones of an IS, the Internet on top, in a window to review before applying. +- **Palo Alto builds the IS** from a configuration alone: zones, VLANs from the sub-interfaces, assets from the address objects, the firewall, then the flows. +- **On the map**: show all or no VLANs per zone, hide empty VLANs, move the Internet cloud, fold the title block, a loading bar for long requests; the overview shows each IS in its compact form. +- **A lighter 3D view**: 8 to 15 times fewer draw calls, drawn only when something changes, and an Economy mode chosen automatically on slow laptops (*Display › Graphics*). +- **Large IS**: one document can now reach `MAX_DOC_MB` (8 MB), and the history stops at `HISTORY_MAX_MB` (200 MB). + ## New in v0.10: incident mode with Muster, the risk register, the steering dashboard - **Incident mode, with Muster** (*Incident* in the controls of an IS): mark assets compromised, suspected or contained; the incident sheet lists who to call, the flows to cut and the firewall where to cut each, the business services and assets that depend on them, what the attacker reaches next. A Muster incident opens on the map and its playbook decisions point at the assets. Each asset also has anonymised *AI prompts*: its major risks, and a guide for an AI to write the response playbooks of its plausible incidents in Muster's format. @@ -229,7 +240,7 @@ Or use the prebuilt image published with each release (tag `vX.Y.Z`, see the `Re Every published image is **signed**: an attestation proves it was built by this repository's `Release` workflow, from the code of the tag. To check it before deploying (you need the [GitHub CLI](https://cli.github.com/)): ```bash -gh attestation verify oci://ghcr.io/solidgeeks/strata:0.10.0 --owner SolidGeeks +gh attestation verify oci://ghcr.io/solidgeeks/strata:0.11.0 --owner SolidGeeks ``` The command must print `Verification succeeded!`. If it fails, do not use the image, and report it (see [SECURITY.md](SECURITY.md)). Images older than 0.5.4 are not signed. @@ -572,7 +583,7 @@ Fields added since v0.2 (all optional): Strata follows [Semantic Versioning](https://semver.org/). Before 1.0 a minor version may still break things, and the [CHANGELOG](CHANGELOG.md) says how to upgrade. From **1.0** on, every 1.x version keeps working: - the `/api/` routes, their methods, roles, request and answer fields; -- authentication (Basic, `Bearer stk_…` tokens), the write headers, `X-Batch` and `X-Batch-Label`; +- authentication (the session cookie of `POST /auth/login`, `Bearer stk_…` tokens), the write headers, `X-Batch` and `X-Batch-Label`; - status codes and the `error` key (not its wording); - the export format: every export written by an earlier version keeps importing; - the module API (`"strataModule": 1`, `ctx`, the SDK) and the store index (`"strataRegistry": 1`); @@ -613,10 +624,10 @@ This map describes your weak spots precisely: treat it as sensitive data. --- -## 13. Limits of v0.10 +## 13. Limits of v0.11 - Storage is a single JSON file, suited to a few thousand assets. Beyond that, a real database will be needed. -- Sessions are kept in memory: restarting Strata signs everyone out. Roles come from the groups at sign-in: a group change applies at the next sign-in (`SESSION_HOURS`, 8 by default), or at once with *End all single sign-on sessions*. LDAP is not read directly: use an OpenID Connect provider in front of it (Keycloak, Authentik, Entra ID). +- Sessions are kept in memory: restarting Strata signs everyone out. Roles come from the groups at sign-in: a group change applies at the next sign-in (`SESSION_HOURS`, 8 by default), or at once with *End all sessions*. LDAP is not read directly: use an OpenID Connect provider in front of it (Keycloak, Authentik, Entra ID). - Two approvers editing the same IS directly at the same time: the last save wins (the history lets you go back). Proposals, on the other hand, detect conflicts. - draw.io or Visio diagrams cannot be imported: only Strata JSON exports can become a proposal. - Opened without its server, the app keeps the map in the browser only: no accounts, history, proposals or connectors. @@ -649,7 +660,7 @@ A pull request must start from the latest release or later: the `base` job fails ### Releasing -1. Merge a pull request that sets the new version in `package.json` (`npm version 0.10.0 --no-git-tag-version`), `docker-compose.yml`, the interface and the docs, and dates its CHANGELOG section. +1. Merge a pull request that sets the new version in `package.json` (`npm version 0.11.0 --no-git-tag-version`), `docker-compose.yml`, the interface and the docs, and dates its CHANGELOG section. 2. *Actions › Release › Run workflow*, on `main`. The workflow reads the version from `package.json` and creates the `vX.Y.Z` tag itself, so there is nothing to type. It waits for a maintainer's approval (the `release` environment), then runs the tests, publishes the signed image and the GitHub release with its notes. Pushing a `vX.Y.Z` tag, or publishing a release from the web page with a new tag on `main`, still works: the tag must then be exactly `v` and the version (`v0.9.0`, not `V0.9.0` or `v.0.9.0`). diff --git a/docker-compose.yml b/docker-compose.yml index fab538f..e0a6574 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ services: strata: # Prebuilt image from GitHub (docker compose pull), or built here (docker compose up -d --build) - image: ghcr.io/solidgeeks/strata:0.10.0 + image: ghcr.io/solidgeeks/strata:0.11.0 build: . container_name: strata restart: unless-stopped diff --git a/docs/api.html b/docs/api.html index 2e106ae..06796f5 100644 --- a/docs/api.html +++ b/docs/api.html @@ -344,7 +344,7 @@

    Deprecation

    Solid Geeks: open source the world.
    -

    Strata 0.10.0 · AGPL-3.0 · github.com/SolidGeeks/strata · Changelog

    +

    Strata 0.11.0 · AGPL-3.0 · github.com/SolidGeeks/strata · Changelog

    Cyberwatch, GLPI, phpIPAM and Palo Alto Networks are trademarks of their owners; Strata is not affiliated with them.

    diff --git a/docs/docs.html b/docs/docs.html index b9cbe54..06a97ce 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -69,7 +69,7 @@

    Install with Docker

    nano .env # set EDIT_PASS docker compose up -d --build # builds the image here

    On Windows: copy .env.example .env then notepad .env.

    -

    Or skip the build: docker compose pull && docker compose up -d fetches the ready-made image ghcr.io/solidgeeks/strata of the release. Every image is signed; check it before deploying with gh attestation verify oci://ghcr.io/solidgeeks/strata:0.10.0 --owner SolidGeeks, which must print Verification succeeded!

    +

    Or skip the build: docker compose pull && docker compose up -d fetches the ready-made image ghcr.io/solidgeeks/strata of the release. Every image is signed; check it before deploying with gh attestation verify oci://ghcr.io/solidgeeks/strata:0.11.0 --owner SolidGeeks, which must print Verification succeeded!

    Open http://localhost:8080 and sign in with EDIT_USER / EDIT_PASS. The map starts empty: Load demo (5 IS) fills it with five sample sites, Delete everything clears it.

    docker compose logs -f strata     # logs
     docker compose restart strata     # restart
    @@ -191,16 +191,20 @@ 

    Accounts and roles

    docker compose exec strata node scripts/users.mjs set alice contributor # asks for a password docker compose exec strata node scripts/users.mjs role alice approver docker compose exec strata node scripts/users.mjs disable alice
    +

    Local accounts sign in with the form of the sign-in page and get a session cookie for SESSION_HOURS (8); Sign out is in the title block. Changing a password, or disabling or deleting an account, ends its other sessions. Scripts use an API token: HTTP Basic is refused since 0.11.

    Repeated failed sign-ins are refused for a while (429), per account and per client address (see Configuration). Only different wrong passwords count, so a tab left open with an old password locks nobody out, and browsers already signed in keep working.

    Single sign-on

    OpenID Connect (Entra ID, Keycloak, Authentik, Google Workspace, Okta): register Strata at the provider with the redirect URI https://strata.example.org/auth/oidc/callback, then set PUBLIC_URL, SSO_ISSUER, SSO_CLIENT_ID, SSO_CLIENT_SECRET (or _FILE) and the groups that give each role (SSO_ROLE_ADMIN, SSO_ROLE_APPROVER, SSO_ROLE_CONTRIBUTOR, SSO_ROLE_VIEWER; SSO_DEFAULT_ROLE for everyone else). Opening Strata shows a sign-in page; the ID token’s signature, issuer, audience, expiry and nonce are checked, with PKCE.

    A sign-in proxy (Authelia, oauth2-proxy, Pomerium): SSO_HEADER_USER and SSO_HEADER_GROUPS name the headers it sets; they are believed only from the addresses in SSO_HEADER_TRUST (exact addresses, no range).

    -

    A name the provider gives that is also a local account is refused, and an e-mail address is used only once verified: prefer a name people cannot change (SSO_USER_CLAIM=oid on Entra ID). Sessions last SESSION_HOURS (8); Account › End all single sign-on sessions signs everyone out.

    +

    A name the provider gives that is also a local account is refused, and an e-mail address is used only once verified: prefer a name people cannot change (SSO_USER_CLAIM=oid on Entra ID). Sessions last SESSION_HOURS (8); Account › End all sessions signs everyone else out.

    Local accounts stay, for tests, small installations and as a way in when the provider is down: LOCAL_LOGIN=admins keeps them for administrators, off turns them off. API tokens for scripts: Account › API tokens (admin), shown once, sent as Authorization: Bearer stk_….

    Building the map

    Sites, zones, VLANs

    -

    An information system (IS) is a site. Zones, contacts sets its zones (one level each, for example Industrial, Office, Core IT, Exposed), its VLANs with their ranges, and the IS contacts shown on assets that have none of their own. Arrange plates lets you drag a plate anywhere on its level; Line up puts a level back in a row.

    +

    An information system (IS) is a site. Zones, contacts sets its zones (one level each, for example Industrial, Office, Core IT, Exposed), its VLANs with their ranges, and the IS contacts shown on assets that have none of their own. Arrange plates lets you drag a plate anywhere on its level, and the Internet cloud too (Put Internet back returns it); Line up puts a level back in a row.

    +

    Import VLANs (Edit › Import VLANs, in an IS) reads a CSV (header in English or French, , ; or tab), a JSON list ([{num, name, cidr, zone}], NetBox’s {"results": […]}) or a Strata export: tick the VLANs to bring, filter, and choose the zone of each, or of all the ticked ones at once. A VLAN whose number is already in the IS is updated. The CSV VLANs module of the store does the same as a proposal, open to contributors.

    +

    Arrange VLANs (Layout) lays each zone out on rows and columns, the VLANs that talk to each other side by side and those without flows last; VLANs per row is automatic or 3 to 12. Suggest zones from the flows (Edit or Layout) proposes zones from the Internet down: each VLAN goes to the floor of its distance from the Internet along the flows, and those the Internet never reaches go below by the role of their assets (internal servers, users, industrial, network and administration). Zones that already hold most of the same VLANs keep their name; a window lets you rename and move before Apply, or Propose for contributors.

    +

    In the plate list, All and None show or hide every VLAN of the IS or of one zone; Plates › Hide empty VLANs leaves out the plates without an asset.

    Assets

    Each asset has a type (server, firewall, router, switch, workstation, PLC…), a VLAN, an IP, a system, applications with their listening ports, vulnerabilities, a criticality, incident contacts and notes. A new asset starts on the first free IP of its VLAN range; an IP outside the range is pointed out. A vulnerability has a CVSS score or a severity, and optionally an EPSS (0 to 1, or a percentage) and a Known exploited (CISA KEV) mark. Search by name or IP to fly to it.

    Flows

    @@ -210,6 +214,9 @@

    Moving around the 3D view

    Drag with the left button to orbit, with the middle (or right) button to move the view left, right, up and down, and scroll to zoom. On a touch screen, one finger orbits, two fingers zoom and move. The view stops at the edge of the model; Reset view centres it again.

    2D diagram

    The 2D diagram view is the same model, flat: drag assets and plates, draw a flow from one asset to another (F), add an asset on a plate (A), move (V). Positions are shared with the 3D model.

    +

    Graphics on small laptops

    +

    Display › Graphics: Auto (the default) draws in Quality until moving the view turns out slow on this computer, then switches to Economy and remembers it; Economy uses the screen resolution, drops shadows and moving dots and hides labels sooner. Animated flows turns the moving dots off on its own. The 3D view is only drawn when something changes.

    +

    The title block folds into one line (▴ in its corner), keeping the name of the IS, the key figures, back to the overview and sign out. A long request shows a loading bar at the top of the screen.

    Theme

    Display › Theme: Auto follows the system, Day and Night force it. N switches.

    @@ -278,13 +285,14 @@

    Palo Alto

    Build the IS (a box of the file import): on an empty or incomplete IS, the configuration also gives what the map lacks. A zone per security zone; a VLAN per layer-3 interface or sub-interface outside the Internet zones (its tag and subnet, named after its comment; an untagged interface gets a number from 4001 on, to change); an asset per host address object of a private range, in the VLAN whose range holds it, its type from its name or description (PC-, TEL-, SQL-, CAM-…), else from the ports opened to it, else a workstation if it only opens connections; and the firewall itself, from its host name and inside address, crossed by the flows that change VLAN. What the map has (a zone of the same name, a VLAN of the same number or range, an asset at the same IP) is kept; the same file again changes nothing. Systems, vulnerabilities and contacts still come from an inventory or a scanner.

    What cannot be drawn (internal any→any, dynamic groups, negations, addresses with no asset) is listed in the proposal. Flows from a rule that disappeared are removed; flows drawn by hand are never touched. A proposal from a file counts as the importer’s own for the four-eyes rule.

    Modules and the module store

    -

    Each connector is a module: a folder with a manifest (its settings, whether it imports files, what it may reach) and its code. The four above ship with Strata; three more come from the store:

    +

    Each connector is a module: a folder with a manifest (its settings, whether it imports files, what it may reach) and its code. The four above ship with Strata; four more come from the store:

    • CSV inventory imports the assets of an IS from a spreadsheet saved as CSV (columns name, ip, type, os, role, vlan, criticality, notes);
    • Nmap scan imports a scan saved with -oX: hosts that are up, the system Nmap guessed, open ports as listening ports of the applications;
    • +
    • CSV VLANs imports the VLANs of an IS from a CSV (number, name, range): after the file, a window lists them, to tick the ones to bring and choose their zone;
    • NetBox syncs devices, virtual machines, VLANs and prefixes with a read-only token, a NetBox site going to an IS through rules such as Lyon HQ = lyon.
    -

    The first two are file imports open to any contributor; NetBox is run by approvers.

    +

    The first three are file imports open to any contributor; NetBox is run by approvers.

    1. Install: Connectors › Module store (admin). Each card says what the module reaches (reads the map, connects to your tool over the network), its version, licence and source. Install downloads the package and refuses it if it differs from the checksum the store publishes.
    2. Set it up: Settings on its card under Installed, then Test the connection. Every module also has Enabled with Sync automatically every … hours (both needed for scheduled syncs), the TLS options and Limit to these IS under More settings.
    3. @@ -381,7 +389,7 @@

      Troubleshooting

      Known limits

      • Storage is a single JSON file, fine for a few thousand assets.
      • -
      • Single sign-on sessions live in memory: a restart signs SSO users out, and a group change applies at the next sign-in.
      • +
      • Sessions live in memory: a restart signs everyone out, and a single sign-on group change applies at the next sign-in.
      • Two approvers editing the same IS directly at the same moment: the last save wins (history lets you go back). Proposals detect conflicts.
      • Connectors follow the public API documentation of each tool and were tested against simulators: test, sync, and read the first proposal carefully. GLPI 11’s new API (/api.php/v2) is not supported yet.
      • Palo Alto: NAT rules, security profiles, dynamic address groups and the Panorama device-group hierarchy are not interpreted.
      • @@ -398,7 +406,7 @@

        Known limits

        Solid Geeks: open source the world.
        -

        Strata 0.10.0 · AGPL-3.0 · github.com/SolidGeeks/strata · Changelog

        +

        Strata 0.11.0 · AGPL-3.0 · github.com/SolidGeeks/strata · Changelog

        Cyberwatch, GLPI, phpIPAM and Palo Alto Networks are trademarks of their owners; Strata is not affiliated with them.

        diff --git a/docs/index.html b/docs/index.html index 53b75e0..0537363 100644 --- a/docs/index.html +++ b/docs/index.html @@ -96,7 +96,7 @@

        What you get,

        A model you can walk around, and a diagram you can draw

        Each information system is a stack of plates: one per zone, one colour per VLAN, an object per asset. Orbit, move across with the middle button, zoom, filter by severity or exposure. Switch to the 2D diagram to add assets, draw flows and arrange plates; both views share the same positions, so what you draw flat is what you see in 3D.

        -
        • Flows routed through the firewalls, routers and switches they cross, each in its own lane
        • One colour per flow, on demand
        • Plates movable anywhere on their level
        • Five sites in the demo, linked by WAN
        +
        • Flows routed through the firewalls, routers and switches they cross, each in its own lane
        • One colour per flow, on demand
        • Plates movable anywhere on their level, or arranged by their flows
        • Zones suggested from the flows, the Internet on top
        • A lighter Economy mode for small laptops
        • Five sites in the demo, linked by WAN
        The 2D diagram of the Lyon site: four levels of VLAN plates with asset cards and flows
        2D diagram of the demo’s Lyon site.
        @@ -167,10 +167,11 @@

        Fed by the tools you already run

        Cyberwatchservers and their CVEs (API v3)vulnerabilities on matching assets, by host name then IP; hand-entered findings kept GLPIcomputers, network devices, printers, phones, software, technician and group, locationnew and updated assets, applications, incident contacts; the location picks the site phpIPAMsections, subnets, VLANs, addressesVLANs with their ranges, asset addresses, assets moved to their subnet’s VLAN - Palo Altoa configuration file, or the XML API of a firewall or Panoramaflows from every active allow rule, address objects matched to assets, public objects as Internet services + Palo Altoa configuration file, or the XML API of a firewall or Panoramaflows from every active allow rule, address objects matched to assets, public objects as Internet services; or the whole IS (zones, VLANs, assets) from the configuration alone NetBox storedevices, virtual machines, VLANs, prefixesVLANs with their ranges, new and completed assets; NetBox sites matched to IS by rules Nmap storea scan saved with -oXhosts with their system and open ports, new hosts typed from their ports CSV storea spreadsheet saved as CSVnew and updated assets of one site + CSV VLANs storea CSV of VLANs (number, name, range)the VLANs you tick, each in the zone you choose
        The Connectors panel: Cyberwatch and GLPI scheduled, their last runs and the proposals they prepared
        Connectors panel after a sync.
        @@ -212,7 +213,7 @@

        Your map stays Solid Geeks: open source the world.
        -

        Strata 0.10.0 · AGPL-3.0 · github.com/SolidGeeks/strata

        +

        Strata 0.11.0 · AGPL-3.0 · github.com/SolidGeeks/strata

        Cyberwatch, GLPI, phpIPAM and Palo Alto Networks are trademarks of their owners; Strata is not affiliated with them.

        diff --git a/package-lock.json b/package-lock.json index cdc9ac7..30401ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "strata", - "version": "0.10.0", + "version": "0.11.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "strata", - "version": "0.10.0", + "version": "0.11.0", "license": "AGPL-3.0-or-later", "dependencies": { "@fontsource/archivo": "^5.0.0", diff --git a/package.json b/package.json index b41cc26..64a9bde 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "strata", - "version": "0.10.0", + "version": "0.11.0", "description": "Strata — self-hosted 3D map of information systems: zones, VLANs, assets, flows, exposure, vulnerabilities and incident contacts.", "private": true, "license": "AGPL-3.0-or-later", diff --git a/public/index.html b/public/index.html index 6d3bdfb..f1d163a 100644 --- a/public/index.html +++ b/public/index.html @@ -197,7 +197,7 @@

        Legend

        -

        Strata 0.10.0, free software under the GNU AGPL v3. Source code

        +

        Strata 0.11.0, free software under the GNU AGPL v3. Source code

        From bdd8365f54c68ab08db31c9609b49118921ef706 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 19:25:53 +0000 Subject: [PATCH 16/16] Nix: npmDepsHash for the 0.11.0 lockfile Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BvKC5ZeSmwYQrWay8EM8mh Signed-off-by: Claude --- nix/package.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/package.nix b/nix/package.nix index a484d71..9129f54 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -35,7 +35,7 @@ buildNpmPackage { nodejs = nodejs_22; # Changes with package-lock.json: Nix prints the new value when it no longer matches # (README, "Nix and NixOS") - npmDepsHash = "sha256-fr4NO/VIuBQB9rc5TnwisLIvPWuTDnobwmiWin87+4A="; + npmDepsHash = "sha256-YGrqCpxo9cZZomxUqMsHz21uIBTZ91QwW3osm4NaNrs="; npmBuildScript = "vendor";