Skip to content
Merged
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
21 changes: 20 additions & 1 deletion lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5312,7 +5312,26 @@ func initSelfSignedHTTPSCert(cfg *servicecfg.Config) (err error) {
}
cfg.Log.Warningf("Generating self-signed key and cert to %v %v.", keyPath, certPath)

creds, err := cert.GenerateSelfSignedCert([]string{cfg.Hostname, "localhost"})
hosts := []string{cfg.Hostname, "localhost"}
var ips []string

// add web public address hosts to self-signed cert
for _, addr := range cfg.Proxy.PublicAddrs {
proxyHost, _, err := net.SplitHostPort(addr.String())
if err != nil {
// log and skip error since this is a nice to have
cfg.Log.Errorf("Error parsing proxy.public_address %v, skipping adding to self-signed cert: %v", addr.String(), err)
Comment thread
stevenGravy marked this conversation as resolved.
continue
}
// If the address is a IP have it added as IP SAN
if ip := net.ParseIP(proxyHost); ip != nil {
Comment thread
zmb3 marked this conversation as resolved.
ips = append(ips, proxyHost)
} else {
hosts = append(hosts, proxyHost)
}
}

creds, err := cert.GenerateSelfSignedCert(hosts, ips)
if err != nil {
return trace.Wrap(err)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/db/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ func (c *testContext) createUserAndRole(ctx context.Context, t *testing.T, userN

// makeTLSConfig returns tls configuration for the test's tls listener.
func (c *testContext) makeTLSConfig(t *testing.T) *tls.Config {
creds, err := cert.GenerateSelfSignedCert([]string{"localhost"})
creds, err := cert.GenerateSelfSignedCert([]string{"localhost"}, []string{})
Comment thread
stevenGravy marked this conversation as resolved.
Comment thread
stevenGravy marked this conversation as resolved.
require.NoError(t, err)
cert, err := tls.X509KeyPair(creds.Cert, creds.PrivateKey)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion lib/teleterm/gateway/db_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func TestDBMiddleware_OnNewConnection(t *testing.T) {
testCert, err := cert.GenerateSelfSignedCert([]string{"localhost"})
testCert, err := cert.GenerateSelfSignedCert([]string{"localhost"}, nil)
require.NoError(t, err)
tlsCert, err := keys.X509KeyPair(testCert.Cert, testCert.PrivateKey)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion lib/teleterm/grpccredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func generateAndSaveCert(targetPath string) (tls.Certificate, error) {
}
defer os.Remove(tempFile.Name())

cert, err := cert.GenerateSelfSignedCert([]string{"localhost"})
cert, err := cert.GenerateSelfSignedCert([]string{"localhost"}, nil)
if err != nil {
return tls.Certificate{}, trace.Wrap(err, "failed to generate the certificate")
}
Expand Down
10 changes: 9 additions & 1 deletion lib/utils/cert/selfsigned.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Credentials struct {

// GenerateSelfSignedCert generates a self-signed certificate that
// is valid for given domain names and ips, returns PEM-encoded bytes with key and cert
func GenerateSelfSignedCert(hostNames []string) (*Credentials, error) {
func GenerateSelfSignedCert(hostNames []string, ipAddresses []string) (*Credentials, error) {
priv, err := native.GenerateRSAPrivateKey()
if err != nil {
return nil, trace.Wrap(err)
Expand Down Expand Up @@ -87,6 +87,14 @@ func GenerateSelfSignedCert(hostNames []string) (*Credentials, error) {
if ips != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: It'd be tempted to rename ips to loopbackIPs or similar - just so its clearly distinct from ipAddresses in purpose.

template.IPAddresses = append(ips, net.ParseIP("::1"))
}
for _, ip := range ipAddresses {
ipParsed := net.ParseIP(ip)
if ipParsed == nil {
return nil, trace.Errorf("Unable to parse IP address for self-signed certificate (%v)", ip)
}
template.IPAddresses = append(template.IPAddresses, ipParsed)
}

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestHostUUIDRegenerateEmpty(t *testing.T) {
func TestSelfSignedCert(t *testing.T) {
t.Parallel()

creds, err := cert.GenerateSelfSignedCert([]string{"example.com"})
creds, err := cert.GenerateSelfSignedCert([]string{"example.com"}, []string{})
require.NoError(t, err)
require.NotNil(t, creds)
require.Equal(t, 4, len(creds.PublicKey)/100)
Expand Down