Skip to content

Commit

Permalink
Merge pull request #202 from octoenergy/improve_creds_related_errors
Browse files Browse the repository at this point in the history
Improve credentials parsing errors
  • Loading branch information
capitancambio authored Apr 5, 2023
2 parents 2538174 + ab1cecf commit 28b8eb6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2023-04-05
### Changed
- Credential files error reporting to help users identify the credentials issues

## [1.0.9] - 2023-01-25
### Added
- Add `tentaclio.streams.api.make_empty_safe` to modify the standard behavoir of creating
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from setuptools.command.install import install


VERSION = "1.0.9"
VERSION = "1.1.0"

REPO_ROOT = pathlib.Path(__file__).parent

Expand Down
1 change: 1 addition & 0 deletions src/tentaclio/credentials/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Credential management module."""
from .api import * # noqa
from .injection import * # noqa
from .reader import TentaclioFileError # noqa
46 changes: 42 additions & 4 deletions src/tentaclio/credentials/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
TENTACLIO_SECRETS_FILE = "TENTACLIO__SECRETS_FILE"


class TentaclioFileError(Exception):
"""Tentaclio secrets file errors."""

TENTACLIO_FILE = os.getenv(TENTACLIO_SECRETS_FILE, "<unknown file>")
ERROR_TEMPLATE = """
#########################################
Your tentaclio secrets file is malformed:
File: {tentaclio_file}
{message}
Check https://github.com/octoenergy/tentaclio#credentials-file for more info about this file
"""

def __init__(self, message: str):
"""Intialise a new TentaclioFileError."""
message = self.ERROR_TEMPLATE.format(message=message, tentaclio_file=self.TENTACLIO_FILE)
super().__init__(message)


def add_credentials_from_env_file(
injector: injection.CredentialsInjector,
) -> injection.CredentialsInjector:
Expand All @@ -29,10 +51,27 @@ def add_credentials_from_env_file(
return injector


def _process_mark_error(error: yaml.MarkedYAMLError) -> str:
error_str = ""
if error.problem_mark is not None:
error_str += str(error.problem) + "\n"
error_str += str(error.problem_mark) + "\n"
if error.context_mark is not None:
error_str += str(error.context) + "\n"
error_str += str(error.context_mark) + "\n"
return error_str


def _load_creds_from_yaml(yaml_reader: protocols.Reader) -> dict:
loaded_data = yaml.safe_load(io.StringIO(yaml_reader.read()))
try:
loaded_data = yaml.safe_load(io.StringIO(yaml_reader.read()))
except yaml.MarkedYAMLError as error:
raise TentaclioFileError(_process_mark_error(error))

if SECRETS not in loaded_data:
raise KeyError(f"no secrets in yaml data. Make sure the file has a '{SECRETS}' element")
raise TentaclioFileError(
"No secrets in yaml data. Make sure the file has a `secrets:` element"
)
return loaded_data[SECRETS]


Expand All @@ -43,8 +82,7 @@ def _load_from_file(
with open(path, "r") as f:
return add_credentials_from_reader(injector, f)
except IOError as e:
logger.error("Error while loading secrets file {path}")
raise e
raise TentaclioFileError("File not found") from e


def add_credentials_from_reader(
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/credentials/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from tentaclio import urls
from tentaclio.credentials import injection, reader
from tentaclio.credentials import TentaclioFileError, injection, reader


@pytest.fixture
Expand Down Expand Up @@ -32,13 +32,13 @@ def creds_yaml_bad_url():


def test_bad_yaml():
with pytest.raises(Exception):
with pytest.raises(TentaclioFileError):
data = io.StringIO("sadfsaf")
reader.add_credentials_from_reader(injection.CredentialsInjector(), data)


def test_no_credentials_in_file(no_creds_yaml):
with pytest.raises(KeyError):
with pytest.raises(TentaclioFileError):
data = io.StringIO(no_creds_yaml)
reader.add_credentials_from_reader(injection.CredentialsInjector(), data)

Expand Down

0 comments on commit 28b8eb6

Please sign in to comment.