# Services and access between projects

Source: https://tiltas.openpanel.sh/docs/partner/services-and-access

> Give a program on a server a private name and address, and choose which projects may reach it.

A Service publishes one program running on one server to the rest of the Network, under its own name. One service
with a private address in your product is one Tiltas Service.

## What a partner Network lets through

- **Device to Device is open.** Every Device reaches every other Device's own address on every port, with approved
  subnet routes and exit nodes, as in one office network.
- **Each Service admits only its own `allowed`.** The Coordinator's policy and the engine on the hosting server both
  check it. Nobody else reaches the Service's address, whatever port they try.

So put anything that should be private to some projects behind a Service, not on a server's own address.

## Create a Service

```sh
curl -sS -X POST "$TILTAS/accounts/acc_x4tq9mzr2e/services" -H "$AUTH" \
  -H 'Content-Type: application/json' -d '{
    "name": "analytics-db",
    "kind": "tcp",
    "device_id": "dev_n3w8kzq5cb",
    "target": "127.0.0.1:15432",
    "ports": [5432],
    "allowed": { "groups": ["grp_c9r4mwxt2s"] }
  }'
```

| Field | Rule |
|---|---|
| `name` | A DNS label, unique among the Network's Devices and Services. Two projects with a `postgres` need different names, for example `<service>-<project>`. |
| `kind` | `tcp` (any TCP protocol, its own address from `100.96.0.0/12`) or `web` (HTTP by name, on port 80 of the server). |
| `device_id` | The server that runs the program. |
| `target` | Where the program listens, as seen from that server. Use loopback, such as `127.0.0.1:15432`; never the server's own `100.64.x.y` address. |
| `ports` | `tcp`: the ports other Devices use; each forwards to the target's port. |
| `allowed` | Who may reach it. **Left out, nobody can.** |

The answer has the Service's `address` (`tcp`) and `url`. Other projects reach it as `analytics-db.<domain>:5432`, or
by the address. The change is live when the call returns.

## Who may reach it: `allowed`

| You want | Send |
|---|---|
| Every Device of the Network | `{ "everyone": true }` |
| Some Devices (servers, computers, phones) | `{ "devices": ["dev_n3w8kzq5cb", "dev_k7p2mxq9tz"] }` |
| People, with all their Devices | `{ "members": ["mem_h2v7tpqx4d"] }` |
| Projects (tags) | `{ "groups": ["grp_c9r4mwxt2s"] }` |

The fields add up. Ids must exist in the Network, or the answer is `422 invalid_rule`. Change it with
`PATCH …/services/{service_id} { "allowed": { … } }`, a full replacement.

## Projects as tags: Groups

A Group is a project tag. It holds servers and people (with all their Devices).

```sh
# one Group per project
curl -sS -X POST "$TILTAS/accounts/acc_x4tq9mzr2e/groups" -H "$AUTH" \
  -H 'Content-Type: application/json' -d '{"name":"proj-analytics","devices":["dev_n3w8kzq5cb"]}'

# tag a server as it joins: its Join key carries the Group
curl -sS -X POST "$TILTAS/accounts/acc_x4tq9mzr2e/join-keys" -H "$AUTH" \
  -H 'Content-Type: application/json' -d '{"name":"web-2","groups":["grp_c9r4mwxt2s"]}'

# retag one server (a full replacement of its Groups)
curl -sS -X PATCH "$TILTAS/accounts/acc_x4tq9mzr2e/devices/dev_k7p2mxq9tz" -H "$AUTH" \
  -H 'Content-Type: application/json' -d '{"groups":["grp_c9r4mwxt2s"]}'
```

A change to a Group applies to every Service that names it, at once. `PATCH …/groups/{group_id}` replaces the whole
Group: send `name`, `members` and `devices` each time. A Group a Service uses cannot be deleted (`409 conflict`) until
the Service stops naming it. Group names are 1–40 characters of `a–z 0–9 -`.

## What Tiltas can tell apart

Tiltas knows who is calling by the calling Device's address. Every container on one server shares that server's
address, so Tiltas tells server B from server C, but not project X from project Y on the same server. Between projects
that share a server, separate them on the server itself (per-project Docker networks or a firewall).

## Change, move or remove

- `PATCH …/services/{service_id}` changes `name`, `target`, `ports`, `identity_headers` or `allowed`. A TCP Service
  keeps its address through renames.
- `kind` and `device_id` never change. To move a Service to another server, delete it and create it there with the
  same name; a TCP Service then gets a new address.
- `DELETE …/services/{service_id}` removes it; its address is held 24 hours before reuse.

With a [webhook](/docs/partner/webhooks), you receive `service.health` whenever a Service's health check flips.
