Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1210 from SUSE/gen-ssl
Browse files Browse the repository at this point in the history
Improve SSL certificate handling when deploying through Helm
  • Loading branch information
nwmac committed Sep 18, 2017
2 parents bfa532e + c161cea commit e402466
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 83 deletions.
19 changes: 15 additions & 4 deletions components/app-core/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func initConnPool(dc datastore.DatabaseConfig) (*sql.DB, error) {

// If our timeout boundary has been exceeded, bail out
if timeout.Sub(time.Now()) < 0 {
return nil, fmt.Errorf("Timeout boundary of %d minutes has been exceeded. Exiting.", TimeoutBoundary)
return nil, fmt.Errorf("timeout boundary of %d minutes has been exceeded. Exiting", TimeoutBoundary)
}

// Circle back and try again
Expand Down Expand Up @@ -340,8 +340,8 @@ func loadDatabaseConfig(dc datastore.DatabaseConfig) (datastore.DatabaseConfig,
return dc, nil
}

func createTempCertFiles(pc interfaces.PortalConfig) (string, string, error) {
log.Debug("createTempCertFiles")
func detectTLSCert(pc interfaces.PortalConfig) (string, string, error) {
log.Debug("detectTLSCert")
certFilename := "pproxy.crt"
certKeyFilename := "pproxy.key"

Expand All @@ -355,6 +355,17 @@ func createTempCertFiles(pc interfaces.PortalConfig) (string, string, error) {
return devCertsDir + certFilename, devCertsDir + certKeyFilename, nil
}

// Check if certificate have been provided as files (as is the case in kubernetes)
if pc.TLSCertPath != "" && pc.TLSCertKeyPath != "" {
log.Infof("Using TLS cert: %s, %s", pc.TLSCertPath, pc.TLSCertKeyPath)
_, errCertMissing := os.Stat(pc.TLSCertPath)
_, errCertKeyMissing := os.Stat(pc.TLSCertKeyPath)
if errCertMissing != nil || errCertKeyMissing != nil {
return "", "", fmt.Errorf("unable to find certificate %s or certificate key %s", pc.TLSCertPath, pc.TLSCertKeyPath)
}
return pc.TLSCertPath, pc.TLSCertKeyPath, nil
}

err := ioutil.WriteFile(certFilename, []byte(pc.TLSCert), 0600)
if err != nil {
return "", "", err
Expand Down Expand Up @@ -436,7 +447,7 @@ func start(config interfaces.PortalConfig, p *portalProxy, addSetupMiddleware *s
}

if config.HTTPS {
certFile, certKeyFile, err := createTempCertFiles(config)
certFile, certKeyFile, err := detectTLSCert(config)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions components/app-core/backend/repository/interfaces/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type PortalConfig struct {
TLSAddress string `configName:"CONSOLE_PROXY_TLS_ADDRESS"`
TLSCert string `configName:"CONSOLE_PROXY_CERT"`
TLSCertKey string `configName:"CONSOLE_PROXY_CERT_KEY"`
TLSCertPath string `configName:"CONSOLE_PROXY_CERT_PATH"`
TLSCertKeyPath string `configName:"CONSOLE_PROXY_CERT_KEY_PATH"`
CFClient string `configName:"CF_CLIENT"`
CFClientSecret string `configName:"CF_CLIENT_SECRET"`
AllowedOrigins []string `configName:"ALLOWED_ORIGINS"`
Expand Down
4 changes: 2 additions & 2 deletions deploy/Dockerfile.all-in-one
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY *.json ./
COPY gulpfile.js ./
COPY components ./components
COPY build ./build/
COPY deploy/ci/scripts/generate_cert.sh generate_cert.sh
COPY deploy/tools/generate_cert.sh generate_cert.sh
COPY deploy/db deploy/db
COPY deploy/all-in-one/config.all-in-one.properties config.properties

Expand All @@ -16,7 +16,7 @@ RUN npm install -g gulp bower \
&& npm run build-cf

# Generate dev-certs
RUN DEV_CERTS_PATH=/go/dev-certs ./generate_cert.sh \
RUN CERTS_PATH=/go/dev-certs ./generate_cert.sh \
&& chmod +x portal-proxy

EXPOSE 443
Expand Down
2 changes: 1 addition & 1 deletion deploy/Dockerfile.bk-preflight.dev
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN apk update && \
WORKDIR /srv
COPY outputs/* /srv/
COPY /deploy/db/scripts/run-preflight-job.sh /run-preflight-job.sh
COPY dev-certs dev-certs
COPY /deploy/tools/generate_cert.sh /generate_cert.sh
RUN chmod +x portal-proxy
EXPOSE 443
CMD ["sh", "-c", "/run-preflight-job.sh; /srv/portal-proxy"]
11 changes: 11 additions & 0 deletions deploy/Dockerfile.bk.k8s
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM alpine:latest

RUN apk update && \
apk add ca-certificates git &&\
mkdir -p /srv

WORKDIR /srv
COPY outputs/* /srv/
RUN chmod +x portal-proxy
EXPOSE 443
ENTRYPOINT ["/srv/portal-proxy"]
4 changes: 2 additions & 2 deletions deploy/ci/tasks/build-images/generate-certs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ run:
- |
apk update
apk add openssl
export DEV_CERTS_PATH=dev-certs-output/dev-certs
./stratos-ui/deploy/ci/scripts/generate_cert.sh
export CERTS_PATH=dev-certs-output/dev-certs
./stratos-ui/deploy/tools/generate_cert.sh
2 changes: 1 addition & 1 deletion deploy/ci/tasks/stratos-ui/prep-proxy-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ run:
npm run build-backend
cd -
cp -r ./stratos-ui/outputs ./portal-proxy-output
sh ./stratos-ui/deploy/ci/scripts/generate_cert.sh
sh ./stratos-ui/deploy/tools/generate_cert.sh
3 changes: 2 additions & 1 deletion deploy/containers/nginx/Dockerfile.k8s
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ RUN mkdir -p /usr/share/doc/suse
COPY ./LICENSE.txt /usr/share/doc/suse/LICENSE.txt
COPY ./conf/nginx.k8s.conf /etc/nginx/nginx.conf
COPY ./dist/ /usr/share/nginx/html
COPY ./run-nginx.sh/ /run-nginx.sh
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
CMD [ "/run-nginx.sh" ]
4 changes: 2 additions & 2 deletions deploy/containers/nginx/conf/nginx.k8s.conf
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ http {
server {
listen 443 ssl;

ssl_certificate /etc/secrets/console-cert;
ssl_certificate_key /etc/secrets/console-cert-key;
ssl_certificate /ENCRYPTION_KEY_VOLUME/console.crt;
ssl_certificate_key /ENCRYPTION_KEY_VOLUME/console.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

Expand Down
12 changes: 12 additions & 0 deletions deploy/containers/nginx/run-nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
sed -i -e 's@ENCRYPTION_KEY_VOLUME@'"${ENCRYPTION_KEY_VOLUME}"'@g' /etc/nginx/nginx.conf
echo "Checking if certificate has been written to the encryption volume!"
while :
do
if [ -f /${ENCRYPTION_KEY_VOLUME}/console.crt ]; then
break;
fi
sleep 1;
done
echo "TLS certificate detected continuing, starting nginx."
nginx -g "daemon off;"
1 change: 1 addition & 0 deletions deploy/db/Dockerfile.preflight-job
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM debian:jessie
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -y openssl
COPY /deploy/db/scripts/run-preflight-job.sh /run-preflight-job.sh
COPY /deploy/tools/generate_cert.sh /generate_cert.sh
CMD ["/run-preflight-job.sh"]
14 changes: 14 additions & 0 deletions deploy/db/scripts/run-preflight-job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ if [ ! -e /$ENCRYPTION_KEY_VOLUME/$ENCRYPTION_KEY_FILENAME ]; then
echo "-- Done."
fi

# Step 3 - Write out or generate SSL certificate data
if [ "${CONSOLE_CERT:-not-set}" = "not-set" -a "${CONSOLE_CERT_KEY:-not-set}" = "not-set" ]; then
echo "CONSOLE_CERT and CONSOLE_CERT_KEY not set, generating..."
export CERTS_PATH=/$ENCRYPTION_KEY_VOLUME
export DEV_CERTS_DOMAIN=console
/generate_cert.sh
echo "Certificates generated."
else
echo "CONSOLE_CERT and CONSOLE_CERT_KEY have been provided, writing them to the Encryption volume"
echo "$CONSOLE_CERT" > /$ENCRYPTION_KEY_VOLUME/console.crt
echo "$CONSOLE_CERT_KEY" > /$ENCRYPTION_KEY_VOLUME/console.key
echo "Wrote out certificates."
fi

exit 0
21 changes: 21 additions & 0 deletions deploy/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,24 @@ kubectl create -f storageclass.yaml
```

See [Storage Class documentation] ( https://kubernetes.io/docs/tasks/administer-cluster/change-default-storage-class/) for more insformation.

## Deploying Stratos UI with your own TLS certificates

By default the console will generate self-signed certificates for demo purposes. To configure Stratos UI to use your provided TLS certificates set the `consoleCert` and `consoleCertKey` overrides.

```
consoleCert: |
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAJooOiQWl1v1MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
...
-----END CERTIFICATE-----
consoleCertKey: |
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDV9+ySh0xZzM41
....
-----END PRIVATE KEY-----
```
Assuming the above is stored in a file called `override-ssl.yaml`, install the chart with the override specified.
```
helm install -f override-ssl.yaml stratos-ui/console --namespace console
```
2 changes: 1 addition & 1 deletion deploy/kubernetes/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function buildProxy {
# publish the container image for the portal proxy
echo
echo "-- Build & publish the runtime container image for the Console Proxy"
buildAndPublishImage stratos-proxy deploy/Dockerfile.bk.dev ${STRATOS_UI_PATH}
buildAndPublishImage stratos-proxy deploy/Dockerfile.bk.k8s ${STRATOS_UI_PATH}
# Build merged preflight & proxy image, used when deploying into multi-node k8s cluster without a shared storage backend
buildAndPublishImage stratos-proxy-noshared deploy/Dockerfile.bk-preflight.dev ${STRATOS_UI_PATH}
}
Expand Down
32 changes: 0 additions & 32 deletions deploy/kubernetes/console/ssl/console.crt

This file was deleted.

27 changes: 0 additions & 27 deletions deploy/kubernetes/console/ssl/console.key

This file was deleted.

31 changes: 28 additions & 3 deletions deploy/kubernetes/console/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ metadata:
data:
stolon: {{ .Values.dbPassword | b64enc }}
db-password: {{ .Values.mariadb.mariadbPassword | b64enc }}
console-cert-key: {{ .Files.Get "ssl/console.key" | b64enc }}
console-cert: {{ .Files.Get "ssl/console.crt" | b64enc }}

---
apiVersion: apps/v1beta1
kind: StatefulSet
Expand All @@ -29,10 +26,16 @@ spec:
containers:
- image: {{.Values.dockerRegistry}}/{{.Values.dockerOrg}}/{{.Values.images.console}}:{{.Values.consoleVersion}}
name: ui
env:
- name: ENCRYPTION_KEY_VOLUME
value: "{{ .Release.Name }}-encryption-key-volume"
volumeMounts:
- mountPath: /etc/secrets/
name: "{{ .Release.Name }}-secret"
readOnly: true
- mountPath: "/{{ .Release.Name }}-encryption-key-volume"
name: "{{ .Release.Name }}-encryption-key-volume"
readOnly: true
ports:
- containerPort: 80
name: http
Expand Down Expand Up @@ -136,6 +139,22 @@ spec:
value: "{{ .Release.Name }}-encryption-key-volume"
- name: ENCRYPTION_KEY_FILENAME
value: key
{{- if .Values.noShared }}
{{- if .Values.consoleCert }}
- name: CONSOLE_CERT
value: |
{{ .Values.consoleCert | indent 12 }}
{{- end }}
{{- if .Values.consoleCertKey }}
- name: CONSOLE_CERT_KEY
value: |
{{ .Values.consoleCertKey | indent 12 }}
{{- end }}
{{- end }}
- name: CONSOLE_PROXY_CERT_PATH
value: "/{{ .Release.Name }}-encryption-key-volume/console.crt"
- name: CONSOLE_PROXY_CERT_KEY_PATH
value: "/{{ .Release.Name }}-encryption-key-volume/console.key"
- name: HTTP_PROXY
{{- if .Values.httpProxy }}
value: {{.Values.httpProxy}}
Expand Down Expand Up @@ -178,3 +197,9 @@ spec:
- name: "{{ .Release.Name }}-secret"
secret:
secretName: "{{ .Release.Name }}-secret"
- name: "{{ .Release.Name }}-encryption-key-volume"
persistentVolumeClaim:
claimName: "{{ .Release.Name }}-encryption-key-volume"
- name: "{{ .Release.Name }}-secret"
secret:
secretName: "{{ .Release.Name }}-secret"
12 changes: 12 additions & 0 deletions deploy/kubernetes/console/templates/pre-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,20 @@ spec:
value: upgrade.lock
- name: ENCRYPTION_KEY_VOLUME
value: "{{ .Release.Name }}-encryption-key-volume"
- name: CERTS_PATH
value: "{{ .Release.Name }}-encryption-key-volume"
- name: ENCRYPTION_KEY_FILENAME
value: key
{{- if .Values.consoleCert }}
- name: CONSOLE_CERT
value: |
{{ .Values.consoleCert | indent 12 }}
{{- end }}
{{- if .Values.consoleCertKey }}
- name: CONSOLE_CERT_KEY
value: |
{{ .Values.consoleCertKey | indent 12 }}
{{- end }}
image: {{.Values.dockerRegistry}}/{{.Values.dockerOrg}}/{{.Values.images.preflight}}:{{.Values.consoleVersion}}
name: "{{ .Release.Name }}-preflight-job"
volumeMounts:
Expand Down
10 changes: 10 additions & 0 deletions deploy/kubernetes/console/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ images:
postflight: stratos-postflight-job
# Specify which storage class should be used for PVCs
#storageClass: default
#consoleCert: |
# -----BEGIN CERTIFICATE-----
# MIIDXTCCAkWgAwIBAgIJAJooOiQWl1v1MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
# ...
# -----END CERTIFICATE-----
#consoleCertKey: |
# -----BEGIN PRIVATE KEY-----
# MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkdgEAAoIBAQDV9+ySh0xZzM41
# ...
# -----END PRIVATE KEYE-----
# MariaDB chart configuration
mariadb:
# Only required for creating the databases
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/sh

# Settings
devcerts_path=${DEV_CERTS_PATH:-portal-proxy-output/dev-certs}
domain=pproxy
commonname=192.168.99.100
country=US
state=Washington
locality=Seattle
devcerts_path=${CERTS_PATH:-portal-proxy-output/dev-certs}
domain=${DEV_CERTS_DOMAIN:-pproxy}
commonname=127.0.0.1
country=UK
state=Bristol
locality=Bristol
organization=SUSE
organizationalunit=HDP
organizationalunit=CAP
email=SUSE

# Generate a key and cert
Expand Down

0 comments on commit e402466

Please sign in to comment.