-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Implement header-based collection and geometry filtering #563
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
Open
bountx
wants to merge
4
commits into
main
Choose a base branch
from
CAT-1624
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """Header-based filtering utilities. | ||
|
|
||
| This module provides functions for parsing filter headers from stac-auth-proxy. | ||
| Headers allow stac-auth-proxy to pass collection and geometry filters to sfeos. | ||
| """ | ||
|
|
||
| import json | ||
| import logging | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| from fastapi import Request | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Header names | ||
| FILTER_COLLECTIONS_HEADER = "X-Filter-Collections" | ||
| FILTER_GEOMETRY_HEADER = "X-Filter-Geometry" | ||
|
|
||
|
|
||
| def parse_filter_collections(request: Request) -> Optional[List[str]]: | ||
| """Parse collection filter from X-Filter-Collections header. | ||
|
|
||
| Args: | ||
| request: FastAPI Request object. | ||
|
|
||
| Returns: | ||
| List of collection IDs if header is present, None otherwise. | ||
| Empty list if header value is empty string. | ||
|
|
||
| Example: | ||
| Header "X-Filter-Collections: col-a,col-b,col-c" returns ["col-a", "col-b", "col-c"] | ||
| """ | ||
| header_value = request.headers.get(FILTER_COLLECTIONS_HEADER) | ||
|
|
||
| if header_value is None: | ||
| return None | ||
|
|
||
| # Handle empty header value | ||
| if not header_value.strip(): | ||
| return [] | ||
|
|
||
| # Parse comma-separated list | ||
| collections = [c.strip() for c in header_value.split(",") if c.strip()] | ||
| logger.debug(f"Parsed filter collections from header: {collections}") | ||
|
|
||
| return collections | ||
|
|
||
|
|
||
| def parse_filter_geometry(request: Request) -> Optional[Dict[str, Any]]: | ||
| """Parse geometry filter from X-Filter-Geometry header. | ||
|
|
||
| Args: | ||
| request: FastAPI Request object. | ||
|
|
||
| Returns: | ||
| GeoJSON geometry dict if header is present and valid, None otherwise. | ||
|
|
||
| Example: | ||
| Header 'X-Filter-Geometry: {"type":"Polygon","coordinates":[...]}' | ||
| returns the parsed GeoJSON dict. | ||
| """ | ||
| header_value = request.headers.get(FILTER_GEOMETRY_HEADER) | ||
|
|
||
| if header_value is None: | ||
| return None | ||
|
|
||
| if not header_value.strip(): | ||
| return None | ||
|
|
||
| try: | ||
| geometry = json.loads(header_value) | ||
| logger.debug( | ||
| f"Parsed filter geometry from header: {geometry.get('type', 'unknown')}" | ||
| ) | ||
| return geometry | ||
| except json.JSONDecodeError as e: | ||
| logger.warning(f"Failed to parse geometry header: {e}") | ||
| return None | ||
|
|
||
|
|
||
| def geometry_intersects_filter( | ||
| item_geometry: Dict[str, Any], filter_geometry: Dict[str, Any] | ||
| ) -> bool: | ||
| """Check if item geometry intersects with the filter geometry. | ||
|
|
||
| Args: | ||
| item_geometry: GeoJSON geometry dict from the item. | ||
| filter_geometry: GeoJSON geometry dict from header filter. | ||
|
|
||
| Returns: | ||
| True if geometries intersect (or if shapely not available), False otherwise. | ||
|
|
||
| Note: | ||
| Requires shapely to be installed. If shapely is not available, | ||
| this function returns True (allows access) to avoid breaking | ||
| deployments without shapely. | ||
| """ | ||
| try: | ||
| from shapely.geometry import shape | ||
| except ImportError: | ||
| logger.warning( | ||
| "shapely not installed - geometry filter check skipped. " | ||
| "Install shapely for full geometry filtering support." | ||
| ) | ||
| return True # Allow access if shapely not available | ||
|
|
||
| try: | ||
| item_shape = shape(item_geometry) | ||
| filter_shape = shape(filter_geometry) | ||
| return item_shape.intersects(filter_shape) | ||
| except Exception as e: | ||
| logger.warning(f"Geometry intersection check failed: {e}") | ||
| # On error, allow access (fail open) | ||
| return True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to remove some code from core.py ie.