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

Add -dev-tls-san flag #22657

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions changelog/22657.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
command/server: add `-dev-tls-san` flag to configure subject alternative names for the certificate generated when using `-dev-tls`.
```
15 changes: 14 additions & 1 deletion command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type ServerCommand struct {
flagDev bool
flagDevTLS bool
flagDevTLSCertDir string
flagDevTLSSans []string
flagDevRootTokenID string
flagDevListenAddr string
flagDevNoStoreToken bool
Expand Down Expand Up @@ -256,6 +257,18 @@ func (c *ServerCommand) Flags() *FlagSets {
"specified. If left unset, files are generated in a temporary directory.",
})

f.StringSliceVar(&StringSliceVar{
Name: "dev-tls-san",
Target: &c.flagDevTLSSans,
Default: nil,
Usage: "Additional Subject Alternative Name (as a DNS name or IP address) " +
"to generate the certificate with if `-dev-tls` is specified. The " +
"certificate will always use localhost, localhost4, localhost6, " +
"localhost.localdomain, and the host name as alternate DNS names, " +
"and 127.0.0.1 as an alternate IP address. This flag can be specified " +
"multiple times to specify multiple SANs",
})

f.StringVar(&StringVar{
Name: "dev-root-token-id",
Target: &c.flagDevRootTokenID,
Expand Down Expand Up @@ -971,7 +984,7 @@ func configureDevTLS(c *ServerCommand) (func(), *server.Config, string, error) {
return nil, nil, certDir, err
}
}
config, err = server.DevTLSConfig(devStorageType, certDir)
config, err = server.DevTLSConfig(devStorageType, certDir, c.flagDevTLSSans)
Copy link
Contributor

Choose a reason for hiding this comment

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

One comment if you've got a minute: do you want to add the listen address here as well? I think it'd help close #18259, though we could always require the explicit SAN if we preferred... Just thinking the UX might be nice of the address+dev-tls (without dev-tls-san), but my 2c. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea! Added in 3deacb8


f = func() {
if err := os.Remove(fmt.Sprintf("%s/%s", certDir, server.VaultDevCAFilename)); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ ui = true
}

// DevTLSConfig is a Config that is used for dev tls mode of Vault.
func DevTLSConfig(storageType, certDir string) (*Config, error) {
func DevTLSConfig(storageType, certDir string, extraSans []string) (*Config, error) {
ca, err := GenerateCA()
if err != nil {
return nil, err
}

cert, key, err := GenerateCert(ca.Template, ca.Signer)
cert, key, err := generateCert(ca.Template, ca.Signer, extraSans)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions command/server/tls_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type CaCert struct {
Signer crypto.Signer
}

// GenerateCert creates a new leaf cert from provided CA template and signer
func GenerateCert(caCertTemplate *x509.Certificate, caSigner crypto.Signer) (string, string, error) {
// generateCert creates a new leaf cert from provided CA template and signer
func generateCert(caCertTemplate *x509.Certificate, caSigner crypto.Signer, extraSans []string) (string, string, error) {
// Create the private key
signer, keyPEM, err := privateKey()
if err != nil {
Expand Down Expand Up @@ -80,6 +80,13 @@ func GenerateCert(caCertTemplate *x509.Certificate, caSigner crypto.Signer) (str
if !foundHostname {
template.DNSNames = append(template.DNSNames, hostname)
}
for _, san := range extraSans {
if ip := net.ParseIP(san); ip != nil {
tomhjp marked this conversation as resolved.
Show resolved Hide resolved
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, san)
}
}

bs, err := x509.CreateCertificate(
rand.Reader, &template, caCertTemplate, signer.Public(), caSigner)
Expand Down
80 changes: 80 additions & 0 deletions command/server/tls_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package server

import (
"crypto/x509"
"encoding/pem"
"testing"

"github.com/hashicorp/go-secure-stdlib/strutil"
)

// TestGenerateCertExtraSans ensures the implementation backing the flag
// -dev-tls-san populates alternate DNS and IP address names in the generated
// certificate as expected.
func TestGenerateCertExtraSans(t *testing.T) {
ca, err := GenerateCA()
if err != nil {
t.Fatal(err)
}

for name, tc := range map[string]struct {
extraSans []string
expectedDNSNames []string
expectedIPAddresses []string
}{
"empty": {},
"DNS names": {
extraSans: []string{"foo", "foo.bar"},
expectedDNSNames: []string{"foo", "foo.bar"},
},
"IP addresses": {
extraSans: []string{"0.0.0.0", "::1"},
expectedIPAddresses: []string{"0.0.0.0", "::1"},
},
"mixed": {
extraSans: []string{"bar", "0.0.0.0", "::1"},
expectedDNSNames: []string{"bar"},
expectedIPAddresses: []string{"0.0.0.0", "::1"},
},
} {
t.Run(name, func(t *testing.T) {
certStr, _, err := generateCert(ca.Template, ca.Signer, tc.extraSans)
if err != nil {
t.Fatal(err)
}

block, _ := pem.Decode([]byte(certStr))
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
t.Fatal(err)
}

expectedDNSNamesLen := len(tc.expectedDNSNames) + 5
if len(cert.DNSNames) != expectedDNSNamesLen {
t.Errorf("Wrong number of DNS names, expected %d but got %v", expectedDNSNamesLen, cert.DNSNames)
}
expectedIPAddrLen := len(tc.expectedIPAddresses) + 1
if len(cert.IPAddresses) != expectedIPAddrLen {
t.Errorf("Wrong number of IP addresses, expected %d but got %v", expectedIPAddrLen, cert.IPAddresses)
}

for _, expected := range tc.expectedDNSNames {
if !strutil.StrListContains(cert.DNSNames, expected) {
t.Errorf("Missing DNS name %s", expected)
}
}
for _, expected := range tc.expectedIPAddresses {
var found bool
for _, ip := range cert.IPAddresses {
if ip.String() == expected {
found = true
break
}
}
if !found {
t.Errorf("Missing IP address %s", expected)
}
}
})
}
}