Skip to content

Commit d154f2d

Browse files
committed
Add IPs to self-baked TLS certs as IPs, not DNS names
Some tests use IP addresses from http test servers for various Pelican server configs (e.g. 'Server.ExternalWebUrl'). When we do this, the certificates Pelican generates for itself are invalid because they contain a SAN that says the IP is a DNS entry. This was exposed in tests for PelicanPlatform#2035 after it picked up the new verification code that checks the cert against configured hostnames.
1 parent 66325f0 commit d154f2d

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

config/init_server_creds.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"fmt"
3131
"io/fs"
3232
"math/big"
33+
"net"
3334
"net/url"
3435
"os"
3536
"os/exec"
@@ -507,15 +508,29 @@ func GenerateCert() error {
507508
BasicConstraintsValid: true,
508509
}
509510

511+
template.DNSNames = []string{}
512+
template.IPAddresses = []net.IP{}
513+
514+
// Some internal tests may not use a hostname, but an IP address like 127.0.0.1 instead
515+
// In this case, the IP address will be used as the SAN but it needs to be added as an IP and
516+
// not a DNS name.
517+
addSAN := func(s string) {
518+
if ip := net.ParseIP(s); ip != nil {
519+
template.IPAddresses = append(template.IPAddresses, ip)
520+
} else {
521+
template.DNSNames = append(template.DNSNames, s)
522+
}
523+
}
524+
510525
// In the course of unit testing (the primary place these self-signed certs are used),
511526
// it may become necessary to mix/match various configurations around the `Server.Hostname`
512527
// and `Server.ExternalWebUrl` parameters. Whenever such a test needs to run config.InitServer(),
513528
// it's necessary that the value of both `Server.Hostname` and `Server.ExternalWebUrl` are baked
514529
// into the cert, and so even if the two don't match we add both to the cert's DNS names.
530+
addSAN(hostname)
515531
externalWebUrl, err := url.Parse(param.Server_ExternalWebUrl.GetString())
516-
template.DNSNames = []string{hostname}
517532
if err == nil && externalWebUrl.Hostname() != hostname {
518-
template.DNSNames = append(template.DNSNames, externalWebUrl.Hostname())
533+
addSAN(externalWebUrl.Hostname())
519534
}
520535

521536
// If there's pre-existing CA certificates, self-sign instead of using the generated CA

0 commit comments

Comments
 (0)