How would you design a key management system (KMS) integrated with hardware security modules (HSMs)?
Implement the request-routing portion of the design. Preserve key affinity while respecting HSM health and per-batch capacity. Return the selected HSM ID for each request, or None when no HSM can serve it.
Input is a list of request objects with key_id and operation, plus HSM objects with id, healthy, and capacity fields. Requests are processed in order.
def route_kms_requests(requests, hsms):