Skip to content

Commit

Permalink
simplify user model and remove gafaelfawr interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
athornton committed Sep 16, 2024
1 parent 9e55c56 commit ba0a0f9
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/_static/openapi.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"openapi": "3.1.0", "info": {"title": "Ghostwriter", "description": "URL shortener/personalizer for Phalanx\n\n[Return to Ghostwriter documentation](.)", "version": "0.1.dev38+gf92a4c3.d20240910"}, "paths": {"/ghostwriter/": {"get": {"summary": "Application metadata", "description": "Document the top-level API here. By default it only returns metadata about the application.", "operationId": "get_index_ghostwriter__get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Index"}}}}}}}, "/ghostwriter/rewrite/{full_path}": {"get": {"summary": "Rewrite", "operationId": "rewrite_ghostwriter_rewrite__full_path__get", "parameters": [{"name": "full_path", "in": "path", "required": true, "schema": {"type": "string", "title": "The URL path to rewrite"}}], "responses": {"307": {"description": "Successful Response"}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}}, "components": {"schemas": {"HTTPValidationError": {"properties": {"detail": {"items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array", "title": "Detail"}}, "type": "object", "title": "HTTPValidationError"}, "Index": {"properties": {"metadata": {"$ref": "#/components/schemas/Metadata", "title": "Package metadata"}}, "type": "object", "required": ["metadata"], "title": "Index", "description": "Metadata returned by the external root URL of the application.\n\nNotes\n-----\nAs written, this is not very useful. Add additional metadata that will be\nhelpful for a user exploring the application, or replace this model with\nsome other model that makes more sense to return from the application API\nroot."}, "Metadata": {"properties": {"name": {"type": "string", "title": "Application name", "examples": ["myapp"]}, "version": {"type": "string", "title": "Version", "examples": ["1.0.0"]}, "description": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Description", "examples": ["Some package description"]}, "repository_url": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Repository URL", "examples": ["https://example.com/"]}, "documentation_url": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Documentation URL", "examples": ["https://example.com/"]}}, "type": "object", "required": ["name", "version"], "title": "Metadata", "description": "Metadata about a package."}, "ValidationError": {"properties": {"loc": {"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, "type": "array", "title": "Location"}, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}}, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError"}}}}
{"openapi": "3.1.0", "info": {"title": "Ghostwriter", "description": "URL shortener/personalizer for Phalanx\n\n[Return to Ghostwriter documentation](.)", "version": "0.1.dev48+g9e55c56.d20240916"}, "paths": {"/ghostwriter/": {"get": {"summary": "Application metadata", "description": "Document the top-level API here. By default it only returns metadata about the application.", "operationId": "get_index_ghostwriter__get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Index"}}}}}}}, "/ghostwriter/rewrite/{full_path}": {"get": {"summary": "Rewrite", "operationId": "rewrite_ghostwriter_rewrite__full_path__get", "parameters": [{"name": "full_path", "in": "path", "required": true, "schema": {"type": "string", "title": "The URL path to rewrite"}}], "responses": {"307": {"description": "Successful Response"}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}}, "components": {"schemas": {"HTTPValidationError": {"properties": {"detail": {"items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array", "title": "Detail"}}, "type": "object", "title": "HTTPValidationError"}, "Index": {"properties": {"metadata": {"$ref": "#/components/schemas/Metadata", "title": "Package metadata"}}, "type": "object", "required": ["metadata"], "title": "Index", "description": "Metadata returned by the external root URL of the application.\n\nNotes\n-----\nAs written, this is not very useful. Add additional metadata that will be\nhelpful for a user exploring the application, or replace this model with\nsome other model that makes more sense to return from the application API\nroot."}, "Metadata": {"properties": {"name": {"type": "string", "title": "Application name", "examples": ["myapp"]}, "version": {"type": "string", "title": "Version", "examples": ["1.0.0"]}, "description": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Description", "examples": ["Some package description"]}, "repository_url": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Repository URL", "examples": ["https://example.com/"]}, "documentation_url": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Documentation URL", "examples": ["https://example.com/"]}}, "type": "object", "required": ["name", "version"], "title": "Metadata", "description": "Metadata about a package."}, "ValidationError": {"properties": {"loc": {"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, "type": "array", "title": "Location"}, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}}, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError"}}}}
11 changes: 5 additions & 6 deletions src/ghostwriter/dependencies/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rubin.nublado.client import NubladoClient
from safir.dependencies.gafaelfawr import (
auth_delegated_token_dependency,
auth_dependency,
auth_logger_dependency,
)
from structlog.stdlib import BoundLogger
Expand Down Expand Up @@ -78,27 +79,25 @@ def __init__(self) -> None:
async def __call__(
self,
request: Request,
username: Annotated[str, Depends(auth_dependency)],
logger: Annotated[BoundLogger, Depends(auth_logger_dependency)],
token: Annotated[str, Depends(auth_delegated_token_dependency)],
) -> RequestContext:
"""Create a per-request context."""
logger.debug("Creating request context.")
pc = self.process_context
client = await pc.client_manager.get_client(token)
client = await pc.client_manager.get_client(username, token)

rc = RequestContext(
request=request,
logger=logger,
user=client.user.username,
user=username,
token=token,
client=client,
factory=Factory(pc, logger),
)

logger.debug(
f"Created request context for {request} by"
f" {client.user.username}"
)
logger.debug(f"Created request context for {request} by {username}")
return rc

@property
Expand Down
6 changes: 0 additions & 6 deletions src/ghostwriter/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .dependencies.config import config_dependency
from .models.v1.mapping import RouteCollection
from .services.client_manager import ClientManager
from .storage.gafaelfawr import GafaelfawrManager

__all__ = ["Factory", "ProcessContext"]

Expand All @@ -32,9 +31,6 @@ class ProcessContext:
mapping
Rewrite mapping; read from file specified in config.
gafaelfawr_manager
Cache for token-to-user-and-capability mappings.
client_manager
Cache for token-to-rsp-client mapping.
"""
Expand All @@ -45,11 +41,9 @@ def __init__(self) -> None:
self.base_url = self.config.environment_url
if self.base_url is None:
raise RuntimeError("config.environment_url must be set")
self.gafaelfawr_manager = GafaelfawrManager(base_url=self.base_url)
self.client_manager = ClientManager(
base_url=self.base_url,
logger=self.logger,
gafaelfawr_manager=self.gafaelfawr_manager,
)
self.mapping = self.load_map()

Expand Down
15 changes: 6 additions & 9 deletions src/ghostwriter/services/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from pydantic import HttpUrl
from rubin.nublado.client import NubladoClient
from rubin.nublado.client.models.user import User
from structlog.stdlib import BoundLogger

from ..constants import HTTP_TIMEOUT
from ..storage.gafaelfawr import GafaelfawrManager


class ClientManager:
Expand All @@ -17,26 +17,23 @@ class ClientManager:
def __init__(
self,
base_url: HttpUrl,
gafaelfawr_manager: GafaelfawrManager,
logger: BoundLogger,
) -> None:
self._base_url = base_url
self._gafaelfawr_manager = gafaelfawr_manager
self._logger = logger
self._client_cache: dict[str, NubladoClient] = {}
self._logger.debug("Initialized ClientManager")

async def get_client(self, token: str) -> NubladoClient:
"""Get a configured Nublado client from a token."""
async def get_client(self, username: str, token: str) -> NubladoClient:
"""Get a configured Nublado client from a user and token."""
if token not in self._client_cache:
user = await self._gafaelfawr_manager.get_user(token)
self._client_cache[token] = NubladoClient(
timeout=datetime.timedelta(seconds=HTTP_TIMEOUT),
logger=self._logger,
user=user,
user=User(username=username, token=token),
base_url=str(self._base_url),
timeout=datetime.timedelta(seconds=HTTP_TIMEOUT),
)
self._logger.debug(f"Built NubladoClient for user {user.username}")
self._logger.debug(f"Built NubladoClient for user {username}")
return self._client_cache[token]

async def aclose(self) -> None:
Expand Down
Empty file.
48 changes: 0 additions & 48 deletions src/ghostwriter/storage/gafaelfawr.py

This file was deleted.

7 changes: 2 additions & 5 deletions tests/services/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import structlog
import yaml
from rubin.nublado.client import NubladoClient
from rubin.nublado.client.models.user import AuthenticatedUser
from rubin.nublado.client.models.user import User

from ghostwriter.config import Configuration
from ghostwriter.models.substitution import Parameters
Expand All @@ -27,11 +27,8 @@ async def test_rewrite(config: Configuration) -> None:
path="tutorials/notebook05",
base_url="https://data.example.com",
client=NubladoClient(
user=AuthenticatedUser(
user=User(
username="rachel",
uidnumber=1101,
gidnumber=1101,
scopes=["exec:notebook", "read:tap", "exec:portal"],
token="token-of-affection",
),
base_url="https://data.example.com",
Expand Down

0 comments on commit ba0a0f9

Please sign in to comment.