# n8n Docker (Security Lab)

n8n runs on the same Docker network as MISP (`security-lab`) so workflows can reach **misp-core** by hostname: `https://misp-core:443`.

## Deploy

```bash
./deploy_n8n.sh
```

- Creates the `security-lab` network and `n8n_data` volume if missing
- Starts n8n with port **5678** and attaches it to `security-lab`
- Idempotent: safe to re-run; if n8n is already running, it is recreated so network/compose changes apply

## Destroy

```bash
./deploy_n8n.sh destroy
```

- Stops and removes the n8n container and its volume (clean re-deploy)

## Environment (optional)

| Variable      | Default        | Description                          |
|---------------|----------------|--------------------------------------|
| `N8N_PORT`    | `5678`         | Host port for n8n                    |
| `N8N_NETWORK` | `security-lab` | Docker network (must match MISP)      |
| `N8N_VOLUME`  | `n8n_data`     | Docker volume for n8n persistence     |

## MISP in workflows

From inside n8n (same Docker network), use:

- **Base URL:** `https://misp-core:443`
- **Allow Unauthorized Certs:** yes (MISP uses self-signed in lab)

If MISP is deployed with `deploy_misp.sh`, it uses the same `security-lab` network; no extra steps needed.

## Troubleshooting: Can't connect to MISP from n8n

If workflows can't reach MISP, both containers must be on the same Docker network and the network must exist.

**1. Check that both are on `security-lab`**

```bash
docker network inspect security-lab --format '{{range .Containers}}{{.Name}} {{end}}'
```

You should see both `n8n` and `misp-core` (or the MISP core container name). If either is missing, connect it (steps below).

**2. Create the network if it doesn't exist**

```bash
docker network create security-lab
```

**3. Attach n8n to `security-lab`** (if it wasn't in the list)

```bash
docker network connect security-lab n8n
```

**4. Attach MISP core to `security-lab`** (if it wasn't in the list)

Use the actual MISP core container name (often `misp-core` or `misp_config-misp-core-1`):

```bash
docker network connect security-lab misp-core
# or, if your container has a project prefix:
docker network connect security-lab misp_config-misp-core-1
```

**5. Re-run deploys so future runs stay correct**

- From `misp_config`: `./deploy_misp.sh` (ensures network exists and connects n8n if found).
- From `n8n_docker`: `./deploy_n8n.sh` (ensures network exists and recreates n8n on it).

**6. n8n MISP credential**

- **Base URL:** `https://misp-core:443` or `https://misp-core`
- **Allow Unauthorized Certificates:** enabled (MISP uses self-signed in lab)

## Management

From this directory (`n8n_docker`):

- **Start:** `docker compose up -d`
- **Stop:** `docker compose down`
- **Logs:** `docker logs -f n8n`
