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

Integrated lets encrypt support #1921

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ deps_backend:
go get -u github.com/elazarl/go-bindata-assetfs/...
go get -u github.com/drone/mq/...
go get -u github.com/tidwall/redlog
go get -u golang.org/x/net/context

Choose a reason for hiding this comment

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

can we use context from the stdlib now that drone is using Go 1.8?


gen: gen_template gen_migrations

Expand Down
101 changes: 87 additions & 14 deletions drone/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"crypto/tls"
"fmt"
"net/http"
"time"

Expand All @@ -10,6 +12,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/gin-gonic/contrib/ginrus"
"golang.org/x/crypto/acme/autocert"
)

var serverCmd = cli.Command{
Expand Down Expand Up @@ -47,6 +50,17 @@ var serverCmd = cli.Command{
Name: "server-key",
Usage: "server ssl key",
},
cli.BoolFlag{
EnvVar: "DRONE_LETS_ENCRYPT_ENABLED",
Name: "lets-encrypt-enabled",
Usage: "enable let's encrypt support",
},
cli.StringFlag{
EnvVar: "DRONE_LETS_ENCRYPT_PATH",
Name: "lets-encrypt-path",
Usage: "let's encrypt cert storage path",
Value: "/var/lib/drone/certs",
},
cli.StringSliceFlag{
EnvVar: "DRONE_ADMIN",
Name: "admin",
Expand Down Expand Up @@ -280,7 +294,6 @@ var serverCmd = cli.Command{
}

func server(c *cli.Context) error {

// debug level if requested by user
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
Expand All @@ -300,19 +313,79 @@ func server(c *cli.Context) error {
middleware.Broker(c),
)

// start the server with tls enabled
if c.String("server-cert") != "" {
return http.ListenAndServeTLS(
c.String("server-addr"),
c.String("server-cert"),
c.String("server-key"),
handler,
if c.Bool("lets-encrypt-enabled") || (c.String("server-cert") != "" && c.String("server-key") != "") {
// define proper accepted curves
curves := []tls.CurveID{
tls.CurveP521,
tls.CurveP384,
tls.CurveP256,
}

// define proper accepted ciphers
ciphers := []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
}

cfg := &tls.Config{
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
CurvePreferences: curves,
CipherSuites: ciphers,
}

if c.Bool("lets-encrypt-enabled") {
if c.String("lets-encrypt-path") == "" {
return fmt.Errorf("No Let's Encrypt cert storage path defined")
}

certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(c.String("lets-encrypt-path")),
Copy link

Choose a reason for hiding this comment

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

Missing HostPolicy? The user can set white list for the custom domains.

m := autocert.Manager{
    Prompt:     autocert.AcceptTOS,
    HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"),
    Cache:      autocert.DirCache("/var/www/.cache"),
}

Copy link
Author

Choose a reason for hiding this comment

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

We don't have any attribute for the domain

}

cfg.GetCertificate = certManager.GetCertificate
} else {
cert, err := tls.LoadX509KeyPair(
c.String("server-cert"),
c.String("server-key"),
)

if err != nil {
return fmt.Errorf("Failed to load SSL certificates. %s", err)
}

cfg.Certificates = []tls.Certificate{
cert,
}
}

// define the server configuration
server := &http.Server{
Addr: c.String("server-addr"),

Choose a reason for hiding this comment

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

I'm wondering, with let's encrypt, do we want to use https: as the address, and then start up a second server that listens for http: and auto-redirects to https?

Copy link
Author

Choose a reason for hiding this comment

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

Maybe yes, however you prefer.

Copy link

Choose a reason for hiding this comment

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

Set Addr as ":https" for let's encrypt

Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
TLSConfig: cfg,
}

// start the server with tls enabled
return server.ListenAndServeTLS(
"",
"",
)
}
} else {
// define the server configuration
server := &http.Server{
Addr: c.String("server-addr"),
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

// start the server without tls enabled
return http.ListenAndServe(
c.String("server-addr"),
handler,
)
// start the server without tls enabled
return server.ListenAndServe()
}
}
27 changes: 27 additions & 0 deletions vendor/golang.org/x/crypto/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/crypto/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading