Files
ghaymah-dns-net-sec-tests/scripts/upgrade_soc_gateway_node.js
T

180 lines
6.0 KiB
JavaScript

const fs = require('fs');
const crypto = require('crypto');
const zlib = require('zlib');
const { getAuth } = require('./auth_manager');
const RESOURCE_ID = '07131052-d08b-4053-8942-1b36a4d89818';
const APP_NAME = 'soc-gateway-live';
const PROJECT_ID = '5be77e31-3ac2-48f5-b97d-735e8b2ad45f';
const DOMAIN = 'soc-gateway-live-7c2ca9361c37.hosted.ghaymah.systems';
function createTar(files) {
const chunks = [];
for (const file of files) {
const name = file.name;
const content = Buffer.isBuffer(file.content) ? file.content : Buffer.from(file.content, 'utf8');
const size = content.length;
const header = Buffer.alloc(512);
header.write(name, 0, 100, 'utf8');
header.write('0000644\0', 100, 8, 'utf8');
header.write('0000000\0', 108, 8, 'utf8');
header.write('0000000\0', 116, 8, 'utf8');
header.write(size.toString(8).padStart(11, '0') + '\0', 124, 12, 'utf8');
const mtime = Math.floor(Date.now() / 1000);
header.write(mtime.toString(8).padStart(11, '0') + '\0', 136, 12, 'utf8');
header.write(' ', 148, 8, 'utf8');
header.write('0', 156, 1, 'utf8');
header.write('ustar\0', 257, 6, 'utf8');
header.write('00', 263, 2, 'utf8');
let sum = 0;
for (let i = 0; i < 512; i++) sum += header[i];
header.write(sum.toString(8).padStart(6, '0') + '\0 ', 148, 8, 'utf8');
chunks.push(header);
chunks.push(content);
const remainder = size % 512;
if (remainder > 0) chunks.push(Buffer.alloc(512 - remainder));
}
chunks.push(Buffer.alloc(1024));
return zlib.gzipSync(Buffer.concat(chunks));
}
async function main() {
const { token, userId } = await getAuth();
console.log(`Upgrading soc-gateway-live (${RESOURCE_ID})...`);
console.log('\n=== Step 1: Packaging Node.js Gateway & Resilient SOC Dashboard ===');
const serverContent = fs.readFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_gateway_ui/server.js', 'utf8');
const htmlContent = fs.readFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_gateway_ui/index.html', 'utf8');
const dockerfile = `FROM node:alpine
WORKDIR /app
COPY . .
EXPOSE 80
ENV PORT=80
CMD ["node", "server.js"]
`;
const tarGz = createTar([
{ name: 'Dockerfile', content: dockerfile },
{ name: 'server.js', content: serverContent },
{ name: 'index.html', content: htmlContent }
]);
const sha256 = crypto.createHash('sha256').update(tarGz).digest('hex');
console.log('Tarball created! Size:', tarGz.length, 'bytes | New SHA256:', sha256);
console.log('\n=== Step 2: Uploading Artifact to S3 Proxy ===');
const uploadUrl = `https://s3-nhost-proxy-83e02743fd61.hosted.ghaymah.systems/files/${userId}/${RESOURCE_ID}.tar.gz`;
const upRes = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/gzip'
},
body: tarGz
});
console.log('Upload status:', upRes.status, upRes.statusText);
if (!upRes.ok) {
throw new Error('Upload failed: ' + (await upRes.text()));
}
console.log('\n=== Step 3: Triggering Resource Update in Ghaymah ===');
const values = {
name: APP_NAME,
projectId: PROJECT_ID,
resourceTier: 't1',
container: {
image: `registry.ghaymah.systems/${userId}/${RESOURCE_ID}:latest`
},
ports: [{ number: 80, expose: true }],
publicAccess: {
baseDomain: 'hosted.ghaymah.systems',
domain: DOMAIN,
enabled: true
},
source: {
enabled: true,
ref: sha256,
type: 'artifact',
url: uploadUrl
}
};
const updateRes = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `mutation UpdateApp($id: uuid!, $values: json!) {
update_ghaymah_cloud_resources_by_pk(
pk_columns: { id: $id },
_set: { values: $values }
) {
id status message
}
}`,
variables: {
id: RESOURCE_ID,
values: values
}
})
});
const updateData = await updateRes.json();
console.log('Update response:', JSON.stringify(updateData, null, 2));
console.log('\n=== Step 4: Monitoring Build & Deployment Status ===');
let finalStatus = null;
for (let i = 1; i <= 30; i++) {
await new Promise(r => setTimeout(r, 3000));
const checkRes = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `query CheckStatus($id: uuid!) {
ghaymah_cloud_resources_by_pk(id: $id) {
id status message
}
}`,
variables: { id: RESOURCE_ID }
})
});
const checkData = await checkRes.json();
const status = checkData.data?.ghaymah_cloud_resources_by_pk?.status;
const msg = checkData.data?.ghaymah_cloud_resources_by_pk?.message;
console.log(`[Attempt ${i}/30] Status: ${status} | Message: ${msg}`);
if (status === 'synced') {
finalStatus = 'synced';
console.log('\n>>> SUCCESS! RESOURCE SYNCED! <<<');
break;
}
if (status === 'failed') {
finalStatus = 'failed';
console.log('Build failed:', msg);
break;
}
}
console.log(`\n=== Step 5: Testing Live Public Endpoints at https://${DOMAIN}/ ===`);
try {
const res = await fetch(`https://${DOMAIN}/`);
console.log('GET / Status:', res.status);
const body = await res.text();
console.log('Title:', body.match(/<title>([^<]+)<\/title>/)?.[1]);
const hRes = await fetch(`https://${DOMAIN}/health`);
console.log('GET /health Status:', hRes.status);
console.log('GET /health Body:', await hRes.json());
const pRes = await fetch(`https://${DOMAIN}/proxy/soc-siem/health`);
console.log('GET /proxy/soc-siem/health Status:', pRes.status);
console.log('GET /proxy/soc-siem/health Body:', await pRes.json());
} catch (e) {
console.error('Fetch test error:', e.message);
}
}
main().catch(console.error);