backups: Read file path of known_hosts directly from plinth.config

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Joseph Nuthalapati 2019-06-14 21:30:10 +05:30
parent 795ed9d735
commit 2c97e1e02e
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
8 changed files with 10 additions and 8 deletions

View File

@ -7,6 +7,7 @@ server_dir = /plinth
actions_dir = /usr/share/plinth/actions actions_dir = /usr/share/plinth/actions
doc_dir = /usr/share/doc/freedombox doc_dir = /usr/share/doc/freedombox
custom_static_dir = /var/www/plinth/custom/static custom_static_dir = /var/www/plinth/custom/static
known_hosts = /var/lib/plinth/.ssh/known_hosts
# file locations # file locations
store_file = %(data_dir)s/plinth.sqlite3 store_file = %(data_dir)s/plinth.sqlite3

View File

@ -7,6 +7,7 @@ server_dir = /plinth
actions_dir = %(file_root)s/actions actions_dir = %(file_root)s/actions
doc_dir = %(file_root)s/doc doc_dir = %(file_root)s/doc
custom_static_dir = %(file_root)s/data/var/www/plinth/custom/static custom_static_dir = %(file_root)s/data/var/www/plinth/custom/static
known_hosts = %(data_dir)s/.ssh/known_hosts
# file locations # file locations
store_file = %(data_dir)s/plinth.sqlite3 store_file = %(data_dir)s/plinth.sqlite3

View File

@ -38,6 +38,7 @@ secure_proxy_ssl_header = None
develop = False develop = False
server_dir = '/' server_dir = '/'
danube_edition = False danube_edition = False
known_hosts = None
config_file = None config_file = None
@ -96,6 +97,7 @@ def read(config_path=None, root_directory=None):
('Path', 'actions_dir', 'string'), ('Path', 'actions_dir', 'string'),
('Path', 'doc_dir', 'string'), ('Path', 'doc_dir', 'string'),
('Path', 'server_dir', 'string'), ('Path', 'server_dir', 'string'),
('Path', 'known_hosts', 'string'),
('Network', 'host', 'string'), ('Network', 'host', 'string'),
('Network', 'port', 'int'), ('Network', 'port', 'int'),
('Network', 'secure_proxy_ssl_header', 'string'), ('Network', 'secure_proxy_ssl_header', 'string'),

View File

@ -149,7 +149,7 @@ def is_ssh_hostkey_verified(hostname):
"""Check whether SSH Hostkey has already been verified. """Check whether SSH Hostkey has already been verified.
hostname: Domain name or IP address of the host hostname: Domain name or IP address of the host
""" """
known_hosts_path = os.path.join(cfg.data_dir, '.ssh', 'known_hosts') known_hosts_path = cfg.known_hosts
if not os.path.exists(known_hosts_path): if not os.path.exists(known_hosts_path):
return False return False

View File

@ -330,10 +330,9 @@ class SshBorgRepository(BorgRepository):
def mount(self): def mount(self):
if self.is_mounted: if self.is_mounted:
return return
known_hosts_path = os.path.join(cfg.data_dir, '.ssh', 'known_hosts')
arguments = [ arguments = [
'mount', '--mountpoint', self.mountpoint, '--path', self._path, 'mount', '--mountpoint', self.mountpoint, '--path', self._path,
'--user-known-hosts-file', known_hosts_path '--user-known-hosts-file', cfg.known_hosts
] ]
arguments, kwargs = self._append_sshfs_arguments( arguments, kwargs = self._append_sshfs_arguments(
arguments, self.credentials) arguments, self.credentials)

View File

@ -327,7 +327,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
@staticmethod @staticmethod
def _add_ssh_hostkey(hostname, key_type): def _add_ssh_hostkey(hostname, key_type):
"""Add the given SSH key to known_hosts.""" """Add the given SSH key to known_hosts."""
known_hosts_path = os.path.join(cfg.data_dir, '.ssh', 'known_hosts') known_hosts_path = cfg.known_hosts
if not os.path.exists(known_hosts_path): if not os.path.exists(known_hosts_path):
os.makedirs(known_hosts_path.rsplit('/', maxsplit=1)[0]) os.makedirs(known_hosts_path.rsplit('/', maxsplit=1)[0])
open(known_hosts_path, 'w').close() open(known_hosts_path, 'w').close()
@ -440,9 +440,7 @@ def _validate_remote_repository(path, credentials, uuid=None):
def _ssh_connection(hostname, username, password): def _ssh_connection(hostname, username, password):
"""Context manager to create and close an SSH connection.""" """Context manager to create and close an SSH connection."""
ssh_client = paramiko.SSHClient() ssh_client = paramiko.SSHClient()
ssh_client.load_host_keys(cfg.known_hosts)
known_hosts_path = os.path.join(cfg.data_dir, '.ssh', 'known_hosts')
ssh_client.load_host_keys(known_hosts_path)
try: try:
ssh_client.connect(hostname, username=username, password=password) ssh_client.connect(hostname, username=username, password=password)

View File

@ -7,6 +7,7 @@ server_dir = /plinth
actions_dir = %(file_root)s/actions actions_dir = %(file_root)s/actions
doc_dir = %(file_root)s/doc doc_dir = %(file_root)s/doc
custom_static_dir = %(file_root)s/data/var/www/plinth/custom/static custom_static_dir = %(file_root)s/data/var/www/plinth/custom/static
known_hosts = %(data_dir)s/.ssh/known_hosts
# file locations # file locations
store_file = %(data_dir)s/plinth.sqlite3 store_file = %(data_dir)s/plinth.sqlite3

View File

@ -120,7 +120,7 @@ def compare_configurations(parser):
"""Compare two sets of configuration values.""" """Compare two sets of configuration values."""
# Note that the count of items within each section includes the number # Note that the count of items within each section includes the number
# of default items (1, for 'root'). # of default items (1, for 'root').
assert len(parser.items('Path')) == 9 assert len(parser.items('Path')) == 10
assert parser.get('Path', 'root') == cfg.root assert parser.get('Path', 'root') == cfg.root
assert parser.get('Path', 'file_root') == cfg.file_root assert parser.get('Path', 'file_root') == cfg.file_root
assert parser.get('Path', 'config_dir') == cfg.config_dir assert parser.get('Path', 'config_dir') == cfg.config_dir