Bookmark this page

Protect Internal Traffic with TLS

Objectives

  • Configure and use automatic service certificates.

Zero-trust Environments

Zero-trust environments assume that every interaction begins in an untrusted state. Users can access only files or objects that are specifically allowed; communication must be encrypted; and client applications must verify the authenticity of servers.

By default, OpenShift encrypts network traffic between nodes and the control plane, and prevents external entities from reading internal traffic. This encryption provides stronger security than default Kubernetes, which does not automatically encrypt internal traffic. Although the control plane traffic is encrypted, applications in OpenShift do not necessarily verify the authenticity of other applications or encrypt application traffic.

Zero-trust environments require that a trusted certificate authority (CA) signs the certificates that are used to encrypt traffic. By referencing the CA certificate, an application can cryptographically verify the authenticity of another application with a signed certificate.

Service Certificates

OpenShift provides the service-ca controller to generate and sign service certificates for internal traffic. The service-ca controller creates a secret that it populates with a signed certificate and key. A deployment can mount this secret as a volume to use the signed certificate. Additionally, client applications need to trust the service-ca controller CA.

Service Certificate Creation

To generate a certificate and key pair, apply the service.beta.openshift.io/serving-cert-secret-name=your-secret annotation to a service. The service-ca controller creates the your-secret secret in the same namespace if it does not exist, and populates it with a signed certificate and key pair for the service.

[user@host ~]$ oc annotate service hello \ 1
    service.beta.openshift.io/serving-cert-secret-name=hello-secret 2
service/hello annotated

1

The hello service is annotated.

2

The secret that contains the certificate and key pair is named hello-secret.

After OpenShift generates the secret, you must mount the secret in the application deployment. The location to place the certificate and key is application-dependent. The following YAML patch is for an NGINX deployment:

spec:
  template:
    spec:
      containers:
        - name: hello
          volumeMounts:
            - name: hello-volume 1
              mountPath: /etc/pki/nginx/ 2
      volumes:
        - name: hello-volume 3
          secret:
            defaultMode: 420 4
            secretName: hello-secret 5
            items:
              - key: tls.crt 6
                path: server.crt 7
              - key: tls.key 8
                path: private/server.key 9

1 3

Defining the volume as hello-volume.

2

The application-specific mount path.

4

The read-write permissions that the application recommends.

5

The secret that the earlier annotation defined.

6 8

The secret has tls.crt as the signed certificate and tls.key as the key.

7 9

The application-specific destinations for the certificate and key.

After mounting the secret to the application container, the application can use the signed certificate for TLS traffic.

Client Service Application Configuration

For a client service application to verify the validity of a certificate, the application needs the CA bundle that signed that certificate. The service-ca controller injects the CA bundle when you apply the service.beta.openshift.io/inject-cabundle=true annotation to an object. You can apply the annotation to configuration maps, API services, custom resource definitions (CRD), mutating webhooks, and validating webhooks.

Configuration Maps

Apply the service.beta.openshift.io/inject-cabundle=true annotation to a configuration map to inject the CA bundle into the data: { service-ca.crt } field. The service-ca controller replaces all data in the selected configuration map with the CA bundle. You must therefore use a dedicated configuration map to prevent overwriting existing data.

[user@host ~]$ oc annotate configmap ca-bundle \
    service.beta.openshift.io/inject-cabundle=true
configmap/ca-bundle annotated
API service

Applying the annotation to an API service injects the CA bundle into the spec.caBundle field.

CRD

Applying the annotation to a CRD injects the CA bundle into the spec.conversion.webhook.clientConfig.caBundle field.

Mutating or validating webhook

Applying the annotation to a mutating webhook or validating webhook injects the CA bundle into the clientConfig.caBundle field.

Key Rotation

The service CA certificate is valid for 26 months by default and is automatically rotated after 13 months. After rotation is a 13-month grace period where the original CA certificate is still valid. During this grace period, each pod that is configured to trust the original CA certificate must be restarted in some way. A service restart automatically injects the new CA bundle.

You can also manually rotate the certificate for the service CA and for generated service certificates. To rotate a generated service certificate, delete the existing secret, and the service-ca controller automatically generates a new one.

[user@host ~]$ oc delete secret certificate-secret
secret/certificate-secret deleted

To manually rotate the service CA certificate, delete the signing-key secret in the openshift-service-ca namespace.

[user@host ~]$ oc delete secret/signing-key -n openshift-service-ca
secret/signing-key deleted

This process immediately invalidates the former service CA certificate. You must restart all pods that use it, for TLS to function.

Alternatives to Service Certificates

Other options can handle TLS encryption inside an OpenShift cluster, such as a service mesh or the certmanager operator.

You can use the certmanager operator to delegate the certificate signing process to a trusted external service, and also to renew a certificate.

You can also use Red Hat OpenShift Service Mesh for encrypted service-to-service communication and for other advanced features. Service mesh is an advanced topic and is not covered in the course.

Patching Kubernetes Resources

You can modify objects in OpenShift in a repeatable way with the oc patch command. The oc patch command updates or adds fields in an existing object from a provided JSON or YAML snippet or file. A software developer might distribute a patch file or snippet to fix problems before a full update is available.

To patch an object from a snippet, use the oc patch command with the -p option and the snippet. The following example updates the hello deployment to have a CPU resource request of 100m with a JSON snippet:

[user@host ~]$ oc patch deployment hello -p \
    '{"spec":{"template":{"spec":{"resources":{"requests":{"cpu": "100m"}}}}}}'
deployment/hello patched

To patch an object from a patch file, use the oc patch command with the --patch-file option and the location of the patch file. The following example updates the hello deployment to include the content of the ~/volume-mount.yaml patch file:

[user@host ~]$ oc patch deployment hello --patch-file ~/volume-mount.yaml
deployment.apps/hello patched

The contents of the patch file describe mounting a persistent volume claim as a volume:

spec:
  template:
    spec:
      containers:
        - name: hello
          volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html/
      volumes:
        - name: www
          persistentVolumeClaim:
            claimName: nginx-www

This patch results in the following manifest for the hello deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
  ...output omitted...
spec:
  ...output omitted...
  template:
    ...output omitted...
    spec:
      containers:
        ...output omitted...
        name: server
        ...output omitted...
        volumeMounts:
        - mountPath: /usr/share/nginx/html/
          name: www
        - mountPath: /etc/nginx/conf.d/
          name: tls-conf
      ...output omitted...
      volumes:
      - configMap:
          defaultMode: 420
          name: tls-conf
        name: tls-conf
      - persistentVolumeClaim:
          claimName: nginx-www
        name: www
...output omitted...

The patch applies to the hello deployment regardless of whether the www volume mount exists. The oc patch command modifies existing fields in the object that are specified in the patch. If the beginning state of the hello deployment already contains data as follows, then the end result is the same as if the fields do not exist:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
  ...output omitted...
spec:
  ...output omitted...
  template:
    ...output omitted...
    spec:
      containers:
        ...output omitted...
        name: server
        ...output omitted...
        volumeMounts:
        - mountPath: /usr/share/nginx/www/ 1
          name: www
        - mountPath: /etc/nginx/conf.d/
          name: tls-conf
      ...output omitted...
      volumes:
      - configMap:
          defaultMode: 420
          name: tls-conf
        name: tls-conf
      - persistentVolumeClaim:
          claimName: deprecated-www 2
        name: www
...output omitted...

1 2

The www volume already exists. The patch replaces the existing data with the new data.

References

For more information about service certificates, refer to the Securing Service Traffic Using Service Serving Certificate Secrets section in the Configuring Certificates chapter in the Red Hat OpenShift Container Platform 4.14 Security and Compliance documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/security_and_compliance#add-service-serving

For more information about service mesh, refer to the About OpenShift Service Mesh section in the Service Mesh 2.x chapter in the Red Hat OpenShift Container Platform 4.14 Service Mesh documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/service_mesh#ossm-about

For more information about the cert-manager operator, refer to the cert-manager Operator for Red Hat OpenShift chapter in the Red Hat OpenShift Container Platform 4.14 Security and Compliance documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/security_and_compliance#cert-manager-operator-for-red-hat-openshift

For more information about the oc patch command, refer to the oc patch section in the OpenShift CLI Developer Command Reference chapter in the Red Hat OpenShift Container Platform 4.14 CLI Tools documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/cli_tools/index#oc-patch

Red Hat Topics - What Is Zero Trust?

Revision: do280-4.14-08d11e1