Skip to content

Commit 3c866bb

Browse files
committed
add DNS validation for annotation
1 parent 02b8f7f commit 3c866bb

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

kubernetes.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/netip"
88
"strings"
99

10+
"github.com/miekg/dns"
1011
nginx_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
1112
k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned"
1213
core "k8s.io/api/core/v1"
@@ -335,7 +336,11 @@ func serviceHostnameIndexFunc(obj interface{}) ([]string, error) {
335336

336337
hostname := service.Name + "." + service.Namespace
337338
if annotation, exists := service.Annotations[hostnameAnnotationKey]; exists {
338-
hostname = annotation
339+
if _, ok := dns.IsDomainName(annotation); ok {
340+
hostname = strings.ToLower(annotation)
341+
} else {
342+
log.Debugf("Invalid domain name in annotation: %s", annotation)
343+
}
339344
}
340345

341346
log.Debugf("Adding index %s for service %s", hostname, service.Name)

test/service-annotation.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: annotation-good
6+
namespace: default
7+
annotations:
8+
"coredns.io/hostname": "good"
9+
spec:
10+
ipFamilyPolicy: RequireDualStack
11+
ports:
12+
- name: 80-80
13+
port: 80
14+
protocol: TCP
15+
targetPort: 80
16+
selector:
17+
app: backend
18+
sessionAffinity: None
19+
type: LoadBalancer
20+
---
21+
apiVersion: v1
22+
kind: Service
23+
metadata:
24+
name: annotation-bad
25+
namespace: default
26+
annotations:
27+
"coredns.io/hostname": "abcd0123456789012345678901234567890123456789012345678901234567890"
28+
spec:
29+
ipFamilyPolicy: RequireDualStack
30+
ports:
31+
- name: 80-80
32+
port: 80
33+
protocol: TCP
34+
targetPort: 80
35+
selector:
36+
app: backend
37+
sessionAffinity: None
38+
type: LoadBalancer

0 commit comments

Comments
 (0)