-
Notifications
You must be signed in to change notification settings - Fork 947
feat: support multiple certificates in coder server and helm #4150
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
Changes from all commits
cbfbc79
2d2df29
da652ab
d4f5448
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"runtime" | ||
"strconv" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -240,20 +241,64 @@ func TestServer(t *testing.T) { | |
err := root.ExecuteContext(ctx) | ||
require.Error(t, err) | ||
}) | ||
t.Run("TLSNoCertFile", func(t *testing.T) { | ||
t.Run("TLSInvalid", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
defer cancelFunc() | ||
|
||
root, _ := clitest.New(t, | ||
"server", | ||
"--in-memory", | ||
"--address", ":0", | ||
"--tls-enable", | ||
"--cache-dir", t.TempDir(), | ||
) | ||
err := root.ExecuteContext(ctx) | ||
require.Error(t, err) | ||
cert1Path, key1Path := generateTLSCertificate(t) | ||
cert2Path, key2Path := generateTLSCertificate(t) | ||
|
||
cases := []struct { | ||
name string | ||
args []string | ||
errContains string | ||
}{ | ||
{ | ||
name: "NoCertAndKey", | ||
args: []string{"--tls-enable"}, | ||
errContains: "--tls-cert-file is required when tls is enabled", | ||
}, | ||
{ | ||
name: "NoCert", | ||
args: []string{"--tls-enable", "--tls-key-file", key1Path}, | ||
errContains: "--tls-cert-file and --tls-key-file must be used the same amount of times", | ||
}, | ||
{ | ||
name: "NoKey", | ||
args: []string{"--tls-enable", "--tls-cert-file", cert1Path}, | ||
errContains: "--tls-cert-file and --tls-key-file must be used the same amount of times", | ||
}, | ||
{ | ||
name: "MismatchedCount", | ||
args: []string{"--tls-enable", "--tls-cert-file", cert1Path, "--tls-key-file", key1Path, "--tls-cert-file", cert2Path}, | ||
errContains: "--tls-cert-file and --tls-key-file must be used the same amount of times", | ||
}, | ||
{ | ||
name: "MismatchedCertAndKey", | ||
args: []string{"--tls-enable", "--tls-cert-file", cert1Path, "--tls-key-file", key2Path}, | ||
errContains: "load TLS key pair", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.name, func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
defer cancelFunc() | ||
|
||
args := []string{ | ||
"server", | ||
"--in-memory", | ||
"--address", ":0", | ||
"--cache-dir", t.TempDir(), | ||
} | ||
args = append(args, c.args...) | ||
root, _ := clitest.New(t, args...) | ||
err := root.ExecuteContext(ctx) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, c.errContains) | ||
}) | ||
} | ||
}) | ||
t.Run("TLSValid", func(t *testing.T) { | ||
t.Parallel() | ||
|
@@ -293,6 +338,86 @@ func TestServer(t *testing.T) { | |
cancelFunc() | ||
require.NoError(t, <-errC) | ||
}) | ||
t.Run("TLSValidMultiple", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
defer cancelFunc() | ||
|
||
cert1Path, key1Path := generateTLSCertificate(t, "alpaca.com") | ||
cert2Path, key2Path := generateTLSCertificate(t, "*.llama.com") | ||
root, cfg := clitest.New(t, | ||
"server", | ||
"--in-memory", | ||
"--address", ":0", | ||
"--tls-enable", | ||
"--tls-cert-file", cert1Path, | ||
"--tls-key-file", key1Path, | ||
"--tls-cert-file", cert2Path, | ||
"--tls-key-file", key2Path, | ||
"--cache-dir", t.TempDir(), | ||
) | ||
errC := make(chan error, 1) | ||
go func() { | ||
errC <- root.ExecuteContext(ctx) | ||
}() | ||
accessURL := waitAccessURL(t, cfg) | ||
require.Equal(t, "https", accessURL.Scheme) | ||
originalHost := accessURL.Host | ||
|
||
var ( | ||
expectAddr string | ||
dials int64 | ||
) | ||
client := codersdk.New(accessURL) | ||
client.HTTPClient = &http.Client{ | ||
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. Could add the certs to the client. But not a big deal as you verify hostname later 🤷 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 going to leave this as-is because we will get better test failure messages if the server somehow returns an unexpected certificate |
||
Transport: &http.Transport{ | ||
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
atomic.AddInt64(&dials, 1) | ||
assert.Equal(t, expectAddr, addr) | ||
|
||
host, _, err := net.SplitHostPort(addr) | ||
require.NoError(t, err) | ||
|
||
// Always connect to the accessURL ip:port regardless of | ||
// hostname. | ||
conn, err := tls.Dial(network, originalHost, &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
//nolint:gosec | ||
InsecureSkipVerify: true, | ||
ServerName: host, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We can't call conn.VerifyHostname because it requires | ||
// that the certificates are valid, so we call | ||
// VerifyHostname on the first certificate instead. | ||
require.Len(t, conn.ConnectionState().PeerCertificates, 1) | ||
err = conn.ConnectionState().PeerCertificates[0].VerifyHostname(host) | ||
assert.NoError(t, err, "invalid cert common name") | ||
return conn, nil | ||
}, | ||
}, | ||
} | ||
|
||
// Use the first certificate and hostname. | ||
client.URL.Host = "alpaca.com:443" | ||
expectAddr = "alpaca.com:443" | ||
_, err := client.HasFirstUser(ctx) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 1, atomic.LoadInt64(&dials)) | ||
|
||
// Use the second certificate (wildcard) and hostname. | ||
client.URL.Host = "hi.llama.com:443" | ||
expectAddr = "hi.llama.com:443" | ||
_, err = client.HasFirstUser(ctx) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 2, atomic.LoadInt64(&dials)) | ||
|
||
cancelFunc() | ||
require.NoError(t, <-errC) | ||
}) | ||
// This cannot be ran in parallel because it uses a signal. | ||
//nolint:paralleltest | ||
t.Run("Shutdown", func(t *testing.T) { | ||
|
@@ -480,16 +605,22 @@ func TestServer(t *testing.T) { | |
}) | ||
} | ||
|
||
func generateTLSCertificate(t testing.TB) (certPath, keyPath string) { | ||
func generateTLSCertificate(t testing.TB, commonName ...string) (certPath, keyPath string) { | ||
dir := t.TempDir() | ||
|
||
commonNameStr := "localhost" | ||
if len(commonName) > 0 { | ||
commonNameStr = commonName[0] | ||
} | ||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
require.NoError(t, err) | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Subject: pkix.Name{ | ||
Organization: []string{"Acme Co"}, | ||
CommonName: commonNameStr, | ||
}, | ||
DNSNames: []string{commonNameStr}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(time.Hour * 24 * 180), | ||
|
||
|
@@ -498,6 +629,7 @@ func generateTLSCertificate(t testing.TB) (certPath, keyPath string) { | |
BasicConstraintsValid: true, | ||
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) | ||
require.NoError(t, err) | ||
certFile, err := os.CreateTemp(dir, "") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{{- if .Values.coder.tls.secretName }} | ||
|
||
WARN: coder.tls.secretName is deprecated and will be removed in a future | ||
release. Please use coder.tls.secretNames instead. | ||
{{- end }} | ||
|
||
Enjoy Coder! Please create an issue at https://github.com/coder/coder if you run | ||
into any problems! :) |
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.
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.
I personally think the old message is better, what do you think @kylecarbs