Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ability to use default ingress TLS certificate #508

Merged
merged 2 commits into from
Nov 6, 2020
Merged

Conversation

mmorhun
Copy link
Contributor

@mmorhun mmorhun commented Oct 29, 2020

Signed-off-by: Mykola Morhun mmorhun@redhat.com

What does this PR do

Adds ability to tell Che operator to use default ingress TLS certificate to secure Che endpoints.
To use default ingress certificate, field k8s.tlsSecretName should be empty string or absent.

Which isseus this PR fixes

eclipse-che/che#18079

How to test

  1. Create a new Minikube instance.
  2. Enable ingress addon: minikube addons enable ingress
  3. Change the default ingress certificate
Automation script
#!/bin/bash

MINIKUBE_DOMAIN=$(minikube ip).nip.io

CA_CN='Custom Minikube CA'
CA_KEY_FILE='ca.key'
CA_CERT_FILE='ca.crt'

CN_SERVER='Minikube Ingress'
SERVER_KEY_FILE='domain.key'
SERVER_CERT_REQUEST_FILE='domain.csr'
SERVER_CERT_FILE='domain.crt'

CHAIN_FILE='chain.pem'

# Detect openssl configuration file
OPENSSL_CNF='/etc/pki/tls/openssl.cnf'
if [ ! -f $OPENSSL_CNF ]; then
    OPENSSL_CNF='/etc/ssl/openssl.cnf'
fi

DIR=/tmp/minikube-cert
rm -rf $DIR && mkdir $DIR && cd $DIR

# Generate private key for root CA
openssl genrsa -out $CA_KEY_FILE 4096

# Generate root CA certificate and sign it with previously generated key.
openssl req -batch -new -x509 -nodes -key $CA_KEY_FILE -sha256 -subj /CN="${CA_CN}" -days 1024 -reqexts SAN -extensions SAN -config <(cat ${OPENSSL_CNF} <(printf '[SAN]\nbasicConstraints=critical, CA:TRUE\nkeyUsage=keyCertSign, cRLSign, digitalSignature')) -outform PEM -out $CA_CERT_FILE

# Generate server prvate key
openssl genrsa -out $SERVER_KEY_FILE 2048

# Create certificate request for the ingress
openssl req --batch -new -sha256 -key $SERVER_KEY_FILE -subj "/CN=${CN_SERVER}" -reqexts SAN -config <(cat $OPENSSL_CNF <(printf "\n[SAN]\nsubjectAltName=DNS:${MINIKUBE_DOMAIN},DNS:*.${MINIKUBE_DOMAIN}\nbasicConstraints=critical, CA:FALSE\nkeyUsage=digitalSignature, keyEncipherment, keyAgreement, dataEncipherment\nextendedKeyUsage=serverAuth")) -outform PEM -out $SERVER_CERT_REQUEST_FILE

# Create certificate for ingress domain based on given certificate request.
openssl x509 -req -in $SERVER_CERT_REQUEST_FILE -CA $CA_CERT_FILE -CAkey $CA_KEY_FILE -CAcreateserial -days 365 -sha256 -extfile <(printf "subjectAltName=DNS:${MINIKUBE_DOMAIN},DNS:*.${MINIKUBE_DOMAIN}\nbasicConstraints=critical, CA:FALSE\nkeyUsage=digitalSignature, keyEncipherment, keyAgreement, dataEncipherment\nextendedKeyUsage=serverAuth") -outform PEM -out $SERVER_CERT_FILE

# Create certificate chain file
cat $SERVER_CERT_FILE $CA_CERT_FILE > $CHAIN_FILE

# Create secret
SECRET_NAME='new-ingress-tls'
kubectl create secret tls ${SECRET_NAME} --key ${SERVER_KEY_FILE} --cert ${CHAIN_FILE} --namespace kube-system

# Patch nginix ingress controller
kubectl patch deployment ingress-nginx-controller --namespace kube-system --type=json -p="[{\"op\": \"add\", \"path\": \"/spec/template/spec/containers/0/args/-\", \"value\": \"--default-ssl-certificate=kube-system/${SECRET_NAME}\"}]"

echo "Minikube TLS certificate has been changed. New CA: ${DIR}/${CA_CERT_FILE}"
  1. Deploy Eclipse Che with command:
./run server:start --platform=minikube --installer=operator --che-operator-image=<image-built-from-this-PR> --che-operator-cr-patch-yaml default-secret-patch.yaml 

where default-secret-patch.yaml is:

spec:
  k8s:
    tlsSecretName: ''
  1. Check that everything works

Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
@flacatus flacatus removed their assignment Nov 6, 2020
@eclipse-che eclipse-che deleted a comment from openshift-ci-robot Nov 6, 2020
if instance.Spec.K8s.TlsSecretName != "" {
// Self-signed certificate should be created to secure Che ingresses
result, err := deploy.K8sHandleCheTLSSecrets(deployContext)
if result.Requeue || result.RequeueAfter > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply.

If err {
 logrus.Error(...)
}
return result, err

Copy link
Contributor Author

@mmorhun mmorhun Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it will cause unexpected return if operator needs to continue

pkg/deploy/ingress.go Show resolved Hide resolved
return true, nil
}
// TLS secret found, consider it as commonly trusted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the case when self-signed-certificate secret exists but che-tls does not? How do we handle that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operator will delete self-signed-certificate secret and will generate new pair.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants