Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #50 from CSCfi/devel
Browse files Browse the repository at this point in the history
bump to 0.5.8
  • Loading branch information
teemukataja authored Dec 1, 2020
2 parents 9fdff8d + 1d8dcd8 commit 9dcf92d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
max-parallel: 4
matrix:
os: [ubuntu-latest]
python-version: [3.7]
python-version: [3.7, 3.8]

runs-on: ${{ matrix.os }}

Expand All @@ -24,5 +24,7 @@ jobs:
pip install tox tox-gh-actions
- name: Test flake8 syntax with tox
run: tox -e flake8
- name: Test mypy typing with tox
run: tox -e mypy
- name: bandit static check
run: tox -e bandit
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
max-parallel: 4
matrix:
os: [ubuntu-latest]
python-version: [3.6, 3.7]
python-version: [3.7, 3.8]

runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion swift_x_account_sharing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


__name__ = "swift_x_account_sharing"
__version__ = "0.5.7"
__version__ = "0.5.8"
__author__ = "CSC Developers"
__license__ = "MIT License"
26 changes: 18 additions & 8 deletions swift_x_account_sharing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async def has_access_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle has-access endpoint query."""
access_list = []
try:
access_list = await request.app["db_conn"].get_access_list(
request.match_info["user"]
Expand All @@ -35,6 +36,7 @@ async def access_details_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle access-details endpoint query."""
access_details = dict()
try:
access_details = \
await request.app["db_conn"].get_access_container_details(
Expand All @@ -56,6 +58,7 @@ async def gave_access_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle gave-access endpoint query."""
shared_list = []
try:
shared_list = await request.app["db_conn"].get_shared_list(
request.match_info["owner"]
Expand All @@ -76,6 +79,7 @@ async def shared_details_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle shared-details endpoint query."""
shared_details = dict()
try:
shared_details = \
await request.app["db_conn"].get_shared_container_details(
Expand All @@ -97,6 +101,7 @@ async def share_container_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle share-container endpoint query."""
shared: bool = False
try:
shared = await request.app["db_conn"].add_share(
request.match_info["owner"],
Expand All @@ -110,7 +115,8 @@ async def share_container_handler(

MODULE_LOGGER.log(
logging.DEBUG,
"Added following new shares: %s", str(shared)
"Added following new shared containers: %s",
str(request.match_info["container"])
)

return aiohttp.web.json_response(shared)
Expand All @@ -120,6 +126,7 @@ async def edit_share_handler(
request: aiohttp.web.Request
) -> aiohttp.web.Response:
"""Handle container shared rights editions."""
edited: bool = False
try:
edited = await request.app["db_conn"].edit_share(
request.match_info["owner"],
Expand All @@ -132,7 +139,8 @@ async def edit_share_handler(

MODULE_LOGGER.log(
logging.DEBUG,
"Edited following shares: %s", str(edited)
"Edited following shared containers: %s",
str(request.match_info["container"])
)

return aiohttp.web.json_response(edited)
Expand All @@ -143,7 +151,7 @@ async def delete_share_handler(
) -> aiohttp.web.Response:
"""Handle unshare-container endpoint query."""
try:
deleted = await request.app["db_conn"].delete_share(
await request.app["db_conn"].delete_share(
request.match_info["owner"],
request.match_info["container"],
request.query["user"].split(",")
Expand All @@ -158,7 +166,8 @@ async def delete_share_handler(

MODULE_LOGGER.log(
logging.DEBUG,
"Deleted following shares: %s", str(deleted)
"Deleted following shared containers: %s",
str(request.match_info["container"])
)

return aiohttp.web.Response(
Expand All @@ -172,7 +181,7 @@ async def delete_container_shares_handler(
) -> aiohttp.web.Response:
"""Delete all shares from a container."""
try:
deleted = await request.app["db_conn"].delete_container_shares(
await request.app["db_conn"].delete_container_shares(
request.match_info["owner"],
request.match_info["container"]
)
Expand All @@ -181,7 +190,8 @@ async def delete_container_shares_handler(

MODULE_LOGGER.log(
logging.DEBUG,
"Deleted following shares: %s", str(deleted)
"Deleted following shared container: %s",
str(request.match_info["container"])
)

return aiohttp.web.Response(
Expand All @@ -202,7 +212,7 @@ async def handle_user_add_token(
except KeyError:
try:
formdata = await request.post()
token = formdata["token"]
token = str(formdata["token"])
except KeyError:
raise aiohttp.web.HTTPBadRequest(
reason="No token present"
Expand Down Expand Up @@ -243,7 +253,7 @@ async def handle_user_list_tokens(
) -> aiohttp.web.Response:
"""Get project token listing."""
project = request.match_info["project"]

tokens = []
try:
tokens = await request.app["db_conn"].get_tokens(project)
except InterfaceError:
Expand Down
5 changes: 3 additions & 2 deletions swift_x_account_sharing/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

async def read_in_keys(
app: aiohttp.web.Application
):
) -> None:
"""Read in keys to the application."""
keys = os.environ.get("SWIFT_UI_API_AUTH_TOKENS", None)
app["tokens"] = keys.split(",") if keys is not None else []
Expand All @@ -51,7 +51,7 @@ async def test_signature(
signature: str,
message: str,
validity: str,
):
) -> None:
"""Validate signature against the given tokens."""
# Check signature expiration
if int(validity) < time.time():
Expand Down Expand Up @@ -87,6 +87,7 @@ async def handle_validate_authentication(
reason="Query string missing validity or signature."
)

project: typing.Union[None, str]
project_tokens = []
try:
project = request.match_info["project"]
Expand Down
6 changes: 3 additions & 3 deletions swift_x_account_sharing/bindings/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class SwiftXAccountSharing:
def __init__(
self,
url: str
):
) -> None:
"""."""
self.url = url
self.session = aiohttp.ClientSession()

async def __aenter__(self):
async def __aenter__(self) -> 'SwiftXAccountSharing':
"""."""
return self

async def __aexit__(self, *excinfo):
async def __aexit__(self, *excinfo: BaseException) -> None:
"""."""
await self.session.close()

Expand Down
2 changes: 1 addition & 1 deletion swift_x_account_sharing/bindings/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def sign_api_request(
to_sign = (valid_until + path).encode("utf-8")

digest = hmac.new(
key=os.environ.get("SWIFT_UI_API_KEY").encode("utf-8"),
key=os.environ.get("SWIFT_UI_API_KEY", "").encode("utf-8"),
msg=to_sign,
digestmod="sha256"
).hexdigest()
Expand Down
16 changes: 8 additions & 8 deletions swift_x_account_sharing/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def handle_dropped_connection(
request: aiohttp.web.Request
):
) -> None:
"""Handle dropped database connection."""
MODULE_LOGGER.log(
logging.ERROR,
Expand All @@ -34,16 +34,16 @@ def handle_dropped_connection(
class DBConn:
"""Class for the account sharing database functionality."""

def __init__(self):
def __init__(self) -> None:
"""Initialize connection variable."""
self.conn = None
self.conn: asyncpg.connection.Connection = None
self.log = MODULE_LOGGER

def erase(self):
def erase(self) -> None:
"""Erase the connection."""
self.conn = None

async def open(self):
async def open(self) -> None:
"""Initialize the database connection."""
while self.conn is None:
try:
Expand Down Expand Up @@ -82,7 +82,7 @@ async def open(self):
slp = random.randint(5, 15) # nosec
await asyncio.sleep(slp)

async def close(self):
async def close(self) -> None:
"""Safely close the database connection."""
if self.conn is not None:
await self.conn.close()
Expand Down Expand Up @@ -341,7 +341,7 @@ async def revoke_token(
self,
token_owner: str,
token_identifier: str
):
) -> None:
"""Remove a token from the database."""
async with self.conn.transaction():
await self.conn.execute(
Expand All @@ -361,7 +361,7 @@ async def add_token(
token_owner: str,
token: str,
identifier: str
):
) -> None:
"""Add a token to the database."""
async with self.conn.transaction():
await self.conn.execute(
Expand Down
5 changes: 4 additions & 1 deletion swift_x_account_sharing/preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@


import aiohttp.web
import typing


async def handle_delete_preflight(_) -> aiohttp.web.Response:
async def handle_delete_preflight(
_: typing.Union[aiohttp.web.Request, None]
) -> aiohttp.web.Response:
"""Serve correct response headers to allowed DELETE preflight query."""
resp = aiohttp.web.Response(
headers={
Expand Down
20 changes: 10 additions & 10 deletions swift_x_account_sharing/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import logging
import asyncio

import typing

import aiohttp.web
import uvloop
Expand Down Expand Up @@ -44,14 +44,14 @@

async def resume_on_start(
app: aiohttp.web.Application
):
) -> None:
"""Resume old instance from start."""
await app["db_conn"].open()


async def save_on_shutdown(
app: aiohttp.web.Application
):
) -> None:
"""Flush the database on shutdown."""
if app["db_conn"] is not None:
await app["db_conn"].close()
Expand All @@ -61,10 +61,10 @@ async def init_server() -> aiohttp.web.Application:
"""Initialize the server."""
app = aiohttp.web.Application(
middlewares=[
add_cors,
check_db_conn,
handle_validate_authentication,
catch_uniqueness_error,
add_cors, # type:ignore
check_db_conn, # type:ignore
handle_validate_authentication, # type:ignore
catch_uniqueness_error, # type:ignore
]
)

Expand Down Expand Up @@ -104,8 +104,8 @@ async def init_server() -> aiohttp.web.Application:


def run_server_devel(
app: aiohttp.web.Application
):
app: typing.Coroutine[typing.Any, typing.Any, aiohttp.web.Application]
) -> None:
"""Run the server in development mode (without HTTPS)."""
aiohttp.web.run_app(
app,
Expand All @@ -114,7 +114,7 @@ def run_server_devel(
)


def main():
def main() -> None:
"""Run the server with the default run function."""
if sys.version_info < (3, 6):
logging.error("swift-x-account-sharing requires >= python3.6")
Expand Down
25 changes: 20 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = flake8, bandit, pytest
envlist = flake8, bandit, pytest, mypy
skipdist = True

[testenv:bandit]
Expand All @@ -8,14 +8,29 @@ deps = bandit
commands = bandit -r ./swift_x_account_sharing

[flake8]
ignore = W503,D203,D212,D100,D104
ignore = W503,D203,D212,D100,D104,ANN101
exclude = .git/, ./venv/, ./.tox/, build/, swift_x_account_sharing.egg-info/
# Not using type hints in tests, ignore all errors
per-file-ignores =
tests/*:ANN

[testenv:flake8]
skip_install = True
deps =
flake8
flake8-docstrings
commands = flake8 .
flake8-annotations
commands = flake8 swift_x_account_sharing tests

[testenv:mypy]
skip_install = true
deps =
-rrequirements.txt
mypy
# Mypy fails if 3rd party library doesn't have type hints configured.
# Alternative to ignoring imports would be to write custom stub files, which
# could be done at some point.
commands = mypy --ignore-missing-imports swift_x_account_sharing/

[testenv:pytest]
deps =
Expand All @@ -31,5 +46,5 @@ deps =

[gh-actions]
python =
3.6: pytest
3.7: flake8, pytest, bandit
3.8: pytest, mypy
3.7: flake8, pytest, bandit, mypy

0 comments on commit 9dcf92d

Please sign in to comment.