envoy: configure upstream IP SAN match as needed

When building an upstream validation context for a particular URL, check
whether the hostname is an IP address. If so, configure the SAN match to
use type IP_ADDRESS rather than DNS.
This commit is contained in:
Kenneth Jenkins 2023-07-20 14:40:15 -07:00
parent 26bbcdfe07
commit 015d1e1fb1
2 changed files with 24 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"crypto/x509"
"encoding/asn1"
"fmt"
"net"
"net/url"
"regexp"
"strings"
@ -24,6 +25,17 @@ func (b *Builder) buildSubjectAltNameMatcher(
sni = overrideName
}
if net.ParseIP(sni) != nil {
return &envoy_extensions_transport_sockets_tls_v3.SubjectAltNameMatcher{
SanType: envoy_extensions_transport_sockets_tls_v3.SubjectAltNameMatcher_IP_ADDRESS,
Matcher: &envoy_type_matcher_v3.StringMatcher{
MatchPattern: &envoy_type_matcher_v3.StringMatcher_Exact{
Exact: sni,
},
},
}
}
if strings.Contains(sni, "*") {
pattern := regexp.QuoteMeta(sni)
pattern = strings.Replace(pattern, "\\*", ".*", -1)

View file

@ -21,6 +21,18 @@ func TestBuildSubjectAltNameMatcher(t *testing.T) {
"exact": "example.com"
}
}`, b.buildSubjectAltNameMatcher(&url.URL{Host: "example.com:1234"}, ""))
testutil.AssertProtoJSONEqual(t, `{
"sanType": "IP_ADDRESS",
"matcher": {
"exact": "10.0.0.1"
}
}`, b.buildSubjectAltNameMatcher(&url.URL{Host: "10.0.0.1:1234"}, ""))
testutil.AssertProtoJSONEqual(t, `{
"sanType": "IP_ADDRESS",
"matcher": {
"exact": "fd12:3456:789a:1::1"
}
}`, b.buildSubjectAltNameMatcher(&url.URL{Host: "[fd12:3456:789a:1::1]:1234"}, ""))
testutil.AssertProtoJSONEqual(t, `{
"sanType": "DNS",
"matcher": {