Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit 292c2c1

Browse files
author
manju956
committed
Update Generic client to handle worker key refresh mechanism
When a worker key gets refreshed during the work order submission, a specific error code is returned to client to indicate worker key refresh happened at the enclave. On receiving this error code, client retrieves the updated worker details and does work order submission again. Signed-off-by: manju956 <[email protected]>
1 parent 4d5b698 commit 292c2c1

File tree

4 files changed

+95
-69
lines changed

4 files changed

+95
-69
lines changed

common/python/error_code/enclave_error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ class EnclaveError(IntEnum):
2828
ENCLAVE_ERR_VALUE = -8,
2929
ENCLAVE_ERR_SYSTEM = -9,
3030
ENCLAVE_ERR_CRYPTO = -11,
31+
ENCLAVE_ERR_KEY_REFRESH = -12,
3132
ENCLAVE_ERR_SYSTEM_BUSY = -10
3233
ENCLAVE_ERR_INVALID_WORKLOAD = -12

common/python/error_code/error_status.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class WorkOrderStatus(IntEnum):
3333
PROCESSING = 7
3434
BUSY = 8
3535
INVALID_WORKLOAD = 9
36-
UNKNOWN_ERROR = 10
36+
WORKER_KEY_REFRESHED = 10
37+
UNKNOWN_ERROR = 11
3738

3839

3940
@unique

examples/apps/generic_client/generic_client.py

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import os
1818
import sys
19+
import time
1920
import json
2021
import argparse
2122
import logging
@@ -36,6 +37,7 @@
3637
from avalon_sdk.direct.jrpc.jrpc_work_order_receipt \
3738
import JRPCWorkOrderReceiptImpl
3839
from error_code.error_status import WorkOrderStatus, ReceiptCreateStatus
40+
from error_code.enclave_error import EnclaveError
3941
import avalon_crypto_utils.signature as signature
4042
from error_code.error_status import SignatureStatus
4143
from avalon_sdk.work_order_receipt.work_order_receipt \
@@ -291,39 +293,17 @@ def _verify_wo_res_signature(work_order_res,
291293
return True
292294

293295

294-
def Main(args=None):
295-
options = _parse_command_line(args)
296-
297-
config = _parse_config_file(options.config)
298-
if config is None:
299-
logger.error("\n Error in parsing config file: {}\n".format(
300-
options.config
301-
))
302-
sys.exit(-1)
303-
304-
# mode should be one of listing or registry (default)
305-
mode = options.mode
306-
307-
# Http JSON RPC listener uri
308-
uri = options.uri
309-
if uri:
310-
config["tcf"]["json_rpc_uri"] = uri
311-
312-
# Address of smart contract
313-
address = options.address
314-
if address:
315-
if mode == "listing":
316-
config["ethereum"]["direct_registry_contract_address"] = \
317-
address
318-
elif mode == "registry":
319-
logger.error(
320-
"\n Only Worker registry listing address is supported." +
321-
"Worker registry address is unsupported \n")
322-
sys.exit(-1)
323-
324-
# worker id
325-
worker_id = options.worker_id
296+
def _start_client(config, options, jrpc_req_id, worker_id, worker_registry):
297+
"""
298+
Generates, submits work order requests. Retrieve response for the
299+
work order requests, verifies the response and generate receipt
326300
301+
@param config - instance of parsed config file
302+
@param options - command line options that are passed to the client
303+
@param jrpc_req_id - JSON RPC request id
304+
@param worker_id - worker id obtained from worker registry
305+
@param worker_registry - worker registry
306+
"""
327307
# work load id of worker
328308
workload_id = options.workload_id
329309
if not workload_id:
@@ -342,42 +322,6 @@ def Main(args=None):
342322
# requester signature for work order requests
343323
requester_signature = options.requester_signature
344324

345-
# setup logging
346-
config["Logging"] = {
347-
"LogFile": "__screen__",
348-
"LogLevel": "INFO"
349-
}
350-
351-
plogger.setup_loggers(config.get("Logging", {}))
352-
sys.stdout = plogger.stream_to_logger(
353-
logging.getLogger("STDOUT"), logging.DEBUG)
354-
sys.stderr = plogger.stream_to_logger(
355-
logging.getLogger("STDERR"), logging.WARN)
356-
357-
logger.info("******* Hyperledger Avalon Generic client *******")
358-
359-
if mode == "registry" and address:
360-
logger.error("\n Worker registry contract address is unsupported \n")
361-
sys.exit(-1)
362-
363-
# Retrieve JSON RPC uri from registry list
364-
if not uri and mode == "listing":
365-
uri = _retrieve_uri_from_registry_list(config)
366-
if uri is None:
367-
logger.error("\n Unable to get http JSON RPC uri \n")
368-
sys.exit(-1)
369-
370-
# Prepare worker
371-
# JRPC request id. Choose any integer value
372-
jrpc_req_id = 31
373-
worker_registry = JRPCWorkerRegistryImpl(config)
374-
if not worker_id:
375-
# Get first worker from worker registry
376-
worker_id = _lookup_first_worker(worker_registry, jrpc_req_id)
377-
if worker_id is None:
378-
logger.error("\n Unable to get worker \n")
379-
sys.exit(-1)
380-
381325
# Retrieve worker details
382326
jrpc_req_id += 1
383327
worker_retrieve_result = worker_registry.worker_retrieve(
@@ -465,6 +409,12 @@ def Main(args=None):
465409
res['result'], session_key, session_iv)
466410
logger.info("\nDecrypted response:\n {}"
467411
.format(decrypted_res))
412+
# Handle worker key refresh scenario
413+
elif "error" in res and \
414+
res["error"]["code"] == WorkOrderStatus.WORKER_KEY_REFRESHED:
415+
logger.error("Worker Key refreshed. Retrieving latest Worker details")
416+
worker_id = _lookup_first_worker(worker_registry, jrpc_req_id)
417+
_start_client(config, options, jrpc_req_id, worker_id, worker_registry)
468418
else:
469419
logger.error("\n Work order get result failed {}\n".format(
470420
res
@@ -483,5 +433,77 @@ def Main(args=None):
483433
sys.exit(1)
484434

485435

436+
def Main(args=None):
437+
options = _parse_command_line(args)
438+
439+
config = _parse_config_file(options.config)
440+
if config is None:
441+
logger.error("\n Error in parsing config file: {}\n".format(
442+
options.config
443+
))
444+
sys.exit(-1)
445+
446+
# mode should be one of listing or registry (default)
447+
mode = options.mode
448+
449+
# Http JSON RPC listener uri
450+
uri = options.uri
451+
if uri:
452+
config["tcf"]["json_rpc_uri"] = uri
453+
454+
# Address of smart contract
455+
address = options.address
456+
if address:
457+
if mode == "listing":
458+
config["ethereum"]["direct_registry_contract_address"] = \
459+
address
460+
elif mode == "registry":
461+
logger.error(
462+
"\n Only Worker registry listing address is supported." +
463+
"Worker registry address is unsupported \n")
464+
sys.exit(-1)
465+
466+
# worker id
467+
worker_id = options.worker_id
468+
469+
# setup logging
470+
config["Logging"] = {
471+
"LogFile": "__screen__",
472+
"LogLevel": "INFO"
473+
}
474+
475+
plogger.setup_loggers(config.get("Logging", {}))
476+
sys.stdout = plogger.stream_to_logger(
477+
logging.getLogger("STDOUT"), logging.DEBUG)
478+
sys.stderr = plogger.stream_to_logger(
479+
logging.getLogger("STDERR"), logging.WARN)
480+
481+
logger.info("******* Hyperledger Avalon Generic client *******")
482+
483+
if mode == "registry" and address:
484+
logger.error("\n Worker registry contract address is unsupported \n")
485+
sys.exit(-1)
486+
487+
# Retrieve JSON RPC uri from registry list
488+
if not uri and mode == "listing":
489+
uri = _retrieve_uri_from_registry_list(config)
490+
if uri is None:
491+
logger.error("\n Unable to get http JSON RPC uri \n")
492+
sys.exit(-1)
493+
494+
# Prepare worker
495+
# JRPC request id. Choose any integer value
496+
jrpc_req_id = 31
497+
worker_registry = JRPCWorkerRegistryImpl(config)
498+
if not worker_id:
499+
# Get first worker from worker registry
500+
worker_id = _lookup_first_worker(worker_registry, jrpc_req_id)
501+
if worker_id is None:
502+
logger.error("\n Unable to get worker \n")
503+
sys.exit(-1)
504+
505+
_start_client(config, options, jrpc_req_id, worker_id, worker_registry)
506+
507+
486508
# -----------------------------------------------------------------------------
487509
Main()

listener/avalon_listener/tcs_work_order_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def WorkOrderGetResult(self, **params):
138138
err_code = WorkOrderStatus.UNKNOWN_ERROR
139139
elif err_code == EnclaveError.ENCLAVE_ERR_INVALID_WORKLOAD:
140140
err_code = WorkOrderStatus.INVALID_WORKLOAD
141+
elif err_code == EnclaveError.ENCLAVE_ERR_KEY_REFRESH:
142+
err_code = WorkOrderStatus.WORKER_KEY_REFRESHED
141143
else:
142144
err_code = WorkOrderStatus.FAILED
143145
raise JSONRPCDispatchException(err_code, err_msg)

0 commit comments

Comments
 (0)