Skip to content

Commit

Permalink
feat(tailsfile): generic tails file upload
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Burdett <burdettadam@gmail.com>
  • Loading branch information
burdettadam committed May 19, 2023
1 parent 0eb5ac4 commit b72608c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
22 changes: 13 additions & 9 deletions aries_cloudagent/anoncreds/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,37 +615,41 @@ def get_public_tails_uri(self, rev_reg_def: RevRegDef):
self._check_url(public_tails_uri)
return public_tails_uri

def get_local_tails_path(self, rev_reg_def: RevRegDef) -> str:
def get_local_tails_path(self, tails_hash: str) -> str:
"""Get the local path to the tails file."""
tails_dir = indy_client_dir("tails", create=False)
return os.path.join(tails_dir, rev_reg_def.value.tails_hash)
return os.path.join(tails_dir, tails_hash)

async def upload_tails_file(self, rev_reg_def: RevRegDef):
async def upload_tails_file(
self, tails_hash: str, cred_def_id: str, tails_location
):
"""Upload the local tails file to the tails server."""
tails_server = self._profile.inject_or(BaseTailsServer)
if not tails_server:
raise AnonCredsIssuerError("Tails server not configured")
if not Path(self.get_local_tails_path(rev_reg_def)).is_file():
if not Path(self.get_local_tails_path(tails_hash)).is_file():
raise AnonCredsIssuerError("Local tails file not found")

(upload_success, result) = await tails_server.upload_tails_file(
self._profile.context,
rev_reg_def.value.tails_hash,
self.get_local_tails_path(rev_reg_def),
tails_hash,
self.get_local_tails_path(tails_hash),
interval=0.8,
backoff=-0.5,
max_attempts=5, # heuristic: respect HTTP timeout
)
if not upload_success:
raise AnonCredsIssuerError(
f"Tails file for rev reg for {rev_reg_def.cred_def_id} "
f"Tails file for rev reg for {cred_def_id} "
"failed to upload: {result}"
)
if rev_reg_def.value.tails_location != result:
if tails_location and tails_location != result:
raise AnonCredsIssuerError(
f"Tails file for rev reg for {rev_reg_def.cred_def_id} "
f"Tails file for rev reg for {cred_def_id} "
"uploaded to wrong location: {result}"
)
# TODO: do we need to set uri? something like..
# await self.set_tails_file_public_uri(profile, result)

async def update_revocation_registry_definition_state(
self, rev_reg_def_id: str, state: str
Expand Down
50 changes: 50 additions & 0 deletions aries_cloudagent/anoncreds/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
response_schema,
)
from marshmallow import fields
from aries_cloudagent.askar.profile import AskarProfile

from aries_cloudagent.revocation.routes import (
RevRegIdMatchInfoSchema,
RevocationModuleResponseSchema,
)

from ..admin.request_context import AdminRequestContext
from ..messaging.models.openapi import OpenAPISchema
Expand All @@ -21,6 +27,8 @@
from .issuer import AnonCredsIssuer, AnonCredsIssuerError
from .models.anoncreds_cred_def import CredDefResultSchema, GetCredDefResultSchema
from .models.anoncreds_revocation import (
AnonCredsRegistryGetRevocationRegistryDefinition,
RevRegDef,
RevRegDefResultSchema,
RevListResultSchema,
)
Expand Down Expand Up @@ -439,6 +447,47 @@ async def rev_list_post(request: web.BaseRequest):
return web.json_response(result.serialize())


@docs(
tags=["revocation"],
summary="Upload local tails file to server",
)
@match_info_schema(RevRegIdMatchInfoSchema())
@response_schema(RevocationModuleResponseSchema(), description="")
async def upload_tails_file(request: web.BaseRequest):
"""
Request handler to upload local tails file for revocation registry.
Args:
request: aiohttp request object
"""
context: AdminRequestContext = request["context"]
profile: AskarProfile = context.profile
anoncreds_registry: AnonCredsRegistry = context.inject(AnonCredsRegistry)
rev_reg_id = request.match_info["rev_reg_id"]
try:
issuer = AnonCredsIssuer(profile)
get_rev_reg_def: AnonCredsRegistryGetRevocationRegistryDefinition = (
await anoncreds_registry.get_revocation_registry_definition(
profile, rev_reg_id
)
)
rev_reg_def: RevRegDef = get_rev_reg_def.revocation_registry
# TODO: Should we check if tails file exists
except StorageNotFoundError as err: # TODO: update error
raise web.HTTPNotFound(reason=err.roll_up) from err
try:
await issuer.upload_tails_file(
rev_reg_def.value.tails_hash,
rev_reg_def.cred_def_id,
rev_reg_def.value.tails_location,
)
except AnonCredsIssuerError as e:
raise web.HTTPInternalServerError(reason=str(e)) from e

return web.json_response({})


async def register(app: web.Application):
"""Register routes."""

Expand All @@ -460,6 +509,7 @@ async def register(app: web.Application):
),
web.post("/anoncreds/revocation-registry-definition", rev_reg_def_post),
web.post("/anoncreds/revocation-list", rev_list_post),
web.put("/revocation/registry/{rev_reg_id}/tails-file", upload_tails_file),
]
)

Expand Down
4 changes: 3 additions & 1 deletion aries_cloudagent/revocation/models/issuer_rev_reg_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ async def create_and_register_def(self, profile: Profile):
self.state = IssuerRevRegRecord.STATE_POSTED
self.tails_hash = result.rev_reg_def.value.tails_hash
self.tails_public_uri = result.rev_reg_def.value.tails_location
self.tails_local_path = issuer.get_local_tails_path(result.rev_reg_def)
self.tails_local_path = issuer.get_local_tails_path(
result.rev_reg_def.value.tails_hash
)

async with profile.session() as session:
await self.save(session, reason="Generated registry")
Expand Down

0 comments on commit b72608c

Please sign in to comment.