Skip to content

Commit c2660eb

Browse files
authored
IN-323 - mock sirius public api (#7)
* Mock sirius for LPA endpoints * Mock secrets manager * run local version in docker inc mock sirius and mock secrets manager
1 parent cf2fcfa commit c2660eb

23 files changed

+1203
-10
lines changed

.secrets.baseline

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": null,
44
"lines": null
55
},
6-
"generated_at": "2020-08-04T16:40:33Z",
6+
"generated_at": "2020-08-04T13:24:40Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -58,7 +58,7 @@
5858
{
5959
"hashed_secret": "dc724af18fbdd4e59189f5fe768a5f8311527050",
6060
"is_verified": false,
61-
"line_number": 38,
61+
"line_number": 60,
6262
"type": "Secret Keyword"
6363
}
6464
]

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,22 @@ LPA related functions.
2222

2323
## Local Environment
2424

25-
Information about spinning up the local environment
25+
#### Running flask app locally, aka the quick way
26+
27+
1. Start a virtual environment
28+
1. Add all the env vars: `source .env`
29+
1. `cd lambda_functions/v1/functions/lpa/app`
30+
1. flask run
31+
1. Endpoints should be available on `http://localhost:5000`
32+
33+
#### Running everything in Docker
34+
35+
1. In the root folder: `docker-compose up`
36+
1. Wait a minute
37+
1. `cd mock_aws_services`
38+
1. `python create_secret.py`
39+
* steps 3 & 4 are temporary, test data will soon be inserted automatically
40+
1. Endpoints should be available on `http://0.0.0.0:4343`
2641

2742
## Unit Tests
2843

docker-compose.yml

+26
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,33 @@ services:
2424
#Uncomment these two options for debugging purposes:
2525
# PACT_BROKER_WEBHOOK_HOST_WHITELIST: circleci.com
2626
# PACT_BROKER_LOG_LEVEL: DEBUG
27+
mock-sirius:
28+
build:
29+
context: ./mock_sirius_backend
30+
dockerfile: Dockerfile
31+
environment:
32+
- MOCKING_ENV
33+
volumes:
34+
- ./mock_sirius_backend/:/var/www/mock_sirius_backend/
35+
ports:
36+
- "5001:5001"
37+
motoserver:
38+
build:
39+
context: ./mock_aws_services
40+
dockerfile: Dockerfile
41+
ports:
42+
- "4584:4584"
2743
api_gateway:
2844
build:
2945
context: ./lambda_functions/v1
3046
ports:
3147
- "4343:4343"
48+
links:
49+
- mock-sirius
50+
- motoserver
51+
depends_on:
52+
- mock-sirius
53+
- motoserver
3254
volumes:
3355
- ./lambda_functions/v1/:/var/www/lambda_functions/v1/
3456
environment:
@@ -39,6 +61,10 @@ services:
3961
AWS_SECURITY_TOKEN: testing
4062
AWS_SESSION_TOKEN: testing
4163
AWS_DEFAULT_REGION: eu-west-1
64+
SIRIUS_BASE_URL: http://mock-sirius:5001
65+
SIRIUS_API_VERSION: v1
66+
SESSION_DATA: publicapi@opgtest.com
67+
LOCALSTACK_HOST: motoserver
4268
networks:
4369
default:
4470
aliases:

lambda_functions/v1/Dockerfile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM python:3.8-slim-buster
2+
WORKDIR /var/www/lambda_functions/v1
3+
4+
5+
6+
# Install Python Dependencies
7+
8+
COPY requirements requirements
9+
RUN cat ./requirements/*.txt | uniq | grep -v "#" > ./requirements/local-requirements.txt
10+
RUN pip install -r ./requirements/local-requirements.txt
11+
12+
EXPOSE 4343
13+
14+
WORKDIR /var/www/lambda_functions/v1/functions/lpa/app
15+
16+
17+
18+
ENV FLASK_APP "lpa_local.py"
19+
ENV FLASK_ENV "development"
20+
ENV FLASK_DEBUG True
21+
22+
CMD flask run --host=0.0.0.0 --port=4343
23+
24+
25+

lambda_functions/v1/functions/__init__.py

Whitespace-only changes.

lambda_functions/v1/functions/lpa/__init__.py

Whitespace-only changes.

lambda_functions/v1/functions/lpa/app/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ def create_app(Flask):
66

77
app.register_blueprint(api_blueprint)
88

9+
routes = [str(p) for p in app.url_map.iter_rules()]
10+
print(f"routes: {routes}")
11+
912
return app

lambda_functions/v1/functions/lpa/app/api/resources.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
logger = custom_logger("")
1111

12-
version = os.getenv("API_VERSION")
12+
version = "v1"
1313
print(f"version: {version}")
1414
api = Blueprint("api", __name__, url_prefix=f"/{version}")
1515

lambda_functions/v1/functions/lpa/app/api/sirius_service.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
import boto3
66
import jwt
7+
import localstack_client.session
8+
79
import requests
810
from botocore.exceptions import ClientError
911

12+
1013
from .helpers import custom_logger
1114

1215
logger = custom_logger("sirius_service")
@@ -55,11 +58,20 @@ def get_secret(environment):
5558
"""
5659

5760
secret_name = f"{environment}/jwt-key"
58-
print(f"secret_name: {secret_name}")
5961
region_name = "eu-west-1"
6062

61-
session = boto3.session.Session()
62-
client = session.client(service_name="secretsmanager", region_name=region_name)
63+
try:
64+
if os.environ["ENVIRONMENT"] == "local":
65+
current_session = localstack_client.session.Session()
66+
67+
else:
68+
current_session = boto3.session.Session()
69+
except KeyError:
70+
current_session = boto3.session.Session()
71+
72+
client = current_session.client(
73+
service_name="secretsmanager", region_name=region_name
74+
)
6375

6476
try:
6577
get_secret_value_response = client.get_secret_value(SecretId=secret_name)

lambda_functions/v1/functions/lpa/app/lpa_local.py

-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@
22
from flask import Flask
33

44
lambda_handler = create_app(Flask)
5-
6-
routes = [str(p) for p in lambda_handler.url_map.iter_rules()]
7-
print(f"routes: {routes}")

lambda_functions/v1/requirements/dev-requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ pyjwt
1313
hypothesis
1414
validators
1515
yarl
16+
localstack-client
17+
tenacity

mock_aws_services/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:alpine
2+
3+
RUN apk add --no-cache gcc musl-dev libffi-dev openssl-dev && \
4+
pip install moto[server] && \
5+
apk del gcc musl-dev libffi-dev openssl-dev
6+
7+
8+
CMD moto_server secretsmanager -H 0.0.0.0 -p 4584
9+

mock_aws_services/create_secret.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
from tenacity import retry
3+
import requests
4+
5+
url = "http://0.0.0.0:4584/?Action=CreateSecret"
6+
7+
8+
payload = {
9+
"Name": "local/jwt-key",
10+
"Description": "Secret for local testing with Docker",
11+
"SecretString": "this_is_a_secret_string",
12+
"ClientRequestToken": "EXAMPLE1-90ab-cdef-fedc-ba987SECRET1",
13+
}
14+
15+
headers = {"Content-Type": "application/json"}
16+
17+
18+
@retry
19+
def insert_secret():
20+
print("Trying....")
21+
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
22+
if response.status_code > 300:
23+
print("Container not ready yet")
24+
raise ConnectionError
25+
else:
26+
print(response.text.encode("utf8"))
27+
28+
29+
insert_secret()

mock_sirius_backend/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# our base image
2+
FROM python:3.6-slim
3+
4+
WORKDIR /var/www/
5+
COPY requirements.txt .
6+
RUN pip install -r requirements.txt
7+
8+
9+
10+
ENV FLASK_APP=mock_sirius_backend/app.py
11+
ENV FLASK_ENV=development
12+
13+
CMD ["python", "mock_sirius_backend/app.py"]

mock_sirius_backend/__init__.py

Whitespace-only changes.

mock_sirius_backend/api/__init__.py

Whitespace-only changes.
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from ..utilities import load_data
2+
3+
from textwrap import wrap
4+
5+
6+
def handle_lpa_get(query_params):
7+
8+
if "lpaonlinetoolid" in query_params:
9+
lpa_online_tool_id = query_params["lpaonlinetoolid"]
10+
print(f"using lpa online tool with id {lpa_online_tool_id}")
11+
12+
if lpa_online_tool_id[0] == "A":
13+
print(f"test_id is a valid lpa-online-tool id: {lpa_online_tool_id}")
14+
15+
response_data = load_data(
16+
parent_folder="lpas",
17+
filename="lpa_online_tool_response.json",
18+
as_json=False,
19+
)
20+
21+
response_data["onlineLpaId"] = lpa_online_tool_id
22+
23+
response_data = [response_data]
24+
25+
return 200, response_data
26+
27+
elif lpa_online_tool_id[:5] == "crash":
28+
print("oh no you crashed sirius")
29+
30+
response_code = lpa_online_tool_id[-3:]
31+
32+
return response_code, f"Sirius broke bad - error {response_code}"
33+
34+
else:
35+
print(f"{lpa_online_tool_id} is not a lpa-online-tool id")
36+
return 404, ""
37+
38+
elif "uid" in query_params:
39+
40+
sirius_uid = str(query_params["uid"])
41+
42+
print(f"using use my lpa with id {sirius_uid}")
43+
44+
if sirius_uid[0] == "7":
45+
print(f"test_id is a valid sirius uid: {sirius_uid}")
46+
47+
response_data = load_data(
48+
parent_folder="lpas", filename="use_an_lpa_response.json", as_json=False
49+
)
50+
51+
response_data["uid"] = "-".join(wrap(sirius_uid, 4))
52+
53+
response_data = [response_data]
54+
55+
return 200, response_data
56+
57+
elif len(sirius_uid) == 3:
58+
print("oh no you crashed sirius")
59+
60+
response_code = sirius_uid
61+
62+
return response_code, f"Sirius broke bad - error {response_code}"
63+
64+
else:
65+
print(f"{sirius_uid} is not a sirius uid")
66+
return 404, ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"onlineLpaId": "A39721583862",
3+
"receiptDate": "2017-04-11",
4+
"registrationDate": null,
5+
"rejectedDate": null,
6+
"status": "Pending"
7+
}

0 commit comments

Comments
 (0)