Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
22
Merge Requests
22
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
nexedi
slapos.core
Commits
45468431
Commit
45468431
authored
6 years ago
by
Guillaume Hervier
Committed by
Rafael Monnerat
6 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
portredir: Validate port redirection config values
parent
8c55938f
master
alain-message
boot/offline
console
feat/node-secrets
feat/nxdbom
feat/openapi
feat/slapos-computer-partitions-capabilities-wip
feat/standalone-service
feat_open_order
fix/request-download-speed
fix/verify
fix/write-history
for_testrunner_1
json-api
json-api-2022
json-api-slapgrid
message-box
proxy
zope4py3
1.16.2
1.16.1
1.16.0
1.15.0
1.14.3
1.14.2
1.14.1
1.14.0
1.13.0
1.12.0
1.11.0
1.10.8
1.10.7
1.10.6
1.10.5
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.3
1.9.2
1.9.1
1.9.0
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.13
1.7.12
1.7.11
1.7.10
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.19
1.6.18
1.6.17
1.6.16
1.6.15
1.6.14
1.6.13
1.6.12
1.6.11
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.12
1.5.11
1.5.10
1.5.9
1.5.8
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.28
1.4.27
1.4.26
1.4.25
1.4.24
1.4.23
1.4.22
1.4.21
1.4.20
1.4.19
1.4.17
1.4.16
1.4.15
1.4.14
1.4.12
1.4.11
1.4.10
1.4.9
v1.8.1
master-20240326
master-20240326+p1
master-20180917
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
16 deletions
+115
-16
slapos/manager/portredir.py
slapos/manager/portredir.py
+38
-16
slapos/tests/slapgrid.py
slapos/tests/slapgrid.py
+77
-0
No files found.
slapos/manager/portredir.py
View file @
45468431
# coding: utf-8
import
json
import
logging
import
netaddr
import
os.path
from
.interface
import
IManager
...
...
@@ -61,7 +62,11 @@ class Manager(object):
# Read it
with
open
(
port_redirect_file_path
)
as
f
:
port_redirects
=
json
.
load
(
f
)
try
:
port_redirects
=
json
.
load
(
f
)
except
:
logger
.
warning
(
'Bad port redirection config file'
,
exc_info
=
True
)
return
# Get partitions IPv6 address
computer_partition
=
partition
.
computer_partition
...
...
@@ -79,22 +84,40 @@ class Manager(object):
socat_programs
=
[]
for
port_redirect
in
port_redirects
:
redir_type
=
port_redirect
.
get
(
'type'
,
'tcp'
)
source_port
=
port_redirect
[
'srcPort'
]
source_addr
=
port_redirect
.
get
(
'srcAddress'
)
source_is_ipv4
=
source_addr
is
None
or
'.'
in
source_addr
dest_port
=
port_redirect
[
'destPort'
]
dest_addr
=
port_redirect
.
get
(
'destAddress'
,
partition_ipv6
)
dest_is_ipv6
=
':'
in
dest_addr
if
dest_is_ipv6
:
dest_addr
=
'[{}]'
.
format
(
dest_addr
)
if
redir_type
.
lower
()
not
in
{
'tcp'
,
'udp'
}:
logger
.
warning
(
'Bad source redirection type: %s'
,
redir_type
)
continue
try
:
source_port
=
int
(
port_redirect
[
'srcPort'
])
except
:
logger
.
warning
(
'Bad source port provided'
,
exc_info
=
True
)
continue
try
:
source_addr
=
port_redirect
.
get
(
'srcAddress'
)
if
source_addr
is
not
None
:
source_addr
=
netaddr
.
IPAddress
(
source_addr
)
except
:
logger
.
warning
(
'Bad source address provided'
,
exc_info
=
True
)
continue
try
:
dest_port
=
int
(
port_redirect
[
'destPort'
])
except
:
logger
.
warning
(
'Bad source port provided'
,
exc_info
=
True
)
continue
try
:
dest_addr
=
port_redirect
.
get
(
'destAddress'
,
partition_ipv6
)
dest_addr
=
netaddr
.
IPAddress
(
dest_addr
)
except
:
logger
.
warning
(
'Bad source address provided'
,
exc_info
=
True
)
continue
command
=
[
'socat'
]
socat_source_version
=
4
if
source_is_ipv4
else
6
socat_source_version
=
source_addr
.
version
if
source_addr
is
not
None
else
4
socat_source_type
=
'{rtype}{version}-LISTEN'
.
format
(
rtype
=
redir_type
.
upper
(),
version
=
socat_source_version
)
socat_source
=
'{}:{}'
.
format
(
socat_source_type
,
source_port
)
if
source_addr
is
not
None
:
...
...
@@ -102,8 +125,7 @@ class Manager(object):
socat_source
+=
',fork'
command
.
append
(
socat_source
)
socat_dest_version
=
6
if
dest_is_ipv6
else
4
socat_dest_type
=
'{rtype}{version}'
.
format
(
rtype
=
redir_type
.
upper
(),
version
=
socat_dest_version
)
socat_dest_type
=
'{rtype}{version}'
.
format
(
rtype
=
redir_type
.
upper
(),
version
=
dest_addr
.
version
)
socat_dest
=
'{}:{}:{}'
.
format
(
socat_dest_type
,
dest_addr
,
dest_port
)
command
.
append
(
socat_dest
)
...
...
This diff is collapsed.
Click to expand it.
slapos/tests/slapgrid.py
View file @
45468431
...
...
@@ -2832,3 +2832,80 @@ class TestSlapgridWithPortRedirection(MasterMixin, unittest.TestCase):
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-tcp-{}'.format(1234), partition_supervisord_config)
self.assertNotIn('socat TCP4-LISTEN:1234,fork TCP4:127.0.0.1:4321', partition_supervisord_config)
def test_port_redirection_config_bad_source_port(self):
with self._mock_requests():
self._setup_instance([
{
'srcPort': 'bad',
'destPort': 4321,
'destAddress': '127.0.0.1',
},
])
# Check the socat command
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-tcp-bad', partition_supervisord_config)
self.assertNotIn('socat TCP4-LISTEN:bad,fork TCP4:127.0.0.1:4321', partition_supervisord_config)
def test_port_redirection_config_bad_dest_port(self):
with self._mock_requests():
self._setup_instance([
{
'srcPort': 1234,
'destPort': 'wolf',
'destAddress': '127.0.0.1',
},
])
# Check the socat command
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-tcp-1234', partition_supervisord_config)
self.assertNotIn('socat TCP4-LISTEN:1234,fork TCP4:127.0.0.1:wolf', partition_supervisord_config)
def test_port_redirection_config_bad_source_address(self):
with self._mock_requests():
self._setup_instance([
{
'srcPort': 1234,
'srcAddress': 'bad',
'destPort': 4321,
'destAddress': '127.0.0.1',
},
])
# Check the socat command
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-tcp-1234', partition_supervisord_config)
self.assertNotIn('socat TCP4-LISTEN:1234,bind=bad,fork TCP4:127.0.0.1:4321', partition_supervisord_config)
def test_port_redirection_config_bad_dest_address(self):
with self._mock_requests():
self._setup_instance([
{
'srcPort': 1234,
'destPort': 4321,
'destAddress': 'wolf',
},
])
# Check the socat command
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-tcp-1234', partition_supervisord_config)
self.assertNotIn('socat TCP4-LISTEN:1234,fork TCP4:wolf:4321', partition_supervisord_config)
def test_port_redirection_config_bad_redir_type(self):
with self._mock_requests():
self._setup_instance([
{
'type': 'htcpcp',
'srcPort': 1234,
'destPort': 4321,
'destAddress': '127.0.0.1',
},
])
# Check the socat command
partition_supervisord_config = self._read_instance_supervisord_config()
self.assertNotIn('socat-htcpcp-1234', partition_supervisord_config)
self.assertNotIn('socat HTCPCP4-LISTEN:1234,fork HTCPCP4:127.0.0.1:4321', partition_supervisord_config)
This diff is collapsed.
Click to expand it.
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