Rename ssl and auth configuration file settings.

Add enable SSL config validator.
Strict configuration file validation: any error will prevent the server to start.
Core server logic moved to a Server class.
This commit is contained in:
grossmj committed 2021-04-12 23:26:42 +09:30
1 parent 1b5a5de4bc
commit 71725aade6
12 files changed
+542 -520

No files matched your search

+1 -1
View File
@@ -353,7 +353,7 @@ def run_around_tests(monkeypatch, config, port_manager):#port_manager, controlle
config.settings.Server.appliances_path = os.path.join(tmppath, 'appliances')
config.settings.Server.ubridge_path = os.path.join(tmppath, 'bin', 'ubridge')
config.settings.Server.local = True
config.settings.Server.auth = False
config.settings.Server.enable_http_auth = False
# Prevent executions of the VM if we forgot to mock something
config.settings.VirtualBox.vboxmanage_path = tmppath
+7 -3
View File
@@ -121,9 +121,13 @@ def test_server_password_hidden():
({"vnc_console_end_port_range": 6000}, False),
({"vnc_console_end_port_range": 1000}, True),
({"vnc_console_start_port_range": 7000, "vnc_console_end_port_range": 6000}, True),
({"auth": True, "user": "user1"}, False),
({"auth": True, "user": ""}, True),
({"auth": True}, True),
({"enable_ssl": True, "certfile": "/path/to/certfile", "certkey": "/path/to/certkey"}, True),
({"enable_ssl": True}, True),
({"enable_ssl": True, "certfile": "/path/to/certfile"}, True),
({"enable_ssl": True, "certkey": "/path/to/certkey"}, True),
({"enable_http_auth": True, "user": "user1"}, False),
({"enable_http_auth": True, "user": ""}, True),
({"enable_http_auth": True}, True),
)
)
def test_server_settings(settings: dict, exception_expected: bool):
-139
View File
@@ -1,139 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import locale
from gns3server import run
from gns3server.config import Config
from gns3server.version import __version__
def test_locale_check():
try:
locale.setlocale(locale.LC_ALL, ("fr_FR", "UTF-8"))
except: # Locale is not available on the server
return
run.locale_check()
assert locale.getlocale() == ('fr_FR', 'UTF-8')
def test_parse_arguments(capsys, config, tmpdir):
server_config = config.settings.Server
with pytest.raises(SystemExit):
run.parse_arguments(["--fail"])
out, err = capsys.readouterr()
assert "usage" in err
assert "fail" in err
assert "unrecognized arguments" in err
# with pytest.raises(SystemExit):
# run.parse_arguments(["-v"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# with pytest.raises(SystemExit):
# run.parse_arguments(["--version"])
# out, _ = capsys.readouterr()
# assert __version__ in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["-h"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["--help"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
assert run.parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
assert run.parse_arguments([]).host == "0.0.0.0"
server_config.host = "192.168.1.2"
assert run.parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
assert run.parse_arguments([]).host == "192.168.1.2"
assert run.parse_arguments(["--port", "8002"]).port == 8002
assert run.parse_arguments([]).port == 3080
server_config.port = 8003
assert run.parse_arguments([]).port == 8003
assert run.parse_arguments(["--ssl"]).ssl
assert run.parse_arguments([]).ssl is False
server_config.ssl = True
assert run.parse_arguments([]).ssl
assert run.parse_arguments(["--certfile", "bla"]).certfile == "bla"
assert run.parse_arguments([]).certfile is None
assert run.parse_arguments(["--certkey", "blu"]).certkey == "blu"
assert run.parse_arguments([]).certkey is None
assert run.parse_arguments(["-L"]).local
assert run.parse_arguments(["--local"]).local
server_config.local = False
assert run.parse_arguments([]).local is False
server_config.local = True
assert run.parse_arguments([]).local
assert run.parse_arguments(["-A"]).allow
assert run.parse_arguments(["--allow"]).allow
assert run.parse_arguments([]).allow is False
server_config.allow_remote_console = True
assert run.parse_arguments([]).allow
assert run.parse_arguments(["-q"]).quiet
assert run.parse_arguments(["--quiet"]).quiet
assert run.parse_arguments([]).quiet is False
assert run.parse_arguments(["-d"]).debug
assert run.parse_arguments([]).debug is False
server_config.debug = True
assert run.parse_arguments([]).debug
def test_set_config_with_args():
config = Config.instance()
args = run.parse_arguments(["--host",
"192.168.1.1",
"--local",
"--allow",
"--port",
"8001",
"--ssl",
"--certfile",
"bla",
"--certkey",
"blu",
"--debug"])
run.set_config(args)
server_config = config.settings.Server
assert server_config.local
assert server_config.allow_remote_console
assert server_config.host
assert server_config.port
assert server_config.ssl
assert server_config.certfile
assert server_config.certkey
assert server_config.debug
+148
View File
@@ -0,0 +1,148 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import locale
import tempfile
from gns3server.server import Server
from gns3server.config import Config
def test_locale_check():
try:
locale.setlocale(locale.LC_ALL, ("fr_FR", "UTF-8"))
except: # Locale is not available on the server
return
Server._locale_check()
assert locale.getlocale() == ('fr_FR', 'UTF-8')
def test_parse_arguments(capsys, config, tmpdir):
server = Server()
server_config = config.settings.Server
with pytest.raises(SystemExit):
server._parse_arguments(["--fail"])
out, err = capsys.readouterr()
assert "usage" in err
assert "fail" in err
assert "unrecognized arguments" in err
# with pytest.raises(SystemExit):
# run.parse_arguments(["-v"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# with pytest.raises(SystemExit):
# run.parse_arguments(["--version"])
# out, _ = capsys.readouterr()
# assert __version__ in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["-h"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["--help"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
assert server._parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
assert server._parse_arguments([]).host == "0.0.0.0"
server_config.host = "192.168.1.2"
assert server._parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
assert server._parse_arguments([]).host == "192.168.1.2"
assert server._parse_arguments(["--port", "8002"]).port == 8002
assert server._parse_arguments([]).port == 3080
server_config.port = 8003
assert server._parse_arguments([]).port == 8003
assert server._parse_arguments(["--ssl"]).ssl
assert server._parse_arguments([]).ssl is False
with tempfile.NamedTemporaryFile(dir=str(tmpdir)) as f:
server_config.certfile = f.name
server_config.certkey = f.name
server_config.enable_ssl = True
assert server._parse_arguments([]).ssl
server_config.certfile = None
server_config.certkey = None
assert server._parse_arguments(["--certfile", "bla"]).certfile == "bla"
assert server._parse_arguments([]).certfile is None
assert server._parse_arguments(["--certkey", "blu"]).certkey == "blu"
assert server._parse_arguments([]).certkey is None
assert server._parse_arguments(["-L"]).local
assert server._parse_arguments(["--local"]).local
server_config.local = False
assert server._parse_arguments([]).local is False
server_config.local = True
assert server._parse_arguments([]).local
assert server._parse_arguments(["-A"]).allow
assert server._parse_arguments(["--allow"]).allow
assert server._parse_arguments([]).allow is False
server_config.allow_remote_console = True
assert server._parse_arguments([]).allow
assert server._parse_arguments(["-q"]).quiet
assert server._parse_arguments(["--quiet"]).quiet
assert server._parse_arguments([]).quiet is False
assert server._parse_arguments(["-d"]).debug
assert server._parse_arguments(["--debug"]).debug
assert server._parse_arguments([]).debug is False
def test_set_config_with_args(tmpdir):
server = Server()
config = Config.instance()
with tempfile.NamedTemporaryFile(dir=str(tmpdir)) as f:
certfile = f.name
certkey = f.name
args = server._parse_arguments(["--host",
"192.168.1.1",
"--local",
"--allow",
"--port",
"8001",
"--ssl",
"--certfile",
certfile,
"--certkey",
certkey,
"--debug"])
server._set_config_defaults_from_command_line(args)
server_config = config.settings.Server
assert server_config.local
assert server_config.allow_remote_console
assert server_config.host
assert server_config.port
assert server_config.enable_ssl
assert server_config.certfile
assert server_config.certkey