Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
1f6735c5
Commit
1f6735c5
authored
Jan 11, 2017
by
Dan Winship
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate NetworkPolicy Ports
Protocol must be "TCP", "UDP", or nil. Integer-valued port must be 1-65535. String-valued port must be a syntactically valid ContainerPort name.
parent
83ac613b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
0 deletions
+99
-0
validation.go
pkg/apis/extensions/validation/validation.go
+17
-0
validation_test.go
pkg/apis/extensions/validation/validation_test.go
+82
-0
No files found.
pkg/apis/extensions/validation/validation.go
View file @
1f6735c5
...
@@ -830,6 +830,23 @@ func ValidateNetworkPolicySpec(spec *extensions.NetworkPolicySpec, fldPath *fiel
...
@@ -830,6 +830,23 @@ func ValidateNetworkPolicySpec(spec *extensions.NetworkPolicySpec, fldPath *fiel
// Validate ingress rules.
// Validate ingress rules.
for
i
,
ingress
:=
range
spec
.
Ingress
{
for
i
,
ingress
:=
range
spec
.
Ingress
{
ingressPath
:=
fldPath
.
Child
(
"ingress"
)
.
Index
(
i
)
ingressPath
:=
fldPath
.
Child
(
"ingress"
)
.
Index
(
i
)
for
i
,
port
:=
range
ingress
.
Ports
{
portPath
:=
ingressPath
.
Child
(
"ports"
)
.
Index
(
i
)
if
port
.
Protocol
!=
nil
&&
*
port
.
Protocol
!=
api
.
ProtocolTCP
&&
*
port
.
Protocol
!=
api
.
ProtocolUDP
{
allErrs
=
append
(
allErrs
,
field
.
NotSupported
(
portPath
.
Child
(
"protocol"
),
*
port
.
Protocol
,
[]
string
{
string
(
api
.
ProtocolTCP
),
string
(
api
.
ProtocolUDP
)}))
}
if
port
.
Port
!=
nil
{
if
port
.
Port
.
Type
==
intstr
.
Int
{
for
_
,
msg
:=
range
validation
.
IsValidPortNum
(
int
(
port
.
Port
.
IntVal
))
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
IntVal
,
msg
))
}
}
else
{
for
_
,
msg
:=
range
validation
.
IsValidPortName
(
port
.
Port
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
StrVal
,
msg
))
}
}
}
}
// TODO: Update From to be a pointer to slice as soon as auto-generation supports it.
// TODO: Update From to be a pointer to slice as soon as auto-generation supports it.
for
i
,
from
:=
range
ingress
.
From
{
for
i
,
from
:=
range
ingress
.
From
{
fromPath
:=
ingressPath
.
Child
(
"from"
)
.
Index
(
i
)
fromPath
:=
ingressPath
.
Child
(
"from"
)
.
Index
(
i
)
...
...
pkg/apis/extensions/validation/validation_test.go
View file @
1f6735c5
...
@@ -2031,6 +2031,10 @@ func TestValidatePSPVolumes(t *testing.T) {
...
@@ -2031,6 +2031,10 @@ func TestValidatePSPVolumes(t *testing.T) {
}
}
func
TestValidateNetworkPolicy
(
t
*
testing
.
T
)
{
func
TestValidateNetworkPolicy
(
t
*
testing
.
T
)
{
protocolTCP
:=
api
.
ProtocolTCP
protocolUDP
:=
api
.
ProtocolUDP
protocolICMP
:=
api
.
Protocol
(
"ICMP"
)
successCases
:=
[]
extensions
.
NetworkPolicy
{
successCases
:=
[]
extensions
.
NetworkPolicy
{
{
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
...
@@ -2063,6 +2067,36 @@ func TestValidateNetworkPolicy(t *testing.T) {
...
@@ -2063,6 +2067,36 @@ func TestValidateNetworkPolicy(t *testing.T) {
},
},
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
{
{
Ports
:
[]
extensions
.
NetworkPolicyPort
{
{
Protocol
:
nil
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
Int
,
IntVal
:
80
},
},
{
Protocol
:
&
protocolTCP
,
Port
:
nil
,
},
{
Protocol
:
&
protocolTCP
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
Int
,
IntVal
:
443
},
},
{
Protocol
:
&
protocolUDP
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
String
,
StrVal
:
"dns"
},
},
},
},
},
},
},
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
Spec
:
extensions
.
NetworkPolicySpec
{
PodSelector
:
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"a"
:
"b"
},
},
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
{
From
:
[]
extensions
.
NetworkPolicyPeer
{
From
:
[]
extensions
.
NetworkPolicyPeer
{
{
{
PodSelector
:
&
metav1
.
LabelSelector
{
PodSelector
:
&
metav1
.
LabelSelector
{
...
@@ -2145,6 +2179,54 @@ func TestValidateNetworkPolicy(t *testing.T) {
...
@@ -2145,6 +2179,54 @@ func TestValidateNetworkPolicy(t *testing.T) {
},
},
},
},
},
},
"invalid ingress.ports.protocol"
:
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
Spec
:
extensions
.
NetworkPolicySpec
{
PodSelector
:
metav1
.
LabelSelector
{},
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
{
Ports
:
[]
extensions
.
NetworkPolicyPort
{
{
Protocol
:
&
protocolICMP
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
Int
,
IntVal
:
80
},
},
},
},
},
},
},
"invalid ingress.ports.port (int)"
:
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
Spec
:
extensions
.
NetworkPolicySpec
{
PodSelector
:
metav1
.
LabelSelector
{},
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
{
Ports
:
[]
extensions
.
NetworkPolicyPort
{
{
Protocol
:
&
protocolTCP
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
Int
,
IntVal
:
123456789
},
},
},
},
},
},
},
"invalid ingress.ports.port (str)"
:
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
Spec
:
extensions
.
NetworkPolicySpec
{
PodSelector
:
metav1
.
LabelSelector
{},
Ingress
:
[]
extensions
.
NetworkPolicyIngressRule
{
{
Ports
:
[]
extensions
.
NetworkPolicyPort
{
{
Protocol
:
&
protocolTCP
,
Port
:
&
intstr
.
IntOrString
{
Type
:
intstr
.
String
,
StrVal
:
"!@#$"
},
},
},
},
},
},
},
"invalid ingress.from.podSelector"
:
{
"invalid ingress.from.podSelector"
:
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"bar"
},
Spec
:
extensions
.
NetworkPolicySpec
{
Spec
:
extensions
.
NetworkPolicySpec
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment