1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
import json
import logging
import os
logging.basicConfig()
logger = logging.getLogger(name=__name__)
logger.setLevel(logging.DEBUG)
import requests
from leap.base import config as baseconfig
from leap.base import constants as baseconstants
from leap.eip import config as eipconfig
from leap.eip import constants as eipconstants
from leap.eip import exceptions as eipexceptions
from leap.util.fileutil import mkdir_p
"""
EIPConfigChecker
----------
this is the first of 3 consecutive checks that we're implementing.
It is used from the eip conductor (a instance of EIPConnection that is
managed from the QtApp), running run_all method before trying to call
`connect` or any other of the state switching methods.
It checks that the needed files are provided or can be discovered over the
net. Much of these tests are not specific to EIP module, and can be splitted
into base.tests to be invoked by the base leap init routines.
However, I'm testing them alltogether for the sake of having the whole unit
reachable and testable as a whole.
Other related checkers - not implemented yet -:
* LeapNetworkChecker
* ProviderCertChecker
"""
class EIPConfigChecker(object):
"""
Several tests needed
to ensure a EIPConnection
can be sucessfully established.
use run_all to run all checks.
"""
def __init__(self, fetcher=requests):
# we do not want to accept too many
# argument on init.
# we want tests
# to be explicitely run.
self.config = None
self.fetcher = fetcher
def run_all(self, checker=None, skip_download=False):
"""
runs all checks in a row.
will raise if some error encountered.
catching those exceptions is not
our responsibility at this moment
"""
if not checker:
checker = self
# let's call all tests
# needed for a sane eip session.
# TODO: get rid of check_default.
# check_complete should
# be enough.
checker.check_default_eipconfig()
checker.check_is_there_default_provider()
checker.fetch_definition(skip_download=skip_download)
checker.fetch_eip_config(skip_download=skip_download)
checker.check_complete_eip_config()
#checker.ping_gateway()
# public checks
def check_default_eipconfig(self):
"""
checks if default eipconfig exists,
and dumps a default file if not
"""
# XXX ONLY a transient check
# because some old function still checks
# for eip config at the beginning.
# it *really* does not make sense to
# dump it right now, we can get an in-memory
# config object and dump it to disk in a
# later moment
logger.debug('checking default eip config')
if not self._is_there_default_eipconfig():
self._dump_default_eipconfig()
def check_is_there_default_provider(self, config=None):
"""
raises EIPMissingDefaultProvider if no
default provider found on eip config.
This is catched by ui and runs FirstRunWizard (MVS+)
"""
# if config is not None:
# config = config
# else: self.get_eipconfig
# XXX parse EIPConfig.
# XXX get default_provider.
logger.debug('checking default provider')
eipcfg = self._get_default_eipconfig_path()
with open(eipcfg, 'r') as fp:
config = json.load(fp)
provider = config.get('provider', None)
if provider is None:
raise eipexceptions.EIPMissingDefaultProvider
if config:
self.config = config
return True
def fetch_definition(self, skip_download=False,
config=None, uri=None):
"""
fetches a definition file from server
"""
# TODO:
# - Implement diff
# - overwrite if different.
logger.debug('fetching definition')
if skip_download:
logger.debug('(fetching def skipped)')
return True
if config is None:
config = self.config
if uri is None:
if config:
domain = config.get('provider', None)
else:
domain = None
uri = self._get_provider_definition_uri(
domain=domain)
# XXX move to JSONConfig Fetcher
request = self.fetcher.get(uri)
request.raise_for_status()
definition_file = os.path.join(
baseconfig.get_default_provider_path(),
baseconstants.DEFINITION_EXPECTED_PATH)
folder, filename = os.path.split(definition_file)
if not os.path.isdir(folder):
mkdir_p(folder)
with open(definition_file, 'wb') as f:
f.write(json.dumps(request.json, indent=4))
def fetch_eip_config(self, skip_download=False,
config=None, uri=None):
if skip_download:
return True
if config is None:
config = self.config
if uri is None:
if config:
domain = config.get('provider', None)
else:
domain = None
uri = self._get_eip_service_uri(
domain=domain)
# XXX move to JSONConfig Fetcher
request = self.fetcher.get(uri)
request.raise_for_status()
definition_file = os.path.join(
baseconfig.get_default_provider_path(),
eipconstants.EIP_SERVICE_EXPECTED_PATH)
folder, filename = os.path.split(definition_file)
if not os.path.isdir(folder):
mkdir_p(folder)
with open(definition_file, 'wb') as f:
f.write(json.dumps(request.json, indent=4))
def check_complete_eip_config(self, config=None):
if config is None:
config = self.config
try:
'trying assertions'
assert 'provider' in config
assert config['provider'] is not None
# XXX assert there is gateway !!
except AssertionError:
raise eipexceptions.EIPConfigurationError
# XXX TODO:
# We should WRITE eip config if missing or
# incomplete at this point
def ping_gateway(self):
raise NotImplementedError
#
# private helpers
#
def _get_default_eipconfig_path(self):
return baseconfig.get_config_file(eipconstants.EIP_CONFIG)
def _is_there_default_eipconfig(self):
return os.path.isfile(
self._get_default_eipconfig_path())
def _dump_default_eipconfig(self):
eipconfig.dump_default_eipconfig(
self._get_default_eipconfig_path())
def _get_provider_definition_uri(self, domain=None, path=None):
if domain is None:
domain = baseconstants.DEFAULT_TEST_PROVIDER
if path is None:
path = baseconstants.DEFINITION_EXPECTED_PATH
return "https://%s/%s" % (domain, path)
def _get_eip_service_uri(self, domain=None, path=None):
if domain is None:
domain = baseconstants.DEFAULT_TEST_PROVIDER
if path is None:
path = eipconstants.EIP_SERVICE_EXPECTED_PATH
return "https://%s/%s" % (domain, path)
|