Skip to content

Commit 834a6e1

Browse files
committed
NE-1476 - Added network policies for DNS
Added the framework for network policies for DNS for the operator and the dns pods. The operator has a deny all network policy that for the openshift-dns-operator namespace and an allow policy for egress to the apiserver and dns ports at any IP. The operator installs a deny all network policy for the openshift-dns namespace. Then for each dns that it manages it installs an allow policy for ingress for dns traffic and metrics. It has to allow ingress from the dns pods to any IP because we allow configuration to set the upstream server and port, so any valid IP and port needs to be allowed. It also needs access to the api server, but that is covered by the wildcard allow policy. https://issues.redhat.com/browse/NE-1476
1 parent 48ebc12 commit 834a6e1

15 files changed

+344
-3
lines changed

hack/release-local.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ if [[ "${TEMP_COMMIT}" == "true" ]]; then
2929
fi
3030

3131
cp -R manifests/* $MANIFESTS
32-
cat manifests/0000_70_dns-operator_02-deployment.yaml | sed "s~openshift/origin-cluster-dns-operator:latest~$REPO:$REV~" > "$MANIFESTS/0000_70_dns-operator_02-deployment.yaml"
32+
cat manifests/0000_70_dns-operator_03-deployment.yaml | sed "s~openshift/origin-cluster-dns-operator:latest~$REPO:$REV~" > "$MANIFESTS/0000_70_dns-operator_03-deployment.yaml"
3333
# To simulate CVO, ClusterOperator resource need to be created by the operator.
34-
rm $MANIFESTS/0000_70_dns-operator_03-cluster-operator.yaml
34+
rm $MANIFESTS/0000_70_dns-operator_04-cluster-operator.yaml
3535

3636
echo "Pushed $REPO:$REV"
3737
echo "Install manifests using:"

manifests/0000_70_dns-operator_00-cluster-role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,11 @@ rules:
139139
- get
140140
- update
141141
- patch
142+
143+
- apiGroups:
144+
- networking.k8s.io
145+
resources:
146+
- networkpolicies
147+
verbs:
148+
- "*"
149+

manifests/0000_70_dns-operator_01-role.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ rules:
2727
- services
2828
verbs:
2929
- "*"
30+
31+
- apiGroups:
32+
- networking.k8s.io
33+
resources:
34+
- networkpolicies
35+
verbs:
36+
- "*"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# We control the namespace, deny anything we do not explicitly allow
2+
apiVersion: networking.k8s.io/v1
3+
kind: NetworkPolicy
4+
metadata:
5+
name: dns-operator-deny-all
6+
namespace: openshift-dns-operator
7+
annotations:
8+
include.release.openshift.io/self-managed-high-availability: "true"
9+
include.release.openshift.io/single-node-developer: "true"
10+
spec:
11+
podSelector: {}
12+
policyTypes:
13+
- Ingress
14+
- Egress
15+
---
16+
### Allow the operators to talk to the apiserver and dns
17+
### Allow access to the metrics ports on the operators
18+
apiVersion: networking.k8s.io/v1
19+
kind: NetworkPolicy
20+
metadata:
21+
name: dns-operator-allow
22+
namespace: openshift-dns-operator
23+
annotations:
24+
include.release.openshift.io/self-managed-high-availability: "true"
25+
include.release.openshift.io/single-node-developer: "true"
26+
spec:
27+
podSelector:
28+
matchLabels:
29+
name: dns-operator
30+
policyTypes:
31+
- Egress
32+
- Ingress
33+
egress:
34+
- ports:
35+
- protocol: TCP
36+
port: 6443
37+
- protocol: TCP
38+
port: 53
39+
- protocol: UDP
40+
port: 53
41+
ingress:
42+
- ports:
43+
- protocol: TCP
44+
port: 9393
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# DNS Pods
2+
# Egress to api server and upstream dns (can be wildcarded, so allow any egress)
3+
# Ingress to dns on port 5353 (TCP and UDP) and to metrics (9154)
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
# name, namespace,labels and annotations are set at runtime
7+
spec:
8+
podSelector:
9+
# matchLabels are set at runtime
10+
matchLabels: {}
11+
ingress:
12+
- ports:
13+
- protocol: TCP
14+
port: 9154
15+
- protocol: UDP
16+
port: 5353
17+
- protocol: TCP
18+
port: 5353
19+
- protocol: TCP
20+
port: 8080
21+
- protocol: TCP
22+
port: 8181
23+
egress:
24+
- to:
25+
- ipBlock:
26+
cidr: 0.0.0.0/0
27+
policyTypes:
28+
- Ingress
29+
- Egress
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Default deny all policy for all pods in the namespace
2+
apiVersion: networking.k8s.io/v1
3+
kind: NetworkPolicy
4+
metadata:
5+
name: openshift-dns-deny-all
6+
namespace: openshift-dns
7+
spec:
8+
podSelector: {}
9+
policyTypes:
10+
- Ingress
11+
- Egress

pkg/manifests/manifests.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import (
77

88
appsv1 "k8s.io/api/apps/v1"
99
corev1 "k8s.io/api/core/v1"
10+
networkingv1 "k8s.io/api/networking/v1"
1011
rbacv1 "k8s.io/api/rbac/v1"
1112

1213
"k8s.io/apimachinery/pkg/util/yaml"
1314
)
1415

1516
const (
17+
NetworkPolicyDenyAllAsset = "assets/networkpolicy-deny-all.yaml"
18+
1619
DNSNamespaceAsset = "assets/dns/namespace.yaml"
1720
DNSServiceAccountAsset = "assets/dns/service-account.yaml"
1821
DNSClusterRoleAsset = "assets/dns/cluster-role.yaml"
1922
DNSClusterRoleBindingAsset = "assets/dns/cluster-role-binding.yaml"
2023
DNSDaemonSetAsset = "assets/dns/daemonset.yaml"
2124
DNSServiceAsset = "assets/dns/service.yaml"
25+
DNSNetworkPolicyAsset = "assets/dns/networkpolicy-allow.yaml"
2226

2327
MetricsClusterRoleAsset = "assets/dns/metrics/cluster-role.yaml"
2428
MetricsClusterRoleBindingAsset = "assets/dns/metrics/cluster-role-binding.yaml"
@@ -55,6 +59,14 @@ func MustAssetReader(asset string) io.Reader {
5559
return bytes.NewReader(MustAsset(asset))
5660
}
5761

62+
func NetworkPolicyDenyAll() *networkingv1.NetworkPolicy {
63+
np, err := NewNetworkPolicy(MustAssetReader(NetworkPolicyDenyAllAsset))
64+
if err != nil {
65+
panic(err)
66+
}
67+
return np
68+
}
69+
5870
func DNSNamespace() *corev1.Namespace {
5971
ns, err := NewNamespace(MustAssetReader(DNSNamespaceAsset))
6072
if err != nil {
@@ -103,6 +115,14 @@ func DNSService() *corev1.Service {
103115
return s
104116
}
105117

118+
func DNSNetworkPolicy() *networkingv1.NetworkPolicy {
119+
np, err := NewNetworkPolicy(MustAssetReader(DNSNetworkPolicyAsset))
120+
if err != nil {
121+
panic(err)
122+
}
123+
return np
124+
}
125+
106126
func MetricsClusterRole() *rbacv1.ClusterRole {
107127
cr, err := NewClusterRole(MustAssetReader(MetricsClusterRoleAsset))
108128
if err != nil {
@@ -220,3 +240,11 @@ func NewNamespace(manifest io.Reader) (*corev1.Namespace, error) {
220240
}
221241
return &ns, nil
222242
}
243+
244+
func NewNetworkPolicy(manifest io.Reader) (*networkingv1.NetworkPolicy, error) {
245+
np := networkingv1.NetworkPolicy{}
246+
if err := yaml.NewYAMLOrJSONDecoder(manifest, 100).Decode(&np); err != nil {
247+
return nil, err
248+
}
249+
return &np, nil
250+
}

0 commit comments

Comments
 (0)