@solongate/proxy 0.3.1 → 0.4.1

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,183 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/pull-push.ts
4
+ import { readFileSync as readFileSync2, writeFileSync, existsSync as existsSync2 } from "fs";
5
+ import { resolve as resolve2 } from "path";
6
+
7
+ // src/config.ts
8
+ import { readFileSync, existsSync } from "fs";
9
+ import { resolve } from "path";
10
+ async function fetchCloudPolicy(apiKey, apiUrl, policyId) {
11
+ let resolvedId = policyId;
12
+ if (!resolvedId) {
13
+ const listRes = await fetch(`${apiUrl}/api/v1/policies`, {
14
+ headers: { "Authorization": `Bearer ${apiKey}` }
15
+ });
16
+ if (!listRes.ok) {
17
+ const body = await listRes.text().catch(() => "");
18
+ throw new Error(`Failed to list policies from cloud (${listRes.status}): ${body}`);
19
+ }
20
+ const listData = await listRes.json();
21
+ const policies = listData.policies ?? [];
22
+ if (policies.length === 0) {
23
+ throw new Error("No policies found in cloud. Create one in the dashboard first.");
24
+ }
25
+ resolvedId = policies[0].id;
26
+ }
27
+ const url = `${apiUrl}/api/v1/policies/${resolvedId}`;
28
+ const res = await fetch(url, {
29
+ headers: { "Authorization": `Bearer ${apiKey}` }
30
+ });
31
+ if (!res.ok) {
32
+ const body = await res.text().catch(() => "");
33
+ throw new Error(`Failed to fetch policy from cloud (${res.status}): ${body}`);
34
+ }
35
+ const data = await res.json();
36
+ return {
37
+ id: String(data.id ?? "cloud"),
38
+ name: String(data.name ?? "Cloud Policy"),
39
+ version: Number(data._version ?? 1),
40
+ rules: data.rules ?? [],
41
+ createdAt: String(data._created_at ?? ""),
42
+ updatedAt: ""
43
+ };
44
+ }
45
+
46
+ // src/pull-push.ts
47
+ var log = (...args) => process.stderr.write(`[SolonGate] ${args.map(String).join(" ")}
48
+ `);
49
+ function loadEnv() {
50
+ if (process.env.SOLONGATE_API_KEY) return;
51
+ const envPath = resolve2(".env");
52
+ if (!existsSync2(envPath)) return;
53
+ try {
54
+ const content = readFileSync2(envPath, "utf-8");
55
+ for (const line of content.split("\n")) {
56
+ const trimmed = line.trim();
57
+ if (!trimmed || trimmed.startsWith("#")) continue;
58
+ const eq = trimmed.indexOf("=");
59
+ if (eq === -1) continue;
60
+ const key = trimmed.slice(0, eq).trim();
61
+ const val = trimmed.slice(eq + 1).trim().replace(/^["']|["']$/g, "");
62
+ if (key === "SOLONGATE_API_KEY") {
63
+ process.env.SOLONGATE_API_KEY = val;
64
+ return;
65
+ }
66
+ }
67
+ } catch {
68
+ }
69
+ }
70
+ function parseCliArgs() {
71
+ loadEnv();
72
+ const args = process.argv.slice(2);
73
+ const command = args[0];
74
+ let apiKey = process.env.SOLONGATE_API_KEY || "";
75
+ let file = "policy.json";
76
+ let policyId;
77
+ for (let i = 1; i < args.length; i++) {
78
+ switch (args[i]) {
79
+ case "--api-key":
80
+ apiKey = args[++i];
81
+ break;
82
+ case "--policy":
83
+ case "--output":
84
+ case "--file":
85
+ case "-f":
86
+ case "-o":
87
+ file = args[++i];
88
+ break;
89
+ case "--policy-id":
90
+ case "--id":
91
+ policyId = args[++i];
92
+ break;
93
+ }
94
+ }
95
+ if (!apiKey) {
96
+ log("ERROR: API key not found.");
97
+ log("");
98
+ log("Set it in .env file:");
99
+ log(" SOLONGATE_API_KEY=sg_live_...");
100
+ log("");
101
+ log("Or pass via environment:");
102
+ log(` SOLONGATE_API_KEY=sg_live_... solongate-proxy ${command}`);
103
+ process.exit(1);
104
+ }
105
+ if (!apiKey.startsWith("sg_live_")) {
106
+ log("ERROR: Pull/push requires a live API key (sg_live_...).");
107
+ process.exit(1);
108
+ }
109
+ return { command, apiKey, file: resolve2(file), policyId };
110
+ }
111
+ async function pull(apiKey, file, policyId) {
112
+ const apiUrl = "https://api.solongate.com";
113
+ log(`Pulling policy from dashboard...`);
114
+ const policy = await fetchCloudPolicy(apiKey, apiUrl, policyId);
115
+ const json = JSON.stringify(policy, null, 2) + "\n";
116
+ writeFileSync(file, json, "utf-8");
117
+ log(`Saved: ${file}`);
118
+ log(` Name: ${policy.name}`);
119
+ log(` Version: ${policy.version}`);
120
+ log(` Rules: ${policy.rules.length}`);
121
+ log("");
122
+ log("Done. Policy pulled from dashboard to local file.");
123
+ }
124
+ async function push(apiKey, file) {
125
+ const apiUrl = "https://api.solongate.com";
126
+ if (!existsSync2(file)) {
127
+ log(`ERROR: File not found: ${file}`);
128
+ process.exit(1);
129
+ }
130
+ const content = readFileSync2(file, "utf-8");
131
+ let policy;
132
+ try {
133
+ policy = JSON.parse(content);
134
+ } catch {
135
+ log(`ERROR: Invalid JSON in ${file}`);
136
+ process.exit(1);
137
+ }
138
+ log(`Pushing policy to dashboard...`);
139
+ log(` File: ${file}`);
140
+ log(` Name: ${policy.name || "Unnamed"}`);
141
+ log(` Rules: ${(policy.rules || []).length}`);
142
+ const res = await fetch(`${apiUrl}/api/v1/policies`, {
143
+ method: "POST",
144
+ headers: {
145
+ "Authorization": `Bearer ${apiKey}`,
146
+ "Content-Type": "application/json"
147
+ },
148
+ body: JSON.stringify({
149
+ id: policy.id || "default",
150
+ name: policy.name || "Local Policy",
151
+ description: policy.description || "Pushed from local file",
152
+ version: policy.version || 1,
153
+ rules: policy.rules || []
154
+ })
155
+ });
156
+ if (!res.ok) {
157
+ const body = await res.text().catch(() => "");
158
+ log(`ERROR: Push failed (${res.status}): ${body}`);
159
+ process.exit(1);
160
+ }
161
+ const data = await res.json();
162
+ log(` Cloud version: ${data._version ?? "created"}`);
163
+ log("");
164
+ log("Done. Policy pushed from local file to dashboard.");
165
+ }
166
+ async function main() {
167
+ const { command, apiKey, file, policyId } = parseCliArgs();
168
+ try {
169
+ if (command === "pull") {
170
+ await pull(apiKey, file, policyId);
171
+ } else if (command === "push") {
172
+ await push(apiKey, file);
173
+ } else {
174
+ log(`Unknown command: ${command}`);
175
+ log("Usage: solongate-proxy pull|push [--file policy.json]");
176
+ process.exit(1);
177
+ }
178
+ } catch (err) {
179
+ log(`ERROR: ${err instanceof Error ? err.message : String(err)}`);
180
+ process.exit(1);
181
+ }
182
+ }
183
+ main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solongate/proxy",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "MCP security proxy — protect any MCP server with customizable policies, path/command constraints, rate limiting, and audit logging. Zero code changes required.",
5
5
  "type": "module",
6
6
  "bin": {