const fs = require('fs'); const crypto = require('crypto'); const zlib = require('zlib'); const { getAuth } = require('./auth_manager'); const GATEWAY_ID = '3d5666fe-95db-4e73-b6b2-dd0e41fca7f6'; const PROJECT_ID = '5be77e31-3ac2-48f5-b97d-735e8b2ad45f'; const DOMAIN = 'soc-gateway-28cd1dc4e034.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 tokenData = await getAuth(); const token = tokenData.token; const userId = tokenData.userId; console.log('=== Step 1: Packaging SOC Command Center Web UI for Gateway ==='); const htmlContent = fs.readFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_gateway_ui/index.html', 'utf8'); const dockerfile = `FROM nginx:alpine COPY ./ /usr/share/nginx/html/ EXPOSE 80 `; const tarGz = createTar([ { name: 'Dockerfile', content: dockerfile }, { name: 'index.html', content: htmlContent } ]); const sha256 = crypto.createHash('sha256').update(tarGz).digest('hex'); console.log('Tarball created! Size:', tarGz.length, 'bytes | SHA256:', sha256); console.log('\n=== Step 2: Uploading Artifact to Ghaymah Storage ==='); const uploadUrl = `https://s3-nhost-proxy-83e02743fd61.hosted.ghaymah.systems/files/${userId}/${GATEWAY_ID}.tar.gz`; const uploadRes = await fetch(uploadUrl, { method: 'PUT', headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/gzip' }, body: tarGz }); console.log('Upload status:', uploadRes.status, uploadRes.statusText); if (!uploadRes.ok) { throw new Error('Upload failed: ' + (await uploadRes.text())); } console.log('\n=== Step 3: Updating soc-gateway Resource in Project ghaymah-soc-microservices ==='); const image = `registry.ghaymah.systems/${userId}/${GATEWAY_ID}:latest`; 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 UpdateGatewayArtifact($id: uuid!, $values: json!) { update_ghaymah_cloud_resources_by_pk( pk_columns: { id: $id }, _set: { values: $values } ) { id status values message } }`, variables: { id: GATEWAY_ID, values: { name: 'soc-gateway', projectId: PROJECT_ID, resourceTier: 't1', container: { image: image }, ports: [ { expose: true, number: 80 } ], publicAccess: { baseDomain: 'hosted.ghaymah.systems', domain: DOMAIN, enabled: true }, source: { enabled: true, ref: sha256, type: 'artifact', url: uploadUrl } } } }) }); const updateData = await updateRes.json(); console.log('Update result:\n', JSON.stringify(updateData, null, 2)); console.log('\n=== Step 4: Waiting for Ghaymah to Build and Sync soc-gateway ==='); for (let i = 1; i <= 20; i++) { await new Promise(r => setTimeout(r, 4000)); 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 CheckGatewayStatus($id: uuid!) { ghaymah_cloud_resources_by_pk(id: $id) { id status message } }`, variables: { id: GATEWAY_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}/20] Status: ${status} | Message: ${msg}`); if (status === 'synced') { console.log('\n>>> SUCCESS! soc-gateway IN PROJECT IS SYNCED! <<<'); break; } } // Step 5: Test the original project domain! console.log('\n=== Step 5: Testing Original Project URL ==='); console.log(`Testing URL: https://${DOMAIN}/ ...`); try { const testRes = await fetch(`https://${DOMAIN}/`); console.log('HTTP Status:', testRes.status); const body = await testRes.text(); console.log('Response Title:', body.match(/