Skip to content

Commit

Permalink
add error handling if env for credentials file not set
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Mar 25, 2024
1 parent 8ee9707 commit 8469424
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
3 changes: 2 additions & 1 deletion logprep/util/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
from logprep.factory_error import FactoryError, InvalidConfigurationError
from logprep.processor.base.exceptions import InvalidRuleDefinitionError
from logprep.util import getter
from logprep.util.credentials import CredentialsEnvNotFoundError
from logprep.util.defaults import DEFAULT_CONFIG_LOCATION
from logprep.util.getter import GetterFactory, GetterNotFoundError
from logprep.util.json_handling import list_json_files_in_directory
Expand Down Expand Up @@ -431,7 +432,7 @@ def from_sources(cls, config_paths: Iterable[str] = None) -> "Configuration":
try:
config = Configuration.from_source(config_path)
configs.append(config)
except (GetterNotFoundError, RequestException) as error:
except (GetterNotFoundError, RequestException, CredentialsEnvNotFoundError) as error:
raise ConfigGetterException(f"{config_path} {error}") from error
except FileNotFoundError as error:
raise ConfigGetterException(
Expand Down
4 changes: 4 additions & 0 deletions logprep/util/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class CredentialsBadRequestError(Exception):
"""Raised when the API returns a 400 Bad Request error"""


class CredentialsEnvNotFoundError(Exception):
"""Raised when the API returns a 401 Not Found"""


class CredentialsFactory:
"""Factory class to create credentials for a given target URL."""

Expand Down
1 change: 1 addition & 0 deletions logprep/util/defaults.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Default values for logprep."""

DEFAULT_CONFIG_LOCATION = "file:///etc/logprep/pipeline.yml"
ENV_NAME_LOGPREP_CREDENTIALS_FILE = "LOGPREP_CREDENTIALS_FILE"
28 changes: 23 additions & 5 deletions logprep/util/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
from typing import Tuple
from urllib.parse import urlparse

import requests
from attrs import define, field, validators

from logprep._version import get_versions
from logprep.abc.exceptions import LogprepException
from logprep.abc.getter import Getter
from logprep.util.credentials import Credentials, CredentialsFactory
from logprep.util.credentials import (
Credentials,
CredentialsEnvNotFoundError,
CredentialsFactory,
)
from logprep.util.defaults import ENV_NAME_LOGPREP_CREDENTIALS_FILE


class GetterNotFoundError(LogprepException):
Expand Down Expand Up @@ -114,7 +120,7 @@ def __attrs_post_init__(self):
raise NotImplementedError(
"Basic auth credentials via commandline are not supported."
"Please use the credential file in connection with the "
"environment variable 'LOGPREP_CREDENTIALS_FILE' to authenticate."
f"environment variable '{ENV_NAME_LOGPREP_CREDENTIALS_FILE}' to authenticate."
)

@property
Expand All @@ -124,9 +130,9 @@ def url(self) -> str:

@property
def credentials(self) -> Credentials:
"""get credentials for target from environment variable LOGPREP_CREDENTIALS_FILE"""
"""get credentials for target from environment variable"""
creds = None
if "LOGPREP_CREDENTIALS_FILE" in os.environ:
if ENV_NAME_LOGPREP_CREDENTIALS_FILE in os.environ:
creds = CredentialsFactory.from_target(self.url)
return creds if creds else Credentials()

Expand All @@ -139,5 +145,17 @@ def get_raw(self) -> bytearray:
self._credentials_registry.update({domain_uri: self.credentials})
session = self._credentials_registry.get(domain_uri).get_session()
resp = session.get(url=self.url, timeout=5, allow_redirects=True, headers=self._headers)
resp.raise_for_status()
try:
resp.raise_for_status()
except requests.exceptions.HTTPError as error:
if error.response.status_code == 401:
if not os.environ.get(ENV_NAME_LOGPREP_CREDENTIALS_FILE):
raise CredentialsEnvNotFoundError(
(
"Credentials file not found. "
"Please set the environment variable "
f"'{ENV_NAME_LOGPREP_CREDENTIALS_FILE}'"
)
) from error
raise error
return resp.content
31 changes: 30 additions & 1 deletion tests/unit/util/test_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ruamel.yaml import YAML

from logprep._version import get_versions
from logprep.util.credentials import Credentials
from logprep.util.credentials import Credentials, CredentialsEnvNotFoundError
from logprep.util.getter import (
FileGetter,
GetterFactory,
Expand Down Expand Up @@ -516,3 +516,32 @@ def test_get_raw_refreshes_token_if_expired(self, tmp_path):
datetime.now() - timedelta(seconds=3600)
)
return_content = http_getter.get_json()

@responses.activate
def test_get_raw_raises_if_credential_file_env_not_set_and_unauthorizes(self):
domain = str(uuid.uuid4())
responses.add(
responses.GET,
f"https://{domain}/bar",
status=401,
)
with pytest.raises(CredentialsEnvNotFoundError):
http_getter: HttpGetter = GetterFactory.from_string(f"https://{domain}/bar")
http_getter.get_json()

@responses.activate
def test_get_raw_raises_if_credential_file_env_set_and_unauthorizes(self):
domain = str(uuid.uuid4())
responses.add(
responses.GET,
f"https://{domain}/bar",
status=401,
)
with pytest.raises(requests.exceptions.HTTPError) as error:
http_getter: HttpGetter = GetterFactory.from_string(f"https://{domain}/bar")
with mock.patch.dict(
"os.environ",
{"LOGPREP_CREDENTIALS_FILE": "quickstart/exampledata/config/credentials.yml"},
):
http_getter.get_json()
assert error.value.response.status_code == 401

0 comments on commit 8469424

Please sign in to comment.