Skip to content

Commit 71e47b5

Browse files
committed
VED-79: refactor authentication, test and pipeline run
1 parent 154a5df commit 71e47b5

File tree

9 files changed

+318
-174
lines changed

9 files changed

+318
-174
lines changed

azure/templates/post-deploy.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ steps:
7777
displayName: Apply Terraform
7878
workingDirectory: "$(Pipeline.Workspace)/s/$(SERVICE_NAME)/$(SERVICE_ARTIFACT_NAME)"
7979
retryCountOnTaskFailure: 2
80+
- bash: |
81+
echo "Creating MNS subscription..."
82+
python3 mns_subscription/src/subscribe_mns.py
83+
displayName: "Subscribe SQS to MNS signal"
8084
8185
- bash: |
8286
set -ex

mns_subscription/models/errors.py

Lines changed: 1 addition & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,7 @@ class Code(str, Enum):
2525
class UnauthorizedError(RuntimeError):
2626
@staticmethod
2727
def to_operation_outcome() -> dict:
28-
msg = f"Unauthorized request"
29-
return create_operation_outcome(
30-
resource_id=str(uuid.uuid4()),
31-
severity=Severity.error,
32-
code=Code.forbidden,
33-
diagnostics=msg,
34-
)
35-
36-
37-
@dataclass
38-
class UnauthorizedVaxError(RuntimeError):
39-
@staticmethod
40-
def to_operation_outcome() -> dict:
41-
msg = "Unauthorized request for vaccine type"
28+
msg = "Unauthorized request"
4229
return create_operation_outcome(
4330
resource_id=str(uuid.uuid4()),
4431
severity=Severity.error,
@@ -60,25 +47,6 @@ def to_operation_outcome() -> dict:
6047
)
6148

6249

63-
@dataclass
64-
class ResourceNotFoundError(RuntimeError):
65-
"""Return this error when the requested FHIR resource does not exist"""
66-
67-
resource_type: str
68-
resource_id: str
69-
70-
def __str__(self):
71-
return f"{self.resource_type} resource does not exist. ID: {self.resource_id}"
72-
73-
def to_operation_outcome(self) -> dict:
74-
return create_operation_outcome(
75-
resource_id=str(uuid.uuid4()),
76-
severity=Severity.error,
77-
code=Code.not_found,
78-
diagnostics=self.__str__(),
79-
)
80-
81-
8250
@dataclass
8351
class ResourceFoundError(RuntimeError):
8452
"""Return this error when the requested FHIR resource does exist"""
@@ -117,71 +85,6 @@ def to_operation_outcome(self) -> dict:
11785
)
11886

11987

120-
class MandatoryError(Exception):
121-
def __init__(self, message=None):
122-
self.message = message
123-
124-
125-
class ValidationError(RuntimeError):
126-
def to_operation_outcome(self) -> dict:
127-
pass
128-
129-
130-
@dataclass
131-
class InvalidPatientId(ValidationError):
132-
"""Use this when NHS Number is invalid or doesn't exist"""
133-
134-
patient_identifier: str
135-
136-
def __str__(self):
137-
return f"NHS Number: {self.patient_identifier} is invalid or it doesn't exist."
138-
139-
def to_operation_outcome(self) -> dict:
140-
return create_operation_outcome(
141-
resource_id=str(uuid.uuid4()),
142-
severity=Severity.error,
143-
code=Code.server_error,
144-
diagnostics=self.__str__(),
145-
)
146-
147-
148-
@dataclass
149-
class InconsistentIdError(ValidationError):
150-
"""Use this when the specified id in the message is inconsistent with the path
151-
see: http://hl7.org/fhir/R4/http.html#update"""
152-
153-
imms_id: str
154-
155-
def __str__(self):
156-
return f"The provided id:{self.imms_id} doesn't match with the content of the message"
157-
158-
def to_operation_outcome(self) -> dict:
159-
return create_operation_outcome(
160-
resource_id=str(uuid.uuid4()),
161-
severity=Severity.error,
162-
code=Code.server_error,
163-
diagnostics=self.__str__(),
164-
)
165-
166-
167-
@dataclass
168-
class CustomValidationError(ValidationError):
169-
"""Custom validation error"""
170-
171-
message: str
172-
173-
def __str__(self):
174-
return self.message
175-
176-
def to_operation_outcome(self) -> dict:
177-
return create_operation_outcome(
178-
resource_id=str(uuid.uuid4()),
179-
severity=Severity.error,
180-
code=Code.invariant,
181-
diagnostics=self.__str__(),
182-
)
183-
184-
18588
@dataclass
18689
class IdentifierDuplicationError(RuntimeError):
18790
"""Fine grain validation"""
@@ -223,46 +126,3 @@ def create_operation_outcome(resource_id: str, severity: Severity, code: Code, d
223126
}
224127
],
225128
}
226-
227-
228-
@dataclass
229-
class ParameterException(RuntimeError):
230-
message: str
231-
232-
def __str__(self):
233-
return self.message
234-
235-
236-
class UnauthorizedSystemError(RuntimeError):
237-
def __init__(self, message="Unauthorized system"):
238-
super().__init__(message)
239-
self.message = message
240-
241-
def to_operation_outcome(self) -> dict:
242-
return create_operation_outcome(
243-
resource_id=str(uuid.uuid4()),
244-
severity=Severity.error,
245-
code=Code.forbidden,
246-
diagnostics=self.message,
247-
)
248-
249-
250-
class MessageNotSuccessfulError(Exception):
251-
"""
252-
Generic error message for any scenario which either prevents sending to the Imms API, or which results in a
253-
non-successful response from the Imms API
254-
"""
255-
256-
def __init__(self, message=None):
257-
self.message = message
258-
259-
260-
class RecordProcessorError(Exception):
261-
"""
262-
Exception for re-raising exceptions which have already occurred in the Record Processor.
263-
The diagnostics dictionary received from the Record Processor is passed to the exception as an argument
264-
and is stored as an attribute.
265-
"""
266-
267-
def __init__(self, diagnostics_dictionary: dict):
268-
self.diagnostics_dictionary = diagnostics_dictionary

mns_subscription/poetry.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mns_subscription/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ version = "0.1.0"
44
description = "subscription to nhs number change event"
55
authors = ["Your Name <[email protected]>"]
66
license = "MIT"
7-
readme = "README.md"
87
packages = [{include = "src"}]
98

109
[tool.poetry.dependencies]
1110
python = "^3.11"
11+
boto3 = "~1.38.42"
1212
pyjwt = "~2.10.1"
1313
moto = "^5.1.6"
1414
requests = "~2.32.4"

mns_subscription/src/authentication.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class Service(Enum):
14-
PDS = "pds"
14+
MNS = "mns"
1515
IMMUNIZATION = "imms"
1616

1717

@@ -22,7 +22,7 @@ def __init__(self, service: Service, secret_manager_client, environment, cache:
2222
self.cache_key = f"{service.value}_access_token"
2323

2424
self.expiry = 30
25-
self.secret_name = f"imms/pds/{environment}/jwt-secrets" if service == Service.PDS else \
25+
self.secret_name = f"imms/mns/{environment}/jwt-secrets" if service == Service.MNS else \
2626
f"imms/immunization/{environment}/jwt-secrets"
2727

2828
self.token_url = f"https://{environment}.api.service.nhs.uk/oauth2/token" \
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
from authentication import AppRestrictedAuth
2+
import os
3+
from cache import Cache
24
from mns_service import MnsService
5+
import boto3
6+
from authentication import Service
7+
from botocore.config import Config
8+
9+
10+
def run_subscription():
11+
mns_env: str = os.getenv("MNS_ENV", "int")
12+
13+
boto_config = Config(region_name="eu-west-2")
14+
cache = Cache(directory="/tmp")
15+
authenticator = AppRestrictedAuth(
16+
service=Service.MNS,
17+
secret_manager_client=boto3.client("secretsmanager", config=boto_config),
18+
environment=mns_env,
19+
cache=cache,
20+
)
21+
mns = MnsService(authenticator, mns_env)
22+
return mns.subscribeNotification()
23+
324

425
if __name__ == "__main__":
5-
auth = AppRestrictedAuth()
6-
mns = MnsService(authenticator=auth)
7-
result = mns.subscribeNotification()
26+
result = run_subscription()
827
print("Subscription Result:", result)

0 commit comments

Comments
 (0)