Skip to content

Commit

Permalink
Merge pull request #977 from atc0005/i952-lscert-adjust-hostname-vali…
Browse files Browse the repository at this point in the history
…dation-behavior

Update `lscert` hostname validation behavior
  • Loading branch information
atc0005 authored Oct 4, 2024
2 parents 18d4497 + b0da4ac commit fc263b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cmd/lscert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,15 @@ func main() {
)
}

hasLeafCert := certs.HasLeafCert(certChain)
hostnameValidationResult := certs.ValidateHostname(
certChain,
cfg.Server,
cfg.DNSName,
config.IgnoreHostnameVerificationFailureIfEmptySANsListFlag,
certs.CertChainValidationOptions{
IgnoreHostnameVerificationFailureIfEmptySANsList: cfg.IgnoreHostnameVerificationFailureIfEmptySANsList,
IgnoreValidationResultHostname: !cfg.ApplyCertHostnameValidationResults(),
IgnoreValidationResultHostname: !hasLeafCert || cfg.DNSName == "",
},
)

Expand All @@ -259,10 +260,21 @@ func main() {
Msgf("%s validation ignored", hostnameValidationResult.CheckName())

fmt.Printf(
"- %s: %s %s\n",
"- %s: %s %s%s\n",
hostnameValidationResult.ServiceState().Label,
hostnameValidationResult.Status(),
hostnameValidationResult.Overview(),
func() string {
switch {
case hasLeafCert:
return fmt.Sprintf(
"(use %q flag to force evaluation)",
config.DNSNameFlagLong,
)
default:
return "(not supported for this cert type)"
}
}(),
)

default:
Expand Down
37 changes: 37 additions & 0 deletions internal/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,43 @@ func IsExpiringCert(cert *x509.Certificate, ageCritical time.Time, ageWarning ti

}

// HasLeafCert receives a slice of x509 certificates and indicates whether
// any of the certificates in the chain are a leaf certificate.
func HasLeafCert(certChain []*x509.Certificate) bool {
for _, cert := range certChain {
if IsLeafCert(cert, certChain) {
return true
}
}

return false
}

// HasIntermediateCert receives a slice of x509 certificates and indicates
// whether any of the certificates in the chain are an intermediate
// certificate.
func HasIntermediateCert(certChain []*x509.Certificate) bool {
for _, cert := range certChain {
if IsIntermediateCert(cert, certChain) {
return true
}
}

return false
}

// HasRootCert receives a slice of x509 certificates and indicates whether any
// of the certificates in the chain are a root certificate.
func HasRootCert(certChain []*x509.Certificate) bool {
for _, cert := range certChain {
if IsRootCert(cert, certChain) {
return true
}
}

return false
}

// HasExpiredCert receives a slice of x509 certificates and indicates whether
// any of the certificates in the chain have expired.
func HasExpiredCert(certChain []*x509.Certificate) bool {
Expand Down

0 comments on commit fc263b8

Please sign in to comment.