1 | # -*- coding: utf-8 -*- |
||
2 | # Copyright (C) 2019-2021 Greenbone Networks GmbH |
||
3 | # |
||
4 | # SPDX-License-Identifier: GPL-3.0-or-later |
||
5 | # |
||
6 | # This program is free software: you can redistribute it and/or modify |
||
7 | # it under the terms of the GNU General Public License as published by |
||
8 | # the Free Software Foundation, either version 3 of the License, or |
||
9 | # (at your option) any later version. |
||
10 | # |
||
11 | # This program is distributed in the hope that it will be useful, |
||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | # GNU General Public License for more details. |
||
15 | # |
||
16 | # You should have received a copy of the GNU General Public License |
||
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | """ |
||
19 | Module to store gvm-tools configuration settings |
||
20 | """ |
||
21 | |||
22 | import configparser |
||
23 | import logging |
||
24 | |||
25 | from gvm.connections import ( |
||
26 | DEFAULT_UNIX_SOCKET_PATH, |
||
27 | DEFAULT_GVM_PORT, |
||
28 | DEFAULT_SSH_PORT, |
||
29 | DEFAULT_HOSTNAME, |
||
30 | ) |
||
31 | |||
32 | logger = logging.getLogger(__name__) |
||
33 | |||
34 | |||
35 | class Config: |
||
36 | def __init__(self): |
||
37 | self._config = configparser.ConfigParser(default_section='main') |
||
38 | |||
39 | self._config = {} |
||
40 | |||
41 | self._config['gmp'] = dict(username='', password='') |
||
42 | self._config['ssh'] = dict( |
||
43 | username='gmp', |
||
44 | password='gmp', |
||
45 | port=DEFAULT_SSH_PORT, |
||
46 | hostname=DEFAULT_HOSTNAME, |
||
47 | ) |
||
48 | self._config['unixsocket'] = dict(socketpath=DEFAULT_UNIX_SOCKET_PATH) |
||
49 | self._config['tls'] = dict( |
||
50 | port=DEFAULT_GVM_PORT, hostname=DEFAULT_HOSTNAME |
||
51 | ) |
||
52 | |||
53 | self._defaults = dict() |
||
54 | |||
55 | def load(self, filepath): |
||
56 | path = filepath.expanduser() |
||
57 | |||
58 | config = configparser.ConfigParser(default_section='main') |
||
59 | |||
60 | with path.open() as f: |
||
61 | config.read_file(f) |
||
62 | |||
63 | if 'Auth' in config: |
||
64 | logger.warning( |
||
65 | "Warning: Loaded config file %s contains deprecated 'Auth' " |
||
66 | "section. This section will be ignored in future.", |
||
67 | str(filepath), |
||
68 | ) |
||
69 | gmp_username = config.get('Auth', 'gmp_username', fallback='') |
||
70 | gmp_password = config.get('Auth', 'gmp_password', fallback='') |
||
71 | self._config['gmp']['username'] = gmp_username |
||
72 | self._config['gmp']['password'] = gmp_password |
||
73 | |||
74 | self._defaults.update(config.defaults()) |
||
75 | |||
76 | for section in config.sections(): |
||
77 | if section == 'Auth': |
||
78 | continue |
||
79 | |||
80 | for key, value in config.items(section): |
||
81 | self._config.setdefault(section, dict())[key] = value |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
82 | |||
83 | def defaults(self): |
||
84 | return self._defaults |
||
85 | |||
86 | def get(self, section, name): |
||
87 | if not section in self._config: |
||
88 | return None |
||
89 | |||
90 | return self._config[section].get(name) |
||
91 |