# Device enrollment

> A device joins your realm once, with a one-time token, and gets a certificate from the realm's own authority for a key that never leaves it.

Enrollment is how a laptop or server becomes one of your realm's devices. You make a one-time token; the device
spends it. The device comes away with its own key, a certificate naming it within your realm, and its security
sensor registered with the realm's security log. It needs nothing else to connect from then on.

You enroll each device once, and again before its certificate's year is up. A lost or replaced device is revoked or
removed, and a new one enrolled with a new token; see [Device revocation](../../revoke-and-remove/revocation/) and
[Device removal](../../revoke-and-remove/removal/).

## Before you begin

- **Privilege:** making a token needs `MODIFY` on the realm's endpoint settings, which `RealmOwner` holds, in the
  realm or a realm above it ([Who can call](../../api/#who-can-call)).
- **Where:** tokens are made through the API; the device is enrolled on the device itself, by someone with
  administrator rights on it.
- **What you need:** the package for the device's platform and the device address (a host and port), both from
  whoever runs your deployment; your realm's sign-in address, `https://foobar.cvyuh.com/oauth2`; and outbound access
  from the device to the device address ([Network](../device-client/#network)).

## How it works

1. **You make a token.** A realm administrator asks for an enrollment token. The token is signed by your realm,
   lasts as long as you asked, and lets the device check it is talking to your realm on its first call.
2. **The device makes its key.** The device client makes a key pair, keeps the private key on the device, and
   saves it before it calls, so an interrupted enrollment retries over the same key instead of making a second one.
3. **The device spends the token.** It sends the token and a certificate request (RFC 2986) for its key. The realm
   checks the token, checks the realm still has room under its [device limits](../device-limits/), registers the
   device's security sensor, records the device, and has the realm's certificate authority sign the request.
4. **The device gets its grant.** It receives its certificate and the authority's chain, and its sensor's
   registration. From then on it opens one connection to the realm, authenticated by that certificate on both ends
   (mutual TLS, RFC 8446).

Each realm has its own certificate authority; its signing key never leaves the realm's key store. A device's
certificate names the device and your realm and lasts 365 days.

## Set it up

1. Make a token, as a realm administrator ([Call the API](../../../develop/api/) shows how to get
   `$CVYUH_SESSION`):

   ```sh
   curl https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1/enrolment-tokens \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{"ttlHours": 4}'
   ```

   The realm answers `201` with `token` and `expiresAt`. Leave out `ttlHours` for the realm's default lifetime. The
   address ends in `enrolment-tokens`, spelled that way.

2. On the device, install the package, then run the device client's `enroll` command with the token, the device
   address and the sign-in address:

   ```sh
   sudo /var/cvyuh/bin/cvyuh-uem enroll \
     --server <device address> --token <enrollment token> \
     --issuer https://foobar.cvyuh.com/oauth2
   ```

   The command is named for your deployment: `cvyuh-uem` in these examples. It's in the `bin` folder of the
   install folder, `/var/cvyuh` on Linux and `/Library/Cvyuh` on macOS.

3. Start the device client if it isn't running ([Device client](../device-client/#set-it-up)).

[Get started with Endpoint Management](../../get-started/) walks through it end to end, and
[Enroll a device at install](../../guides/enroll-at-install/) has the installer do it.

## What the person enrolling sees

On success, `enroll` prints the name the device was enrolled as and the date to enroll it again before:

```
enrolled maya-laptop as <certificate name> against <device address>; enrol again before <RFC 3339 time>
```

On a refusal it prints `enrol failed: enrol refused,` then the status and the realm's reason, and exits with an error:

```
enrol failed: enrol refused, 409 Conflict: {"code":"DISABLED","message":"the realm enrols its most devices, 100","detail":null}
```

## If enrollment fails

**A refused enrollment doesn't spend the token.** Fix the cause and run `enroll` again with the same token while it
lasts; a refusal at the device cap or a failure on the realm's side hands the token back too.

| Status | The reason says | What it means | What to do |
|---|---|---|---|
| `401` | `the token is already spent` | Another device, or this one with a new key, already enrolled with it | Make a new token |
| `401` | `the token has expired` | Its lifetime ran out before it was spent | Make a new token, with a lifetime long enough to install |
| `401` | `the token is malformed` | It was cut short or changed on the way | Copy it again, whole |
| `401` | `the token names another realm`, `is unknown`, `pins another authority`, or `names no realm served here` | The token isn't one this realm made, or the device address is another deployment's | Check the device address, then make a new token in the right realm |
| `400` | `` `<name>` is not an endpoint name `` | The name breaks the [naming rule](#limits-and-defaults) | Pass `--name` with a name that keeps to it |
| `409` | `the realm enrols its most devices, <cap>` | The realm is at its [device cap](../device-limits/) | Remove a device you no longer need, or ask a root realm administrator to raise the cap |
| `409` | That the name is already held | Another device enrolled under the same name, often this device's own earlier enrollment | Enroll under another name, or [remove](../../revoke-and-remove/removal/) the old record first |
| `503` | `this enrolment is in flight; ask again` | The same device's first try is still being made | Run `enroll` again in a moment |

## Limits and defaults

| What | Default | Range | Set by |
|---|---|---|---|
| Token lifetime | 24 hours | 1 hour to the realm's setting, at most 168 | [Device limits](../device-limits/) |
| Devices a token enrolls | One | Fixed | |
| Devices in the realm | 100 | 1 and up | A root realm administrator ([Device limits](../device-limits/)) |
| Certificate lifetime | 365 days | Fixed | |
| Device name | The host name | 1 to 64 letters, digits, `-`, `_` and `.` | `--name` |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `enroll` prints its usage and stops | The device address, token or sign-in address is missing | Pass `--server`, `--token` and `--issuer`, or set their [variables](#enroll-options) |
| `enrol failed:` with a connection error, before any status | The device can't reach the device address, or a proxy or a TLS-inspecting firewall stands in the way | Check the address and port, and open a direct outbound path ([Network](../device-client/#network)) |
| `the issuer ... is not an https URL` | The sign-in address is mistyped | Use `https://foobar.cvyuh.com/oauth2` |
| `already spent` on a token no one here has used | Someone else enrolled a machine with it | Treat it as leaked: list the realm's devices, [revoke](../../revoke-and-remove/revocation/) or remove the one you don't expect, and make a new token |
| The device enrolled but never shows `connected` | The device client isn't running, or can't reach the device address | See the device client's [Troubleshooting](../device-client/#troubleshooting) |

## Audit record

Making a token writes nothing to the realm's audit trail: keep your own note of whom you gave each token to. The
device's record being made is recorded on the trail as a change (`CREATED`); see
[What is recorded](../../../audit-reports/audit-trail/what-is-recorded/).

## Good to know

- **One use.** A token enrolls one device. A token that has been spent, or has expired, enrolls nothing. A retry by the
  same device, over the same key, gets the same grant back.
- **One realm.** A token enrolls a device only into the realm it was made for.
- **A token can't be withdrawn.** Once made, it lasts until it's spent or expires. If one leaks, lower the realm's
  token lifetime, watch the realm's devices for one you don't expect, and remove it.
- **Made on your session.** A token is made on the administrator's own session, never on a service's credentials.
- **The private key never leaves the device.** The realm sees only the certificate request.
- **The sensor needs no secret of its own.** The realm registers the sensor and keeps the key its messages are
  sealed under; see [Sensor registration](../../../detection-response/sensors/registration/).
- **Linux and macOS.** Packages exist for Linux (deb and rpm, on amd64 and arm64) and macOS; Windows is
  [planned](../whats-next/#windows-devices).
- **A year, then enroll again.** Renewal is [planned](../whats-next/#certificate-renewal). Until then, enroll the
  device again before the date `enroll` printed. Enrolling again makes a **new** record; the old one stays, and
  counts toward the device cap, until you remove it. The date is printed by `enroll`; the realm doesn't show it yet.
  See [Enroll a device again](../../guides/enroll-a-device-again/).

## Reference

### `enroll` options

| Option | Environment variable | What it is |
|---|---|---|
| `--server` | `CVYUH_SERVER` | The device address, a host and port |
| `--token` | `CVYUH_TOKEN` | The enrollment token |
| `--issuer` | `CVYUH_ISSUER` | Your realm's sign-in address, `https://foobar.cvyuh.com/oauth2` |
| `--issuer-ca` | `CVYUH_ISSUER_CA` | A file holding the certificate authority the sign-in address's certificate chains to, when it isn't a public one |
| `--name` | `CVYUH_NAME`, at install only | The device's name; the host name by default |

The variables are named for your deployment too: its name in capitals, then `_SERVER` and the rest.

## Related

- [Get started with Endpoint Management](../../get-started/): enroll a device, start to finish.
- [Device limits](../device-limits/): how many devices, and how long a token may last.
- [Device client](../device-client/): what runs on the device once it's enrolled.
- [Enroll a device again](../../guides/enroll-a-device-again/): before its certificate's year is up.
