-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
|
@@ -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{ | ||
|
@@ -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", | ||
|
@@ -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) | ||
|
@@ -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")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"),
Cache: autocert.DirCache("/var/www/.cache"),
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering, with let's encrypt, do we want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe yes, however you prefer. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?