@adityum/studio 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,311 @@
1
+ const DNS_1123_REGEX = /^[a-z][a-z0-9-]{1,48}[a-z0-9]$/;
2
+ const SECRET_KEY_PATTERN = /(PASSWORD|SECRET|TOKEN|PRIVATE_KEY|API_KEY|MASTER_KEY)/i;
3
+ /**
4
+ * Pure client-side & CLI architecture security, graph cycle, CIS hardening,
5
+ * topological deploy-order, and INR BYOC sizing validator.
6
+ */
7
+ export function validateStudioTopology(topology) {
8
+ const findings = [];
9
+ const nodeById = new Map(topology.nodes.map((n) => [n.id, n]));
10
+ // 1. Validate DNS-1123 slugs & duplicate names (ADI-STUDIO-006)
11
+ const seenNames = new Map();
12
+ for (const node of topology.nodes) {
13
+ if (!DNS_1123_REGEX.test(node.name)) {
14
+ findings.push({
15
+ id: `slug-${node.id}`,
16
+ code: "ADI-STUDIO-006",
17
+ severity: "critical",
18
+ nodeId: node.id,
19
+ nodeName: node.name,
20
+ title: "Invalid Service Slug (Must be DNS-1123)",
21
+ message: `Service '${node.name}' must be lowercase alphanumeric with hyphens (3-50 chars, starting with a letter).`,
22
+ remediation: `Rename '${node.name}' to a valid slug like '${node.name.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/^-+|-+$/g, "") || "service-01"}'.`,
23
+ });
24
+ }
25
+ if (seenNames.has(node.name)) {
26
+ findings.push({
27
+ id: `dup-${node.id}`,
28
+ code: "ADI-STUDIO-006",
29
+ severity: "critical",
30
+ nodeId: node.id,
31
+ nodeName: node.name,
32
+ title: "Duplicate Service Slug Detected",
33
+ message: `Multiple services share the DNS hostname '${node.name}', which causes Docker bridge network collisions.`,
34
+ remediation: `Give each service a unique slug on the canvas.`,
35
+ });
36
+ }
37
+ else {
38
+ seenNames.set(node.name, node.id);
39
+ }
40
+ }
41
+ // 2. Contract #2: Data Plane Isolation — Databases/Caches must NEVER have publicIngress (ADI-STUDIO-002)
42
+ for (const node of topology.nodes) {
43
+ if ((node.kind === "postgres" || node.kind === "redis") && node.publicIngress) {
44
+ findings.push({
45
+ id: `pub-db-${node.id}`,
46
+ code: "ADI-STUDIO-002",
47
+ severity: "critical",
48
+ nodeId: node.id,
49
+ nodeName: node.name,
50
+ title: "Database or Cache Exposed to Public Internet",
51
+ message: `'${node.name}' (${node.kind}) has Public Ingress enabled. Adityum Contract #2 strictly forbids exposing raw DB/cache ports to 0.0.0.0.`,
52
+ remediation: `Disable 'Public Ingress' on '${node.name}' so it is reachable only on the private 'adi-internal' Docker bridge network.`,
53
+ });
54
+ }
55
+ }
56
+ // 3. Edge / Tunnel Ingress Verification (ADI-STUDIO-003)
57
+ const publicAppNodes = topology.nodes.filter((n) => (n.kind === "web" || n.kind === "api") && n.publicIngress);
58
+ const tunnelNodes = topology.nodes.filter((n) => n.kind === "tunnel");
59
+ if (publicAppNodes.length > 0 && tunnelNodes.length === 0) {
60
+ findings.push({
61
+ id: "missing-tunnel",
62
+ code: "ADI-STUDIO-003",
63
+ severity: "high",
64
+ title: "Public Services Missing Cloudflare Named Tunnel",
65
+ message: `${publicAppNodes.length} public service(s) lack an upstream Cloudflare Named Tunnel node for zero-open-port HTTPS ingress.`,
66
+ remediation: "Add a 'Cloudflare Tunnel' node and connect it to your public web/api services (Contract #2).",
67
+ });
68
+ }
69
+ else {
70
+ for (const app of publicAppNodes) {
71
+ const hasTunnelEdge = topology.edges.some((e) => e.targetId === app.id && nodeById.get(e.sourceId)?.kind === "tunnel");
72
+ if (!hasTunnelEdge) {
73
+ findings.push({
74
+ id: `unwired-tunnel-${app.id}`,
75
+ code: "ADI-STUDIO-003",
76
+ severity: "medium",
77
+ nodeId: app.id,
78
+ nodeName: app.name,
79
+ title: "Public Service Not Wired to Edge Tunnel",
80
+ message: `'${app.name}' is marked public but has no incoming edge from a Cloudflare Tunnel node.`,
81
+ remediation: `Wire your Cloudflare Tunnel node → '${app.name}' so ingress YAML routes traffic cleanly.`,
82
+ });
83
+ }
84
+ }
85
+ }
86
+ // 4. Contract #3: Secret Hygiene — Detect hardcoded plaintext secrets (ADI-STUDIO-004)
87
+ for (const node of topology.nodes) {
88
+ for (const env of node.envVars) {
89
+ const looksSecret = env.isSecret || SECRET_KEY_PATTERN.test(env.key);
90
+ const isPlaceholder = env.value.includes("${") || env.value.trim() === "";
91
+ if (looksSecret && !isPlaceholder) {
92
+ findings.push({
93
+ id: `secret-${node.id}-${env.key}`,
94
+ code: "ADI-STUDIO-004",
95
+ severity: "critical",
96
+ nodeId: node.id,
97
+ nodeName: node.name,
98
+ title: `Plaintext Secret Detected in '${env.key}'`,
99
+ message: `Node '${node.name}' stores a literal value for sensitive variable '${env.key}'. Secrets must never be committed to declarative manifests.`,
100
+ remediation: `Replace the value with a Vault reference like '\${VAULT:${env.key}}' (encrypted via AES-256-GCM AAD on deploy).`,
101
+ });
102
+ }
103
+ }
104
+ }
105
+ // 5. CIS Container Hardening & Healthcheck Coverage (ADI-STUDIO-005)
106
+ for (const node of topology.nodes) {
107
+ const isCompute = node.kind === "web" ||
108
+ node.kind === "api" ||
109
+ node.kind === "worker" ||
110
+ node.kind === "cron";
111
+ if (isCompute && !node.nonRootUser) {
112
+ findings.push({
113
+ id: `root-${node.id}`,
114
+ code: "ADI-STUDIO-005",
115
+ severity: "high",
116
+ nodeId: node.id,
117
+ nodeName: node.name,
118
+ title: "Container Configured as Root (UID 0)",
119
+ message: `Compute service '${node.name}' has non-root isolation disabled, violating CIS Docker Benchmark 4.1.`,
120
+ remediation: `Enable 'Non-Root User (UID 10001)' in the node inspector for '${node.name}'.`,
121
+ });
122
+ }
123
+ if ((node.kind === "web" || node.kind === "api") && !node.healthcheckPath?.trim()) {
124
+ findings.push({
125
+ id: `hc-${node.id}`,
126
+ code: "ADI-STUDIO-005",
127
+ severity: "medium",
128
+ nodeId: node.id,
129
+ nodeName: node.name,
130
+ title: "Missing HTTP Healthcheck Endpoint",
131
+ message: `Service '${node.name}' has no healthcheck path configured for zero-downtime rollouts and watchdog recovery.`,
132
+ remediation: `Set healthcheckPath to '/api/v1/health' on '${node.name}'.`,
133
+ });
134
+ }
135
+ }
136
+ // 6. Directed Graph Cycle Detection & Topological Deploy Ordering (ADI-STUDIO-001)
137
+ // Edge source -> target means "source depends on target" EXCEPT tunnel -> app (where tunnel depends on app being healthy).
138
+ // In all cases, an edge A -> B means B should start before A in docker-compose depends_on.
139
+ const adjacency = new Map();
140
+ const inDegree = new Map();
141
+ for (const node of topology.nodes) {
142
+ adjacency.set(node.id, []);
143
+ inDegree.set(node.id, 0);
144
+ }
145
+ for (const edge of topology.edges) {
146
+ if (!nodeById.has(edge.sourceId) || !nodeById.has(edge.targetId))
147
+ continue;
148
+ if (edge.sourceId === edge.targetId) {
149
+ findings.push({
150
+ id: `self-loop-${edge.id}`,
151
+ code: "ADI-STUDIO-001",
152
+ severity: "critical",
153
+ nodeId: edge.sourceId,
154
+ nodeName: nodeById.get(edge.sourceId)?.name,
155
+ title: "Self-Referential Service Dependency",
156
+ message: `Service '${nodeById.get(edge.sourceId)?.name}' cannot depend on itself.`,
157
+ remediation: "Remove the self-loop edge on the topology canvas.",
158
+ });
159
+ continue;
160
+ }
161
+ // Dependency direction for startup order: target starts BEFORE source
162
+ adjacency.get(edge.targetId)?.push(edge.sourceId);
163
+ inDegree.set(edge.sourceId, (inDegree.get(edge.sourceId) || 0) + 1);
164
+ }
165
+ // Kahn's Algorithm for topological deploy order & cycle detection
166
+ const queue = [];
167
+ for (const [id, deg] of inDegree.entries()) {
168
+ if (deg === 0)
169
+ queue.push(id);
170
+ }
171
+ const orderedIds = [];
172
+ while (queue.length > 0) {
173
+ const curr = queue.shift();
174
+ orderedIds.push(curr);
175
+ for (const dependent of adjacency.get(curr) || []) {
176
+ const nextDeg = (inDegree.get(dependent) || 1) - 1;
177
+ inDegree.set(dependent, nextDeg);
178
+ if (nextDeg === 0)
179
+ queue.push(dependent);
180
+ }
181
+ }
182
+ const hasCycle = orderedIds.length !== topology.nodes.length;
183
+ if (hasCycle) {
184
+ findings.push({
185
+ id: "graph-cycle",
186
+ code: "ADI-STUDIO-001",
187
+ severity: "critical",
188
+ title: "Circular Dependency Cycle Detected in Topology",
189
+ message: "Two or more services form a circular dependency loop, preventing deterministic Docker startup ordering.",
190
+ remediation: "Break the cycle by introducing an async queue (Redis) or removing the circular edge.",
191
+ });
192
+ }
193
+ // 7. Orphaned Node Check (ADI-STUDIO-007)
194
+ if (topology.nodes.length > 1) {
195
+ for (const node of topology.nodes) {
196
+ const connected = topology.edges.some((e) => e.sourceId === node.id || e.targetId === node.id);
197
+ if (!connected) {
198
+ findings.push({
199
+ id: `orphan-${node.id}`,
200
+ code: "ADI-STUDIO-007",
201
+ severity: "info",
202
+ nodeId: node.id,
203
+ nodeName: node.name,
204
+ title: "Isolated Service Node",
205
+ message: `Service '${node.name}' has no incoming or outgoing edges on the canvas.`,
206
+ remediation: "Connect it to another service or remove it if unused.",
207
+ });
208
+ }
209
+ }
210
+ }
211
+ // 8. Resource & Sovereign INR Sizing Calculation
212
+ const resources = computeResourceSummary(topology);
213
+ // 9. Compute Security Score (0 - 100)
214
+ let score = 100;
215
+ for (const f of findings) {
216
+ if (f.severity === "critical")
217
+ score -= 30;
218
+ else if (f.severity === "high")
219
+ score -= 15;
220
+ else if (f.severity === "medium")
221
+ score -= 8;
222
+ else
223
+ score -= 2;
224
+ }
225
+ score = Math.max(0, Math.min(100, score));
226
+ const grade = score >= 95
227
+ ? "A+"
228
+ : score >= 85
229
+ ? "A"
230
+ : score >= 70
231
+ ? "B"
232
+ : score >= 55
233
+ ? "C"
234
+ : score >= 40
235
+ ? "D"
236
+ : "F";
237
+ const deployOrder = (hasCycle ? topology.nodes.map((n) => n.id) : orderedIds)
238
+ .map((id) => nodeById.get(id)?.name || id)
239
+ .filter(Boolean);
240
+ const hasCritical = findings.some((f) => f.severity === "critical");
241
+ return {
242
+ score,
243
+ grade,
244
+ isDeployReady: !hasCritical && topology.nodes.length > 0,
245
+ hasCycle,
246
+ deployOrder,
247
+ findings,
248
+ resources,
249
+ };
250
+ }
251
+ export function computeResourceSummary(topology) {
252
+ let totalVcpu = 0;
253
+ let totalMemoryMb = 0;
254
+ let computeServices = 0;
255
+ let dataServices = 0;
256
+ for (const node of topology.nodes) {
257
+ const multiplier = Math.max(1, node.replicas || 1);
258
+ totalVcpu += (node.cpuLimit || 0) * multiplier;
259
+ totalMemoryMb += (node.memoryMb || 0) * multiplier;
260
+ if (node.kind === "web" ||
261
+ node.kind === "api" ||
262
+ node.kind === "worker" ||
263
+ node.kind === "cron") {
264
+ computeServices += 1;
265
+ }
266
+ else if (node.kind === "postgres" ||
267
+ node.kind === "redis" ||
268
+ node.kind === "storage") {
269
+ dataServices += 1;
270
+ }
271
+ }
272
+ totalVcpu = Math.round(totalVcpu * 100) / 100;
273
+ // Map required resources (with 25% OS/Coolify headroom) to transparent BYOC INR VPS tiers
274
+ const neededRamMb = Math.ceil(totalMemoryMb * 1.25) + 512;
275
+ let recommendedTierName = "BYOC Micro (Hetzner CX22 / DO 2GB)";
276
+ let recommendedSpecs = "2 vCPU · 4 GB RAM · 40 GB NVMe";
277
+ let estimatedMonthlyInr = 450;
278
+ let estimatedAwsEquivalentInr = 3800;
279
+ if (neededRamMb > 12288 || totalVcpu > 6) {
280
+ recommendedTierName = "BYOC Dedicated Sovereign Node (16GB)";
281
+ recommendedSpecs = "8 vCPU · 16 GB RAM · 160 GB NVMe";
282
+ estimatedMonthlyInr = 2100;
283
+ estimatedAwsEquivalentInr = 16500;
284
+ }
285
+ else if (neededRamMb > 6144 || totalVcpu > 3.5) {
286
+ recommendedTierName = "BYOC Production Scale (Hetzner CX32 / DO blr1 8GB)";
287
+ recommendedSpecs = "4 vCPU · 8 GB RAM · 80 GB NVMe";
288
+ estimatedMonthlyInr = 950;
289
+ estimatedAwsEquivalentInr = 8900;
290
+ }
291
+ else if (neededRamMb > 3072 || totalVcpu > 2) {
292
+ recommendedTierName = "BYOC Growth Node (Hetzner CX22 / DO blr1 4GB)";
293
+ recommendedSpecs = "2 vCPU · 4 GB RAM · 40 GB NVMe";
294
+ estimatedMonthlyInr = 650;
295
+ estimatedAwsEquivalentInr = 5600;
296
+ }
297
+ const monthlySavingsPercent = Math.round(((estimatedAwsEquivalentInr - estimatedMonthlyInr) / estimatedAwsEquivalentInr) *
298
+ 100);
299
+ return {
300
+ serviceCount: topology.nodes.length,
301
+ computeServices,
302
+ dataServices,
303
+ totalVcpu,
304
+ totalMemoryMb,
305
+ recommendedTierName,
306
+ recommendedSpecs,
307
+ estimatedMonthlyInr,
308
+ estimatedAwsEquivalentInr,
309
+ monthlySavingsPercent,
310
+ };
311
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./engines/types.js";
2
+ export * from "./engines/blueprints.js";
3
+ export * from "./engines/validator.js";
4
+ export * from "./engines/codegen.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./engines/types.js";
2
+ export * from "./engines/blueprints.js";
3
+ export * from "./engines/validator.js";
4
+ export * from "./engines/codegen.js";
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@adityum/studio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Adityum Studio — Visual Microservice Topology Engine, Sovereign Security Graph Auditor & Declarative Code Generator (Zero Runtime Dependencies)",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "adi-studio": "bin/adi-studio.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "bin",
14
+ "README.md"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "prepack": "npm run build",
25
+ "test": "vitest run",
26
+ "typecheck": "tsc -p tsconfig.test.json --noEmit"
27
+ },
28
+ "dependencies": {},
29
+ "devDependencies": {
30
+ "@types/node": "^20.19.0",
31
+ "typescript": "^5.9.0",
32
+ "vitest": "^4.1.12"
33
+ }
34
+ }