summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/services/__init__.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2013-09-18 12:34:31 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2013-09-18 12:34:31 -0300
commit9f0968da51cc9a16577c563887ae336066ddc0eb (patch)
tree7f0e744a12e0b67fab2818216a353b54bc8ba730 /src/leap/bitmask/services/__init__.py
parent735111cee1c28ccc07c87d806bc5412aaffcedf7 (diff)
parent13f5d8fcee038f441dd91ef16dfdb254e1f0dd3f (diff)
Merge remote-tracking branch 'kali/bug/start_smtp_without_eip' into develop
Diffstat (limited to 'src/leap/bitmask/services/__init__.py')
-rw-r--r--src/leap/bitmask/services/__init__.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/leap/bitmask/services/__init__.py b/src/leap/bitmask/services/__init__.py
index 339f9cc6..2646235d 100644
--- a/src/leap/bitmask/services/__init__.py
+++ b/src/leap/bitmask/services/__init__.py
@@ -17,8 +17,22 @@
"""
Services module.
"""
+import logging
+import os
+
from PySide import QtCore
+
+from leap.bitmask.crypto.srpauth import SRPAuth
+from leap.bitmask.util.constants import REQUEST_TIMEOUT
from leap.bitmask.util.privilege_policies import is_missing_policy_permissions
+from leap.bitmask.util.request_helpers import get_content
+
+from leap.common.check import leap_assert
+from leap.common.config.baseconfig import BaseConfig
+from leap.common.files import get_mtime
+
+logger = logging.getLogger(__name__)
+
DEPLOYED = ["openvpn", "mx"]
@@ -70,3 +84,88 @@ def get_supported(services):
:rtype: list of str
"""
return filter(lambda s: s in DEPLOYED, services)
+
+
+def download_service_config(provider_config, service_config,
+ session,
+ download_if_needed=True):
+ """
+ Downloads config for a given service.
+
+ :param provider_config: an instance of ProviderConfig
+ :type provider_config: ProviderConfig
+
+ :param service_config: an instance of a particular Service config.
+ :type service_config: BaseConfig
+
+ :param session: an instance of a fetcher.session
+ (currently we're using requests only, but it can be
+ anything that implements that interface)
+ :type session: requests.sessions.Session
+ """
+ service_name = service_config.name
+ service_json = "{0}-service.json".format(service_name)
+ headers = {}
+ mtime = get_mtime(os.path.join(service_config.get_path_prefix(),
+ "leap",
+ "providers",
+ provider_config.get_domain(),
+ service_json))
+ if download_if_needed and mtime:
+ headers['if-modified-since'] = mtime
+
+ api_version = provider_config.get_api_version()
+
+ config_uri = "%s/%s/config/%s-service.json" % (
+ provider_config.get_api_uri(),
+ api_version,
+ service_name)
+ logger.debug('Downloading %s config from: %s' % (
+ service_name.upper(),
+ config_uri))
+
+ # XXX make and use @with_srp_auth decorator
+ srp_auth = SRPAuth(provider_config)
+ session_id = srp_auth.get_session_id()
+ cookies = None
+ if session_id:
+ cookies = {"_session_id": session_id}
+
+ res = session.get(config_uri,
+ verify=provider_config.get_ca_cert_path(),
+ headers=headers,
+ timeout=REQUEST_TIMEOUT,
+ cookies=cookies)
+ res.raise_for_status()
+
+ service_config.set_api_version(api_version)
+
+ # Not modified
+ service_path = ("leap", "providers", provider_config.get_domain(),
+ service_json)
+ if res.status_code == 304:
+ logger.debug(
+ "{0} definition has not been modified".format(
+ service_name.upper()))
+ service_config.load(os.path.join(*service_path))
+ else:
+ service_definition, mtime = get_content(res)
+ service_config.load(data=service_definition, mtime=mtime)
+ service_config.save(service_path)
+
+
+class ServiceConfig(BaseConfig):
+ """
+ Base class used by the different service configs
+ """
+
+ _service_name = None
+
+ @property
+ def name(self):
+ """
+ Getter for the service name.
+ Derived classes should assign it.
+ """
+ leap_assert(self._service_name is not None)
+ return self._service_name