Skip to content
Guides

Use tunnels on your FortiGate

Keep your IPsec tunnels on your own FortiGate and let MedHook watch them. Create a read-only API user, connect the FortiGate (or FortiManager, for every FortiGate it manages), import its tunnels, and get status, reachability tests, diagnosis, alerts and peer names on Messages. Optionally, bring a tunnel up or create new tunnels from MedHook, with a preview and a second admin's approval.

On this page

You may already end every healthcare organization's IPsec tunnel on your own FortiGate. You don't have to move them to use MedHook. Connect the FortiGate, pick the tunnels MedHook should watch, and they show on the Tunnels page next to MedHook's own:

  • status per tunnel and per phase 2, the peer, the remote subnets, traffic and the last change
  • the reachability test and the diagnosis checks
  • tunnel alerts (down, phase 2 down, recovered) through your alert rules
  • the healthcare organization's name next to each peer on Messages › Connections, and on each message in the Stream (filter with peer:"<name>")

By default MedHook only reads. It doesn't change the FortiGate, and it never asks for or stores a pre-shared key. If you want it to, an admin can allow two kinds of change on a FortiGate connection (never through FortiManager): operators can bring a tunnel up, and admins can create new tunnels, each previewed and approved by a second admin before anything is applied.

Before you start

  • FortiOS 7.0, 7.2 or 7.4. MedHook reads the REST API those versions answer.
  • The engine can reach the FortiGate's management address over HTTPS. A private address must be on the engine's SSRF_ALLOWLIST, the same as any private host the engine calls.
  • The engine's network routes each organization's subnet to the FortiGate. MedHook adds no route; the reachability test shows whether one is there.

Reading directly from each FortiGate works whether or not FortiManager manages it. If FortiManager manages your FortiGates, you can instead connect FortiManager once and reach every FortiGate it manages: see Connect through FortiManager.

1. Create a read-only API user on the FortiGate

On the FortiGate, as a super admin:

  1. System › Admin Profiles › Create New. Name it medhook-read. Set every permission to None, then set VPN to Read. In the CLI:

    text
    config system accprofile
        edit "medhook-read"
            set vpngrp read
            set sysgrp read
        next
    end

    sysgrp read lets MedHook read the version, host name and serial for the connection test. Leave everything else at none.

    Optional: read on address objects. If your phase 2 selectors name a firewall address or address group instead of typing a subnet, also let the profile read addresses. In the GUI, set Firewall to Custom and Address to Read. In the CLI:

    text
    config system accprofile
        edit "medhook-read"
            set fwgrp custom
            config fwgrp-permission
                set address read
            end
        next
    end

    MedHook then reads the address objects so a tunnel's subnets are known even while it is down. Without it, MedHook uses the subnets the FortiGate reports for the live tunnel, as before.

  2. System › Administrators › Create New › REST API Admin. Name it medhook, pick the medhook-read profile, and turn off PKI Group.

  3. Under Trusted Hosts, enter the address the engine's requests come from (its node subnet or its NAT address). The FortiGate refuses the token from anywhere else.

  4. If you use VDOMs, give the user the VDOM that holds the tunnels.

  5. Click OK and copy the API token. The FortiGate shows it once.

2. Connect the FortiGate

On Tunnels, under On your FortiGate, click Connect a FortiGate. Only admins see this button.

FieldWhat to enter
NameA name you recognize, such as Main FortiGate
Management URLhttps://fortigate.example.test or https://fortigate.example.test:8443
REST API tokenThe token from step 1
VDOMThe VDOM that holds the tunnels, or leave it empty for the user's default

Click Test. Most FortiGates use their factory certificate, which the engine doesn't trust yet, so the test shows the certificate's SHA-256 fingerprint. Compare it with the one under System › Certificates on the FortiGate. If they match, click Trust this certificate. MedHook then trusts exactly that certificate and no other. If your FortiGate has a certificate from your own CA, open Verify with a CA certificate instead and paste the CA.

A good test reads "Connected: FortiOS v7.2.8 on fw-main. 50 tunnels, 48 up." Click Save. The engine stores the token encrypted and never shows it again.

You can connect more than one FortiGate, or the same one once per VDOM.

Connect through FortiManager

One FortiManager connection covers every FortiGate FortiManager manages. MedHook reads each FortiGate's tunnels through FortiManager's device proxy, with the same requests as a direct connection, so status, alerts, tests, diagnosis and the names on Messages work the same. The engine needs to reach FortiManager only, not each FortiGate.

On FortiManager, as a super admin:

  1. System Settings › Admin › Profile. Create a profile with Device Manager set to Read-Only and everything else to None.
  2. System Settings › Admin › Administrators › Create New. Pick that profile, set JSON API Access to Read, and give it the ADOMs that hold your FortiGates. Under Trusted Hosts, enter the engine's address.
  3. Choose how MedHook signs in:
    • API key (FortiManager 7.2.2 and later): make the user an API user and generate its key (execute api-user generate-key <user> in the CLI). Copy it; FortiManager shows it once.
    • User name and password: MedHook logs in for each read and logs out after it. FortiManager's event log shows each login.

Then, on Tunnels, click Connect a FortiGate, choose FortiManager as the connection type, and fill in:

FieldWhat to enter
NameA name you recognize, such as Main FortiManager
FortiManager URLhttps://fortimanager.example.test
Sign in withAPI key, or User name and password
ADOMOne ADOM to read, or leave it empty to read every FortiGate ADOM
VDOMThe VDOM to read on every FortiGate, or leave it empty for each one's default

Test shows FortiManager's version, how many FortiGates it manages, their tunnels, and any FortiGate FortiManager can't reach right now. Trust the certificate the same way as a FortiGate's.

When you import, each tunnel is listed as <FortiGate>/<phase 1>, such as fw-east/to-clinic-a, so two FortiGates with a tunnel of the same name stay apart. Each tracked tunnel remembers the FortiGate it lives on.

If FortiManager can't reach one of its FortiGates, that FortiGate's tunnels keep their last status: MedHook doesn't know they're down, so it doesn't alert. The connection shows an error only when FortiManager can't reach any of them. Diagnose on such a tunnel says which FortiGate FortiManager can't reach.

MedHook still only reads. Through FortiManager it sends only get requests, and asks the device proxy only to GET. It never installs, runs a script, or changes a device.

3. Import the tunnels

Click Import tunnels on the connection. MedHook lists every phase 1 the FortiGate has, with its status, peer and remote subnets. Tick the ones MedHook should watch and type the healthcare organization's name for each. Leave a name empty to use the phase 1 name. Click Import.

Each tracked tunnel now shows on the Tunnels page. MedHook reads the FortiGate every minute and when you click Refresh, at most once every 5 seconds per FortiGate.

StatusMeans
UpEvery phase 2 is up
Phase 2 downSome phase 2s are down
DownNo phase 2 is up
Not on the FortiGateThe phase 1 was removed or renamed

4. Alerts

A tracked tunnel alerts through your tunnel events alert rules, the same as MedHook's own tunnels:

  • Down goes out when the tunnel has been down for 2 minutes, so a short drop never pages.
  • Phase 2 down goes out when some phase 2s drop.
  • Recovered closes the alert when the tunnel is back.

The row shows the latest alert and when it goes out. MedHook only alerts: it doesn't fail over or bring the tunnel up on its own. Fix the tunnel on the FortiGate, or, where actions are allowed, click Bring tunnel up (below).

5. Overlapping subnets and NAT

With many healthcare organizations, two of them often use the same private range, such as 10.1.0.0/16. Usually the FortiGate translates one of them, and the engine sees the translated range.

Open the tunnel's row and enter the ranges the engine sees in Subnets the engine sees, such as 100.64.12.0/24. MedHook uses them to name peers on Messages › Connections and to bound the reachability test.

Subnets from address objects

When a phase 2 names an address or address group, MedHook reads it from the FortiGate and uses its subnets. Groups inside groups are followed. An address range becomes the subnets that cover it exactly. FQDN, country, wildcard and dynamic addresses aren't subnets, so MedHook leaves them out and says which. A group that excludes addresses is left out too.

The phase 2 line in the tunnel's row says where its subnets came from:

  • From address object name: read from the object on the FortiGate.
  • From the tunnel's live selectors: MedHook couldn't use the object, and says why, for example the API profile can't read addresses (HTTP 403). It uses the subnets the FortiGate reports for the live tunnel instead.

MedHook reads the address tables once per refresh, not once per tunnel, and only when a phase 2 names an object.

When two tunnels still cover the same address, MedHook picks the more specific subnet. If they are equally specific, it doesn't guess: the tunnel shows an overlap warning, and Messages › Connections and the Stream show Ambiguous with both names.

Test and diagnose

Open a tunnel's row. Enter a host inside its subnets and a port, such as the organization's MLLP listener, and click Test reachability. The engine opens a TCP connection to it. Only addresses inside the tunnel's subnets can be tested.

Diagnose runs these checks in order and stops at the first failure:

  1. FortiGate answers. The API read works.
  2. FortiGate reports phase 2 up.
  3. Route from the engine's network to the remote subnet. On Google Cloud, with the engine's project set, MedHook checks that a route covers each subnet. Elsewhere it skips this check.
  4. TCP reachability to the host and port you entered.

Bring a tunnel up (optional)

Off by default, and only on a FortiGate connection (a FortiManager connection only reads). When it is on, operators can ask the FortiGate to bring a tracked tunnel up from its row, the same as Bring Up under VPN › IPsec Tunnels on the FortiGate.

To turn it on:

  1. On the FortiGate, give the API user's profile write access to VPN: System › Admin Profiles, edit medhook-read (or a copy of it), and set VPN to Read/Write. In the CLI: set vpngrp read-write. A read-only profile refuses the action.
  2. In MedHook, an admin opens the connection with Edit, turns on Allow actions and clicks Save.

Then open a tunnel's row and click Bring tunnel up. MedHook asks you to confirm on the page and names the phase 2s it will bring up: the ones that are down, or all of them when none is. Click Bring up. The status changes on the next read, once the organization's peer answers. If the peer doesn't answer, the tunnel stays down; diagnose it.

Each tunnel can be asked 3 times a minute, and each person can ask 6 times a minute across tunnels.

MedHook sends POST /api/v2/monitor/vpn/ipsec/tunnel_up with the phase 1 and phase 2 names.

What MedHook reads

All reads are GET, with the token in the Authorization header. Bringing a tunnel up, when allowed, is the POST above; creating a tunnel uses the writes listed below.

RequestFor
/api/v2/monitor/system/statusVersion, host name and serial
/api/v2/monitor/vpn/ipsecEach tunnel's phase 2 status, subnets, traffic and peer
/api/v2/cmdb/vpn.ipsec/phase1-interfacePeer, IKE version and proposal, by field name, never the key
/api/v2/cmdb/vpn.ipsec/phase2-interfacePhase 2 names and subnets, or the address object they name
/api/v2/cmdb/firewall/address, addrgrpThe named addresses and groups (only when a phase 2 names one; optional)
/api/v2/cmdb/firewall/address6, addrgrp6The same for IPv6 selectors

Through FortiManager, every request is a JSON-RPC call to /jsonrpc:

CallFor
exec /sys/login/user, /sys/logoutOnly with a user name and password; an API key goes in Authorization
get /sys/statusFortiManager's version, host name and serial
get /dvmdb/adomThe FortiGate ADOMs, when you don't name one
get /dvmdb/adom/<adom>/deviceThe FortiGates it manages
exec /sys/proxy/jsonThe FortiGate requests above, as GET, to each FortiGate

Create a tunnel on the FortiGate

This is off unless you turn it on, and only on a FortiGate connection. When it's on, an admin can set up a new healthcare organization's tunnel from MedHook instead of typing it into the FortiGate. A second admin approves it before anything changes.

Give the API user write permission

The read-only profile from step 1 can't create anything. To use this feature, give the API user an admin profile with Read/Write for:

  • VPN (phase 1 and phase 2)
  • Firewall › Address, Firewall › Service and Firewall › Policy
  • Network › Router (static routes, and read on the routing table)

Without write permission, an apply stops at the first step with "HTTP 403 … needs read-write permission". Nothing is created.

Turn it on

On Tunnels › On your FortiGate, under the connection, an admin clicks Allow MedHook to create tunnels. Stop creating tunnels turns it off again. Both are in the audit log.

Changing the connection afterwards (its address, VDOM, token, trusted certificate, or turning tunnel creation on or off) sends every approved request on it back for a new approval. Only renaming it doesn't.

Ask for a tunnel

You need to be signed in: an API token can't ask for, change or approve a tunnel. Click New tunnel and fill in:

FieldDefaultNotes
Healthcare organizationBecomes the tunnel's name under On your FortiGate
Tunnel name on the FortiGatemh-<organization>15 characters at most (a FortiOS limit)
Their firewall's public IPThe peer. A private address is allowed, with a warning
Your subnets, their subnetsCIDRs, up to 16 each
IKE versionIKEv2IKEv1 works, with a warning
Phase 1 and phase 2 proposalsaes256-sha256GCM and ChaCha20 phase 1 proposals need IKEv2
Diffie-Hellman groups1414, 15, 16, 19, 20 or 21
Internet-facing interfacewan1Where phase 1 listens
Interface your subnets are oninternalUsed by the two firewall policies
Ports they connect toMLLP listener portsFrom their network to yours: your engine's MLLP listener ports, TCP (and UDP, usually none), e.g. 2575 or 6661-6670
Their MLLP portFrom your network to theirs: where MedHook sends them messages, e.g. 6661. Required unless you allow every service that way
Allow every service (ALL)OffTicked separately for each direction, only when you need it; the preview warns about it
Pre-shared key20 characters at least. Generate makes a strong one in your browser and shows it once so you can give it to their firewall's admin. Never shown again

Click Preview changes. MedHook lists every change in order, as the FortiOS REST call (method, path and JSON body) and as the same FortiOS CLI. The key always shows as ********. The preview also reads the FortiGate and tells you if any of the names already exist, and if their subnets overlap a route the FortiGate already uses (a connected subnet, a static route or another tunnel), as "10.1.0.0/16 overlaps existing route 10.1.0.0/16 via dh-org-48". MedHook doesn't refuse an overlap, but you have to acknowledge it to ask for it. Each warning is listed by a short key, such as 10.1.0.0/16 via 10.1.0.0/16 dh-org-48, and ticking I've checked each of these stores exactly those keys in the request. A subnet wider than /16 needs the same, as 10.0.0.0/12 wider than /16. The approver sees the keys you acknowledged and, when they open the request, MedHook reads the FortiGate's routes again and shows any new overlap you didn't acknowledge (or says it couldn't check).

The route check reads the FortiGate's active routing table. It doesn't see static routes that aren't active (for example through a tunnel that is down), and it doesn't check that the interface and zone names exist. Check those on the FortiGate.

The changes, in order:

  1. An address object for each of your subnets (an address group when there are several)
  2. The same for their subnets
  3. Phase 1, with dead-peer detection and NAT-T set explicitly (set dpd on-idle, set dpd-retryinterval 5, set dpd-retrycount 3, set nattraversal enable), the same as MedHook's FortiGate peer config
  4. Phase 2, its selectors set to those address objects
  5. Two custom services: <tunnel name>-svc-in, the ports they connect to, and <tunnel name>-svc-out, their MLLP port (either is left out when you allow every service that way)
  6. Two firewall policies: your network to the tunnel (-svc-out), and the tunnel to your network (-svc-in), or ALL where you ticked it; no NAT; all traffic logged
  7. A static route for each of their subnets through the tunnel
  8. A blackhole route for each of their subnets at distance 254. This is Fortinet's recommended practice for route-based VPNs: while the tunnel is down, traffic for those subnets is dropped instead of leaving by your default route. You can leave it out.

Click Ask for approval. Nothing is sent to the FortiGate yet.

Approve and apply

Another admin opens the request and clicks Approve. The person who asked can't approve their own request, and neither can an API token. An approval covers the request exactly as the approver saw it: if it changed while they were reading it, the approval is refused and they look again. An approval lasts 24 hours, and so does a request nobody approves. If anyone changes the request, or the connection, it needs a new approval. An approval is also only for the FortiGate the connection pointed at when it was given.

Then an admin clicks Apply to the FortiGate. MedHook:

  1. Reads the FortiGate again. If any of the names now exist, or their subnets now overlap a route the request didn't acknowledge, it stops, changes nothing and names the routes. Edit the request, acknowledge them, and ask for a new approval.
  2. Creates each object in order and shows each step's outcome. After each one it saves what it has created, so the record stays right even if the engine restarts mid-apply.
  3. If a step fails, deletes what it created, in reverse order, and shows each removal. The request says "rolled back" only when every object was removed. Anything it couldn't remove is listed under Still on the FortiGate, with the path to delete, so you can remove it there.

If the FortiGate doesn't answer a change (it times out, or the connection drops after the change was sent), it may have made it anyway. The step shows No answer, may exist. For a named object (an address, group, phase 1 or 2, or service), the rollback deletes it by name; if the FortiGate says it has no such name, it was never created. A policy or route has no name to look it up by, so it's listed under Still on the FortiGate as "may exist": check for it by its comment, which names the healthcare organization.

If the engine stops mid-apply, the FortiGate watcher ends the request once no apply could still be running (each step's time limit and its records, twice over, plus a minute): it's marked failed, its key is deleted, and whatever it had created is listed under Still on the FortiGate. If that apply was only slow and is still running, it notices at its next step that the request was ended, sends no further changes and removes what it had created.

When every step works, the tunnel shows under On your FortiGate like an imported one, with status, alerts and the reachability test.

The pre-shared key

MedHook stores the key encrypted, the same way as the API token, only until the request ends. It's deleted when the request is applied, fails, is rejected or cancelled, or expires, and the audit log records that it was deleted. It's never shown again or written to the audit log.

Who can do what

AdminOperatorViewer
Connect, edit or remove a FortiGateYesNoNo
Import tunnels, rename them, set subnetsYesNoNo
See status, test reachability, diagnoseYesYesNo
Allow actions on a connectionYesNoNo
Bring a tunnel up, where allowedYesYesNo
Allow MedHook to create tunnelsYesNoNo
Ask for, change or cancel a new tunnelYesNoNo
Approve a new tunnel (not your own)YesNoNo
Apply an approved tunnelYesNoNo

Every change is in the audit log: fortigate.connection_created, fortigate.connection_updated, fortigate.connection_actions_changed, fortigate.connection_deleted, fortigate.connection_tested, fortigate.tunnels_imported and fortigate.tunnel_brought_up (whether it worked or not), plus the tunnel's down and up events. New tunnels add fortigate.provision_requested, _changed, _approved, _rejected, _cancelled, _expired, _apply_refused, _apply_started, one fortigate.provision_step per change and one fortigate.provision_rollback_step per removal, _applied or _failed, fortigate.provision_approval_cleared when a connection change sends an approved request back, and fortigate.provision_psk_discarded. None of them holds the key or a request body.

Troubleshooting

You seeDo this
"a private or reserved address"Add the management address to SSRF_ALLOWLIST
"rejected the API token (HTTP 401)"Check the token, and that the engine's address is in the API user's trusted hosts
FortiManager: "no permission"Give the FortiManager user JSON API access and the ADOM that holds the FortiGates
FortiManager: "couldn't reach"FortiManager has lost its link to that FortiGate; check it in Device Manager
"HTTP 403"The admin profile can't read VPN; set VPN to Read
"refused to bring … up (HTTP 403)"The profile is read only for VPN; set VPN to Read/Write, or turn Allow actions off
"Actions are off"An admin turns on Allow actions on the connection
"needs read-write permission"Creating tunnels needs Read/Write for VPN, firewall address and policy, and router; see above
"The FortiGate already has …"Pick another tunnel name, or remove those objects on the FortiGate first
"overlaps existing route"Check that route on the FortiGate. If the tunnel should still carry that subnet, tick I've checked each of these
"Not acknowledged: …" when applyingThe FortiGate's routes changed since the request: edit it, acknowledge those routes, and ask for a new approval
"made before MedHook split the policies' ports"Edit the request, add their MLLP port, and ask for a new approval
"No answer, may exist"Check the FortiGate for that object; the rollback removed it if it had a name
"Sign in to request a change"Tunnel requests need a named person: sign in to MedHook instead of using an API token
"The request changed after you opened it"Open the request again, review what it says now, and approve again
"Still on the FortiGate"Delete the listed objects on the FortiGate (the path is shown), then ask again
"read for tunnel requests 20 times"Wait a minute: previews, requests and changes read the FortiGate, at most 20 times a minute per connection
"is a FortiManager connection"Act on the FortiGate itself, or connect that FortiGate directly and allow actions or tunnel creation there
"From the tunnel's live selectors" on a phase 2MedHook couldn't use the phase 2's address object. To use it, give the profile read on addresses (step 1)
"presented a different certificate"The FortiGate's certificate changed. Check the new fingerprint, then trust it again from Edit
The reachability test times outCheck the route in the engine's network and the FortiGate's firewall policy for the organization's subnet

Reference

  • Not yet: changing or removing a tunnel MedHook created, and any change through FortiManager (a FortiManager connection only reads).
  • Site-to-site tunnels covers the tunnels MedHook creates itself.