Skip to content

Commit

Permalink
Merge pull request #533 from sklump/error-audit-no-ledger
Browse files Browse the repository at this point in the history
humane error for no ledger, including via missing wallet-type
  • Loading branch information
andrewwhitehead authored May 29, 2020
2 parents 9e2ccc8 + ce5f98f commit 00df08a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
3 changes: 2 additions & 1 deletion aries_cloudagent/core/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ async def start(self) -> None:
public_did = await wallet_config(context)

# Configure the ledger
await ledger_config(context, public_did)
if not await ledger_config(context, public_did):
LOGGER.warning("No ledger configured")

# Start up transports
try:
Expand Down
20 changes: 16 additions & 4 deletions aries_cloudagent/ledger/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ async def register_ledger_nym(request: web.BaseRequest):
context = request.app["request_context"]
ledger = await context.inject(BaseLedger, required=False)
if not ledger:
raise web.HTTPForbidden()
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

did = request.query.get("did")
verkey = request.query.get("verkey")
Expand Down Expand Up @@ -151,7 +154,10 @@ async def get_did_verkey(request: web.BaseRequest):
context = request.app["request_context"]
ledger = await context.inject(BaseLedger, required=False)
if not ledger:
raise web.HTTPForbidden()
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

did = request.query.get("did")
if not did:
Expand All @@ -176,7 +182,10 @@ async def get_did_endpoint(request: web.BaseRequest):
context = request.app["request_context"]
ledger = await context.inject(BaseLedger, required=False)
if not ledger:
raise web.HTTPForbidden()
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

did = request.query.get("did")
if not did:
Expand All @@ -203,7 +212,10 @@ async def ledger_get_taa(request: web.BaseRequest):
context = request.app["request_context"]
ledger: BaseLedger = await context.inject(BaseLedger, required=False)
if not ledger or ledger.LEDGER_TYPE != "indy":
raise web.HTTPForbidden()
reason = "No indy ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

taa_info = await ledger.get_txn_author_agreement()
accepted = None
Expand Down
16 changes: 14 additions & 2 deletions aries_cloudagent/messaging/credential_definitions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ async def credential_definitions_send_credential_definition(request: web.BaseReq
support_revocation = bool(body.get("support_revocation"))
tag = body.get("tag")

ledger: BaseLedger = await context.inject(BaseLedger)
ledger: BaseLedger = await context.inject(BaseLedger, required=False)
if not ledger:
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

issuer: BaseIssuer = await context.inject(BaseIssuer)
async with ledger:
credential_definition_id, credential_definition = await shield(
Expand Down Expand Up @@ -191,7 +197,13 @@ async def credential_definitions_get_credential_definition(request: web.BaseRequ

credential_definition_id = request.match_info["cred_def_id"]

ledger: BaseLedger = await context.inject(BaseLedger)
ledger: BaseLedger = await context.inject(BaseLedger, required=False)
if not ledger:
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

async with ledger:
credential_definition = await ledger.get_credential_definition(
credential_definition_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ async def test_send_credential_definition(self):
{"credential_definition_id": CRED_DEF_ID}
)

async def test_send_credential_definition_no_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app,
json=async_mock.CoroutineMock(
return_value={
"schema_id": "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
"support_revocation": False,
"tag": "tag",
}
),
)

self.context.injector.clear_binding(BaseLedger)
with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.credential_definitions_send_credential_definition(
mock_request
)

async def test_created(self):
mock_request = async_mock.MagicMock(
app=self.app, match_info={"cred_def_id": CRED_DEF_ID},
Expand All @@ -94,9 +112,25 @@ async def test_get_credential_definition(self):
{"credential_definition": {"cred": "def"}}
)

async def test_get_credential_definition_no_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app, match_info={"cred_def_id": CRED_DEF_ID},
)

self.context.injector.clear_binding(BaseLedger)
with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.credential_definitions_get_credential_definition(
mock_request
)

async def test_register(self):
mock_app = async_mock.MagicMock()
mock_app.add_routes = async_mock.MagicMock()

await test_module.register(mock_app)
mock_app.add_routes.assert_called_once()

async def test_post_process_routes(self):
mock_app = async_mock.MagicMock(_state={"swagger_dict": {}})
test_module.post_process_routes(mock_app)
assert "tags" in mock_app._state["swagger_dict"]
16 changes: 14 additions & 2 deletions aries_cloudagent/messaging/schemas/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ async def schemas_send_schema(request: web.BaseRequest):
schema_version = body.get("schema_version")
attributes = body.get("attributes")

ledger: BaseLedger = await context.inject(BaseLedger)
ledger: BaseLedger = await context.inject(BaseLedger, required=False)
if not ledger:
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

issuer: BaseIssuer = await context.inject(BaseIssuer)
async with ledger:
try:
Expand Down Expand Up @@ -165,7 +171,13 @@ async def schemas_get_schema(request: web.BaseRequest):

schema_id = request.match_info["schema_id"]

ledger: BaseLedger = await context.inject(BaseLedger)
ledger: BaseLedger = await context.inject(BaseLedger, required=False)
if not ledger:
reason = "No ledger available"
if not context.settings.get_value("wallet.type"):
reason += ": missing wallet-type?"
raise web.HTTPForbidden(reason=reason)

async with ledger:
try:
schema = await ledger.get_schema(schema_id)
Expand Down
28 changes: 28 additions & 0 deletions aries_cloudagent/messaging/schemas/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ async def test_send_schema(self):
{"schema_id": SCHEMA_ID, "schema": {"schema": "def"}}
)

async def test_send_schema_no_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app,
json=async_mock.CoroutineMock(
return_value={
"schema_name": "schema_name",
"schema_version": "1.0",
"attributes": ["table", "drink", "colour"],
}
),
)

self.context.injector.clear_binding(BaseLedger)
with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.schemas_send_schema(mock_request)

async def test_send_schema_x_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app,
Expand Down Expand Up @@ -103,6 +119,18 @@ async def test_get_schema(self):
assert result == mock_response.return_value
mock_response.assert_called_once_with({"schema": {"schema": "def"}})

async def test_get_schema_no_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app, match_info={"schema_id": SCHEMA_ID},
)
self.ledger.get_schema = async_mock.CoroutineMock(
side_effect=test_module.LedgerError("Down for routine maintenance")
)

self.context.injector.clear_binding(BaseLedger)
with self.assertRaises(test_module.web.HTTPForbidden):
await test_module.schemas_get_schema(mock_request)

async def test_get_schema_x_ledger(self):
mock_request = async_mock.MagicMock(
app=self.app, match_info={"schema_id": SCHEMA_ID},
Expand Down

0 comments on commit 00df08a

Please sign in to comment.