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
24eb1a08
Commit
24eb1a08
authored
Mar 16, 2015
by
Filip Grzadkowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate Service.Spec.publicIPs to be a valid IP that is not a localhost
parent
d0b468f4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
0 deletions
+65
-0
validation.go
pkg/api/validation/validation.go
+8
-0
validation_test.go
pkg/api/validation/validation_test.go
+21
-0
validation.go
pkg/util/validation.go
+6
-0
validation_test.go
pkg/util/validation_test.go
+30
-0
No files found.
pkg/api/validation/validation.go
View file @
24eb1a08
...
@@ -758,6 +758,14 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
...
@@ -758,6 +758,14 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
}
}
}
}
for
_
,
ip
:=
range
service
.
Spec
.
PublicIPs
{
if
ip
==
"0.0.0.0"
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
"spec.publicIPs"
,
ip
,
"is not an IP address"
))
}
else
if
util
.
IsValidIP
(
ip
)
&&
net
.
ParseIP
(
ip
)
.
IsLoopback
()
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
"spec.publicIPs"
,
ip
,
"publicIP cannot be a loopback"
))
}
}
return
allErrs
return
allErrs
}
}
...
...
pkg/api/validation/validation_test.go
View file @
24eb1a08
...
@@ -1171,6 +1171,27 @@ func TestValidateService(t *testing.T) {
...
@@ -1171,6 +1171,27 @@ func TestValidateService(t *testing.T) {
numErrs
:
1
,
numErrs
:
1
,
},
},
{
{
name
:
"invalid publicIPs localhost"
,
makeSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
PublicIPs
=
[]
string
{
"127.0.0.1"
}
},
numErrs
:
1
,
},
{
name
:
"invalid publicIPs"
,
makeSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
PublicIPs
=
[]
string
{
"0.0.0.0"
}
},
numErrs
:
1
,
},
{
name
:
"valid publicIPs host"
,
makeSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
PublicIPs
=
[]
string
{
"myhost.mydomain"
}
},
numErrs
:
0
,
},
{
name
:
"nil selector"
,
name
:
"nil selector"
,
makeSvc
:
func
(
s
*
api
.
Service
)
{
makeSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Selector
=
nil
s
.
Spec
.
Selector
=
nil
...
...
pkg/util/validation.go
View file @
24eb1a08
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
util
package
util
import
(
import
(
"net"
"regexp"
"regexp"
)
)
...
@@ -94,3 +95,8 @@ func IsCIdentifier(value string) bool {
...
@@ -94,3 +95,8 @@ func IsCIdentifier(value string) bool {
func
IsValidPortNum
(
port
int
)
bool
{
func
IsValidPortNum
(
port
int
)
bool
{
return
0
<
port
&&
port
<
65536
return
0
<
port
&&
port
<
65536
}
}
// IsValidIP tests that the argument is a valid IPv4 address.
func
IsValidIP
(
value
string
)
bool
{
return
net
.
ParseIP
(
value
)
!=
nil
&&
net
.
ParseIP
(
value
)
.
To4
()
!=
nil
}
pkg/util/validation_test.go
View file @
24eb1a08
...
@@ -222,3 +222,33 @@ func TestIsValidLabelValue(t *testing.T) {
...
@@ -222,3 +222,33 @@ func TestIsValidLabelValue(t *testing.T) {
}
}
}
}
}
}
func
TestIsValidIP
(
t
*
testing
.
T
)
{
goodValues
:=
[]
string
{
"1.1.1.1"
,
"1.1.1.01"
,
"255.0.0.1"
,
"1.0.0.0"
,
"0.0.0.0"
,
}
for
_
,
val
:=
range
goodValues
{
if
!
IsValidIP
(
val
)
{
t
.
Errorf
(
"expected true for %q"
,
val
)
}
}
badValues
:=
[]
string
{
"2a00:79e0:2:0:f1c3:e797:93c1:df80"
,
// This is valid IPv6
"a"
,
"myhost.mydomain"
,
"-1.0.0.0"
,
"1.0.0.256"
,
"1.0.0.1.1"
,
"1.0.0.1."
,
}
for
_
,
val
:=
range
badValues
{
if
IsValidIP
(
val
)
{
t
.
Errorf
(
"expected false for %q"
,
val
)
}
}
}
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