Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add elasticsearch prefix #217

Merged
merged 5 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.10"]
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.10"]
python-version: ["3.10", "3.11"]
es-version: ["6.8.23"]
task:
- name: Run tests
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- "*"

env:
POETRY_VERSION: 1.1.13
POETRY_VERSION: 1.3.1

jobs:
checks:
Expand All @@ -22,8 +22,7 @@ jobs:
max-parallel: 4
fail-fast: false
matrix:
python-version: ["3.10"]
# python-version: ["3.10", "3.11-dev"] TODO: enable when greenlet works
python-version: ["3.10", "3.11"]
os: [ubuntu-latest]
task:
- name: Lint code
Expand Down Expand Up @@ -107,8 +106,7 @@ jobs:
max-parallel: 4
fail-fast: false
matrix:
python-version: ["3.10"]
# python-version: ["3.10", "3.11-dev"] TODO: enable when greenlet works
python-version: ["3.10", "3.11"]
es:
- use: false
version: NO
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- "*"

env:
POETRY_VERSION: 1.1.13
POETRY_VERSION: 1.3.1

jobs:
coverage:
Expand All @@ -35,7 +35,7 @@ jobs:
max-parallel: 4
fail-fast: false
matrix:
python-version: ["3.10"]
python-version: ["3.10", "3.11"]
es-version: ["6.8.23"]
os: [ubuntu-latest]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Deploy docs
uses: mhausenblas/mkdocs-deploy-gh-pages@master
Expand Down
11 changes: 11 additions & 0 deletions karp/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@
GetResourcePermissions,
IsResourceProtected,
)

__all__ = [
"errors",
"User",
"AuthService",
"AuthServiceConfig",
"AccessToken",
"PermissionLevel",
"GetResourcePermissions",
"IsResourceProtected",
]
6 changes: 5 additions & 1 deletion karp/auth/application/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .resources import GetResourcePermissions, ResourcePermissionDto, IsResourceProtected
from .resources import (
GetResourcePermissions,
ResourcePermissionDto,
IsResourceProtected,
)
6 changes: 3 additions & 3 deletions karp/auth/application/queries/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@


class Scope(str, enum.Enum):
admin = 'ADMIN'
write = 'WRITE'
read = 'READ'
admin = "ADMIN"
write = "WRITE"
read = "READ"


class ResourcePermissionDto(pydantic.BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion karp/auth/domain/auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

logger = logging.getLogger("karp")


class AuthServiceConfig:
pass


class AuthService(abc.ABC):

@abc.abstractmethod
def authenticate(self, scheme: str, credentials: str) -> User:
return User("dummy", {}, {})
Expand Down
1 change: 0 additions & 1 deletion karp/auth/domain/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .user import User

20 changes: 11 additions & 9 deletions karp/auth/domain/entities/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(
self,
identifier: str,
permissions: typing.Dict[str, int],
levels: typing.Dict[str, int]
levels: typing.Dict[str, int],
):
self._identifier = identifier
self._permissions = permissions
Expand All @@ -23,16 +23,18 @@ def identifier(self) -> str:

def has_enough_permissions(self, resource_id: str, level: PermissionLevel) -> bool:
try:
return (self._permissions.get(resource_id) is not None) and (self._permissions[resource_id] >= self._levels[level])
return (self._permissions.get(resource_id) is not None) and (
self._permissions[resource_id] >= self._levels[level]
)
except KeyError:
logger.exception(
'error checking permissions',
"error checking permissions",
extra={
'resource_id': resource_id,
'level': level,
'identifier': self._identifier,
'levels': self._levels,
'permissions': self._permissions
}
"resource_id": resource_id,
"level": level,
"identifier": self._identifier,
"levels": self._levels,
"permissions": self._permissions,
},
)
raise
4 changes: 1 addition & 3 deletions karp/auth/domain/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


class AuthError(Exception):
'''Auth base exception.'''
"""Auth base exception."""


class ResourceNotFound(AuthError):
Expand Down
2 changes: 1 addition & 1 deletion karp/auth/domain/value_objects/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class AccessToken(pydantic.BaseModel):

def as_header(self) -> typing.Dict[str, str]:
return {
'Authorization': f'{self.token_type} {self.access_token}',
"Authorization": f"{self.token_type} {self.access_token}",
}
28 changes: 17 additions & 11 deletions karp/auth_infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@
GetResourcePermissions,
IsResourceProtected,
)
from karp.auth_infrastructure.queries import LexGetResourcePermissions, LexIsResourceProtected
from karp.auth_infrastructure.services import DummyAuthService, JWTAuthService, JWTAuthServiceConfig
from karp.lex.application.queries import GetPublishedResources, ReadOnlyResourceRepository
from karp.auth_infrastructure.queries import (
LexGetResourcePermissions,
LexIsResourceProtected,
)
from karp.auth_infrastructure.services import (
DummyAuthService,
JWTAuthService,
JWTAuthServiceConfig,
)
from karp.lex.application.queries import (
GetPublishedResources,
ReadOnlyResourceRepository,
)


class AuthInfrastructure(injector.Module):
@injector.provider
def resource_permissions(
self,
get_published_resources: GetPublishedResources
self, get_published_resources: GetPublishedResources
) -> GetResourcePermissions:
return LexGetResourcePermissions(get_published_resources)

@injector.provider
def is_resource_protected(
self,
resource_repo: ReadOnlyResourceRepository
self, resource_repo: ReadOnlyResourceRepository
) -> IsResourceProtected:
return LexIsResourceProtected(resource_repo)

Expand All @@ -46,10 +54,8 @@ def jwt_auth_service_config(self) -> AuthServiceConfig:

@injector.provider
def jwt_auth_service(
self,
is_resource_protected: IsResourceProtected
self, is_resource_protected: IsResourceProtected
) -> AuthService:
return JWTAuthService(
pubkey_path=self.pubkey_path,
is_resource_protected=is_resource_protected
pubkey_path=self.pubkey_path, is_resource_protected=is_resource_protected
)
24 changes: 13 additions & 11 deletions karp/auth_infrastructure/queries/lex_resources.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import typing

from karp.auth.domain import errors
from karp.auth.application.queries import GetResourcePermissions, ResourcePermissionDto, IsResourceProtected
from karp.auth.application.queries import (
GetResourcePermissions,
ResourcePermissionDto,
IsResourceProtected,
)
from karp.foundation.value_objects.permission_level import PermissionLevel
from karp.lex.application.queries import GetPublishedResources, ReadOnlyResourceRepository
from karp.lex.application.queries import (
GetPublishedResources,
ReadOnlyResourceRepository,
)


class LexGetResourcePermissions(GetResourcePermissions):
Expand Down Expand Up @@ -36,19 +43,14 @@ def query(self) -> typing.List[ResourcePermissionDto]:


class LexIsResourceProtected(IsResourceProtected):
def __init__(
self,
resource_repo: ReadOnlyResourceRepository
) -> None:
def __init__(self, resource_repo: ReadOnlyResourceRepository) -> None:
super().__init__()
self.resource_repo = resource_repo

def query(self, resource_id: str, level: PermissionLevel) -> bool:
if level in [PermissionLevel.write, PermissionLevel.admin]:
return True
resource = self.resource_repo.get_by_resource_id(
resource_id=resource_id)
resource = self.resource_repo.get_by_resource_id(resource_id=resource_id)
if not resource:
raise errors.ResourceNotFound(
f"Can't find resource '{resource_id}'")
return resource.config.get('protected', {}).get('read', False)
raise errors.ResourceNotFound(f"Can't find resource '{resource_id}'")
return resource.config.get("protected", {}).get("read", False)
1 change: 1 addition & 0 deletions karp/auth_infrastructure/services/dummy_auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

logger = logging.getLogger(__name__)


class DummyAuthService(auth_service.AuthService):
def __init__(self):
logger.warning("Using DummyAuthService: Don't use this in production!")
Expand Down
19 changes: 9 additions & 10 deletions karp/cliapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ def set_app_context(
ctx: typer.Context,
version: Optional[bool] = typer.Option(
None, "--version", callback=version_callback, is_eager=True
)
),
):
if ctx.invoked_subcommand is None:
ctx.obj = {}
else:
ctx.obj = {}
ctx.obj['connection'] = app_context.container.get(Connection)
ctx.obj['session'] = Session(bind=ctx.obj['connection'])
logger.debug('create session', extra={
'session': ctx.obj['session']})
ctx.obj['container'] = app_context.container.create_child_injector(
ctx.obj["connection"] = app_context.container.get(Connection)
ctx.obj["session"] = Session(bind=ctx.obj["connection"])
logger.debug("create session", extra={"session": ctx.obj["session"]})
ctx.obj["container"] = app_context.container.create_child_injector(
modules.request_configuration(
conn=ctx.obj['connection'],
session=ctx.obj['session'],
conn=ctx.obj["connection"],
session=ctx.obj["session"],
)
)

Expand All @@ -46,12 +45,12 @@ def set_app_context(

def version_callback(value: bool):
if value:
typer.echo(f'{config.PROJECT_NAME} CLI {config.VERSION}')
typer.echo(f"{config.PROJECT_NAME} CLI {config.VERSION}")
raise typer.Exit()


def load_commands(app: typer.Typer):
modules.load_modules('karp.clicommands', app=app)
modules.load_modules("karp.clicommands", app=app)


cliapp = create_app()
Expand Down
4 changes: 2 additions & 2 deletions karp/cliapp/typer_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import typer


T = TypeVar('T')
T = TypeVar("T")


def inject_from_ctx(klass: Type[T], ctx: typer.Context) -> T:
return ctx.obj['container'].get(klass)
return ctx.obj["container"].get(klass)
9 changes: 3 additions & 6 deletions karp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def get_loglevel(loglevel: str) -> int:
return getattr(logging, loglevel.upper(), logging.WARNING)


CONSOLE_LOG_LEVEL = config(
"CONSOLE_LOG_LEVEL", cast=get_loglevel, default="INFO")
CONSOLE_LOG_LEVEL = config("CONSOLE_LOG_LEVEL", cast=get_loglevel, default="INFO")
# LOG_TO_SLACK = strtobool(os.environ.get("LOG_TO_SLACK", "n"))
# SLACK_SECRET = os.environ.get("SLACK_SECRET")
# JWT_AUTH = strtobool(os.environ.get("JWT_AUTH", "n"))
Expand All @@ -80,8 +79,7 @@ def get_loglevel(loglevel: str) -> int:
print("Using testing jwt key")
JWT_AUTH_PUBKEY_PATH = Path(__file__).parent / "../tests/data/pubkey.pem"
else:
JWT_AUTH_PUBKEY_PATH = config(
"JWT_AUTH_PUBKEY_PATH", cast=Path, default=None)
JWT_AUTH_PUBKEY_PATH = config("JWT_AUTH_PUBKEY_PATH", cast=Path, default=None)

MYSQL_FORMAT = "mysql://{user}:{passwd}@{dbhost}/{dbname}"

Expand All @@ -96,8 +94,7 @@ class Config:
if "ELASTICSEARCH_HOST" in os.environ
else None
)
ELASTICSEARCH_ENABLED = strtobool(
os.environ.get("ELASTICSEARCH_ENABLED", "n"))
ELASTICSEARCH_ENABLED = strtobool(os.environ.get("ELASTICSEARCH_ENABLED", "n"))
CONSOLE_LOG_LEVEL = getattr(
logging, os.environ.get("CONSOLE_LOG_LEVEL", "INFO"), logging.INFO
)
Expand Down
5 changes: 2 additions & 3 deletions karp/db_infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ def __init__(self, db_url: str) -> None:
kwargs = {}
if str(db_url).startswith("sqlite"):
kwargs["poolclass"] = pool.SingletonThreadPool
kwargs['connect_args'] = {
'check_same_thread': False
}
kwargs["connect_args"] = {"check_same_thread": False}
self._engine = create_engine(db_url, echo=True, future=True, **kwargs)
self.session_factory = orm.sessionmaker(
autocommit=False,
Expand All @@ -19,6 +17,7 @@ def __init__(self, db_url: str) -> None:
def disconnect(self):
pass


# @contextmanager
# def session(self) -> Callable[..., AbstractContextManager[Session]]:
# session: Session = self._session_factory()
Expand Down
1 change: 0 additions & 1 deletion karp/db_infrastructure/sql_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ def unset_session(self):
def _check_has_session(self):
if self._session is None:
raise RuntimeError("No session, use with unit_of_work.")

Loading