diff options
Diffstat (limited to 'src/leap/base')
-rw-r--r-- | src/leap/base/config.py | 202 | ||||
-rw-r--r-- | src/leap/base/constants.py | 15 | ||||
-rw-r--r-- | src/leap/base/providers.py | 2 | ||||
-rw-r--r-- | src/leap/base/tests/test_config.py | 64 | ||||
-rw-r--r-- | src/leap/base/tests/test_providers.py | 6 |
5 files changed, 154 insertions, 135 deletions
diff --git a/src/leap/base/config.py b/src/leap/base/config.py index 5a52637c..a468a849 100644 --- a/src/leap/base/config.py +++ b/src/leap/base/config.py @@ -18,6 +18,9 @@ from leap.base import exceptions from leap.base import constants from leap.util.fileutil import (mkdir_p) +# move to base! +from leap.eip import exceptions as eipexceptions + class BaseLeapConfig(object): slug = None @@ -145,9 +148,9 @@ class JSONLeapConfig(BaseLeapConfig): config[k] = v() self._config.serialize(to) - def load(self, fromfile=None, from_uri=None, fetcher=None): + def load(self, fromfile=None, from_uri=None, fetcher=None, verify=False): if from_uri is not None: - fetched = self.fetch(from_uri, fetcher=fetcher) + fetched = self.fetch(from_uri, fetcher=fetcher, verify=verify) if fetched: return if fromfile is None: @@ -156,12 +159,21 @@ class JSONLeapConfig(BaseLeapConfig): # XXX check for no errors, etc self._config.config = newconfig - def fetch(self, uri, fetcher=None): + def fetch(self, uri, fetcher=None, verify=True): if not fetcher: fetcher = self.fetcher - request = fetcher.get(uri) + logger.debug('verify: %s', verify) + request = fetcher.get(uri, verify=verify) + + # XXX get 404, ... + # and raise a UnableToFetch... request.raise_for_status() fd, fname = tempfile.mkstemp(suffix=".json") + if not request.json: + try: + json.loads(request.content) + except ValueError: + raise eipexceptions.LeapBadConfigFetchedError with open(fname, 'w') as tmp: tmp.write(json.dumps(request.json)) self._loadtemp(fname) @@ -239,7 +251,7 @@ def get_config_file(filename, folder=None): def get_default_provider_path(): default_subpath = os.path.join("providers", - constants.DEFAULT_TEST_PROVIDER) + constants.DEFAULT_PROVIDER) default_provider_path = get_config_file( '', folder=default_subpath) @@ -266,55 +278,55 @@ def get_groupname(): # json stuff # XXX merge with JSONConfig / EIPChecks as appropiate. -def get_config_json(config_file=None): - """ - will replace get_config function be developing them - in parralel for branch purposes. - @param: configuration file - @type: file - @rparam: configuration turples - @rtype: dictionary - """ - if not config_file: +#def get_config_json(config_file=None): + #""" + #will replace get_config function be developing them + #in parralel for branch purposes. + #@param: configuration file + #@type: file + #@rparam: configuration turples + #@rtype: dictionary + #""" + #if not config_file: #TODO: NOT SURE WHAT this default should be, if anything - fpath = get_config_file('eip.json') - if not os.path.isfile(fpath): - dpath, cfile = os.path.split(fpath) - if not os.path.isdir(dpath): - mkdir_p(dpath) - with open(fpath, 'wb') as configfile: - configfile.flush() - try: - return json.load(open(fpath)) - except ValueError: - raise exceptions.MissingConfigFileError - - else: + #fpath = get_config_file('eip.json') + #if not os.path.isfile(fpath): + #dpath, cfile = os.path.split(fpath) + #if not os.path.isdir(dpath): + #mkdir_p(dpath) + #with open(fpath, 'wb') as configfile: + #configfile.flush() + #try: + #return json.load(open(fpath)) + #except ValueError: + #raise exceptions.MissingConfigFileError +# + #else: #TODO: add validity checks of file - try: - return json.load(open(config_file)) - except IOError: - raise exceptions.MissingConfigFileError - - -def get_definition_file(url=None): - """ - """ + #try: + #return json.load(open(config_file)) + #except IOError: + #raise exceptions.MissingConfigFileError +# +# +#def get_definition_file(url=None): + #""" + #""" #TODO: determine good default location of definition file. - r = requests.get(url) - return r.json - - -def is_internet_up(): - """TODO: Build more robust network diagnosis capabilities - """ - try: - requests.get('http://128.30.52.45', timeout=1) - return True - except requests.Timeout: # as err: - pass - return False - + #r = requests.get(url) + #return r.json +# +# +#def is_internet_up(): + #"""TODO: Build more robust network diagnosis capabilities + #""" + #try: + #requests.get('http://128.30.52.45', timeout=1) + #return True + #except requests.Timeout: # as err: + #pass + #return False +# # XXX DEPRECATE. # move to eip.checks # @@ -323,49 +335,49 @@ def is_internet_up(): # moving it here transiently until I clean merge commit. # -- kali 2012-08-24 00:32 # - - -class Configuration(object): - """ - All configurations (providers et al) will be managed in this class. - """ - def __init__(self, provider_url=None): - try: +# +# +#class Configuration(object): + #""" + #All configurations (providers et al) will be managed in this class. + #""" + #def __init__(self, provider_url=None): + #try: #requests.get('foo') - self.providers = {} - self.error = False - provider_file = self.check_and_get_definition_file(provider_url) - self.providers['default'] = get_config_json(provider_file) - except (requests.HTTPError, requests.RequestException) as e: - self.error = e.message - except requests.ConnectionError as e: - if e.message == "[Errno 113] No route to host": - if not is_internet_up: + #self.providers = {} + #self.error = False + #provider_file = self.check_and_get_definition_file(provider_url) + #self.providers['default'] = get_config_json(provider_file) + #except (requests.HTTPError, requests.RequestException) as e: + #self.error = e.message + #except requests.ConnectionError as e: + #if e.message == "[Errno 113] No route to host": + #if not is_internet_up: # this was meant to be a function invocation I guess... - self.error = "No valid internet connection found" - else: - self.error = "Provider server appears currently down." - - def check_and_get_definition_file(self, provider_url): - """ - checks if provider definition.json file is present. - if not downloads one from the web. - """ - default_provider_path = get_default_provider_path() - - if not os.path.isdir(default_provider_path): - mkdir_p(default_provider_path) - - definition_file = get_config_file( - 'definition.json', - folder=default_provider_path) - - if os.path.isfile(definition_file): - return - - else: - r = requests.get(provider_url) - r.raise_for_status() - with open(definition_file, 'wb') as f: - f.write(json.dumps(r.json, indent=4)) - return definition_file + #self.error = "No valid internet connection found" + #else: + #self.error = "Provider server appears currently down." +# + #def check_and_get_definition_file(self, provider_url): + #""" + #checks if provider definition.json file is present. + #if not downloads one from the web. + #""" + #default_provider_path = get_default_provider_path() +# + #if not os.path.isdir(default_provider_path): + #mkdir_p(default_provider_path) +# + #definition_file = get_config_file( + #'definition.json', + #folder=default_provider_path) +# + #if os.path.isfile(definition_file): + #return +# + #else: + #r = requests.get(provider_url) + #r.raise_for_status() + #with open(definition_file, 'wb') as f: + #f.write(json.dumps(r.json, indent=4)) + #return definition_file diff --git a/src/leap/base/constants.py b/src/leap/base/constants.py index 6266c693..7a1415fb 100644 --- a/src/leap/base/constants.py +++ b/src/leap/base/constants.py @@ -1,23 +1,26 @@ """constants to be used in base module""" -APP_NAME = "leap" +from leap import __branding +APP_NAME = __branding.get("short_name", "leap") # default provider placeholder # using `example.org` we make sure that this # is not going to be resolved during the tests phases # (we expect testers to add it to their /etc/hosts -DEFAULT_TEST_PROVIDER = "testprovider.example.org" +DEFAULT_PROVIDER = __branding.get( + "provider_domain", + "testprovider.example.org") -DEFINITION_EXPECTED_PATH = "provider-definition.json" +DEFINITION_EXPECTED_PATH = "provider.json" DEFAULT_PROVIDER_DEFINITION = { - u'api_uri': u'https://api.testprovider.example.org/', + u'api_uri': u'https://api.%s/' % DEFAULT_PROVIDER, u'api_version': u'0.1.0', u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e', - u'ca_cert_uri': u'https://testprovider.example.org/cacert.pem', + u'ca_cert_uri': u'https://%s/cacert.pem' % DEFAULT_PROVIDER, u'description': {u'en': u'This is a test provider'}, u'display_name': {u'en': u'Test Provider'}, - u'domain': u'testprovider.example.org', + u'domain': u'%s' % DEFAULT_PROVIDER, u'enrollment_policy': u'open', u'public_key': u'cb7dbd679f911e85bc2e51bd44afd7308ee19c21', u'serial': 1, diff --git a/src/leap/base/providers.py b/src/leap/base/providers.py index ce30d4a4..7b219cc7 100644 --- a/src/leap/base/providers.py +++ b/src/leap/base/providers.py @@ -9,7 +9,7 @@ class LeapProviderDefinition(baseconfig.JSONLeapConfig): def _get_slug(self): provider_path = baseconfig.get_default_provider_path() return baseconfig.get_config_file( - 'provider-definition.json', + 'provider.json', folder=provider_path) def _set_slug(self, *args, **kwargs): diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py index 40461b99..bede5ea1 100644 --- a/src/leap/base/tests/test_config.py +++ b/src/leap/base/tests/test_config.py @@ -65,15 +65,15 @@ class ProviderTest(BaseLeapTest): pass -class BareHomeTestCase(ProviderTest): +# XXX depreacated. similar test in eip.checks - __name__ = "provider_config_tests_bare_home" - - # XXX review. is it still needed? - - def test_should_raise_if_missing_eip_json(self): - with self.assertRaises(exceptions.MissingConfigFileError): - config.get_config_json(os.path.join(self.home, 'eip.json')) +#class BareHomeTestCase(ProviderTest): +# + #__name__ = "provider_config_tests_bare_home" +# + #def test_should_raise_if_missing_eip_json(self): + #with self.assertRaises(exceptions.MissingConfigFileError): + #config.get_config_json(os.path.join(self.home, 'eip.json')) class ProviderDefinitionTestCase(ProviderTest): @@ -94,8 +94,10 @@ class ProviderDefinitionTestCase(ProviderTest): json.dump(eipconstants.EIP_SAMPLE_JSON, fp) -# these tests below should move to wherever -# we put the fetcher for provider files and related stuff. +# these tests below should move to +# eip.checks +# config.Configuration has been deprecated + # TODO: # - We're instantiating a ProviderTest because we're doing the home wipeoff # on setUpClass instead of the setUp (for speedup of the general cases). @@ -112,26 +114,26 @@ class ProviderDefinitionTestCase(ProviderTest): # (so we can pass mock easily). -class ProviderFetchConError(ProviderTest): - def test_connection_error(self): - with mock.patch.object(requests, "get") as mock_method: - mock_method.side_effect = requests.ConnectionError - cf = config.Configuration() - self.assertIsInstance(cf.error, str) - - -class ProviderFetchHttpError(ProviderTest): - def test_file_not_found(self): - with mock.patch.object(requests, "get") as mock_method: - mock_method.side_effect = requests.HTTPError - cf = config.Configuration() - self.assertIsInstance(cf.error, str) - - -class ProviderFetchInvalidUrl(ProviderTest): - def test_invalid_url(self): - cf = config.Configuration("ht") - self.assertTrue(cf.error) +#class ProviderFetchConError(ProviderTest): + #def test_connection_error(self): + #with mock.patch.object(requests, "get") as mock_method: + #mock_method.side_effect = requests.ConnectionError + #cf = config.Configuration() + #self.assertIsInstance(cf.error, str) +# +# +#class ProviderFetchHttpError(ProviderTest): + #def test_file_not_found(self): + #with mock.patch.object(requests, "get") as mock_method: + #mock_method.side_effect = requests.HTTPError + #cf = config.Configuration() + #self.assertIsInstance(cf.error, str) +# +# +#class ProviderFetchInvalidUrl(ProviderTest): + #def test_invalid_url(self): + #cf = config.Configuration("ht") + #self.assertTrue(cf.error) # end provider fetch tests @@ -218,7 +220,7 @@ class ConfigHelperFunctions(BaseLeapTest): config.get_default_provider_path(), os.path.expanduser( '~/.config/leap/providers/%s/' % - constants.DEFAULT_TEST_PROVIDER) + constants.DEFAULT_PROVIDER) ) # validate ip diff --git a/src/leap/base/tests/test_providers.py b/src/leap/base/tests/test_providers.py index 23f63a95..9e0ff90c 100644 --- a/src/leap/base/tests/test_providers.py +++ b/src/leap/base/tests/test_providers.py @@ -6,9 +6,11 @@ except ImportError: import os +from leap import __branding as BRANDING from leap.testing.basetest import BaseLeapTest from leap.base import providers + EXPECTED_DEFAULT_CONFIG = { "api_version": "0.1.0", "description": "test provider", @@ -45,8 +47,8 @@ class TestLeapProviderDefinition(BaseLeapTest): os.path.join( self.home, '.config', 'leap', 'providers', - 'testprovider.example.org', - 'provider-definition.json')) + '%s' % BRANDING.get('provider_domain'), + 'provider.json')) with self.assertRaises(AttributeError): self.definition.slug = 23 |