jxskiss / simplessl

On the fly SSL certificate issue and renewal inside OpenResty with Let's Encrypt
MIT License
45 stars 13 forks source link
openrestry ssl-certificate

simplessl

On the fly free SSL registration and renewal inside OpenResty/nginx, Envoy and any Golang TLS program, with Let's Encrypt.

The simplessl automatically and transparently issues SSL certificates from Let's Encrypt as requests are received, when a certificate needs a renewal, it automatically renews the certificate asynchronously in background.

The OpenResty plugin uses the ssl_certificate_by_lua functionality in OpenResty 1.9.7.2+.

For Envoy, simplessl implements SDS server to provide SSL certificates to the data plane.

By using simplessl to register SSL certificates with Let's Encrypt, you agree to the Let's Encrypt Subscriber Agreement.

Disclaimer: I got initial inspires and stole some code from the awesome project lua-resty-auto-ssl and Go's autocert package. Also, this program uses Lego to solve dns-01 challenge. Many thanks 😀

Features

  1. Minimal dependency, easy deployment, friendly to distributed environment.
  2. High performance, very low latency added to user requests.
  3. Issue and renew certificate for each domain using http-01 challenge, support OpenResty and Golang.
  4. Issue and renew certificate for each domain using tls-alpn-01 challenge, support Golang.
  5. Issue and renew SAN certificate using tls-alpn-01 challenge, support Golang.
  6. Issue and renew wildcard certificate, using dns-01 challenge, support OpenResty and Golang.
  7. Serve manually-managed certificates.
  8. Serve OCSP stapling, with cache and asynchronous renewal, the latency is negligible.
  9. Generate and serve self-signed certificate.
  10. Graceful restart like Nginx without losing any requests.
  11. Support directory and Redis as cache storage, adding new storage support is easy.

NOTE: currently this program is designed to be used inside intranet, security features are not seriously considered, be sure to PROTECT your certificate server properly and keep an eye on security concerns.

Centric certificate server

Compared to other similar projects, this project provides a centric certificate server to manage all your certificates (both auto issued or manually managed, and self-signed) in one place. The OpenResty plugin and Golang TLS config library acts as client to the server.

By this design, there are several advantages:

  1. Offload ACME related work and communication with storage to the backend Golang server, let Nginx/OpenResty do what it is designed for and best at;
  2. It's more friendly to distributed deployments, one can manage all certificates in a single place, the OpenResty plugin and Golang library deployment is simple and straightforward; you get single certificate for a domain, not as many certificates as your web server instances (as some other similar project does);
  3. Golang program is considered easier to maintain and do troubleshooting than doing ACME work and storage with Lua;
  4. Also, Golang program is considered easier to extend to support new type of storage, or new features (e.g. wildcard certificates, security, etc.);

A multi-layered cache mechanism is used to help frontend Nginx and Golang web servers automatically update to renewed certificates with negligible performance penalty, and without any reloading:

The cached certificates and OCSP staple is automatically renewed and refreshed in backend simplessl server.

Status

Considered BETA.

Although this program has been running for nearly 5 years supporting my personal sites, however this is a spare-time project and has not known deployment for large production systems.

Anyone interested with this is HIGHLY RECOMMENDED to do testing in your environment.

Installation

For OpenResty

The lua library is published with OPM, the following command will install the simplessl library, as well as it's dependency "lua-resty-http".

opm get jxskiss/simplessl

If you do not have opm, you can install the lua libraries manually, take OpenResty installed under "/usr/local/openresty" as example (you may need to use sudo to grant proper permission):

mkdir -p /usr/local/openresty/site/lualib/resty
cd /usr/local/openresty/site/lualib/resty
wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http.lua
wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http_connect.lua
wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http_headers.lua
wget https://raw.githubusercontent.com/jxskiss/simplessl/master/lib/resty/simplessl.lua

For Golang TLS program

go get github.com/jxskiss/simplessl/lib/tlsconfig@latest

See the following doc for example of using lib/tlsconfig.

Run simplessl

Download the cert server service binary file, either build by yourself:

go install github.com/jxskiss/simplessl@latest

or download prebuilt binaries from the release page.

Copy example.conf.yaml to your favorite location and edit it to fit your need. Configuration options are explained in the example file.

Run your cert server:

/path/to/simplessl run -c /path/to/your/conf.yaml

Or to generate a self-signed certificate, see simplessl generate-self-signed -h.

Now you can configure your OpenResty or Golang program to use the cert server for SSL certificates, see the following examples.

Nginx configuration example

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    lua_shared_dict ssl_certs_cache 1m;

    init_by_lua_block {
        -- Define a function to determine which SNI domains to automatically
        -- handle and register new certificates for. Defaults to not allowing
        -- any domain, so this must be configured.
        function allow_domain(domain)
            if domain:find("example.com$") then
                return true
            end
            return false
        end

        -- Initialize backend certificate server instance.
        -- Change lru_maxitems according to your deployment, default 100.
        simplessl = (require "resty.simplessl").new({
            backend = '127.0.0.1:8999',
            allow_domain = allow_domain,
            lru_maxitems = 100,
        })
    }

    # HTTPS Server
    server {
        listen 443 ssl;

        # Works also with non-default HTTPS port.
        listen 8443 ssl;

        server_name hello.example.com;

        # Dynamic handler for issuing or returning certs for SNI domains.
        ssl_certificate_by_lua_block {
            simplessl:ssl_certificate()
        }

        # Fallback certificate required by nginx, self-signed is ok.
        # simplessl generate-self-signed \
        #   -days 3650 \
        #   -cert-out /etc/nginx/certs/fallback-self-signed.crt \
        #   -key-out /etc/nginx/certs/fallback-self-signed.key
        ssl_certificate /etc/nginx/certs/fallback-self-signed.crt;
        ssl_certificate_key /etc/nginx/certs/fallback-self-signed.key;

        location / {
            content_by_lua_block {
                ngx.say("It works!")
            }
        }
    }

    # HTTP Server
    server {
        listen 80;
        server_name hello.example.com;

        # Endpoint used for performing domain verification with Let's Encrypt.
        location /.well-known/acme-challenge/ {
            content_by_lua_block {
                simplessl:challenge_server()
            }
        }
    }

}

Golang lib/tlsconfig

You may use the package lib/tlsconfig to run Golang program with TLS. eg:

func main() {
    handler := func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("It works!"))
    }

    // See doc of tlsconfig.Options for available options.
    tlsConfig := tlsconfig.NewConfig("127.0.0.1:8999", tlsconfig.Options{})
    listener, err := tls.Listen("tcp", ":8443", tlsConfig)
    if err != nil {
        log.Fatal(err)
    }

    http.Serve(listener, http.HandlerFunc(handler))
}

Dependency

Change history

v0.6.2 @ 2022-11-02

v0.6.0 @ 2022-09-03

v0.5.0 @ 2022-06-12

v0.4.3 @ 2022-05-17

v0.4.2 @ 2021-06-02

v0.4.1 @ 2020-08-23

v0.4.0 @ 2020-08-16

Update: this release has known bugs, please upgrade to newer release.

This release is a major change with quite a lot of new features and improvements.

v0.3.0 @ 2020-03-13

v0.2.1 @ 2018-10-10

v0.2.0 @ 2018-08-11

v0.1.2 @ 2018-06-20

v0.1.1 @ 2018-01-06

Initial public release.