Catalyst / admin/Synapse-Sonar 14.8 GB / 57.8 GB 40.0 GB free
Help Sign in

admin / Synapse-Sonar

public

Attack Surface Simulation

Code Issues Pull requests Pipelines Packages Security Insights Wiki Settings
Synapse-Sonar / synapse-sonar / components / AgentsDownload.tsx 13805 B · main
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"use client";

import { useEffect, useState } from "react";

type Platform = "linux" | "windows" | "sensor";

interface BundleFile {
  name: string; // filename inside the zip
  href: string; // static source to fetch
}

interface PlatformDef {
  label: string;
  blurb: string;
  configName: string;
  bundleFiles: BundleFile[];
  buildConfig: (v: Values) => string;
  install: (v: Values) => string;
  extraFields?: ("iface" | "flush" | "interval")[];
}

interface Values {
  sonarUrl: string;
  ingestKey: string;
  verifyTls: boolean;
  interval: string;
  iface: string;
  flush: string;
}

const PLATFORMS: Record<Platform, PlatformDef> = {
  linux: {
    label: "Linux host agent",
    blurb: "Debian / Ubuntu. Reports the host's own connections. Runs unprivileged.",
    configName: "sonar-agent.env",
    extraFields: ["interval"],
    bundleFiles: [
      { name: "sonar-agent.py", href: "/agents/host-agent/sonar-agent.py" },
      { name: "sonar-agent.service", href: "/agents/host-agent/sonar-agent.service" },
      { name: "install.sh", href: "/agents/host-agent/install.sh" },
      { name: "sonar-agent.env.example", href: "/agents/host-agent/sonar-agent.env.example" },
    ],
    buildConfig: (v) =>
      [
        `SONAR_URL=${v.sonarUrl}`,
        `SONAR_INGEST_KEY=${v.ingestKey}`,
        `SONAR_INTERVAL=${v.interval || "30"}`,
        `SONAR_VERIFY_TLS=${v.verifyTls}`,
        "",
      ].join("\n"),
    install: () =>
      [
        "# Unzip, then from this folder:",
        "sudo ./install.sh",
        "sudo cp sonar-agent.env /etc/sonar-agent/sonar-agent.env",
        "sudo systemctl enable --now sonar-agent",
        "journalctl -u sonar-agent -f",
      ].join("\n"),
  },
  windows: {
    label: "Windows host agent",
    blurb: "Reports the host's own TCP connections. Installs as a SYSTEM scheduled task.",
    configName: "config.json",
    extraFields: ["interval"],
    bundleFiles: [
      { name: "sonar-agent.ps1", href: "/agents/windows-agent/sonar-agent.ps1" },
      { name: "install.ps1", href: "/agents/windows-agent/install.ps1" },
      {
        name: "sonar-agent.config.example.json",
        href: "/agents/windows-agent/sonar-agent.config.example.json",
      },
    ],
    buildConfig: (v) =>
      JSON.stringify(
        {
          SonarUrl: v.sonarUrl,
          IngestKey: v.ingestKey,
          IntervalSeconds: Number(v.interval || 30),
          VerifyTls: v.verifyTls,
        },
        null,
        2
      ),
    install: () =>
      [
        "# Unzip, then from an elevated PowerShell in this folder:",
        "powershell -ExecutionPolicy Bypass -File .\\install.ps1",
        'copy config.json "$env:ProgramData\\SonarAgent\\config.json"',
        "Restart-ScheduledTask -TaskName SynapseSonarAgent",
      ].join("\n"),
  },
  sensor: {
    label: "Passive sensor (agentless)",
    blurb: "Run on a SPAN/mirror port to monitor devices that can't take an agent.",
    configName: "sonar-sensor.env",
    extraFields: ["iface", "flush"],
    bundleFiles: [
      { name: "sonar-sensor.py", href: "/agents/sensor/sonar-sensor.py" },
      { name: "sonar-sensor.service", href: "/agents/sensor/sonar-sensor.service" },
      { name: "install.sh", href: "/agents/sensor/install.sh" },
      { name: "sonar-sensor.env.example", href: "/agents/sensor/sonar-sensor.env.example" },
    ],
    buildConfig: (v) =>
      [
        `SONAR_URL=${v.sonarUrl}`,
        `SONAR_INGEST_KEY=${v.ingestKey}`,
        `SONAR_IFACE=${v.iface || "eth1"}`,
        `SONAR_FLUSH=${v.flush || "30"}`,
        `SONAR_VERIFY_TLS=${v.verifyTls}`,
        "",
      ].join("\n"),
    install: () =>
      [
        "# Unzip on the host attached to the mirror port, then:",
        "sudo ./install.sh",
        "sudo cp sonar-sensor.env /etc/sonar-sensor/sonar-sensor.env",
        "sudo systemctl enable --now sonar-sensor",
        "journalctl -u sonar-sensor -f",
      ].join("\n"),
  },
};

function downloadBlob(filename: string, blob: Blob) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  URL.revokeObjectURL(url);
}

export default function AgentsDownload() {
  const [platform, setPlatform] = useState<Platform>("linux");
  const [values, setValues] = useState<Values>({
    sonarUrl: "",
    ingestKey: "",
    verifyTls: true,
    interval: "30",
    iface: "eth1",
    flush: "30",
  });
  const [generating, setGenerating] = useState(false);
  const [keyNote, setKeyNote] = useState<string | null>(null);
  const [bundling, setBundling] = useState(false);
  const [bundleError, setBundleError] = useState<string | null>(null);
  const [copied, setCopied] = useState(false);

  useEffect(() => {
    setValues((v) => ({ ...v, sonarUrl: window.location.origin }));
  }, []);

  const def = PLATFORMS[platform];
  const set = (k: keyof Values, v: string | boolean) =>
    setValues((prev) => ({ ...prev, [k]: v }));
  const ready = Boolean(values.sonarUrl.trim() && values.ingestKey.trim());

  const generateKey = async () => {
    setGenerating(true);
    setKeyNote(null);
    try {
      const res = await fetch("/api/integrations/ingest-key", { method: "POST" });
      const json = await res.json().catch(() => ({}));
      if (res.ok && json.ingestKey) {
        set("ingestKey", json.ingestKey);
        setKeyNote("New key issued. This replaces any previous key — old agents stop working.");
      } else {
        setKeyNote(json.error ?? "Could not generate a key (Tenant Admin required).");
      }
    } finally {
      setGenerating(false);
    }
  };

  const downloadBundle = async () => {
    setBundling(true);
    setBundleError(null);
    try {
      const { default: JSZip } = await import("jszip");
      const zip = new JSZip();

      for (const f of def.bundleFiles) {
        const res = await fetch(f.href);
        if (!res.ok) throw new Error(`Could not fetch ${f.name}`);
        const text = await res.text();
        const exec = f.name.endsWith(".sh") || f.name.endsWith(".py");
        zip.file(f.name, text, exec ? { unixPermissions: 0o755 } : undefined);
      }

      // The pre-filled config + a quickstart, generated from the form values.
      zip.file(def.configName, def.buildConfig(values));
      zip.file("QUICKSTART.txt", def.install(values) + "\n");

      const blob = await zip.generateAsync({ type: "blob", platform: "UNIX" });
      downloadBlob(`synapse-sonar-${platform}-agent.zip`, blob);
    } catch (e) {
      setBundleError((e as Error).message ?? "Bundle failed");
    } finally {
      setBundling(false);
    }
  };

  const copyInstall = async () => {
    try {
      await navigator.clipboard.writeText(def.install(values));
      setCopied(true);
      setTimeout(() => setCopied(false), 2000);
    } catch {
      /* ignore */
    }
  };

  return (
    <div className="min-h-screen bg-[#05080f] px-8 py-10">
      <div className="mx-auto max-w-3xl">
        <header className="mb-8">
          <h1 className="text-2xl font-semibold text-slate-100">Agents &amp; Collectors</h1>
          <p className="mt-1 text-sm text-slate-400">
            Set the Sonar address and ingest key, then download a single
            ready-to-run bundle for each platform.
          </p>
        </header>

        {/* platform tabs */}
        <div className="mb-6 grid grid-cols-1 gap-3 sm:grid-cols-3">
          {(Object.keys(PLATFORMS) as Platform[]).map((p) => (
            <button
              key={p}
              onClick={() => setPlatform(p)}
              className={`rounded-xl border p-4 text-left transition ${
                platform === p
                  ? "border-cyan-500/60 bg-cyan-500/10"
                  : "border-slate-700/60 bg-gradient-to-b from-[#1e293b] to-[#0f172a] hover:border-slate-500/60"
              }`}
            >
              <div className="text-sm font-semibold text-slate-100">{PLATFORMS[p].label}</div>
              <div className="mt-1 text-xs text-slate-400">{PLATFORMS[p].blurb}</div>
            </button>
          ))}
        </div>

        {/* configuration */}
        <div className="rounded-xl border border-slate-700/60 bg-gradient-to-b from-[#1e293b] to-[#0f172a] p-6">
          <h2 className="text-sm font-semibold text-slate-100">Configuration</h2>

          <div className="mt-4 space-y-4">
            <Field label="Synapse Sonar address" hint="Where agents send data. Use a hostname/IP reachable by your hosts.">
              <input
                value={values.sonarUrl}
                onChange={(e) => set("sonarUrl", e.target.value)}
                placeholder="https://sonar.corp.internal:3000"
                className={`${inputCls} font-mono`}
              />
            </Field>

            <Field
              label="Agent credential (ingest key)"
              hint="Agents authenticate with this key — not a user login. Keep it secret."
            >
              <div className="flex gap-2">
                <input
                  value={values.ingestKey}
                  onChange={(e) => set("ingestKey", e.target.value)}
                  placeholder="nsx_sensor_…  (paste, or generate)"
                  className={`${inputCls} flex-1 font-mono`}
                />
                <button
                  onClick={generateKey}
                  disabled={generating}
                  className="shrink-0 rounded-lg border border-cyan-500/50 px-4 py-2 text-xs font-semibold text-cyan-200 transition hover:bg-cyan-500/10 disabled:opacity-50"
                >
                  {generating ? "Generating…" : "Generate"}
                </button>
              </div>
              {keyNote && <span className="mt-1 block text-[11px] text-amber-300">{keyNote}</span>}
            </Field>

            <div className="grid grid-cols-2 gap-4">
              {def.extraFields?.includes("interval") && (
                <Field label="Push interval (seconds)">
                  <input
                    value={values.interval}
                    onChange={(e) => set("interval", e.target.value.replace(/\D/g, ""))}
                    className={inputCls}
                  />
                </Field>
              )}
              {def.extraFields?.includes("flush") && (
                <Field label="Aggregation window (seconds)">
                  <input
                    value={values.flush}
                    onChange={(e) => set("flush", e.target.value.replace(/\D/g, ""))}
                    className={inputCls}
                  />
                </Field>
              )}
              {def.extraFields?.includes("iface") && (
                <Field label="Mirror interface">
                  <input
                    value={values.iface}
                    onChange={(e) => set("iface", e.target.value)}
                    placeholder="eth1"
                    className={`${inputCls} font-mono`}
                  />
                </Field>
              )}
            </div>

            <label className="flex items-center gap-2 text-xs text-slate-300">
              <input
                type="checkbox"
                checked={values.verifyTls}
                onChange={(e) => set("verifyTls", e.target.checked)}
                className="h-4 w-4 accent-cyan-500"
              />
              Verify TLS certificate (uncheck only for self-signed Sonar certs)
            </label>
          </div>

          {/* download */}
          <div className="mt-6 border-t border-slate-700/50 pt-5">
            <button
              onClick={downloadBundle}
              disabled={!ready || bundling}
              className="rounded-lg bg-cyan-500 px-5 py-2.5 text-sm font-semibold text-[#05080f] transition hover:bg-cyan-400 disabled:opacity-40"
              title={ready ? "" : "Set the Sonar address and ingest key first"}
            >
              {bundling ? "Building…" : `Download ${def.label} bundle (.zip)`}
            </button>
            <p className="mt-2 text-[11px] text-slate-500">
              The zip contains the agent, installer, your pre-filled config, and a QUICKSTART.
            </p>
            {!ready && (
              <p className="mt-1 text-[11px] text-slate-500">
                Set the Sonar address and ingest key to enable the download.
              </p>
            )}
            {bundleError && <p className="mt-1 text-[11px] text-rose-400">{bundleError}</p>}
          </div>

          {/* install commands */}
          <div className="mt-6 border-t border-slate-700/50 pt-5">
            <div className="flex items-center justify-between">
              <h3 className="text-xs font-semibold uppercase tracking-wider text-slate-500">
                Install
              </h3>
              <button onClick={copyInstall} className="text-xs font-medium text-cyan-300 hover:text-cyan-200">
                {copied ? "Copied ✓" : "Copy"}
              </button>
            </div>
            <pre className="mt-2 overflow-x-auto rounded-lg border border-slate-700/60 bg-[#05080f] p-3 font-mono text-xs leading-relaxed text-slate-300">
{def.install(values)}
            </pre>
          </div>
        </div>
      </div>
    </div>
  );
}

const inputCls =
  "w-full rounded-lg border border-slate-600/60 bg-[#05080f] px-3 py-2 text-sm text-slate-100 outline-none transition placeholder:text-slate-600 focus:border-cyan-500";

function Field({ label, hint, children }: { label: string; hint?: string; children: React.ReactNode }) {
  return (
    <div>
      <label className="mb-1.5 block text-xs font-medium text-slate-300">{label}</label>
      {children}
      {hint && <span className="mt-1 block text-[11px] text-slate-500">{hint}</span>}
    </div>
  );
}