Skip to content

Commit be35d37

Browse files
Hang Lesampsapenna
andcommitted
Allow anonymous get to id cache for discovery (merge commit)
Merge branch 'bugfix/expose-id-get' into 'main' * allow x-project access when fetching public keys via CLI * allow anonymous get to id cache for discovery Closes #1258 See merge request https://gitlab.ci.csc.fi/sds-dev/sd-connect/swift-browser-ui/-/merge_requests/413 Approved-by: Hang Le <lhang@csc.fi> Co-authored-by: Sampsa Penna <sampsa.penna@csc.fi> Merged by Hang Le <lhang@csc.fi>
2 parents 3da2964 + 5936371 commit be35d37

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

swift_browser_ui/common/common_middleware.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ async def handle_validate_authentication(
6565
handler: swift_browser_ui.common.types.AiohttpHandler,
6666
) -> aiohttp.web.Response:
6767
"""Handle the authentication of a response as a middleware function."""
68+
# TODO: better configuration for conditional skipping of anonymous endpoints
6869
if request.path == "/health":
6970
return await handler(request)
71+
if "/ids/" in request.path and request.method in {"GET", "OPTIONS"}:
72+
return await handler(request)
7073

7174
try:
7275
signature = request.query["signature"]
@@ -83,7 +86,11 @@ async def handle_validate_authentication(
8386

8487
if "db_conn" in request.app:
8588
project = ""
86-
if "project" in request.match_info:
89+
if "/keys" in request.path and request.method == "GET" and "for" in request.query:
90+
project = request.query["for"]
91+
LOGGER.debug("Using x-project access for project public key.")
92+
LOGGER.debug(f"Using {project} as project for request token.")
93+
elif "project" in request.match_info:
8794
LOGGER.debug(f"Using main project for {request}.")
8895
project = request.match_info["project"]
8996
elif "owner" in request.match_info:

swift_browser_ui/common/vault_client.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from typing import Any, Dict, List
1010

1111
from aiohttp import ClientSession, ClientTimeout
12-
from aiohttp.web import HTTPError, HTTPGatewayTimeout, HTTPInternalServerError
12+
from aiohttp.web import (
13+
HTTPError,
14+
HTTPGatewayTimeout,
15+
HTTPInternalServerError,
16+
HTTPNotFound,
17+
)
1318
from yarl import URL
1419

1520
from swift_browser_ui.ui.settings import setd
@@ -230,7 +235,7 @@ async def _request(
230235
message = "Unexpected issue when connecting to service provider."
231236
raise VaultServerError(text=message, reason=message) from exc
232237

233-
async def get_public_key(self, project: str) -> str:
238+
async def get_public_key(self, project: str, skip_create: bool = False) -> str:
234239
"""Get a project specific public key.
235240
236241
If a key is not found for a project, creates a new one and fetches it.
@@ -249,7 +254,14 @@ async def get_key() -> str:
249254

250255
LOGGER.debug("Getting public key for project %r", project)
251256
key = await get_key()
252-
if not key:
257+
if not key and skip_create:
258+
LOGGER.debug(
259+
"No key: %s found for project % r, but not creating a new one for foreign access.",
260+
key,
261+
project,
262+
)
263+
raise HTTPNotFound
264+
elif not key and not skip_create:
253265
LOGGER.debug(
254266
"No key: %s found for project %r, creating a new one.", key, project
255267
)

swift_browser_ui/upload/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ async def handle_project_key(request: aiohttp.web.Request) -> aiohttp.web.Respon
282282
"""Answer project specific encryption keys."""
283283
vault_client: VaultClient = request.app[VAULT_CLIENT]
284284
project = request.match_info["project"]
285-
public_key = await vault_client.get_public_key(project)
285+
# Skip creating public keys for x-project access
286+
skip_create = "for" in request.query
287+
public_key = await vault_client.get_public_key(project, skip_create=skip_create)
286288

287289
return aiohttp.web.Response(
288290
text=public_key,

0 commit comments

Comments
 (0)