129 lines
5.1 KiB
JavaScript
129 lines
5.1 KiB
JavaScript
import { spawnSync } from 'node:child_process'
|
|
import { randomUUID } from 'node:crypto'
|
|
import assert from 'node:assert/strict'
|
|
|
|
const image = process.env.SYNCOVA_IMAGE || 'syncova-ticket:local'
|
|
const name = `syncova-image-check-${randomUUID()}`
|
|
const password = `Test-${randomUUID()}!`
|
|
const env = {
|
|
...process.env,
|
|
ADMIN_EMAIL: 'image-check@example.invalid',
|
|
ADMIN_PASSWORD: password,
|
|
APP_ORIGIN: 'https://image-check.example.invalid',
|
|
COOKIE_SECURE: 'true',
|
|
TRUST_PROXY: '1',
|
|
MAIL_MODE: 'disabled',
|
|
}
|
|
function docker(args, required = true) {
|
|
const result = spawnSync('docker', args, { encoding: 'utf8', timeout: 60000, env })
|
|
if (required && result.status !== 0)
|
|
throw Error(
|
|
(result.stderr || result.error?.message || 'Docker fehlgeschlagen').replaceAll(
|
|
password,
|
|
'[redacted]',
|
|
),
|
|
)
|
|
return result
|
|
}
|
|
const configuration = JSON.parse(docker(['image', 'inspect', image]).stdout)[0].Config
|
|
assert.equal(configuration.User, 'node')
|
|
assert(
|
|
!configuration.Env.some((v) => /^(ADMIN_PASSWORD|REGISTRY_TOKEN|SMTP_ENCRYPTION_KEY)=.+/.test(v)),
|
|
'Keine Zugangsdaten im Image',
|
|
)
|
|
docker([
|
|
'run',
|
|
'--rm',
|
|
'--entrypoint',
|
|
'node',
|
|
image,
|
|
'--input-type=module',
|
|
'-e',
|
|
`
|
|
import assert from 'node:assert/strict'; import fs from 'node:fs';
|
|
assert.deepEqual(fs.readdirSync('/app/data'), [], 'Image muss ein leeres Datenverzeichnis enthalten');
|
|
for(const file of ['/app/.env','/app/.env.docker','/app/.git','/app/docs','/app/tests']) assert(!fs.existsSync(file),file+' darf nicht im Laufzeitimage liegen');
|
|
`,
|
|
])
|
|
try {
|
|
docker([
|
|
'run',
|
|
'--detach',
|
|
'--name',
|
|
name,
|
|
'--read-only',
|
|
'--tmpfs',
|
|
'/app/data:rw,noexec,nosuid,uid=1000,gid=1000,mode=0700',
|
|
'--tmpfs',
|
|
'/tmp:rw,noexec,nosuid',
|
|
'--env',
|
|
'ADMIN_EMAIL',
|
|
'--env',
|
|
'ADMIN_PASSWORD',
|
|
'--env',
|
|
'APP_ORIGIN',
|
|
'--env',
|
|
'COOKIE_SECURE',
|
|
'--env',
|
|
'TRUST_PROXY',
|
|
'--env',
|
|
'MAIL_MODE',
|
|
image,
|
|
])
|
|
let healthy = false
|
|
for (let attempt = 0; attempt < 30; attempt++) {
|
|
const result = docker(
|
|
[
|
|
'exec',
|
|
name,
|
|
'node',
|
|
'-e',
|
|
"fetch('http://127.0.0.1:3001/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
|
|
],
|
|
false,
|
|
)
|
|
if (result.status === 0) {
|
|
healthy = true
|
|
break
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
}
|
|
assert(healthy, 'Produktionsimage muss starten und gesund werden')
|
|
docker([
|
|
'exec',
|
|
name,
|
|
'node',
|
|
'--input-type=module',
|
|
'-e',
|
|
`
|
|
import Database from 'better-sqlite3'; import assert from 'node:assert/strict';
|
|
const db=new Database('/app/data/syncova.db',{readonly:true});
|
|
for(const table of ['tickets','messages','message_recipients','ticket_search','attachments','notifications','outbox','message_originals','attachment_inline','reply_templates','queue_tags','tag_senders','ticket_tags','ticket_checklist']) assert.equal(db.prepare('SELECT count(*) n FROM '+table).get().n,0,table+' muss leer sein');
|
|
const users=db.prepare('SELECT name,email FROM users').all(); assert.equal(users.length,1); assert.equal(users[0].email,process.env.ADMIN_EMAIL); assert.equal(users[0].name,'Administrator');
|
|
db.close();
|
|
const login=await fetch('http://127.0.0.1:3001/api/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({email:process.env.ADMIN_EMAIL,password:process.env.ADMIN_PASSWORD})});
|
|
assert.equal(login.status,200,'Bootstrap-Anmeldung muss funktionieren');
|
|
assert(login.headers.get('set-cookie').includes('; Secure'),'Produktionscookies müssen Secure sein');
|
|
assert.equal(login.headers.get('strict-transport-security'),'max-age=31536000');
|
|
assert(login.headers.get('content-security-policy').includes("script-src 'self';"));
|
|
const preview=await fetch('http://127.0.0.1:3001/api/html-preview?theme=dark&images=1',{headers:{cookie:login.headers.get('set-cookie').split(';')[0]}});
|
|
assert.equal(preview.status,200,'HTML-Vorschau muss mit Produktionssitzung laden');
|
|
assert(preview.headers.get('content-security-policy').includes('img-src data: https:;'));
|
|
assert(preview.headers.get('content-security-policy').includes('sandbox allow-scripts'));
|
|
assert(!preview.headers.get('content-security-policy').includes('allow-same-origin'));
|
|
const sessionHeaders={cookie:login.headers.get('set-cookie').split(';')[0]};
|
|
const page=await (await fetch('http://127.0.0.1:3001/api/tickets?view=unassigned&page=1&pageSize=25',{headers:sessionHeaders})).json();
|
|
assert.equal(page.total,0);assert.equal(page.pageSize,25);assert.deepEqual(page.items,[]);
|
|
assert.equal(page.counts.unassigned,0);
|
|
const summary=await (await fetch('http://127.0.0.1:3001/api/tickets/summary',{headers:sessionHeaders})).json();
|
|
assert.equal(summary.counts.total,0);assert(!('items' in summary));
|
|
assert.equal((await fetch('http://127.0.0.1:3001/theme.js')).status,200);
|
|
`,
|
|
])
|
|
console.log(
|
|
'Image geprüft: leeres Datenverzeichnis, keine lokalen Daten oder Zugangsdaten, frische Produktion ohne Demotickets/-benutzer, Bootstrap-Anmeldung erfolgreich.',
|
|
)
|
|
} finally {
|
|
docker(['rm', '--force', name], false)
|
|
}
|