200 lines
6.9 KiB
JavaScript
200 lines
6.9 KiB
JavaScript
const fs = require('fs');
|
|
|
|
async function genDomainHash(name, id) {
|
|
const input = `${name}-${id}`;
|
|
const data = new TextEncoder().encode(input);
|
|
const hash = await crypto.subtle.digest('SHA-256', data);
|
|
const hex = Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
return hex.substring(0, 12);
|
|
}
|
|
|
|
async function main() {
|
|
const tokenData = JSON.parse(fs.readFileSync('D:/cumin/ghaymah_dns_net_sec_tests/token.json', 'utf8'));
|
|
const token = tokenData.token;
|
|
const userId = tokenData.userId;
|
|
|
|
console.log('--- Step 1: Creating Project: ghaymah-soc-microservices ---');
|
|
const projectId = crypto.randomUUID();
|
|
const projRes = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
|
|
method: 'POST',
|
|
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
query: `mutation CreateProject($id: uuid!, $userId: uuid!, $chartName: String!, $chartVersion: String!, $values: json!) {
|
|
insert_ghaymah_cloud_resources_one(object: {
|
|
id: $id,
|
|
user_id: $userId,
|
|
chart_name: $chartName,
|
|
chart_version: $chartVersion,
|
|
values: $values
|
|
}) {
|
|
id status
|
|
}
|
|
}`,
|
|
variables: {
|
|
id: projectId,
|
|
userId: userId,
|
|
chartName: 'ghaymah-project',
|
|
chartVersion: '0.0',
|
|
values: {
|
|
name: 'ghaymah-soc-microservices',
|
|
userId: userId
|
|
}
|
|
}
|
|
})
|
|
});
|
|
const projData = await projRes.json();
|
|
console.log('Project creation response:', JSON.stringify(projData));
|
|
|
|
// IDs for services
|
|
const redisId = crypto.randomUUID();
|
|
const backendId = crypto.randomUUID();
|
|
const gatewayId = crypto.randomUUID();
|
|
const testerId = crypto.randomUUID();
|
|
|
|
// Compute uniqueNames
|
|
const redisHash = await genDomainHash('soc-redis', redisId);
|
|
const redisUnique = `soc-redis-${redisHash}`;
|
|
|
|
const backendHash = await genDomainHash('soc-backend', backendId);
|
|
const backendUnique = `soc-backend-${backendHash}`;
|
|
|
|
const gatewayHash = await genDomainHash('soc-gateway', gatewayId);
|
|
const gatewayUnique = `soc-gateway-${gatewayHash}`;
|
|
const gatewayDomain = `${gatewayUnique}.hosted.ghaymah.systems`;
|
|
|
|
const testerHash = await genDomainHash('soc-tester', testerId);
|
|
const testerUnique = `soc-tester-${testerHash}`;
|
|
const testerDomain = `${testerUnique}.hosted.ghaymah.systems`;
|
|
|
|
console.log('\n--- Service Topology Plan ---');
|
|
console.log('1. Database/Cache: soc-redis');
|
|
console.log(' ID:', redisId);
|
|
console.log(' Internal DNS UniqueName:', redisUnique);
|
|
console.log(' Port: 6379 (TCP private)');
|
|
|
|
console.log('2. SOC Backend: soc-backend');
|
|
console.log(' ID:', backendId);
|
|
console.log(' Internal DNS UniqueName:', backendUnique);
|
|
console.log(' Port: 4000 (HTTP/TCP private)');
|
|
|
|
console.log('3. Edge Gateway: soc-gateway');
|
|
console.log(' ID:', gatewayId);
|
|
console.log(' Internal DNS UniqueName:', gatewayUnique);
|
|
console.log(' Public Domain:', gatewayDomain);
|
|
console.log(' Port: 7681/3000 (HTTP public)');
|
|
|
|
console.log('4. Security Diagnostic Pod: soc-tester');
|
|
console.log(' ID:', testerId);
|
|
console.log(' Internal DNS UniqueName:', testerUnique);
|
|
console.log(' Public Domain:', testerDomain);
|
|
console.log(' Port: 7681 (ttyd terminal public)');
|
|
|
|
const topology = {
|
|
projectId,
|
|
projectName: 'ghaymah-soc-microservices',
|
|
createdAt: new Date().toISOString(),
|
|
services: {
|
|
redis: { id: redisId, name: 'soc-redis', uniqueName: redisUnique, port: 6379, expose: false },
|
|
backend: { id: backendId, name: 'soc-backend', uniqueName: backendUnique, port: 4000, expose: false },
|
|
gateway: { id: gatewayId, name: 'soc-gateway', uniqueName: gatewayUnique, domain: gatewayDomain, port: 7681, expose: true },
|
|
tester: { id: testerId, name: 'soc-tester', uniqueName: testerUnique, domain: testerDomain, port: 7681, expose: true }
|
|
}
|
|
};
|
|
fs.writeFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_project_topology.json', JSON.stringify(topology, null, 2));
|
|
|
|
// Function to deploy app
|
|
async function deployApp(id, name, values) {
|
|
console.log(`Deploying ${name}...`);
|
|
const res = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
|
|
method: 'POST',
|
|
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
query: `mutation CreateApp($id: uuid!, $userId: uuid!, $chartName: String!, $chartVersion: String!, $values: json!) {
|
|
insert_ghaymah_cloud_resources_one(object: {
|
|
id: $id,
|
|
user_id: $userId,
|
|
chart_name: $chartName,
|
|
chart_version: $chartVersion,
|
|
values: $values
|
|
}) {
|
|
id status
|
|
}
|
|
}`,
|
|
variables: {
|
|
id: id,
|
|
userId: userId,
|
|
chartName: 'ghaymah-app',
|
|
chartVersion: '0.1',
|
|
values: values
|
|
}
|
|
})
|
|
});
|
|
const d = await res.json();
|
|
console.log(` -> Response for ${name}:`, JSON.stringify(d));
|
|
}
|
|
|
|
console.log('\n--- Step 2: Deploying Microservice 1: soc-redis ---');
|
|
await deployApp(redisId, 'soc-redis', {
|
|
name: 'soc-redis',
|
|
projectId: projectId,
|
|
resourceTier: 't1',
|
|
ports: [{ number: 6379, expose: false }],
|
|
container: { image: 'redis:alpine' },
|
|
publicAccess: { enabled: false }
|
|
});
|
|
|
|
console.log('\n--- Step 3: Deploying Microservice 2: soc-backend ---');
|
|
// soc-backend runs an alpine container configured for SOC backend
|
|
await deployApp(backendId, 'soc-backend', {
|
|
name: 'soc-backend',
|
|
projectId: projectId,
|
|
resourceTier: 't1',
|
|
ports: [{ number: 4000, expose: false }, { number: 7681, expose: false }],
|
|
container: { image: 'tsl0922/ttyd:alpine' },
|
|
publicAccess: { enabled: false },
|
|
env: {
|
|
PORT: '4000',
|
|
REDIS_HOST: redisUnique,
|
|
REDIS_PORT: '6379',
|
|
SERVICE_NAME: 'soc-backend'
|
|
}
|
|
});
|
|
|
|
console.log('\n--- Step 4: Deploying Microservice 3: soc-gateway ---');
|
|
await deployApp(gatewayId, 'soc-gateway', {
|
|
name: 'soc-gateway',
|
|
projectId: projectId,
|
|
resourceTier: 't1',
|
|
ports: [{ number: 7681, expose: true }],
|
|
container: { image: 'tsl0922/ttyd:alpine' },
|
|
publicAccess: {
|
|
enabled: true,
|
|
domain: gatewayDomain,
|
|
baseDomain: 'hosted.ghaymah.systems'
|
|
},
|
|
env: {
|
|
PORT: '7681',
|
|
BACKEND_URL: `http://${backendUnique}:4000`,
|
|
SERVICE_NAME: 'soc-gateway'
|
|
}
|
|
});
|
|
|
|
console.log('\n--- Step 5: Deploying Microservice 4: soc-tester ---');
|
|
await deployApp(testerId, 'soc-tester', {
|
|
name: 'soc-tester',
|
|
projectId: projectId,
|
|
resourceTier: 't1',
|
|
ports: [{ number: 7681, expose: true }],
|
|
container: { image: 'tsl0922/ttyd:alpine' },
|
|
publicAccess: {
|
|
enabled: true,
|
|
domain: testerDomain,
|
|
baseDomain: 'hosted.ghaymah.systems'
|
|
}
|
|
});
|
|
|
|
console.log('\nAll 4 microservices created successfully in project ghaymah-soc-microservices!');
|
|
}
|
|
|
|
main().catch(console.error);
|