Skip to content

Commit 413471a

Browse files
Added logic for current and previous hash checks.
1 parent 864060a commit 413471a

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

src/eligibility_signposting_api/processors/hashing_service.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111

1212
def _hash(nhs_number: str, secret_value: str) -> str:
13+
if not secret_value: return None
14+
1315
nhs_str = str(nhs_number)
1416

1517
return hmac.new(
@@ -37,3 +39,17 @@ def hash_with_current_secret(self, nhs_number: str) -> str:
3739
def hash_with_previous_secret(self, nhs_number: str) -> str:
3840
secret_value = self.secret_repo.get_secret_previous(self.hash_secret_name)["AWSPREVIOUS"]
3941
return _hash(nhs_number, secret_value)
42+
43+
# def hash_with_secret(self, nhs_number: str, version_stage: str) -> str:
44+
# if version_stage == "AWSCURRENT":
45+
# secret_dict = self.secret_repo.get_secret_current(self.hash_secret_name)
46+
# elif version_stage == "AWSPREVIOUS":
47+
# secret_dict = self.secret_repo.get_secret_previous(self.hash_secret_name)
48+
# #
49+
#
50+
# if secret_dict:
51+
# secret_value = secret_dict.get(version_stage)
52+
# hashed_value = _hash(nhs_number, secret_value)
53+
# return hashed_value
54+
# else:
55+
# return None

src/eligibility_signposting_api/repos/person_repo.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,34 @@ def __init__(self, table: Annotated[Any, Inject(qualifier="person_table")],
3939
self.table = table
4040
self._hashing_service = hashing_service
4141

42+
def get_person_record(self, nhs_hash):
43+
if nhs_hash:
44+
response = self.table.query(KeyConditionExpression=Key("NHS_NUMBER").eq(nhs_hash))
45+
46+
items = response.get("Items", [])
47+
has_person = any(item.get("ATTRIBUTE_TYPE") == "PERSON" for item in items)
48+
49+
if has_person:
50+
return items
51+
else:
52+
logger.error("No person record found for hash of nhs_number")
53+
54+
return None
55+
56+
4257
def get_eligibility_data(self, nhs_number: NHSNumber) -> Person:
58+
59+
# AWSCURRENT secret
4360
nhs_hash = self._hashing_service.hash_with_current_secret(nhs_number)
44-
response = self.table.query(KeyConditionExpression=Key("NHS_NUMBER").eq(nhs_hash))
61+
items = self.get_person_record(nhs_hash)
4562

46-
if not (items := response.get("Items")) or not next(
47-
(item for item in items if item.get("ATTRIBUTE_TYPE") == "PERSON"), None
48-
):
63+
if not items:
64+
# AWSPREVIOUS secret
4965
nhs_hash = self._hashing_service.hash_with_previous_secret(nhs_number)
50-
response = self.table.query(KeyConditionExpression=Key("NHS_NUMBER").eq(nhs_hash))
66+
items = self.get_person_record(nhs_hash)
5167

52-
if not (items := response.get("Items")) or not next(
53-
(item for item in items if item.get("ATTRIBUTE_TYPE") == "PERSON"), None
54-
):
55-
message = f"Person not found with nhs_number {nhs_number}"
68+
if not items:
69+
message = f"Person not found for NHS number hash with current or previous secret."
5670
raise NotFoundError(message)
5771

5872
return Person(data=items)

src/eligibility_signposting_api/repos/secret_repo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ def _get_secret_by_stage(self, secret_name: str, stage: str) -> dict[str, str]:
2727
VersionStage=stage,
2828
)
2929
return {stage: response["SecretString"]}
30+
# Add in other errors for AWS Secrets Manager
3031
except ClientError as e:
3132
logger.error("Failed to get secret %s at stage %s: %s", secret_name, stage, e)
32-
raise
33+
#raise
34+
return {}
3335

3436
def get_secret_current(self, secret_name: str) -> dict[str, str]:
3537
"""Fetch the AWSCURRENT version of the secret."""

tests/integration/repo/test_person_repo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,24 @@ def test_items_found_but_person_attribute_type_not_found_raises_error(
5555
## When, Then
5656
with pytest.raises(NotFoundError):
5757
repo.get_eligibility_data(persisted_person_with_no_person_attribute_type)
58+
59+
60+
# def test_person_found_with_current_secret(person_table: Any,
61+
# persisted_person: NHSNumber,
62+
# hashing_service: HashingService,):
63+
# # Given
64+
# repo = PersonRepo(person_table, hashing_service)
65+
#
66+
# # When
67+
# actual = repo.get_eligibility_data(persisted_person)
68+
#
69+
# # Then
70+
# nhs_num_hash = hashing_service.hash_with_current_secret(persisted_person)
71+
#
72+
# assert_that(
73+
# actual.data,
74+
# contains_inanyorder(
75+
# has_entries({"NHS_NUMBER": nhs_num_hash, "ATTRIBUTE_TYPE": "PERSON"}),
76+
# has_entries({"NHS_NUMBER": nhs_num_hash, "ATTRIBUTE_TYPE": "COHORTS"}),
77+
# ),
78+
# )

0 commit comments

Comments
 (0)