2022-01-17 22:36:13 +00:00
|
|
|
package policy
|
2022-01-03 11:25:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-27 16:18:33 +00:00
|
|
|
"golang.org/x/net/idna"
|
2022-01-03 11:25:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NamePolicyOption func(e *NamePolicyEngine) error
|
|
|
|
|
|
|
|
// TODO: wrap (more) errors; and prove a set of known (exported) errors
|
|
|
|
|
2022-01-17 21:49:47 +00:00
|
|
|
func WithSubjectCommonNameVerification() NamePolicyOption {
|
2022-01-03 14:32:58 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
e.verifySubjectCommonName = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:49:47 +00:00
|
|
|
func WithAllowLiteralWildcardNames() NamePolicyOption {
|
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
e.allowLiteralWildcardNames = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:49:23 +00:00
|
|
|
func WithPermittedCommonNames(commonNames ...string) NamePolicyOption {
|
|
|
|
return func(g *NamePolicyEngine) error {
|
2022-05-06 11:58:48 +00:00
|
|
|
normalizedCommonNames := make([]string, len(commonNames))
|
|
|
|
for i, commonName := range commonNames {
|
|
|
|
normalizedCommonName, err := normalizeAndValidateCommonName(commonName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse permitted common name constraint %q: %w", commonName, err)
|
|
|
|
}
|
|
|
|
normalizedCommonNames[i] = normalizedCommonName
|
|
|
|
}
|
|
|
|
g.permittedCommonNames = normalizedCommonNames
|
2022-04-28 12:49:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExcludedCommonNames(commonNames ...string) NamePolicyOption {
|
|
|
|
return func(g *NamePolicyEngine) error {
|
2022-05-06 11:58:48 +00:00
|
|
|
normalizedCommonNames := make([]string, len(commonNames))
|
|
|
|
for i, commonName := range commonNames {
|
|
|
|
normalizedCommonName, err := normalizeAndValidateCommonName(commonName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse excluded common name constraint %q: %w", commonName, err)
|
|
|
|
}
|
|
|
|
normalizedCommonNames[i] = normalizedCommonName
|
|
|
|
}
|
|
|
|
g.excludedCommonNames = normalizedCommonNames
|
2022-04-28 12:49:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedDNSDomains(domains ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedDomains := make([]string, len(domains))
|
|
|
|
for i, domain := range domains {
|
|
|
|
normalizedDomain, err := normalizeAndValidateDNSDomainConstraint(domain)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse permitted domain constraint %q: %w", domain, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedDomains[i] = normalizedDomain
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.permittedDNSDomains = normalizedDomains
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedDNSDomains(domains ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedDomains := make([]string, len(domains))
|
|
|
|
for i, domain := range domains {
|
|
|
|
normalizedDomain, err := normalizeAndValidateDNSDomainConstraint(domain)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse excluded domain constraint %q: %w", domain, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedDomains[i] = normalizedDomain
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.excludedDNSDomains = normalizedDomains
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedIPRanges(ipRanges ...*net.IPNet) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
e.permittedIPRanges = ipRanges
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedCIDRs(cidrs ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
networks := make([]*net.IPNet, len(cidrs))
|
|
|
|
for i, cidr := range cidrs {
|
2022-01-03 11:25:24 +00:00
|
|
|
_, nw, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse permitted CIDR constraint %q", cidr)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
networks[i] = nw
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
e.permittedIPRanges = networks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedCIDRs(cidrs ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
networks := make([]*net.IPNet, len(cidrs))
|
|
|
|
for i, cidr := range cidrs {
|
2022-01-03 11:25:24 +00:00
|
|
|
_, nw, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse excluded CIDR constraint %q", cidr)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
networks[i] = nw
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
e.excludedIPRanges = networks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedIPsOrCIDRs(ipsOrCIDRs ...string) NamePolicyOption {
|
2022-01-18 13:39:21 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
networks := make([]*net.IPNet, len(ipsOrCIDRs))
|
|
|
|
for i, ipOrCIDR := range ipsOrCIDRs {
|
|
|
|
_, nw, err := net.ParseCIDR(ipOrCIDR)
|
|
|
|
if err == nil {
|
|
|
|
networks[i] = nw
|
|
|
|
} else if ip := net.ParseIP(ipOrCIDR); ip != nil {
|
|
|
|
networks[i] = networkFor(ip)
|
|
|
|
} else {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse permitted constraint %q as IP nor CIDR", ipOrCIDR)
|
2022-01-18 13:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e.permittedIPRanges = networks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedIPsOrCIDRs(ipsOrCIDRs ...string) NamePolicyOption {
|
2022-01-18 13:39:21 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
networks := make([]*net.IPNet, len(ipsOrCIDRs))
|
|
|
|
for i, ipOrCIDR := range ipsOrCIDRs {
|
|
|
|
_, nw, err := net.ParseCIDR(ipOrCIDR)
|
|
|
|
if err == nil {
|
|
|
|
networks[i] = nw
|
|
|
|
} else if ip := net.ParseIP(ipOrCIDR); ip != nil {
|
|
|
|
networks[i] = networkFor(ip)
|
|
|
|
} else {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse excluded constraint %q as IP nor CIDR", ipOrCIDR)
|
2022-01-18 13:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e.excludedIPRanges = networks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedIPRanges(ipRanges ...*net.IPNet) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
|
|
|
e.excludedIPRanges = ipRanges
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedEmailAddresses(emailAddresses ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedEmailAddresses := make([]string, len(emailAddresses))
|
|
|
|
for i, email := range emailAddresses {
|
|
|
|
normalizedEmailAddress, err := normalizeAndValidateEmailConstraint(email)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse permitted email constraint %q: %w", email, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedEmailAddresses[i] = normalizedEmailAddress
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.permittedEmailAddresses = normalizedEmailAddresses
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedEmailAddresses(emailAddresses ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedEmailAddresses := make([]string, len(emailAddresses))
|
|
|
|
for i, email := range emailAddresses {
|
|
|
|
normalizedEmailAddress, err := normalizeAndValidateEmailConstraint(email)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse excluded email constraint %q: %w", email, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedEmailAddresses[i] = normalizedEmailAddress
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.excludedEmailAddresses = normalizedEmailAddresses
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedURIDomains(uriDomains ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedURIDomains := make([]string, len(uriDomains))
|
|
|
|
for i, domain := range uriDomains {
|
|
|
|
normalizedURIDomain, err := normalizeAndValidateURIDomainConstraint(domain)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse permitted URI domain constraint %q: %w", domain, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedURIDomains[i] = normalizedURIDomain
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.permittedURIDomains = normalizedURIDomains
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedURIDomains(domains ...string) NamePolicyOption {
|
2022-01-03 11:25:24 +00:00
|
|
|
return func(e *NamePolicyEngine) error {
|
2022-04-04 13:31:28 +00:00
|
|
|
normalizedURIDomains := make([]string, len(domains))
|
|
|
|
for i, domain := range domains {
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedURIDomain, err := normalizeAndValidateURIDomainConstraint(domain)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return fmt.Errorf("cannot parse excluded URI domain constraint %q: %w", domain, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
normalizedURIDomains[i] = normalizedURIDomain
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
e.excludedURIDomains = normalizedURIDomains
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithPermittedPrincipals(principals ...string) NamePolicyOption {
|
2022-01-17 22:36:13 +00:00
|
|
|
return func(g *NamePolicyEngine) error {
|
|
|
|
g.permittedPrincipals = principals
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:14:03 +00:00
|
|
|
func WithExcludedPrincipals(principals ...string) NamePolicyOption {
|
2022-01-17 22:36:13 +00:00
|
|
|
return func(g *NamePolicyEngine) error {
|
|
|
|
g.excludedPrincipals = principals
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:39:21 +00:00
|
|
|
func networkFor(ip net.IP) *net.IPNet {
|
|
|
|
var mask net.IPMask
|
|
|
|
if !isIPv4(ip) {
|
|
|
|
mask = net.CIDRMask(128, 128)
|
|
|
|
} else {
|
|
|
|
mask = net.CIDRMask(32, 32)
|
|
|
|
}
|
|
|
|
nw := &net.IPNet{
|
|
|
|
IP: ip,
|
|
|
|
Mask: mask,
|
|
|
|
}
|
|
|
|
return nw
|
|
|
|
}
|
|
|
|
|
|
|
|
func isIPv4(ip net.IP) bool {
|
|
|
|
return ip.To4() != nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 11:58:48 +00:00
|
|
|
func normalizeAndValidateCommonName(constraint string) (string, error) {
|
|
|
|
normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
|
|
|
|
if normalizedConstraint == "" {
|
|
|
|
return "", fmt.Errorf("contraint %q can not be empty or white space string", constraint)
|
|
|
|
}
|
|
|
|
if normalizedConstraint == "*" {
|
|
|
|
return "", fmt.Errorf("wildcard constraint %q is not supported", constraint)
|
|
|
|
}
|
|
|
|
return normalizedConstraint, nil
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:49:47 +00:00
|
|
|
func normalizeAndValidateDNSDomainConstraint(constraint string) (string, error) {
|
2022-01-18 13:39:21 +00:00
|
|
|
normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
|
2022-01-27 16:18:33 +00:00
|
|
|
if normalizedConstraint == "" {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("contraint %q can not be empty or white space string", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
if strings.Contains(normalizedConstraint, "..") {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("domain constraint %q cannot have empty labels", constraint)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-04-04 13:31:28 +00:00
|
|
|
if strings.HasPrefix(normalizedConstraint, ".") {
|
|
|
|
return "", fmt.Errorf("domain constraint %q with wildcard should start with *", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
|
|
|
if strings.LastIndex(normalizedConstraint, "*") > 0 {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("domain constraint %q can only have wildcard as starting character", constraint)
|
|
|
|
}
|
2022-04-21 10:14:03 +00:00
|
|
|
if len(normalizedConstraint) >= 2 && normalizedConstraint[0] == '*' && normalizedConstraint[1] != '.' {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("wildcard character in domain constraint %q can only be used to match (full) labels", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
if strings.HasPrefix(normalizedConstraint, "*.") {
|
|
|
|
normalizedConstraint = normalizedConstraint[1:] // cut off wildcard character; keep the period
|
|
|
|
}
|
2022-01-27 16:18:33 +00:00
|
|
|
normalizedConstraint, err := idna.Lookup.ToASCII(normalizedConstraint)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("domain constraint %q can not be converted to ASCII: %w", constraint, err)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
if _, ok := domainToReverseLabels(normalizedConstraint); !ok {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("cannot parse domain constraint %q", constraint)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
return normalizedConstraint, nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:49:47 +00:00
|
|
|
func normalizeAndValidateEmailConstraint(constraint string) (string, error) {
|
2022-01-18 13:39:21 +00:00
|
|
|
normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
|
2022-01-27 16:18:33 +00:00
|
|
|
if normalizedConstraint == "" {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("email contraint %q can not be empty or white space string", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
if strings.Contains(normalizedConstraint, "*") {
|
2022-01-27 16:18:33 +00:00
|
|
|
return "", fmt.Errorf("email constraint %q cannot contain asterisk wildcard", constraint)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
if strings.Count(normalizedConstraint, "@") > 1 {
|
|
|
|
return "", fmt.Errorf("email constraint %q contains too many @ characters", constraint)
|
|
|
|
}
|
|
|
|
if normalizedConstraint[0] == '@' {
|
|
|
|
normalizedConstraint = normalizedConstraint[1:] // remove the leading @ as wildcard for emails
|
|
|
|
}
|
|
|
|
if normalizedConstraint[0] == '.' {
|
|
|
|
return "", fmt.Errorf("email constraint %q cannot start with period", constraint)
|
|
|
|
}
|
|
|
|
if strings.Contains(normalizedConstraint, "@") {
|
2022-01-27 16:18:33 +00:00
|
|
|
mailbox, ok := parseRFC2821Mailbox(normalizedConstraint)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("cannot parse email constraint %q as RFC 2821 mailbox", constraint)
|
|
|
|
}
|
|
|
|
// According to RFC 5280, section 7.5, emails are considered to match if the local part is
|
|
|
|
// an exact match and the host (domain) part matches the ASCII representation (case-insensitive):
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc5280#section-7.5
|
|
|
|
domainASCII, err := idna.Lookup.ToASCII(mailbox.domain)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("email constraint %q domain part %q cannot be converted to ASCII: %w", constraint, mailbox.domain, err)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
|
|
|
normalizedConstraint = mailbox.local + "@" + domainASCII
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
normalizedConstraint, err = idna.Lookup.ToASCII(normalizedConstraint)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("email constraint %q cannot be converted to ASCII: %w", constraint, err)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
if _, ok := domainToReverseLabels(normalizedConstraint); !ok {
|
|
|
|
return "", fmt.Errorf("cannot parse email domain constraint %q", constraint)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
return normalizedConstraint, nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:49:47 +00:00
|
|
|
func normalizeAndValidateURIDomainConstraint(constraint string) (string, error) {
|
2022-01-18 13:39:21 +00:00
|
|
|
normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
|
2022-01-27 16:18:33 +00:00
|
|
|
if normalizedConstraint == "" {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain contraint %q cannot be empty or white space string", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
2022-01-31 14:34:02 +00:00
|
|
|
if strings.Contains(normalizedConstraint, "://") {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q contains scheme (not supported yet)", constraint)
|
2022-01-31 14:34:02 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
if strings.Contains(normalizedConstraint, "..") {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q cannot have empty labels", constraint)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(normalizedConstraint, ".") {
|
|
|
|
return "", fmt.Errorf("URI domain constraint %q with wildcard should start with *", constraint)
|
|
|
|
}
|
|
|
|
if strings.LastIndex(normalizedConstraint, "*") > 0 {
|
|
|
|
return "", fmt.Errorf("URI domain constraint %q can only have wildcard as starting character", constraint)
|
2022-01-17 21:49:47 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(normalizedConstraint, "*.") {
|
|
|
|
normalizedConstraint = normalizedConstraint[1:] // cut off wildcard character; keep the period
|
|
|
|
}
|
2022-01-27 16:18:33 +00:00
|
|
|
// we're being strict with square brackets in domains; we don't allow them, no matter what
|
|
|
|
if strings.Contains(normalizedConstraint, "[") || strings.Contains(normalizedConstraint, "]") {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q contains invalid square brackets", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
|
|
|
if _, _, err := net.SplitHostPort(normalizedConstraint); err == nil {
|
|
|
|
// a successful split (likely) with host and port; we don't currently allow ports in the config
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q cannot contain port", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
|
|
|
// check if the host part of the URI domain constraint is an IP
|
|
|
|
if net.ParseIP(normalizedConstraint) != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q cannot be an IP", constraint)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
|
|
|
normalizedConstraint, err := idna.Lookup.ToASCII(normalizedConstraint)
|
|
|
|
if err != nil {
|
2022-04-04 13:31:28 +00:00
|
|
|
return "", fmt.Errorf("URI domain constraint %q cannot be converted to ASCII: %w", constraint, err)
|
2022-01-27 16:18:33 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
_, ok := domainToReverseLabels(normalizedConstraint)
|
2022-01-03 11:25:24 +00:00
|
|
|
if !ok {
|
2022-01-17 21:49:47 +00:00
|
|
|
return "", fmt.Errorf("cannot parse URI domain constraint %q", constraint)
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
2022-01-17 21:49:47 +00:00
|
|
|
return normalizedConstraint, nil
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|