-
Notifications
You must be signed in to change notification settings - Fork 0
Build cryptographic secret gateway service #2
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
GPTI314
wants to merge
2
commits into
main
Choose a base branch
from
claude/secret-gateway-crypto-01GndC91pA9wU4eCkyjwWzgc
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| .pytest_cache/ |
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,136 @@ | ||
| """ | ||
| API Router for SecretGateway Token Management | ||
| """ | ||
|
|
||
| from fastapi import APIRouter, HTTPException, status | ||
| from typing import Dict | ||
| import logging | ||
|
|
||
| from app.services.crypto import ( | ||
| TokenService, | ||
| TokenIssuanceRequest, | ||
| TokenIssuanceResponse, | ||
| TokenValidationRequest, | ||
| TokenValidationResponse | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| router = APIRouter(prefix="/tokens", tags=["tokens"]) | ||
|
|
||
| # Global token service instance | ||
| _token_service: TokenService = TokenService() | ||
|
|
||
|
|
||
| def get_token_service() -> TokenService: | ||
| """Get the global token service instance""" | ||
| return _token_service | ||
|
|
||
|
|
||
| @router.post( | ||
| "/issue", | ||
| response_model=TokenIssuanceResponse, | ||
| status_code=status.HTTP_201_CREATED, | ||
| summary="Issue ephemeral token", | ||
| description="Issue a short-lived token with specified scope and TTL" | ||
| ) | ||
| async def issue_token(request: TokenIssuanceRequest) -> TokenIssuanceResponse: | ||
| """ | ||
| Issue a new ephemeral token | ||
|
|
||
| - **scope**: Token scope with resource and actions | ||
| - **ttl_seconds**: Time-to-live (1-3600 seconds) | ||
| - **metadata**: Optional metadata dictionary | ||
|
|
||
| Returns the token ID and expiration details. | ||
| """ | ||
| try: | ||
| response = _token_service.issue_token_from_request(request) | ||
| logger.info(f"Token issued: {response.token_id[:8]}... for {request.scope.resource}") | ||
| return response | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f"Token issuance failed: {e}") | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail="Failed to issue token" | ||
| ) | ||
|
|
||
|
|
||
| @router.post( | ||
| "/validate", | ||
| response_model=TokenValidationResponse, | ||
| summary="Validate token", | ||
| description="Validate a token and check if it's still valid" | ||
| ) | ||
| async def validate_token(request: TokenValidationRequest) -> TokenValidationResponse: | ||
| """ | ||
| Validate a token | ||
|
|
||
| - **token_id**: Token identifier to validate | ||
|
|
||
| Returns validation result with token details if valid. | ||
| """ | ||
| response = _token_service.validate_token(request.token_id) | ||
| return response | ||
|
|
||
|
|
||
| @router.delete( | ||
| "/{token_id}", | ||
| status_code=status.HTTP_204_NO_CONTENT, | ||
| summary="Revoke token", | ||
| description="Revoke (delete) a token before it expires" | ||
| ) | ||
| async def revoke_token(token_id: str) -> None: | ||
| """ | ||
| Revoke a token | ||
|
|
||
| - **token_id**: Token identifier to revoke | ||
|
|
||
| Returns 204 No Content on success, 404 if token not found. | ||
| """ | ||
| revoked = _token_service.revoke_token(token_id) | ||
| if not revoked: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Token not found" | ||
| ) | ||
|
|
||
|
|
||
| @router.get( | ||
| "/stats", | ||
| response_model=Dict[str, int], | ||
| summary="Get token statistics", | ||
| description="Get statistics about active tokens" | ||
| ) | ||
| async def get_token_stats() -> Dict[str, int]: | ||
| """ | ||
| Get token statistics | ||
|
|
||
| Returns count of active tokens. | ||
| """ | ||
| return { | ||
| "active_tokens": _token_service.get_active_token_count() | ||
| } | ||
|
|
||
|
|
||
| @router.post( | ||
| "/cleanup", | ||
| response_model=Dict[str, int], | ||
| summary="Cleanup expired tokens", | ||
| description="Manually trigger cleanup of expired tokens" | ||
| ) | ||
| async def cleanup_expired_tokens() -> Dict[str, int]: | ||
| """ | ||
| Manually trigger cleanup of expired tokens | ||
|
|
||
| Returns count of tokens removed. | ||
| """ | ||
| count = _token_service.cleanup_expired_tokens() | ||
| return { | ||
| "removed_tokens": count | ||
| } | ||
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,33 @@ | ||
| """ | ||
| SecretGateway Cryptographic Services Module | ||
|
|
||
| Provides core cryptographic utilities for secure secret management: | ||
| - Secret generation | ||
| - Symmetric encryption/decryption | ||
| - Secure hashing with salt | ||
| - Ephemeral token issuance and validation | ||
| """ | ||
|
|
||
| from .crypto_service import CryptoService | ||
| from .token_service import TokenService | ||
| from .token_store import InMemoryTokenStore | ||
| from .token_models import ( | ||
| Token, | ||
| TokenScope, | ||
| TokenIssuanceRequest, | ||
| TokenIssuanceResponse, | ||
| TokenValidationRequest, | ||
| TokenValidationResponse | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "CryptoService", | ||
| "TokenService", | ||
| "InMemoryTokenStore", | ||
| "Token", | ||
| "TokenScope", | ||
| "TokenIssuanceRequest", | ||
| "TokenIssuanceResponse", | ||
| "TokenValidationRequest", | ||
| "TokenValidationResponse", | ||
| ] |
Oops, something went wrong.
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.
Using a global singleton token service instance may cause issues in production deployments with multiple workers. Consider using FastAPI's dependency injection system to manage the TokenService lifecycle properly. This would allow for better testing, isolation between requests, and proper cleanup on application shutdown.