Beispiel #1
0
 def validate_config_credentials(self):
     """Validate configured credentials"""
     # No credentials dict in the config
     try:
         credentials = self.config.credentials
     except AttributeError:
         raise MisconfiguredError(
             f"Missing credentials configuration for provider {self.provider}"
         )
     # Empty credentials dict
     if not credentials:
         raise MisconfiguredError(
             f"Missing credentials for provider {self.provider}"
         )
     # Credentials keys but values are None.
     missing_credentials = [
         cred_name
         for cred_name, cred_value in credentials.items()
         if cred_value is None
     ]
     if missing_credentials:
         raise MisconfiguredError(
             "The following credentials are missing for provider {}: {}".format(
                 self.provider, ", ".join(missing_credentials)
             )
         )
Beispiel #2
0
 def __init__(self, provider, config):
     super(OIDCAuthorizationCodeFlowAuth, self).__init__(provider, config)
     if getattr(self.config, "token_provision",
                None) not in ("qs", "header"):
         raise MisconfiguredError(
             'Provider config parameter "token_provision" must be one of "qs" or "header"'
         )
     if self.config.token_provision == "qs" and not getattr(
             self.config, "token_qs_key", ""):
         raise MisconfiguredError(
             'Provider config parameter "token_provision" with value "qs" must have '
             '"token_qs_key" config parameter as well')
     self.session = requests.Session()
Beispiel #3
0
    def authenticate_user(self, state):
        """Authenticate user"""
        params = {
            "client_id": self.config.client_id,
            "response_type": self.RESPONSE_TYPE,
            "scope": self.SCOPE,
            "state": state,
            "redirect_uri": self.config.redirect_uri,
        }
        authorization_response = self.session.get(
            self.config.authorization_uri, params=params)

        login_document = etree.HTML(authorization_response.text)
        login_form = login_document.xpath(self.config.login_form_xpath)[0]
        try:
            # Get the form data to pass to the login form from config or from the login form
            login_data = {
                key: self._constant_or_xpath_extracted(value, login_form)
                for key, value in getattr(
                    self.config, "additional_login_form_data", {}).items()
            }
            # Add the credentials
            login_data.update(self.config.credentials)
            auth_uri = getattr(self.config, "authentication_uri", None)
            # Retrieve the authentication_uri from the login form if so configured
            if self.config.authentication_uri_source == "login-form":
                # Given that the login_form_xpath resolves to an HTML element, if suffices to add '/@action' to get
                # the value of its action attribute to this xpath
                auth_uri = login_form.xpath(
                    self.config.login_form_xpath.rstrip("/") + "/@action")[0]
            return self.session.post(auth_uri, data=login_data)
        except AttributeError as err:
            if "credentials" in err.args:
                raise MisconfiguredError(
                    "Missing Credentials for provider: %s", self.provider)
 def __init__(self, config):
     super(FilterLatestByName, self).__init__(config)
     name_pattern = config.pop("name_pattern")
     if not self.NAME_PATTERN_CONSTRAINT.search(name_pattern):
         raise MisconfiguredError(
             "Name pattern should respect the regex: {}".format(
                 self.NAME_PATTERN_CONSTRAINT.pattern))
     self.name_pattern = re.compile(name_pattern)
Beispiel #5
0
 def authenticate(self):
     """Authenticate"""
     try:
         self.access_key = self.config.credentials["aws_access_key_id"]
         self.secret_key = self.config.credentials["aws_secret_access_key"]
         return self.access_key, self.secret_key
     except AttributeError as err:
         if "credentials" in err:
             raise MisconfiguredError(
                 "Missing Credentials for provider: %s", self.provider)
Beispiel #6
0
 def authenticate(self):
     """Authenticate"""
     try:
         headers = {
             header: value.format(**self.config.credentials)
             for header, value in self.config.headers.items()
         }
         return HeaderAuth(headers)
     except AttributeError as err:
         if "credentials" in err:
             raise MisconfiguredError(
                 "Missing Credentials for provider: %s", self.provider
             )
Beispiel #7
0
 def _init_api(self) -> None:
     """Initialize Sentinelsat API if needed (connection and link)."""
     if not self.api:
         try:
             logger.debug("Initializing Sentinelsat API")
             self.api = SentinelAPI(
                 self.config.credentials["username"],
                 self.config.credentials["password"],
                 self.config.endpoint,
             )
         except KeyError as ex:
             raise MisconfiguredError(ex) from ex
     else:
         logger.debug("Sentinelsat API already initialized")
Beispiel #8
0
    def __filter_item_model_properties(self, item_model, product_type):
        """Filter item model depending on product type metadata and its extensions.
        Removes not needed parameters, and adds supplementary ones as
        part of eodag extension.

        :param item_model: item model from stac_config
        :type item_model: dict
        :param product_type: product type
        :type product_type: str

        :returns: filtered item model
        :rtype: dict
        """
        try:
            product_type_dict = [
                pt for pt in self.eodag_api.list_product_types(
                    provider=self.provider) if pt["ID"] == product_type
            ][0]
        except IndexError:
            raise MisconfiguredError(
                "Product type {} not available for {}".format(
                    product_type, self.provider))

        result_item_model = copy.deepcopy(item_model)

        if product_type_dict["sensorType"] != "RADAR":
            result_item_model["stac_extensions"].remove("sar")

        extensions_names = self.get_stac_extensions_dict(
            result_item_model["stac_extensions"]).keys()
        for k, v in item_model["properties"].items():
            # remove key if extension not in stac_extensions
            if ":" in k and k.split(":")[0] not in extensions_names:
                result_item_model["properties"].pop(k, None)

        # build jsonpath for eodag product properties and adapt path
        eodag_properties_dict = {
            k: string_to_jsonpath(k, v.replace("$.", "$.product."))
            for k, v in DEFAULT_METADATA_MAPPING.items()
            if "$.properties." in v
        }
        # add missing properties as oseo:missingProperty
        for k, v in eodag_properties_dict.items():
            if (v not in result_item_model["properties"].values()
                    and k not in self.stac_config["metadata_ignore"]):
                result_item_model["properties"]["oseo:" +
                                                k] = string_to_jsonpath(k, v)

        return result_item_model
Beispiel #9
0
 def authenticate(self):
     """Authenticate"""
     # First get the token
     try:
         response = requests.post(self.config.auth_uri,
                                  data=self.config.credentials)
         try:
             response.raise_for_status()
         except HTTPError as e:
             raise e
         else:
             if getattr(self.config, "token_type", "text") == "json":
                 token = response.json()[self.config.token_key]
             else:
                 token = response.text
             return RequestsTokenAuth(token, "header")
     except AttributeError as err:
         if "credentials" in err.args:
             raise MisconfiguredError(
                 "Missing Credentials for provider: %s", self.provider)
Beispiel #10
0
 def authenticate(self):
     """Authenticate"""
     method = getattr(self.config, "method", None)
     try:
         if not method:
             method = "basic"
         if method == "basic":
             return HTTPBasicAuth(
                 self.config.credentials["username"],
                 self.config.credentials["password"],
             )
         if method == "digest":
             return HTTPDigestAuth(
                 self.config.credentials["username"],
                 self.config.credentials["password"],
             )
     except AttributeError as err:
         if "credentials" in err.args:
             raise MisconfiguredError(
                 "Missing Credentials for provider: %s", self.provider
             )
Beispiel #11
0
 def __init__(self, provider, config):
     super(HTTPDownload, self).__init__(provider, config)
     if not hasattr(self.config, "base_uri"):
         raise MisconfiguredError(
             "{} plugin require a base_uri configuration key".format(self.__name__)
         )