> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/1.6/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/cgw/1.6/tutorials/certificate_based_vpn.md).

# Certificate based VPN

CGW has the option to use certificates for VPN connections.

To use these, the certificates and keys have to be managed as *Kubernetes secrets*.
These will then be mounted into the CGW pod.

To configure CGW to use secrets for VPN connections there are two steps to fulfill:

* concert the certificates and keys to secrets
* configure Strongswan to use these

* [prerequisites](#prerequisites)
* [enable mounting of certificates](#enable-mounting-of-certificates)
* [manage secrets](#manage-secrets)
  * [define secrets for certificates and keys](#define-secrets-for-certificates-and-keys)
  * [convert certificates and keys into secrets](#convert-certificates-and-keys-into-secrets)
  * [set up secrets manually](#set-up-secrets--manually)
* [configure strongswan](#configure-strongswan)
  * [setting up the CA](#setting-up-the-ca)
  * [configure connections](#configure-connections)
    * [setting the private key](#setting-the-private-key)

## prerequisites

To follow this tutorial it is assumed, that the following components are already available:

* the certificate of the CA is available which signs the trusted keys
* a private/public-key-pair is created for this instance of the CGW
* a *certificate signing request (CSR)* was created
* the keys were signed and the certificate is available

The [Strongswan Documentation](https://wiki.strongswan.org/projects/strongswan/wiki/SimpleCA) can be used
as a starting point to learn how to create keys and certificates.

## enable mounting of certificates

Set the following parameters to enable mounting of certificates to be used in later configurations:

```yaml
ipsec:
  certBasedIPSEC: true
```

## manage secrets

There are four different kinds of data, which are managed in secrets and are part
of the certificate based VPN configuration:

* public/private keys
* certificates
* CA certificates
* certificate revocation lists

All these files follow the same schema.

### define secrets for certificates and keys

To configure CGW to use the certificates and keys you have stored on you harddrive,
you have to add them to the configuration:

```yaml
ipsec:
  certs:
    privateKeys:     # add private keys here
      keypair1.der: <base64 encoded file>
      keypair2.der: <base64 encoded file>
    certificates:    # add existing certificates here
      cert1.der: <base64 encoded file>
    caCertificates:  # add CA certificates here
      myCA.der: <base64 encoded file>
    certificateRevocationLists: # add CRLs here
      crl1.der: <base64 encoded file>
```

If a one of the four categories are not defined,
no secret for the corresponding category will be defined
and no folder will be mounted.

### convert certificates and keys into secrets

To convert you files to a base64 encoded string you can use the following in your shell:

```sh
cat keypair1.der | base64
```

### set up secrets  manually

You can also create the secrets to store your certificates manually, for example to keep them outside of your
values files.

To do so you first have to create a secret:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-company-ca-certificate
  labels:
    company: "ACME Corp."
  namespace: interconnection
data:
  cert1.der: <base64 encoded file>
```

Then you have to store it to a file and apply it to your kubernetes cluster:

```sh
kubectl apply -f ca-secret.yaml
```

After that, you have to configure the certificate name in the CGW configuration:

```yaml
ipsec:
  certs:
    externalCaCertificates: my-company-ca-certificate  # name of the secret
```

The same applies for the values:

* `externalCertificates`
* `externalCertificateRevocationLists`
* `externalPrivateKeys`

NOTE: If the files are defined in the value files as well as external certificates are set,
the external ones will preferred:

```yaml
ipsec:
  certs:
    caCertificates:  # add CA certificates here
      myCA.der: <base64 encoded file>
    externalCaCertificates: external-secret-name # <-- This one will be preferred
```

It is though recommended to delete or comment the unused part out.

## configure strongswan

The following explainations are considering the strongswan configurations and secerts.

The main [README](https://docs.cennso.com/components/cgw/1.6.md#manual-strongswan-configuration) describes how to set up the manual configuration.

### setting up the CA

Concerning the [Strongswan Documentation](https://wiki.strongswan.org/projects/strongswan/wiki/CaSection)
the setup of a CA section in the configuration file is optional.

All CA certificates which are defined as described above will be automatically loaded.

It can be configured though add URIs of CRLs os OCSP for online verification of certificates.

### configure connections

To configure a connection to a peer in a site-to-site scenario you have to set the corresponding ids and
certificates in the corresponding `ipsec.conf` file:

`ipsec.test.conf:`

```
conn sts-base
    ike=aes192-sha256-modp3072,default
    esp=aes192-sha256-ecp256-modp3072#
    leftid=foobar
    leftcert=foobar.pem

# this conn is set up for a remote host with a static IP
conn site-1-static-ip
    also=sts-base
    keyexchange=ikev2
    leftsubnet=10.1.2.0/24,10.1.1.0/24
    rightsubnet=10.1.3.0/24
    right=1.2.3.4
    rightcert=1.2.3.4.pem
    auto=route
```

When no `rightid` is given, it is taken from the certificate itself.
Further a defined id has to match the id in the certificate.

For futher information about configuration of connections refer to the [Strongswan Documentation](https://wiki.strongswan.org/projects/strongswan/wiki/ConnSection)

#### setting the private key

The private key to be used by the CGW has to be define in the `ipsec.secrets` file:

`ipsec.test.secrets:`

```
# ipsec.test.secrets
: RSA cgwKey.pem
```

For futher information about configuration of secrets refer to the [Strongswan Documentation](https://wiki.strongswan.org/projects/strongswan/wiki/Ipsecsecrets)
