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

Added support for custom SSL certificate #969

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kpumuk
Copy link
Contributor

@kpumuk kpumuk commented Sep 24, 2024

Following the change in kamal-proxy, this MR introduces a configuration option to load custom SSL certificate and the corresponding private key from disk:

proxy:
  ssl: true
  ssl_certificate_path: /data/cert/foo.example.com/fullchain.pem
  ssl_private_key_path: /data/cert/foo.example.com/privkey.pem

Documentation preview:

image

@yogeshjain999
Copy link

I'm a newbie to kamal (and docker) and was wondering what's the recommended way to get those custom PEM files on server.

Copy using kamal+Dockerfile or do it manually ? Maybe better alternative would be to load the values via ENV variables (given kamal-proxy could support it) ?

@kpumuk
Copy link
Contributor Author

kpumuk commented Sep 30, 2024

I'm a newbie to kamal (and docker) and was wondering what's the recommended way to get those custom PEM files on server.

The easiest way would be to use pre-proxy-reboot. For example, if you use 1Password to manage secrets:

  1. Put both cert.pem and key.pem under the item you use for secrets
  2. Create .kamal/hooks/pre-proxy-reboot:
    #!/bin/sh
    
    set -euo pipefail
    
    KAMAL_PROXY_TLS_CERT=$(op read "op://Private/Kamal Demo/cert.pem")
    KAMAL_PROXY_TLS_PRIVATE_KEY=$(op read "op://Private/Kamal Demo/key.pem")
    
    for ip in ${KAMAL_HOSTS//,/ }; do
      ssh -q -T -o BatchMode=yes ubuntu@"${ip}" bash --noprofile <<-EOF
        mkdir -p .kamal/apps/${KAMAL_SERVICE}/tls
        echo '${KAMAL_PROXY_TLS_CERT}' > .kamal/apps/${KAMAL_SERVICE}/tls/cert.pem
        echo "${KAMAL_PROXY_TLS_PRIVATE_KEY}" > .kamal/apps/${KAMAL_SERVICE}/tls/key.pem
    EOF
    done
  3. Edit config/deploy.yml to mount TLS certificates to Kamal's image and then enable them:
    proxy:
      ssl: true
      host: app.example.com
      ssl_certificate_path: /home/kamal-proxy/.config/certs/cert.pem
      ssl_private_key_path: /home/kamal-proxy/.config/certs/key.pem
      volumes:
        - "/home/ubuntu/.kamal/apps/demo/certs:/home/kamal-proxy/.config/certs"
  4. Run kamal proxy reboot to deploy

Copy using kamal+Dockerfile or do it manually ? Maybe better alternative would be to load the values via ENV variables (given kamal-proxy could support it) ?

kamal-proxy does not support environment variables.

TODO: Add volumes support to proxy.

@yogeshjain999
Copy link

Cool, setting it up via pre-proxy-reboot sounds good approach. Thanks!!

@kpumuk
Copy link
Contributor Author

kpumuk commented Oct 2, 2024

@djmb to follow up on the thread in kamal-proxy, here is the documentation update + support for the recent custom TLS cert changes.

@agu-z
Copy link

agu-z commented Oct 9, 2024

Is it possible to specify a client certificate too? I need this in order to enable CloudFlare's Authenticated Origin Pulls

@mtmckenna
Copy link

With this change, would it be possible to remove the ensure_one_host_for_ssl requirement when providing a cert and key?

Context: I'm looking for a way to have end-to-end in-transit encryption in a regulated environment that requires TLS between the load balancer and server node. I'd also like to be able to use multiple app servers.

By providing my own cert to kamal-proxy, I was thinking I should be able to have the load balancer terminate SSL and then re-encrypt the traffic to kamal-proxy, which would use my supplied cert. wdyt?

Thank you!

@kpumuk
Copy link
Contributor Author

kpumuk commented Oct 14, 2024

By providing my own cert to kamal-proxy, I was thinking I should be able to have the load balancer terminate SSL and then re-encrypt the traffic to kamal-proxy, which would use my supplied cert. wdyt?

Yep, that's exactly how it would work with custom certificates, and it removes the limitation of one host behind the load balancer.

@mtmckenna
Copy link

Yep, that's exactly how it would work with custom certificates, and it removes the limitation of one host behind the load balancer.

That's great! Would ensure_one_host_for_ssl be updated in a separate PR?

def ensure_one_host_for_ssl

Thank you!

@kpumuk
Copy link
Contributor Author

kpumuk commented Oct 14, 2024

That's great! Would ensure_one_host_for_ssl be updated in a separate PR?

Great catch, I missed this change in the upstream :-) Added a commit to this MR

@qinmingyuan
Copy link

qinmingyuan commented Oct 17, 2024

I have used with wildcard domain and multi hosts, Works fine. Please merge this PR.

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

Successfully merging this pull request may close these issues.

5 participants