Skip to content

Commit

Permalink
feat(issuer): add remove_device method and automate db setup
Browse files Browse the repository at this point in the history
  • Loading branch information
arminveres committed Nov 15, 2023
1 parent a9380dc commit cd2f412
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions impl/src/agents/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ async def onboard_node(self, domain: str, node_name: str, node_did: str):
await self.issue_credential(node_did, node_name, node_cred, domain)

async def mass_onboard(self):
"""
Rudimentally onboards a list of devices
"""
devices = []
device = {}
with open(".agent_cache/mass_onboarding", mode="r", encoding="utf-8") as file:
Expand All @@ -345,6 +348,17 @@ async def mass_onboard(self):
for node in devices:
await self.onboard_node(DB_NAME, node["name"], node["did"])

async def remove_device(self, node_name: str):
db_result = await self.db_client.query_key(DB_NAME, node_name)
await self.revoke_credential(
"",
DB_NAME,
node_name,
{"reason": "manually revoked by maintainer"},
)
_ = await self.db_client.delete_key(DB_NAME, node_name)
self.log(f"Successfully removed node: {node_name}")


# =================================================================================================
# Helper functions
Expand Down Expand Up @@ -446,6 +460,7 @@ def add_option(options: dict, key: str, value: str) -> dict:
# Setup options and make them dicts, so that they can be changed at runtime
prompt_options = {
"setup_db": " [1]: Setup/Load existing Database, Schema and Credential\n",
"rm_node": " [2]: Remove an onboarded node from the infrastructure\n",
"exit": " [x]: Exit\n",
"help": " [h]: Print help\n",
}
Expand Down Expand Up @@ -474,32 +489,33 @@ def get_prompt():
log_msg("Please give an option")

# run options
if option == "1":
if not prompt_options.get("setup_db"):
log_msg(f"invalid option, {option}")
# if option == "1":
# if not prompt_options.get("setup_db"):
# log_msg(f"invalid option, {option}")
# continue

# await setup_database(agent_container, DB_NAME)
# prompt_options.pop("setup_db")
# # add onboarding option and update order by values
# prompt_options = add_option(
# prompt_options, "onboard", " [3]: Onboard node with public DID\n"
# )
# prompt_options = add_option(
# prompt_options, "mass_onboard", " [4]: Onboard fleet with public DID\n"
# )

if option == "2":
node_name = await prompt("Enter Node Name: ")
if node_name is None or node_name == "":
log_msg("Aborting Node onboarding...")
continue
node_name = node_name.strip()
await agent_container.agent.remove_device(node_name)

await setup_database(agent_container, DB_NAME)
prompt_options.pop("setup_db")
# add onboarding option and update order by values
prompt_options = add_option(
prompt_options, "onboard", " [2]: Onboard node with public DID\n"
)
prompt_options = add_option(
prompt_options, "mass_onboard", " [3]: Onboard fleet with public DID\n"
)

elif option == "2":
elif option == "3":
if not prompt_options.get("onboard"):
log_msg(f"invalid option, {option}")
continue
# We could also go the route of exception, which I do not prefer.
# try: # we can now Ctrl-c out of the input
# node_name = (await prompt("Enter Node Name: ")).strip()
# node_did = (await prompt("Enter Node DID: ")).strip()
# except (KeyboardInterrupt, EOFError):
# log_status("Stopping node onboarding...")
# continue

node_name = await prompt("Enter Node Name: ")
if node_name is None or node_name == "":
Expand All @@ -518,10 +534,10 @@ def get_prompt():
node_name=node_name,
)

elif option == "3":
elif option == "4":
await agent_container.agent.mass_onboard()

elif option == "4":
elif option == "5":
log_json(
[
await agent_container.agent.db_client.query_key(DB_NAME, device)
Expand Down

0 comments on commit cd2f412

Please sign in to comment.