# SSL and TLS

Read and change the SSL mode of a domain, whether HTTPS is forced, its TLS profile and its HSTS header.

This part of the API controls how a domain speaks HTTPS: the SSL mode, the redirect from HTTP to HTTPS, the lowest TLS version it accepts and the HSTS header. The paths start with `/v2/cdn/ssl/{domain_public_id}/`, where the id is the public id of the domain, taken from [the domains list](https://docs.diamwall.com/products/domains/#list-your-domains).

- [SSL mode](#ssl-mode-and-forced-https): Choose the SSL mode and send HTTP on to HTTPS.
- [TLS profile](#tls-profile): Set the lowest version of TLS the domain accepts.
- [HSTS](#hsts): Switch the HSTS header on and decide what it says.

The examples on this page use these shell variables:

```bash
export DIAMWALL_API_KEY="your key"
export SSL="https://api.diamwall.com/v2/cdn/ssl/your-domain-public-id"
```

## How the three groups work

The settings are split into three groups. Each one has a read and an edit of its own, at the same path:

| Group | Path | Fields you can change |
| --- | --- | --- |
| `ssl` | `settings/ssl/` | `mode`, `force_https` |
| `tls` | `settings/tls/` | `profile` |
| `hsts` | `settings/hsts/` | `enabled`, `max_age_seconds`, `include_subdomains`, `preload` |

An edit of one group never touches another. Changing HSTS or the SSL mode does not move the TLS profile.

### Reading a group

Every read answers in the same form. Here is the `ssl` group:

```bash
curl "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:4dbac6895cfc...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "mode": { "state": "resolved", "value": "full" },
    "force_https": { "state": "resolved", "value": true },
    "origin_ports_consistent": { "state": "resolved", "value": true }
  }
}
```

- Each field comes back as `{ "state": "resolved", "value": ... }`. A field with a `state` of `unknown` has no `value`. Treat it as unavailable, never as `false`, `0` or `off`, and do not write over it on a guess.
- `consistency` is `verified`, `degraded` or `unknown`.
- `publication.status` is `verified` when the CDN verified what it published, and `unknown` otherwise. `publication.rollout` is always `unknown`.
- The query string is not read.

> **When the whole group is unknown**
> If the CDN could not read the group, the answer has `"state": "unknown"`, a `revision` of `null`, no `data` and no `ETag` header. There is nothing to send in `If-Match`, so do not try to edit the group until a read answers with data.

### Edits need the latest revision

Every edit is a `PATCH` that needs the `ETag` header of a read of the same group, sent back unchanged in `If-Match`:

```bash
ETAG=$(curl -s -o /dev/null -D - "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')
```

- Without `If-Match` the edit answers `428 PRECONDITION_REQUIRED`.
- If the group changed since your read, the edit answers `412 REVISION_MISMATCH`. Read again and retry.
- A successful edit answers `200` with the group as it is now, in the same form as a read, and a new `ETag` header for your next edit.
- An `ETag` belongs to its group. Read `settings/tls/` before you edit `settings/tls/`.

Send only the fields that change. A field you leave out keeps its value. A boolean you leave out is never read as `false`.

## SSL mode and forced HTTPS

### Read the SSL settings

[`GET /v2/cdn/ssl/{domain_public_id}/settings/ssl/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/GET/v2/cdn/ssl/{domain_public_id}/settings/ssl/)

```bash
curl "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

| Field | Meaning |
| --- | --- |
| `mode` | The SSL mode of the domain. See the table below. |
| `force_https` | Whether a request over HTTP is sent on to HTTPS. |
| `origin_ports_consistent` | Read-only. `false` means the origin ports of the domain are inconsistent and deserve a warning. `true` means no warning. It cannot be written. |

| `mode` | Meaning |
| --- | --- |
| `off` | No HTTPS |
| `flexible` | HTTPS between the visitor and the network |
| `full` | HTTPS to the origin too |
| `other` | A mode this API does not know. Only seen on a read. Show it as it is, never as `off`. |

### Change the SSL settings

[`PATCH /v2/cdn/ssl/{domain_public_id}/settings/ssl/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/PATCH/v2/cdn/ssl/{domain_public_id}/settings/ssl/)

Send `mode`, `force_https` or both. `mode` is `off`, `flexible` or `full`, in lower case. `force_https` is a JSON boolean.

**Set the mode**

```bash
curl -X PATCH "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "mode": "full" }'
```

**Force HTTPS**

```bash
curl -X PATCH "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "force_https": true }'
```

**Both**

```bash
curl -X PATCH "$SSL/settings/ssl/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "mode": "flexible", "force_https": false }'
```

> **No forced HTTPS with the mode off**
> `"mode": "off"` with `"force_https": true` is refused with `400 INVALID_REQUEST`. This also holds when you send only one of the two: the edit is checked against the value the domain already has for the other.

## TLS profile

The TLS profile is the lowest version of TLS the domain accepts.

### Read the TLS profile

[`GET /v2/cdn/ssl/{domain_public_id}/settings/tls/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/GET/v2/cdn/ssl/{domain_public_id}/settings/tls/)

```bash
curl "$SSL/settings/tls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:0d74f618848c...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "profile": { "state": "resolved", "value": "1.3" }
  }
}
```

| `profile` | Can you select it? |
| --- | --- |
| `1.3` | Yes |
| `1.2` | Yes |
| `1.1`, `1.0` | No. A domain that is already on one is shown on it. |
| `legacy` | No. Any other profile this API does not know. |

Show an older profile as what it is, never as `1.2`.

### Select a TLS profile

[`PATCH /v2/cdn/ssl/{domain_public_id}/settings/tls/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/PATCH/v2/cdn/ssl/{domain_public_id}/settings/tls/)

The body is exactly `profile`, the string `"1.2"` or `"1.3"`. The number `1.3` is refused.

```bash
TLS_ETAG=$(curl -s -o /dev/null -D - "$SSL/settings/tls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')

curl -X PATCH "$SSL/settings/tls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $TLS_ETAG" \
  -d '{ "profile": "1.3" }'
```

> **There is no way back to 1.0 or 1.1**
> A domain on `1.0`, `1.1` or `legacy` can only move to `1.2` or `1.3`. Asking for `1.0` or `1.1` answers `400 INVALID_REQUEST` with `profile` in `fields`, so once a domain leaves an older profile this API cannot put it back.

## HSTS

With HSTS on, the domain answers with a header that tells browsers to use HTTPS for it.

### Read the HSTS settings

[`GET /v2/cdn/ssl/{domain_public_id}/settings/hsts/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/GET/v2/cdn/ssl/{domain_public_id}/settings/hsts/)

```bash
curl "$SSL/settings/hsts/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:48615bfb7850...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "enabled": { "state": "resolved", "value": true },
    "max_age_seconds": { "state": "resolved", "value": 31536000 },
    "include_subdomains": { "state": "resolved", "value": true },
    "preload": { "state": "resolved", "value": false }
  }
}
```

| Field | Meaning | An edit takes |
| --- | --- | --- |
| `enabled` | Whether the domain answers with the HSTS header | A JSON boolean |
| `max_age_seconds` | How long a browser remembers to use HTTPS for the domain, in seconds | A whole number from `0` to `63072000` (two years) |
| `include_subdomains` | Whether the header covers the subdomains too | A JSON boolean |
| `preload` | Whether the header asks browsers to preload the domain | A JSON boolean |

A read shows `max_age_seconds` as it is stored, so it can show a value above the range an edit takes.

### Change the HSTS settings

[`PATCH /v2/cdn/ssl/{domain_public_id}/settings/hsts/` in the API reference](https://docs.diamwall.com/api/#tag/ssl/PATCH/v2/cdn/ssl/{domain_public_id}/settings/hsts/)

Read `settings/hsts/` to get its `ETag`, then send at least one of the four fields:

**Switch on**

```bash
curl -X PATCH "$SSL/settings/hsts/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $HSTS_ETAG" \
  -d '{ "enabled": true, "max_age_seconds": 31536000 }'
```

**Preload**

```bash
curl -X PATCH "$SSL/settings/hsts/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $HSTS_ETAG" \
  -d '{
    "max_age_seconds": 63072000,
    "include_subdomains": true,
    "preload": true
  }'
```

**Switch off**

```bash
curl -X PATCH "$SSL/settings/hsts/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $HSTS_ETAG" \
  -d '{ "enabled": false }'
```

Some fields depend on each other. These combinations are refused with `400 INVALID_REQUEST`:

| Refused | Why |
| --- | --- |
| `enabled: true` with `max_age_seconds: 0` | HSTS that is on needs an age above 0. |
| `preload: true` with `include_subdomains: false` | Preload needs the subdomains included. |
| `preload: true` with a `max_age_seconds` under `31536000` | Preload needs an age of at least one year. |

The rules apply to the settings as they would be after the edit. If you send only `preload`, it is checked against the age and the subdomain setting the domain already has.

## Rules for every edit

- **Strict values.** Nothing is converted. `"true"`, `1` and `null` are not booleans, and a field the group does not have is refused. A refused field is named in `fields`, except a field the API does not know.
- **No body or an empty one.** A request with no body answers `400 INVALID_REQUEST`. On `ssl` and `hsts`, a body of `{}` answers `400 NO_CHANGES`.
- **Refused before anything is written.** A `400` or a `428` writes nothing.

> **When the outcome is not confirmed**
> An edit that answers `FILE_SERVER_V2_ERROR`, `502 FILE_SERVER_OUTCOME_UNKNOWN` or `500` may or may not have been written, in whole or in part. Do not send it again as it is. Read the group again, compare it with what you meant, and only then decide whether to send the change, with the `ETag` of that new read. `FILE_SERVER_V2_ERROR` comes with a `request_id`: quote it to support.

## Limits and permissions

- **Credits.** Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. Every call on this page costs 8 credits.
- **What costs nothing.** An edit refused with `400` or `428` before it reaches the CDN spends none of the domain's credits. A read answered from the API's short-lived copy spends none either, but still counts toward the user limit.
- **Out of credits.** The call answers `429 TOO_MANY_REQUESTS`, with the wait in `details.retry_after_seconds` and in the `Retry-After` header.
- **Permissions.** Reads need `ssl.view` on the domain. Edits need `ssl.edit`. Being able to read the settings does not let you change them.

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field is wrong, two fields do not go together, or the body is missing. The answer names the fields in `fields` when it can. |
| `400` | `NO_CHANGES` | An edit of `ssl` or `hsts` was sent with an empty body. |
| `400` | `INVALID_PRECONDITION` | The `If-Match` header is not valid. Send the `ETag` back exactly as it came. |
| `409` | `DOMAIN_WITHOUT_KEY` | The domain is not active on the network yet. |
| `409` | `DOMAIN_BUSY` | Something else is changing the domain right now. Try again shortly. |
| `409` | `DOMAIN_REPLACEMENT_PENDING` | The domain is being renamed. Nothing was written. |
| `412` | `REVISION_MISMATCH` | The group changed since your read. `details.current_revision` has the current one. Read again and retry. |
| `415` | `UNSUPPORTED_MEDIA_TYPE` | The body is not JSON. Nothing was written. |
| `428` | `PRECONDITION_REQUIRED` | The `If-Match` header is missing. |
| `429` | `TOO_MANY_REQUESTS` | The domain is out of credits for now. |
| `502` | `FILE_SERVER_NO_ANSWER` | The CDN gave no usable answer. A read changed nothing and can be sent again. |
| `502` | `FILE_SERVER_OUTCOME_UNKNOWN` | The edit may have been written. Read the group before you repeat it. |
| `503` | `STATE_CHANGING` | The state of the domain is changing. An edit was refused. Read again before you send the change again. |

The errors that come from the key itself, such as `API_KEY_IP_NOT_ALLOWED`, are on the [Errors](https://docs.diamwall.com/reference/errors/) page.

## All SSL and TLS endpoints

Each row opens the endpoint in the API reference, with every parameter, rule and response.

See the [API reference](https://docs.diamwall.com/api/#tag/ssl) for every endpoint, with parameters and responses.
