Skip to content

Commit 0f0b07f

Browse files
authored
Merge pull request #36 from NHSDigital/AMB-804-add-js-for-setting-logs
AMB-804 test js policy to set logging fields
2 parents 7d03ba7 + a4ba95a commit 0f0b07f

File tree

8 files changed

+58
-54
lines changed

8 files changed

+58
-54
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ release: clean publish build-proxy
4646
mkdir -p dist
4747
for f in $(_dist_include); do cp -r $$f dist; done
4848
cp ecs-proxies-deploy.yml dist/ecs-deploy-sandbox.yml
49+
cp ecs-proxies-deploy.yml dist/ecs-deploy-internal-dev.yml
4950
cp ecs-proxies-deploy.yml dist/ecs-deploy-internal-qa-sandbox.yml
5051
cp ecs-proxies-deploy.yml dist/ecs-deploy-internal-dev-sandbox.yml
5152

@@ -55,4 +56,4 @@ test:
5556

5657
smoketest:
5758
# this target is for end to end smoketests this would be run 'post deploy' to verify an environment is working
58-
poetry run pytest -v tests/api_tests.py --junitxml=smoketest-report.xml -s
59+
poetry run pytest -v tests/api_tests.py --junitxml=smoketest-report.xml -s

proxies/live/apiproxy/policies/AssignMessage.AddPayloadToSplunkTest.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

proxies/live/apiproxy/proxies/default.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
<Name>OauthV2.VerifyAccessToken</Name>
5757
</Step>
5858
</Request>
59-
<Response>
60-
<Step>
61-
<Name>AssignMessage.AddPayloadToSplunkTest</Name>
62-
</Step>
63-
</Response>
6459
<Condition>(proxy.pathsuffix MatchesPath "/splunk-test") and ((request.verb = "GET") or (request.verb = "HEAD"))</Condition>
6560
</Flow>
6661
<Flow name="ApiKeyProtectedEndpoint">
@@ -99,15 +94,6 @@
9994
<RouteRule name="NoRouteUserServiceRole">
10095
<Condition>(proxy.pathsuffix MatchesPath "/user-role-service") and ((request.verb = "GET") or (request.verb = "HEAD"))</Condition>
10196
</RouteRule>
102-
<RouteRule name="NoRouteSplunkTest">
103-
<Condition>(proxy.pathsuffix MatchesPath "/splunk-test") and ((request.verb = "GET") or (request.verb = "HEAD"))</Condition>
104-
</RouteRule>
105-
<RouteRule name="NoRouteOpenAccess">
106-
<Condition>(proxy.pathsuffix MatchesPath "/open-access") and ((request.verb = "GET") or (request.verb = "HEAD"))</Condition>
107-
</RouteRule>
108-
<RouteRule name="NoRouteApiKeyProtected">
109-
<Condition>(proxy.pathsuffix MatchesPath "/apikey-protected") and ((request.verb = "GET") or (request.verb = "HEAD"))</Condition>
110-
</RouteRule>
11197
<RouteRule name="shared-flow-testing-target">
11298
<TargetEndpoint>shared-flow-testing-target</TargetEndpoint>
11399
</RouteRule>

proxies/live/apiproxy/targets/sft-target.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
</FaultRules>
1313
<PreFlow>
1414
<Request>
15-
<Step>
16-
<Name>OauthV2.VerifyAccessToken</Name>
17-
</Step>
1815
<Step>
1916
<Name>Quota</Name>
2017
</Step>
@@ -36,11 +33,6 @@
3633
</HTTPTargetConnection>
3734
-->
3835
<HTTPTargetConnection>
39-
<URL>http://mocktarget.apigee.net</URL>
40-
<Properties>
41-
<Property name="supports.http10">true</Property>
42-
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property>
43-
<Property name="retain.queryparams">apikey</Property>
44-
</Properties>
36+
{{ HOSTED_TARGET_CONNECTION }}
4537
</HTTPTargetConnection>
4638
</TargetEndpoint>

sandbox/app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ app.get("/_ping", handlers.status);
127127
app.get("/_status", handlers.status);
128128
app.get("/health", handlers.status);
129129
app.all("/hello", handlers.hello);
130+
131+
app.get("/open-access", handlers.sampleResponse);
132+
app.get("/splunk-test", handlers.sampleResponse);
133+
app.get("/apikey-protected", handlers.sampleResponse);
134+
130135
app.use(on_error)
131136
app.use(after_request);
132137

133-
module.exports = {start: start, setup: setup};
138+
module.exports = {start: start, setup: setup};

sandbox/handlers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ async function hello(req, res, next) {
6262
next();
6363
}
6464

65+
async function sampleResponse(req, res, next) {
66+
res.json({message: "share-flow-testing"})
67+
}
68+
6569
module.exports = {
6670
status: status,
67-
hello: hello
71+
hello: hello,
72+
sampleResponse: sampleResponse
6873
};

tests/splunk_logging_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
},
121121
"client_id": {
122122
"type": "string",
123-
"pattern": "(^[0-9A-Za-z]{32}$)|Not provided"
123+
"pattern": "(^[0-9A-Za-z]{32}$)|empty"
124124
},
125125
"application_id": {
126126
"type": "string",

tests/test_splunk_logging.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TestSplunkLogging:
1414
oauth_protected_url = f"https://{ENVIRONMENT}.api.service.nhs.uk/{SERVICE_BASE_PATH}/splunk-test"
1515
apikey_protected_url = f"https://{ENVIRONMENT}.api.service.nhs.uk/{SERVICE_BASE_PATH}/apikey-protected"
1616
open_access_url = f"https://{ENVIRONMENT}.api.service.nhs.uk/{SERVICE_BASE_PATH}/open-access"
17+
ping_url = f"https://{ENVIRONMENT}.api.service.nhs.uk/{SERVICE_BASE_PATH}/_ping"
1718

1819
@staticmethod
1920
async def _get_payload_from_splunk(debug):
@@ -145,7 +146,7 @@ async def test_splunk_auth_with_nhs_login_token_exchange(self, get_token_nhs_log
145146
async def test_splunk_auth_with_invalid_token(self, debug):
146147
# Given
147148
token = "invalid token"
148-
expected_hashed_token = ""
149+
expected_hashed_token = "empty"
149150

150151
# When
151152
await debug.start_trace()
@@ -160,16 +161,16 @@ async def test_splunk_auth_with_invalid_token(self, debug):
160161
assert auth["access_token_hash"] == expected_hashed_token
161162

162163
auth_meta = auth["meta"]
163-
assert auth_meta["auth_type"] == "app"
164+
assert auth_meta["auth_type"] == "unknown"
164165
assert auth_meta["grant_type"] == ""
165-
assert auth_meta["level"] == "Level0"
166+
assert auth_meta["level"] == "-"
166167
assert auth_meta["provider"] == "apim"
167168

168169
auth_user = auth["user"]
169170
assert auth_user["user_id"] == ""
170171

171172
meta = payload["meta"]
172-
assert meta["client_id"] == "Not provided"
173+
assert meta["client_id"] == "empty"
173174
assert meta["application"] == "unknown"
174175
assert meta["product"] == ""
175176

@@ -178,7 +179,7 @@ async def test_splunk_auth_with_invalid_token(self, debug):
178179
async def test_splunk_auth_with_expired_token(self, debug):
179180
# Given
180181
token = "zRygtc34R2pwxbiUktLsMJWX0iJW"
181-
expected_hashed_token = ""
182+
expected_hashed_token = "empty"
182183

183184
# When
184185
await debug.start_trace()
@@ -193,22 +194,21 @@ async def test_splunk_auth_with_expired_token(self, debug):
193194
assert auth["access_token_hash"] == expected_hashed_token
194195

195196
auth_meta = auth["meta"]
196-
assert auth_meta["auth_type"] == "app"
197+
assert auth_meta["auth_type"] == "unknown"
197198
assert auth_meta["grant_type"] == ""
198-
assert auth_meta["level"] == "Level0"
199+
assert auth_meta["level"] == "-"
199200
assert auth_meta["provider"] == "apim"
200201

201202
auth_user = auth["user"]
202203
assert auth_user["user_id"] == ""
203204

204205
meta = payload["meta"]
205-
assert meta["client_id"] == "Not provided"
206+
assert meta["client_id"] == "empty"
206207
assert meta["application"] == "unknown"
207208
assert meta["product"] == ""
208209

209210
@pytest.mark.splunk
210211
@pytest.mark.asyncio
211-
@pytest.mark.debug
212212
async def test_splunk_auth_with_apikey(self, debug):
213213
# Given
214214
apikey = APP_CLIENT_ID
@@ -228,7 +228,7 @@ async def test_splunk_auth_with_apikey(self, debug):
228228
auth_meta = auth["meta"]
229229
assert auth_meta["auth_type"] == "app"
230230
assert auth_meta["grant_type"] == ""
231-
assert auth_meta["level"] == "Level0"
231+
assert auth_meta["level"] == "-"
232232
assert auth_meta["provider"] == "apim"
233233

234234
auth_user = auth["user"]
@@ -258,14 +258,14 @@ async def test_splunk_auth_with_invalid_apikey(self, debug):
258258
auth_meta = auth["meta"]
259259
assert auth_meta["auth_type"] == "app"
260260
assert auth_meta["grant_type"] == ""
261-
assert auth_meta["level"] == "Level0"
261+
assert auth_meta["level"] == "-"
262262
assert auth_meta["provider"] == "apim"
263263

264264
auth_user = auth["user"]
265265
assert auth_user["user_id"] == ""
266266

267267
meta = payload["meta"]
268-
assert meta["client_id"] == "Not provided"
268+
assert meta["client_id"] == ""
269269
assert meta["application"] == "unknown"
270270
assert meta["product"] == ""
271271

@@ -286,14 +286,42 @@ async def test_splunk_auth_open_access(self, debug):
286286
auth_meta = auth["meta"]
287287
assert auth_meta["auth_type"] == "app"
288288
assert auth_meta["grant_type"] == ""
289-
assert auth_meta["level"] == "Level0"
289+
assert auth_meta["level"] == "open"
290+
assert auth_meta["provider"] == "apim"
291+
292+
auth_user = auth["user"]
293+
assert auth_user["user_id"] == ""
294+
295+
meta = payload["meta"]
296+
assert meta["client_id"] == "empty"
297+
assert meta["application"] == "unknown"
298+
299+
@pytest.mark.splunk
300+
@pytest.mark.asyncio
301+
async def test_splunk_auth_open_access_ping(self, debug):
302+
# There is nothing especial about /_ping itself. It's an endpoint that doesn't have a target backend
303+
# When
304+
await debug.start_trace()
305+
requests.get(
306+
url=self.ping_url,
307+
)
308+
payload = await self._get_payload_from_splunk(debug)
309+
310+
# Then
311+
auth = payload["auth"]
312+
assert auth["access_token_hash"] == ""
313+
314+
auth_meta = auth["meta"]
315+
assert auth_meta["auth_type"] == "app"
316+
assert auth_meta["grant_type"] == ""
317+
assert auth_meta["level"] == "open"
290318
assert auth_meta["provider"] == "apim"
291319

292320
auth_user = auth["user"]
293321
assert auth_user["user_id"] == ""
294322

295323
meta = payload["meta"]
296-
assert meta["client_id"] == "Not provided"
324+
assert meta["client_id"] == "empty"
297325
assert meta["application"] == "unknown"
298326

299327
@pytest.mark.splunk

0 commit comments

Comments
 (0)