Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRMP-1185 Create workflow and script to subscribe to MNS notifications #467

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
079375d
[PRMP-1185] start mns sub pipeline
steph-torres-nhs Nov 20, 2024
6e54701
[PRMP-1185] adjust script to use variables passed down from the runne…
steph-torres-nhs Nov 26, 2024
3d37432
[PRMP-1185] Removed make and start venv and updated python to v5
oliverbeumkes-nhs Nov 26, 2024
90db88a
Merge branch 'main' into PRMP-1185
steph-torres-nhs Nov 27, 2024
53c9a29
[PRMP-1185] catchup with origin
steph-torres-nhs Nov 27, 2024
eca2211
[PRMP-1185] address PR comments
steph-torres-nhs Nov 27, 2024
f49a8a4
[PRMP-1185] ensure environment is used on job run
steph-torres-nhs Nov 27, 2024
dcd4521
[PRMP-1185] use nhs auth service to generate token
steph-torres-nhs Nov 27, 2024
976150a
[PRMP-1185] correct typo var => vars
steph-torres-nhs Nov 27, 2024
9e26406
[PRMP-1185] ammend run steps in action
steph-torres-nhs Nov 27, 2024
f84a507
[PRMP-1185] add new step to pipeline
steph-torres-nhs Nov 27, 2024
c77497c
[PRMP-1185] ammend bad request
steph-torres-nhs Nov 27, 2024
f540a3b
[PRMP-1185] PR changes
steph-torres-nhs Dec 2, 2024
5690454
[PRMP-1185] change sandbox input to choice
steph-torres-nhs Dec 2, 2024
e78d39e
prmp-1185 change sandbox name from test to ndr-test
NogaNHS Dec 10, 2024
58d367e
prmp-1185 change step name in github actions workflow
NogaNHS Dec 10, 2024
54416f7
Merge branch 'main' into PRMP-1185
NogaNHS Dec 10, 2024
95e8a99
Merge branch 'main' into PRMP-1185
NogaNHS Dec 10, 2024
d09b427
Merge branch 'main' into PRMP-1185
oliverbeumkes-nhs Dec 16, 2024
5cc7b0d
add env
steph-torres-nhs Dec 20, 2024
f2f8118
Merge branch 'PRMP-1185' of https://github.com/nhsconnect/national-do…
steph-torres-nhs Dec 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/subscribe-to-mns.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Subscribe to MNS

on:
workflow_dispatch:
inputs:
sandbox:
description: Which sandbox would you like to run against?
required: true
type: choice
options:
- ndr-dev
- test
oliverbeumkes-nhs marked this conversation as resolved.
Show resolved Hide resolved
- pre-prod
- prod
environment:
description: Which environment settings to use?
required: true
type: string
default: development

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

env:
SANDBOX: ${{ inputs.sandbox }}
AWS_REGION: ${{ vars.AWS_REGION }}
URL: ${{ vars.MNS_SUBSCRIPTION_URL }}

jobs:
Subscribe_to_MNS:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ASSUME_ROLE }}
role-skip-session-tagging: true
aws-region: ${{ vars.AWS_REGION }}
mask-aws-account-id: true

- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install dependencies and run script
run: |
pip install boto3 requests pyjwt cryptography
echo "Installed requirements"
- name: Run script
oliverbeumkes-nhs marked this conversation as resolved.
Show resolved Hide resolved
working-directory: ./lambdas
run: |
python3 -m scripts.mns_subscription
echo "Subscription complete"
65 changes: 65 additions & 0 deletions lambdas/scripts/mns_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import uuid
from urllib.error import HTTPError

import boto3
import requests
from services.base.nhs_oauth_service import NhsOauthService
from services.base.ssm_service import SSMService

env_prefix = os.getenv("SANDBOX")
url = os.getenv("URL")

ssm_service = SSMService()
auth_service = NhsOauthService(ssm_service)


headers = {
"authorization": f"Bearer {auth_service.get_active_access_token()}",
"x-correlation-id": str(uuid.uuid4()),
}

events = {
"pds-change-of-gp-1": f"/ndr/{env_prefix}/mns/subscription-id/pds-change-of-gp-1",
"pds-death-notification-1": f"/ndr/{env_prefix}/mns/subscription-id/pds-death-notification-1",
}

sqs_client = boto3.client("sqs")
sqs_url = sqs_client.get_queue_url(QueueName=f"{env_prefix}-mns-notification-queue")[
"QueueUrl"
]

sqs_arn = sqs_client.get_queue_attributes(
QueueUrl=sqs_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]


def get_subscription_id(event_type):
request_body = {
"resourceType": "Subscription",
"status": "requested",
"reason": "Integration with the National Document Repository.",
"criteria": f"eventType={event_type}",
"channel": {
"type": "message",
"endpoint": sqs_arn,
"payload": "application/json",
},
}
try:
response = requests.post(url, headers=headers, json=request_body)
response.raise_for_status()
subscription_id = response.json().get("id")
return subscription_id
except HTTPError as err:
print(err)


if __name__ == "__main__":
for event, parameter in events.items():
subscription_id = get_subscription_id(event)
ssm_service.update_ssm_parameter(
parameter_key=parameter,
parameter_value=subscription_id,
parameter_type="SecureString",
)