Skip to content

Commit ca8142d

Browse files
authored
Merge pull request #44 from suborbital/connor/tls-options
Add TLS config and port options
2 parents d061e2e + 16726ee commit ca8142d

4 files changed

Lines changed: 59 additions & 20 deletions

File tree

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The included `OptionsModifiers` are:
3636
Option | Description | ENV key
3737
--- | --- | ---
3838
UseDomain(domain string) | Enable LetsEncrypt support with the provided domain name (will serve on :80 and :443 for challenge server and API server). LetsEncrypt is disabled by default. | `VK_DOMAIN`
39+
UseTLSConfig(config *tls.Config) | Enable TLS and use the provided TLS config to serve HTTPS. This will override the `domain` option. | N/A
40+
UseTLSPort(port int) | Choose an HTTPS port on which to serve requests. | `VK_TLS_PORT`
3941
UseHTTPPort(port int) | Choose an HTTP port on which to serve requests. When using TLS, the LetsEncrypt challenge server will run on the configured HTTP port. | `VK_HTTP_PORT`
4042
UseAppName(name string) | When the application starts, `name` will be logged. Empty by default. | `VK_APP_NAME`
4143
UseEnvPrefix(prefix string) | Use `prefix` instead of `VK` for environment variables, for example `APP_HTTP_PORT` instead of `VK_HTTP_PORT`. | N/A

vk/modifiers.go renamed to vk/optionmodifiers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package vk
22

33
import (
4+
"crypto/tls"
5+
46
"github.com/suborbital/vektor/vlog"
57
)
68

@@ -14,6 +16,21 @@ func UseDomain(domain string) OptionsModifier {
1416
}
1517
}
1618

19+
// UseTLSConfig sets a TLS config that will be used for HTTPS
20+
// This will take precedence over the Domain option in all cases
21+
func UseTLSConfig(config *tls.Config) OptionsModifier {
22+
return func(o *Options) {
23+
o.TLSConfig = config
24+
}
25+
}
26+
27+
// UseTLSPort sets the HTTPS port to be used:
28+
func UseTLSPort(port int) OptionsModifier {
29+
return func(o *Options) {
30+
o.TLSPort = port
31+
}
32+
}
33+
1734
// UseHTTPPort sets the HTTP port to be used:
1835
// If domain is set, HTTP port will be used for LetsEncrypt challenge server
1936
// If domain is NOT set, this option will put VK in insecure HTTP mode

vk/options.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vk
22

33
import (
44
"context"
5+
"crypto/tls"
56

67
"github.com/pkg/errors"
78
"github.com/sethvargo/go-envconfig"
@@ -10,10 +11,12 @@ import (
1011

1112
// Options are the available options for Server
1213
type Options struct {
13-
AppName string `env:"_APP_NAME"`
14-
Domain string `env:"_DOMAIN"`
15-
HTTPPort int `env:"_HTTP_PORT"`
16-
EnvPrefix string `env:"-"`
14+
AppName string `env:"_APP_NAME"`
15+
Domain string `env:"_DOMAIN"`
16+
HTTPPort int `env:"_HTTP_PORT"`
17+
TLSPort int `env:"_TLS_PORT"`
18+
TLSConfig *tls.Config `env:"-"`
19+
EnvPrefix string `env:"-"`
1720
Logger *vlog.Logger
1821
}
1922

@@ -35,9 +38,9 @@ func newOptsWithModifiers(mods ...OptionsModifier) *Options {
3538
return options
3639
}
3740

38-
// ShouldUseTLS returns true if domain is set and TLS should be used
41+
// ShouldUseTLS returns true if domain is set and/or TLS is configured
3942
func (o *Options) ShouldUseTLS() bool {
40-
return o.Domain != ""
43+
return o.Domain != "" || o.TLSConfig != nil
4144
}
4245

4346
// HTTPPortSet returns true if the HTTP port is set
@@ -77,4 +80,8 @@ func (o *Options) replaceFieldsIfNeeded(replacement *Options) {
7780
if replacement.HTTPPort != 0 {
7881
o.HTTPPort = replacement.HTTPPort
7982
}
83+
84+
if replacement.TLSPort != 0 {
85+
o.TLSPort = replacement.TLSPort
86+
}
8087
}

vk/server.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,41 @@ func createGoServer(options *Options, handler http.Handler) *http.Server {
6363
}
6464

6565
func goTLSServerWithDomain(options *Options, handler http.Handler) *http.Server {
66-
if options.Domain != "" {
66+
if options.TLSConfig != nil {
67+
options.Logger.Info("configured for HTTPS with custom configuration")
68+
} else if options.Domain != "" {
6769
options.Logger.Info("configured for HTTPS using domain", options.Domain)
6870
}
6971

70-
m := &autocert.Manager{
71-
Cache: autocert.DirCache("~/.autocert"),
72-
Prompt: autocert.AcceptTOS,
73-
HostPolicy: autocert.HostWhitelist(options.Domain),
74-
}
72+
tlsConfig := options.TLSConfig
7573

76-
addr := fmt.Sprintf(":%d", options.HTTPPort)
77-
if options.HTTPPort == 0 {
78-
addr = ":8080"
79-
}
74+
if tlsConfig == nil {
75+
m := &autocert.Manager{
76+
Cache: autocert.DirCache("~/.autocert"),
77+
Prompt: autocert.AcceptTOS,
78+
HostPolicy: autocert.HostWhitelist(options.Domain),
79+
}
80+
81+
addr := fmt.Sprintf(":%d", options.HTTPPort)
82+
if options.HTTPPort == 0 {
83+
addr = ":8080"
84+
}
85+
86+
options.Logger.Info("serving TLS challenges on", addr)
8087

81-
options.Logger.Info("serving TLS challenges on", addr)
88+
go http.ListenAndServe(addr, m.HTTPHandler(nil))
8289

83-
go http.ListenAndServe(addr, m.HTTPHandler(nil))
90+
tlsConfig = &tls.Config{GetCertificate: m.GetCertificate}
91+
}
92+
93+
addr := fmt.Sprintf(":%d", options.TLSPort)
94+
if options.TLSPort == 0 {
95+
addr = ":443"
96+
}
8497

8598
s := &http.Server{
86-
Addr: ":443",
87-
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
99+
Addr: addr,
100+
TLSConfig: tlsConfig,
88101
Handler: handler,
89102
}
90103

0 commit comments

Comments
 (0)