|
1 | 1 | --- |
2 | 2 | title: HTTPS, TLS and Certificates |
3 | | -description: Mokapi is able to mock the behaviour of multiple hostnames and present a valid X.509 Certificate for them. This guide shows you how to configure TLS. |
| 3 | +description: Mokapi can mock the behaviour of multiple hostnames and present valid X.509 certificates for them. This guide explains how to configure TLS in Mokapi. |
4 | 4 | --- |
5 | 5 | # HTTPS, TLS and Certificates |
6 | | -Mokapi is able to mock the behaviour of multiple hostnames and present a valid X.509 Certificate |
7 | | -for them. This guide shows you how to configure TLS. |
| 6 | +Mokapi can simulate multiple hostnames and present a valid X.509 certificate for each of |
| 7 | +them, making it possible to test HTTPS endpoints with realistic TLS behavior. |
| 8 | +This guide explains how TLS works in Mokapi and how you can configure certificates to suit |
| 9 | +your needs. |
| 10 | + |
| 11 | +## Example: Mocking HTTPS Endpoints |
| 12 | + |
| 13 | +When defining your API, you can specify HTTPS URLs in the `servers` section. Mokapi will |
| 14 | +automatically generate certificates for these hostnames at runtime if none are provided. |
8 | 15 |
|
9 | 16 | ```yaml |
10 | 17 | openapi: 3.0.0 |
11 | | -info: {} |
| 18 | +info: |
| 19 | + title: Petstore |
| 20 | + version: "1" |
12 | 21 | servers: |
13 | 22 | - url: https://demo |
14 | 23 | - url: https://demo:8443 |
15 | 24 | paths: {} |
16 | 25 | ``` |
17 | 26 |
|
18 | | -## Certificate Authority |
19 | | -By default, Mokapi signs all generated certificate with its own CA certificate in the [git repo](https://github.com/marle3003/mokapi/tree/master/assets). |
| 27 | +In this example: |
| 28 | +- Mokapi will respond over HTTPS for https://demo and https://demo:8443. |
| 29 | +- If no certificate is provided, Mokapi will generate one signed by its own Certificate Authority. |
20 | 30 |
|
21 | | -This example demonstrates how to reference a custom CA certificate as an environment variable. |
| 31 | +## Certificate Authority (CA) |
| 32 | +By default, Mokapi uses its own built-in Certificate Authority to sign any certificates it |
| 33 | +generates. The Root CA certificate and key are stored in the [Mokapi GitHub repository](https://github.com/marle3003/mokapi/tree/master/assets). |
| 34 | +
|
| 35 | +If you want to use your own CA for signing, you can specify it, for example, by setting environment variables. |
22 | 36 | ```yaml |
23 | 37 | MOKAPI_RootCaCert: /path/to/caCert.pem |
24 | 38 | MOKAPI_RootCaKey: /path/to/caKey.pem |
25 | 39 | ``` |
26 | 40 |
|
27 | | -## Server Certificate |
28 | | -You can set your custom server certificate or let Mokapi generate it on runtime. Mokapi |
29 | | -generates a new certificate at first request, if you not provide a suitable certificate. |
| 41 | +Why use your own CA? |
| 42 | +- To avoid browser or client certificate warnings (if your CA is trusted by the OS or application). |
| 43 | +- To align with an existing internal PKI (Public Key Infrastructure). |
| 44 | +- To test certificate chains with your organization’s security policies. |
| 45 | +
|
| 46 | +## Server Certificates |
| 47 | +
|
| 48 | +Mokapi supports two ways of providing server certificates: |
| 49 | +
|
| 50 | +### Option A — Let Mokapi Generate Certificates |
| 51 | +
|
| 52 | +If you do not supply a certificate for a hostname, Mokapi will generate one dynamically the first |
| 53 | +time that hostname is requested. The certificate will be valid for the requested hostname and |
| 54 | +signed by the configured CA (Mokapi’s default or your custom CA). |
| 55 | +
|
| 56 | +### Option B — Provide Your Own Certificates |
| 57 | +
|
| 58 | +If you already have certificates, you can configure Mokapi to use them instead of generating new ones. |
| 59 | +Add them to the static configuration: |
30 | 60 |
|
31 | 61 | ```yaml |
32 | | -mokapi: 1.0 |
33 | 62 | certificates: |
34 | | - - certFile: ./domainCert.pem |
35 | | - keyFile: ./domainKey.pem |
36 | | - - certFile: /path/to/other-domain.cert |
37 | | - keyFile: /path/to/other-domain.key |
38 | | -``` |
| 63 | + static: |
| 64 | + - cert: ./domainCert.pem |
| 65 | + key: ./domainKey.pem |
| 66 | + - cert: /path/to/other-domain.cert |
| 67 | + key: /path/to/other-domain.key |
| 68 | +``` |
| 69 | +
|
| 70 | +You can list multiple certificate/key pairs to support multiple hostnames. |
| 71 | +
|
| 72 | +## Tips for Working with Certificates |
| 73 | +
|
| 74 | +- **Trust the CA** — If you use Mokapi’s default CA, importing its root certificate into your OS or browser’s trust store will prevent certificate warnings. |
| 75 | +- **Match Hostnames** — The certificate’s Common Name (CN) or Subject Alternative Name (SAN) must match the hostname in your API definition (servers list). |
| 76 | +- **Use Custom Domains** — For local testing, you can map custom domains in your /etc/hosts file or Windows hosts file to 127.0.0.1 so the hostname matches the certificate. |
| 77 | +
|
| 78 | +## Summary |
| 79 | +
|
| 80 | +- Mokapi can generate certificates dynamically or use certificates you provide. |
| 81 | +- Certificates are signed by Mokapi’s default CA unless you specify a custom one. |
| 82 | +- You can configure both the CA and the server certificates via environment variables or the static configuration. |
| 83 | +- Trusting the CA in your OS avoids browser or client security warnings. |
0 commit comments