-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract port range validation to api/utils/net
- Loading branch information
Showing
4 changed files
with
140 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2024 Gravitational, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package net | ||
|
||
import ( | ||
"github.com/gravitational/trace" | ||
) | ||
|
||
// ValidatePortRange checks if the given port range is within bounds. If endPort is not zero, it | ||
// also checks if it's bigger than port. A port range with zero as endPort is assumed to describe a | ||
// single port. | ||
func ValidatePortRange(port, endPort int) error { | ||
const minPort = 1 | ||
const maxPort = 65535 | ||
|
||
if port < minPort || port > maxPort { | ||
return trace.BadParameter("port must be between %d and %d, but got %d", minPort, maxPort, port) | ||
} | ||
|
||
if endPort != 0 { | ||
if endPort < minPort+1 || endPort > maxPort { | ||
return trace.BadParameter("end port must be between %d and %d, but got %d", minPort+1, maxPort, endPort) | ||
} | ||
|
||
if endPort <= port { | ||
return trace.BadParameter("end port must be greater than port (%d vs %d)", endPort, port) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2024 Gravitational, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package net | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gravitational/trace" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidatePortRange(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
port int | ||
endPort int | ||
check require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "valid single port", | ||
port: 1337, | ||
endPort: 0, | ||
check: require.NoError, | ||
}, | ||
{ | ||
name: "valid port range", | ||
port: 1337, | ||
endPort: 3456, | ||
check: require.NoError, | ||
}, | ||
{ | ||
name: "port smaller than 1", | ||
port: 0, | ||
endPort: 0, | ||
check: badParameterError, | ||
}, | ||
{ | ||
name: "port bigger than max port", | ||
port: 98765, | ||
endPort: 0, | ||
check: badParameterError, | ||
}, | ||
{ | ||
name: "end port smaller than 2", | ||
port: 5, | ||
endPort: 1, | ||
check: badParameterErrorAndContains("end port must be between"), | ||
}, | ||
{ | ||
name: "end port bigger than max port", | ||
port: 5, | ||
endPort: 98765, | ||
check: badParameterErrorAndContains("end port must be between"), | ||
}, | ||
{ | ||
name: "end port smaller than port", | ||
port: 10, | ||
endPort: 5, | ||
check: badParameterErrorAndContains("end port must be greater than port"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.check(t, ValidatePortRange(tt.port, tt.endPort)) | ||
}) | ||
} | ||
} | ||
|
||
func badParameterError(t require.TestingT, err error, msgAndArgs ...interface{}) { | ||
require.True(t, trace.IsBadParameter(err), "expected bad parameter error, got %+v", err) | ||
} | ||
|
||
func badParameterErrorAndContains(msg string) require.ErrorAssertionFunc { | ||
return func(t require.TestingT, err error, msgAndArgs ...interface{}) { | ||
require.True(t, trace.IsBadParameter(err), "expected bad parameter error, got %+v", err) | ||
require.ErrorContains(t, err, msg, msgAndArgs...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters