add download to issued cert

This commit is contained in:
2025-09-08 17:32:28 -03:00
parent b327b6c177
commit ac07ee0101
15 changed files with 94 additions and 400 deletions

View File

@@ -58,6 +58,7 @@ app.include_router(enrollments.slots, prefix='/enrollments')
app.include_router(enrollments.enroll, prefix='/enrollments')
app.include_router(enrollments.cancel, prefix='/enrollments')
app.include_router(enrollments.deduplication_window, prefix='/enrollments')
app.include_router(enrollments.issued_cert, prefix='/enrollments')
app.include_router(orders.router, prefix='/orders')
app.include_router(users.router, prefix='/users')
app.include_router(users.add, prefix='/users')

View File

@@ -1,7 +1,17 @@
import os
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_cognito_idp.client import CognitoIdentityProviderClient
from mypy_boto3_dynamodb.client import DynamoDBClient
from mypy_boto3_s3 import S3Client
else:
CognitoIdentityProviderClient = object
DynamoDBClient = object
S3Client = object
def get_dynamodb_client():
running_sam_local = os.getenv('AWS_SAM_LOCAL')
@@ -16,5 +26,6 @@ def get_dynamodb_client():
return boto3.client('dynamodb', endpoint_url=f'http://{host}:8000')
dynamodb_client = get_dynamodb_client()
idp_client = boto3.client('cognito-idp')
s3_client: S3Client = boto3.client('s3')
dynamodb_client: DynamoDBClient = get_dynamodb_client()
idp_client: CognitoIdentityProviderClient = boto3.client('cognito-idp')

View File

@@ -3,9 +3,10 @@ import os
USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore
ORDER_TABLE: str = os.getenv('ORDER_TABLE') # type: ignore
ENROLLMENT_TABLE: str = os.getenv('ENROLLMENT_TABLE') # type: ignore
NEW_ENROLLMENT_TABLE: str = os.getenv('NEW_ENROLLMENT_TABLE') # type: ignore
COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore
BUCKET_NAME: str = os.getenv('BUCKET_NAME') # type: ignore
KONVIVA_API_URL: str = os.getenv('KONVIVA_API_URL') # type: ignore
KONVIVA_SECRET_KEY: str = os.getenv('KONVIVA_SECRET_KEY') # type: ignore
@@ -16,22 +17,9 @@ MEILISEARCH_API_KEY: str = os.getenv('MEILISEARCH_API_KEY') # type: ignore
match os.getenv('AWS_SAM_LOCAL'), os.getenv('PYTEST_VERSION'):
case str() as SAM_LOCAL, _ if SAM_LOCAL: # Only when running `sam local start-api`
MEILISEARCH_HOST = 'http://host.docker.internal:7700'
ELASTIC_CONN = {
'hosts': 'http://host.docker.internal:9200',
}
case _, str() as PYTEST if PYTEST: # Only when running `pytest`
MEILISEARCH_HOST = 'http://127.0.0.1:7700'
ELASTIC_CONN = {
'hosts': 'http://127.0.0.1:9200',
}
case _:
MEILISEARCH_HOST: str = os.getenv('MEILISEARCH_HOST') # type: ignore
ELASTIC_CLOUD_ID = os.getenv('ELASTIC_CLOUD_ID')
ELASTIC_AUTH_PASS = os.getenv('ELASTIC_AUTH_PASS')
ELASTIC_CONN = {
'cloud_id': ELASTIC_CLOUD_ID,
'basic_auth': ('elastic', ELASTIC_AUTH_PASS),
}
USER_POOOL_ID = 'sa-east-1_s6YmVSfXj'

View File

@@ -1,46 +0,0 @@
import math
from typing import TypedDict
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
MAX_PAGE_SIZE = 100
class PaginatedResult(TypedDict):
total_items: int
total_pages: int
items: list[dict]
def search(
index: str,
*,
query: dict,
page_size: int = 25,
elastic_client: Elasticsearch,
) -> PaginatedResult:
if page_size > MAX_PAGE_SIZE:
page_size = MAX_PAGE_SIZE
s = Search(
using=elastic_client,
index=index,
)
s.update_from_dict(query)
s.extra(size=page_size)
try:
r = s.execute()
except Exception:
return {
'total_items': 0,
'total_pages': 0,
'items': [],
}
else:
return {
'total_items': r.hits.total.value, # type: ignore
'total_pages': math.ceil(r.hits.total.value / page_size), # type: ignore
'items': [hit.to_dict() for hit in r],
}

View File

@@ -15,10 +15,11 @@ from middlewares import Tenant, TenantMiddleware
from .cancel import router as cancel
from .deduplication_window import router as deduplication_window
from .download_issued_cert import router as issued_cert
from .enroll import router as enroll
from .slots import router as slots
__all__ = ['slots', 'cancel', 'enroll', 'deduplication_window']
__all__ = ['slots', 'cancel', 'enroll', 'deduplication_window', 'issued_cert']
router = Router()

View File

@@ -0,0 +1,27 @@
from aws_lambda_powertools.event_handler.api_gateway import Router
from layercake.dynamodb import DynamoDBPersistenceLayer
from boto3clients import dynamodb_client, s3_client
from config import BUCKET_NAME, ENROLLMENT_TABLE
EXPIRES_IN = 300
router = Router()
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@router.get('/<id>/download')
def download(id: str):
params = {
'Bucket': BUCKET_NAME,
'Key': f'issuedcerts/{id}.pdf',
'ResponseContentDisposition': f'attachment; filename="{id}.pdf"',
}
return {
'presigned_url': s3_client.generate_presigned_url(
'get_object',
Params=params,
ExpiresIn=EXPIRES_IN,
)
}