2018-03-05 20:54:04 +01:00
package types
import (
"strings"
)
2019-07-19 11:52:04 +02:00
// +k8s:deepcopy-gen=true
2018-11-14 10:18:03 +01:00
// Domain holds a domain name with SANs.
2018-03-05 20:54:04 +01:00
type Domain struct {
2022-06-29 11:04:09 +02:00
// Main defines the main domain name.
Main string ` description:"Default subject name." json:"main,omitempty" toml:"main,omitempty" yaml:"main,omitempty" `
// SANs defines the subject alternative domain names.
2019-07-01 11:30:05 +02:00
SANs [ ] string ` description:"Subject alternative names." json:"sans,omitempty" toml:"sans,omitempty" yaml:"sans,omitempty" `
2018-03-05 20:54:04 +01:00
}
2018-11-14 10:18:03 +01:00
// ToStrArray convert a domain into an array of strings.
2018-03-26 14:12:03 +02:00
func ( d * Domain ) ToStrArray ( ) [ ] string {
var domains [ ] string
if len ( d . Main ) > 0 {
domains = [ ] string { d . Main }
}
return append ( domains , d . SANs ... )
}
2018-11-14 10:18:03 +01:00
// Set sets a domains from an array of strings.
2018-03-26 14:12:03 +02:00
func ( d * Domain ) Set ( domains [ ] string ) {
if len ( domains ) > 0 {
d . Main = domains [ 0 ]
d . SANs = domains [ 1 : ]
}
}
2018-11-14 10:18:03 +01:00
// MatchDomain returns true if a domain match the cert domain.
2020-07-07 14:42:03 +02:00
func MatchDomain ( domain , certDomain string ) bool {
2018-03-27 10:18:03 -04:00
if domain == certDomain {
return true
}
for len ( certDomain ) > 0 && certDomain [ len ( certDomain ) - 1 ] == '.' {
certDomain = certDomain [ : len ( certDomain ) - 1 ]
}
labels := strings . Split ( domain , "." )
for i := range labels {
labels [ i ] = "*"
candidate := strings . Join ( labels , "." )
if certDomain == candidate {
return true
}
}
return false
}
2018-11-14 10:18:03 +01:00
// CanonicalDomain returns a lower case domain with trim space.
func CanonicalDomain ( domain string ) string {
return strings . ToLower ( strings . TrimSpace ( domain ) )
}