# Domain and TLS

Give Platforma a host name and a certificate, so that users connect with TLS.

Users reach Platforma by a host name, and TLS protects their sessions and data on the way. You choose the host name, where TLS ends, and where the certificate comes from:

- [TLS at the entry point](/configuration-domain-and-tls/#tls-at-the-entry-point): Kubernetes (A system that runs programs on a group of machines (a cluster). Platforma can run in a Kubernetes cluster.) cluster, the default.
- [TLS on the Backend](/configuration-domain-and-tls/#tls-on-the-backend): Single Server mode (The mode in which the Backend and all its jobs run on one Linux machine, the server, with no Kubernetes cluster.), or inside the cluster.
- [Self-signed certificate](/configuration-domain-and-tls/#self-signed-certificate): Single Server mode, tests only.
- Without TLS: Single Server mode in a trusted network only. See [TLS on the Backend](/configuration-domain-and-tls/#tls-on-the-backend).

In a Kubernetes cluster, TLS between the clients (the Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.) and pl-cli (The command-line client of Platforma. Administrators use it to manage users and projects on the Backend.)) and the entry point (The way in to the Backend from outside the cluster, a load balancer or an Ingress with a DNS name. It usually ends TLS and sends the traffic to the Backend pod.) is required. For what Platforma expects from the entry point, the certificate and the DNS records, see [Entry point and TLS](/infrastructure-contract-entry-point/). For which connections use TLS, see [Encryption in transit](/security-data-flows/#encryption-in-transit). For the steps that users do to connect, see [Connect clients](/install-connect-clients/).

For an installation made with an installer, see [Install in cloud with installer](/install-cloud-installer/).

## Host name and DNS

Choose the host name before the installation: the certificate and the DNS record are made for it, and users type it in the Desktop App.

#### Variant: Kubernetes cluster

Each entry point serves one host. One installation can have several entry points, for example a public host and an internal host: all of them lead to the same Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.).

**With the cloud-specific Terraform modules (The Terraform modules platforma-tf-cloud-base and platforma-tf-cloud-cluster. There is one pair for each cloud, AWS and Google Cloud. The roots cloud and cluster apply them. They make the network, the Kubernetes cluster, the storage, the cloud identities and the registry caches. They give the hand-off file cluster_config to the cloud-agnostic Terraform modules.)**, give the hosts to `platforma-tf-cloud-base`. The cluster module then makes one entry point on the Gateway for each host, so you do not write the entry points yourself:

| Input of `platforma-tf-cloud-base` | AWS                                                                                                                                                           | Google Cloud                                                                                               |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `hosts`                            | The host names. `*` matches exactly one label                                                                                                                 | The same. At least one host                                                                                |
| `dns`                              | `zone_id` of an existing Route 53 zone, or `create_zone_name` for a new zone. The records of all hosts go into this zone. Without `dns`, you make the records | `managed_zone` and `project_id` of a Cloud DNS zone. The module writes the A records of all hosts into it  |
| `gateway.scheme`                   | `external` (default) or `internal`                                                                                                                            | The same                                                                                                   |
| `gateway.allowed_cidrs`            | The networks of the users that can reach the entry point, as the rules of the security group of the load balancer. Default `["0.0.0.0/0"]`: all addresses     | The same networks, as a Cloud Armor security policy. Default `["0.0.0.0/0"]`: all addresses, and no policy |

**Example:**

*Inputs of platforma-tf-cloud-base (AWS)*

```hcl
hosts   = ["platforma.example.com"]
dns     = { zone_id = "Z0123456789ABCDEFGHIJ" }
tls     = { mode = "managed" } # the default
gateway = { allowed_cidrs = ["203.0.113.0/24"] }
```

`gateway.allowed_cidrs` limits the networks that can reach the entry point. The default `["0.0.0.0/0"]` opens the entry point to all addresses, so users connect from any network. Each request still needs a session of a user. To let only the networks of the users reach it, set them in this input.

On Google Cloud, a list without `0.0.0.0/0` makes a Cloud Armor security policy: a global policy for `gateway.scheme = "external"`, a regional policy for `"internal"`. The policy has one allow rule for each 10 networks of the list. A default rule refuses all other addresses with HTTP 403. The policy costs about 7 USD each month and a fee for each request (see [What you pay all the time](/plan-sizing-and-cost/#what-you-pay-all-the-time)). Google Cloud starts to apply a new list about 10 minutes after the apply: wait before you test it.

`kubernetes.api_allowed_cidrs` sets the networks that can reach the Kubernetes API of the cluster. On AWS, it limits the public endpoint of EKS; on Google Cloud, it sets the authorized networks of GKE. It does not have a default: the plan stops until you set it. We recommend the networks of the computers that run Terraform and `kubectl` only. `["0.0.0.0/0"]` opens the Kubernetes API to the whole internet.

With `gateway.scheme = "internal"`, the load balancer has private addresses only, and users reach it through a VPN or another private connection to the network of the cluster. On Google Cloud, the module writes the private address into the zone, and the computers of users must resolve the name through the DNS of their own network.

**With the Helm chart, or with `platforma-tf-services` and an `infrastructure` value that you write**, list the entry points. Each item has a `host` and one way in: `gateway` (a route to a Gateway of the cluster) or `ingress`. The DNS records belong to the infrastructure: make them before users connect.

*values.yaml*

```yaml
network:
  entryPoints:
    - host: platforma.example.com
      gateway: { implementation: aws-load-balancer-controller }
```

*platforma-tf-services*

```hcl
infrastructure = {
  # ... other keys of infrastructure
  network = {
    hosts        = ["platforma.example.com"]
    entry_points = [
      { host = "platforma.example.com", gateway = { implementation = "generic" } },
    ]
  }
}
```

In Terraform, `network.hosts` lists the hosts that have a DNS record and a certificate. The host of each entry point must be in this list, or match a wildcard in it: else the plan stops. The route goes to the Gateway `platforma-gateway` in the namespace `platforma-gateway`, listener `platforma`, unless you set `name`, `namespace` and `sectionName` (`section_name`) of `gateway`. For the implementations of `gateway` and the Ingress controllers that Platforma supports, see [Entry point and TLS](/infrastructure-contract-entry-point/).

#### Variant: Single Server

The clients connect to the server directly, by its DNS name or IP address. When you use a certificate, make a DNS record for the server: the certificate must be valid for the name that users give to the client.

By default, the Backend listens on `127.0.0.1` only, so only programs on the server can connect. To accept connections from the computers of users, set the listen address:

| Option                     | Environment variable        | Default                                                              | What it sets                                                                                                                                                                             |
| -------------------------- | --------------------------- | -------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--listen-address`         | `PL_LISTEN_ADDRESS`         | `127.0.0.1`                                                          | The address on which the Backend listens, without a port. `0.0.0.0` means all addresses of the server                                                                                    |
| `--listen-port`            | `PL_LISTEN_PORT`            | `6345`                                                               | The gRPC port                                                                                                                                                                            |
| `--listen-http-port`       | `PL_LISTEN_HTTP_PORT`       | `6347`                                                               | The HTTP port: the data address, where the Desktop App uploads and downloads the files of a folder storage                                                                               |
| `--primary-storage-fs-url` | `PL_PRIMARY_STORAGE_FS_URL` | The listen address and the HTTP port, with `https://` when TLS is on | The data address that the Backend gives to the Desktop App. With the listen address `0.0.0.0`, set it to the host name that users type, for example `https://platforma.example.com:6347` |

For what goes over each port and which ports the firewall of the server must open, see [Open ports](/security-overview/#open-ports).

## TLS at the entry point

In a Kubernetes cluster, the entry point terminates TLS: the certificate is on the load balancer or on the Ingress controller, and the Backend does not read it. Thus a renewal of this certificate does not stop the Backend. The certificate must be valid for each host, and the computers of users must trust it (see [What the Desktop App trusts](/configuration-domain-and-tls/#what-clients-trust)).

### AWS

Set `tls.mode` in `platforma-tf-cloud-base`:

- **`managed`** (default): the module makes one ACM certificate for all hosts and validates it in the DNS zone of `dns`, so it needs `hosts` and `dns`. The first host is the domain name of the certificate, the other hosts are its alternative names. ACM renews it.
- **`existing`**: ACM certificates that exist, by ARN in `tls.certificates`. The first one is the default certificate of the load balancer. The load balancer selects one of the others by the host name (SNI). Make sure that the certificates cover all hosts. ACM renews the certificates that it issued; replace an imported certificate before it expires.
- **`none`**: no certificate, the entry point is plain HTTP. The Desktop App needs TLS: use this mode for tests only.

*Inputs of platforma-tf-cloud-base (AWS)*

```hcl
tls = {
  mode = "existing"
  certificates = [
    "arn:aws:acm:eu-central-1:123456789012:certificate/<id>",
    "arn:aws:acm:eu-central-1:123456789012:certificate/<id-2>",
  ]
}
```

### Google Cloud

Set `tls.mode` in `platforma-tf-cloud-base`:

- **`managed`** (default): a certificate that Google manages, with a DNS authorization in the zone of `dns`, so it needs `hosts` and `dns.managed_zone`. Google renews it. The zone must be public: the certificate authority checks the DNS authorization record over public DNS. With a private zone, use `existing`. With `gateway.scheme = "internal"`, the certificate is regional.
- **`existing`**: certificates that you make in Google Cloud before the apply, by name in `tls.certificates`. You renew them. With `gateway.scheme = "internal"`, they must be regional self-managed SSL certificates in the region of the installation.
- **`none`**: no certificate, the entry point is plain HTTP. Use this mode for tests only.

*Inputs of platforma-tf-cloud-base (Google Cloud)*

```hcl
tls = {
  mode         = "existing"
  certificates = ["platforma-example-com"]
}
```

### Own cluster

- **Gateway**: the listener of the Gateway holds the certificate. The Gateway is a part of the infrastructure, so set the certificate there, for example with cert-manager.
- **Ingress**: give the name of a Secret of type `kubernetes.io/tls` in the namespace of the instance. The Ingress controller reads it. A tool such as cert-manager can make and renew this Secret: Platforma does not make it.

*values.yaml*

```yaml
network:
  entryPoints:
    - host: platforma.example.com
      ingress:
        className: <ingress-class>
        tlsSecret: { name: platforma-entry-tls }
```

With the Terraform modules (The Terraform code that MiLaboratories provides to install Platforma. The cloud-specific Terraform modules make the cluster and its cloud parts in AWS or Google Cloud; the cloud-agnostic Terraform modules install Platforma into the cluster.), set the same keys in `cloud_values.network` of `cluster_config` (`platforma-tf-services-cluster`), or in `infrastructure.network` of `platforma-tf-services` when you write the input `infrastructure` by hand:

*cluster_config (part)*

```hcl
cloud_values = {
  # ... other keys of cloud_values
  network = {
    hosts = ["platforma.example.com"]
    entry_points = [
      {
        host    = "platforma.example.com"
        ingress = { class_name = "<ingress-class>", tls_secret = { name = "platforma-entry-tls" } }
      },
    ]
  }
}
```

## TLS on the Backend

#### Variant: Kubernetes cluster

By default, the entry point sends the traffic to the Backend pod as plain HTTP/2 inside the cluster. Turn on TLS on the Backend when the entry point requires TLS to the pod, or when a policy of the company requires encryption inside the cluster too. Then all ports of the Backend pod serve TLS only: gRPC (6345), metrics (9090) and, when it is on, the debug API (9091). The probes of the pod switch to HTTPS.

| Helm value                | Terraform input of `platforma-tf-services` | Default                 |
| ------------------------- | ------------------------------------------ | ----------------------- |
| `network.tls.enabled`     | `backend_service.network.tls.enabled`      | `false`                 |
| `network.tls.secret.name` | `backend_service.network.tls.secret.name`  | `platforma-backend-tls` |

1. Make the Secret with the certificate and its key, in PEM format, in the namespace of the instance:

   ```sh
   kubectl -n platforma create secret tls platforma-backend-tls \
     --cert=tls.crt --key=tls.key
   ```

2. Turn on TLS on the Backend and apply. The Backend pod starts again with TLS.

   *terraform.tfvars*

   ```hcl
   backend_service = {
     network = {
       tls = { enabled = true, secret = { name = "platforma-backend-tls" } }
     }
   }
   ```

   *values.yaml*

   ```yaml
   network:
     tls:
       enabled: true
       secret: { name: platforma-backend-tls }
   ```

3. Make the entry point send TLS to the pod:

   - With the AWS entry point (implementation `aws-load-balancer-controller`), the chart does this: it switches the target group and its health check to HTTPS.
   - With another entry point, TLS on the Backend works only when you set TLS to the pod in that entry point, for example with an annotation of the Ingress controller.

   Until the entry point sends TLS, it cannot reach the Backend.

4. Make the metrics collector scrape port 9090 with HTTPS and trust the certificate. For the ports, see [Open ports](/security-overview/#open-ports).

The Backend reads its certificate only when it starts. After the Secret changes, also after an automatic renewal (for example by cert-manager), restart the Backend. The Deployment has the name of the Helm release (default `platforma`):

```sh
kubectl -n platforma rollout restart deployment/platforma
```

For what the restart does, see [Restart of the Backend](/configuration-storage/#restart-of-the-backend).

#### Variant: Single Server

In Single Server mode, the Backend itself serves TLS, on all its listeners: gRPC, HTTP, metrics and the debug API. The listeners then refuse plain connections.

| Option                                       | Environment variable | Default | What it sets                                                                                                                         |
| -------------------------------------------- | -------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `--listen-tls <certificate file>,<key file>` | `PL_LISTEN_TLS`      | off     | TLS with a certificate and its key in PEM format                                                                                     |
| `--self-signed-tls`                          | —                    | off     | TLS with a certificate that the Backend makes. See [Self-signed certificate](/configuration-domain-and-tls/#self-signed-certificate) |

Set only one of them: the Backend does not start with both.

```sh
platforma --listen-address 0.0.0.0 \
          --listen-tls /etc/platforma/tls/tls.crt,/etc/platforma/tls/tls.key \
          <other options>
```

The system user of the Backend must be able to read the key file, and other users of the server must not.

The Backend reads the certificate only when it starts. To renew the certificate, replace the two files, then restart the Backend.

**Without TLS**, the sessions of users and the files go through the network unencrypted. Use it only in a network that you trust, and only when nobody outside this network can reach the server.

## Self-signed certificate

In Single Server mode, the Backend can make its own certificate, so that you can try TLS without a certificate authority. Each computer with the Desktop App must then trust this certificate separately, in the trust store of the operating system (see [What the Desktop App trusts](/configuration-domain-and-tls/#what-clients-trust)). Thus use it for tests only: for the work of a team, use a certificate from a public or corporate certificate authority with `--listen-tls` (see [TLS on the Backend](/configuration-domain-and-tls/#tls-on-the-backend)).

| Option                            | Environment variable    | What it sets                                                      |
| --------------------------------- | ----------------------- | ----------------------------------------------------------------- |
| `--self-signed-tls`               | —                       | Turns on TLS with a self-signed certificate                       |
| `--self-signed-tls-domain <name>` | `PL_LISTEN_TLS_DOMAINS` | A DNS name of the server. Repeat the option for more names        |
| `--self-signed-tls-ip <address>`  | `PL_LISTEN_TLS_IPS`     | An IP address of the server. Repeat the option for more addresses |
| `--self-signed-tls-org <name>`    | `PL_LISTEN_TLS_ORG`     | The organization in the certificate. Default `Platforma`          |

Give at least one name or address: the Backend does not start without one. Give each name and address that users type in the Desktop App: the certificate is valid only for them.

```sh
platforma --listen-address 0.0.0.0 \
          --self-signed-tls --self-signed-tls-domain platforma.lab.example.com \
          <other options>
```

## What clients trust

The Desktop App checks the certificate of the entry point, or of the Backend in Single Server mode. It trusts the public certificate authorities. In addition, it trusts these certificate authorities:

- on Windows, the authorities in the certificate store of the system;
- on macOS, the authorities in the keychain;
- on Linux, the system certificate authorities (for example, added with `update-ca-certificates`);
- on each operating system, the authorities in a PEM file that the environment variable `NODE_EXTRA_CA_CERTS` gives when the Desktop App starts.

Thus:

- a certificate from a public certificate authority works on each computer as it is;
- a certificate from a corporate certificate authority works on the computers that trust this authority, usually the computers that the company manages;
- a self-signed certificate works only on the computers where it is in the trust store.

The Desktop App reads the certificate authorities when it starts: after a certificate authority is added, users start the Desktop App again. The same trust applies to a proxy that inspects TLS: see [TLS inspection](/configuration-proxy-and-custom-ca/#tls-inspection).

### What pl-cli trusts

With an address that starts with `https://`, pl-cli checks the certificate of the entry point, or of the Backend in Single Server mode. It trusts the certificate authorities that come with Node.js. It does not read the trust store of the operating system. Thus:

- a certificate from a public certificate authority works as it is;
- for a certificate of a corporate certificate authority, or a self-signed certificate, give pl-cli the certificate of the authority in a PEM file, in the environment variable `NODE_EXTRA_CA_CERTS`.

```sh
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corporate-ca.pem
```

Node.js reads this variable when it starts: set it before you run pl-cli.

## Changes on a running installation

| Change                                         | What happens                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | What to do                                                                                                                                                                                              |
| ---------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Add a host                                     | A new entry point. The users of the other hosts do not see a change                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | Make the DNS record, and make sure that the certificate covers the new host                                                                                                                             |
| Change the host name                           | The old address stops working                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Give users the new address: they change it in the Desktop App                                                                                                                                           |
| Renew the certificate at the entry point       | Nothing changes for the Backend and for users                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Nothing in Platforma. The certificate must cover the same hosts                                                                                                                                         |
| Renew the certificate of the Backend           | The Backend uses the old certificate until it starts again                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | Restart the Backend (see [Restart of the Backend](/configuration-storage/#restart-of-the-backend))                                                                                                      |
| Turn TLS on the Backend on or off (Kubernetes) | The Backend pod starts again. All its ports change between TLS and plain                                                                                                                                                                                                                                                                                                                                                                                                                                                               | Change the entry point and the metrics collector at the same time                                                                                                                                       |
| Turn TLS on or off (Single Server mode)        | The Backend starts again. The address with or without TLS changes for users                                                                                                                                                                                                                                                                                                                                                                                                                                                            | Give users the new address                                                                                                                                                                              |
| Change the entry point from public to private  | Users reach Platforma only through the private network. On AWS, a change of the input `gateway.scheme` of `platforma-tf-cloud-base` replaces the load balancer, so its DNS name changes. The DNS records follow only when the module manages DNS (input `dns`). On Google Cloud, the address, the certificate and the DNS records are replaced, and the change takes 2 or 3 applies of the root `cloud`: see [Change the entry point type on Google Cloud](/configuration-domain-and-tls/#change-the-entry-point-type-on-google-cloud) | Make sure that users have the private connection before the change. Plan a change window: users cannot connect until the new DNS records resolve. On AWS without `dns`, change the DNS records yourself |

### Change the entry point type on Google Cloud

On Google Cloud, `gateway.scheme` of the root `cloud` sets the type of the entry point: `external` (a public address) or `internal` (a private address only). A change of the type replaces the load balancer, its address, its certificate and the DNS records of the hosts. Users cannot connect until the new entry point works, so plan a change window.

Before you change to `internal`, make sure that:

- users have a private connection to the network of the cluster in its region, for example a VPN;
- the certificate fits the private entry point. With `tls.mode = "managed"`, the DNS zone must still be public. With a private zone, use `tls.mode = "existing"`. Then make regional self-managed SSL certificates in the region of the installation before the apply (see [Certificate at the entry point](/configuration-domain-and-tls/#tls-at-the-entry-point)).

1. **Apply the root `cloud`.** Change `gateway.scheme` in its `terraform.tfvars`, apply, and write the hand-off files again:

   ```sh
   cd <platforma-install>/terraform/gcp/examples/cloud
   terraform apply
   terraform output -json platforma_infrastructure > ../handoff/platforma-infrastructure.json
   terraform output -json cluster_connection > ../handoff/cluster-connection.json
   ```

   This apply stops with an error. It makes the new objects of the entry point, but it cannot remove the old ones. The Gateway still uses the old certificate, address, proxy-only subnet and Cloud Armor policy.

2. **Apply the root `cluster`.** It moves the Gateway to the new entry point.

   ```sh
   cd ../cluster
   terraform apply
   terraform output -json infrastructure > ../handoff/infrastructure.json
   ```

   From `internal` to `external`, the plan can show no change of the Gateway: the failed apply of step 1 did not update the outputs. Then apply the root `cloud` again, write its hand-off files again, and apply the root `cluster`.

3. **Apply the root `cloud` again**, about 5 minutes after step 2. It removes the old objects and ends without an error.

   ```sh
   cd ../cloud
   terraform apply
   ```

4. **Check the root `services`.** Run `terraform plan` in it, and apply it when the plan shows changes.

5. **Check DNS and the certificate** as in the [steps of the installation](/install-cloud-terraform/#steps). With an address allow-list (`gateway.allowed_cidrs`), wait about 10 minutes before you test it: Google Cloud starts to apply the list later than the apply ends.

From `external` to `internal`, the change takes 2 applies of the root `cloud`. From `internal` to `external`, it takes up to 3.

## Problems

**The Desktop App does not trust the certificate.** Cause: the certificate authority is not in the trust store of the computer, or the Desktop App started before it was added. Fix: add the certificate authority to the trust store of the operating system, then start the Desktop App again. See [What the Desktop App trusts](/configuration-domain-and-tls/#what-clients-trust).

**The name does not match the certificate.** Cause: the certificate does not cover the host that users type. A wildcard covers exactly one label: `*.example.com` does not cover `platforma.lab.example.com`. Fix: add the host to the certificate. See [DNS](/infrastructure-contract-entry-point/#dns).

**The connection breaks after 30 to 60 seconds.** Cause: a part of the path cuts the long gRPC streams. Fix: set the timeouts that Platforma needs. See [What each entry point must carry](/infrastructure-contract-entry-point/#what-each-entry-point-must-carry).

**The Terraform plan stops because a host is not covered.** Cause: the host of an entry point is not in `infrastructure.network.hosts` and does not match a wildcard in it. Fix: add the host to the list, with its DNS record and certificate.

**After TLS on the Backend is on, the entry point cannot reach the Backend.** Cause: the entry point still sends plain HTTP/2 to the pod. Fix: set TLS to the pod in the entry point.

For other connection problems, see [Troubleshooting: connection](/maintain-troubleshooting-connection/).

## Next

- [Connect clients](/install-connect-clients/)
- [Proxy and custom CA](/configuration-proxy-and-custom-ca/)
- [Entry point and TLS](/infrastructure-contract-entry-point/)

Source: https://docs.platforma.bio/configuration-domain-and-tls/
