Skip to content

Commit 174f26f

Browse files
refactor(examples): fix issues reported by SonarCloud and Scorecard (#5315)
* Fixing problems with Sonar + Scorecard * Fixing gh action
1 parent 4bc5731 commit 174f26f

12 files changed

+40
-22
lines changed

Diff for: .github/workflows/layer_rename.yml

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@ on:
1616
options:
1717
- beta
1818
- prod
19-
default: Gamma
19+
default: beta
2020
required: true
2121
version:
2222
description: Layer version to duplicate
23-
type: number
23+
type: string
2424
required: true
2525
workflow_call:
2626
inputs:
2727
environment:
2828
description: Deployment environment
2929
type: string
30-
default: Gamma
3130
required: true
3231
version:
3332
description: Layer version to duplicate
34-
type: number
33+
type: string
3534
required: true
3635

3736
name: Layer Rename
3837
run-name: Layer Rename - ${{ inputs.environment }}
3938

39+
permissions:
40+
contents: read
41+
4042
jobs:
4143
download:
4244
runs-on: ubuntu-latest
@@ -136,7 +138,7 @@ jobs:
136138
- name: Verify Layer Signature
137139
run: |
138140
SHA=$(jq -r '.Content.CodeSha256' ${{ matrix.layer }}_x86_64.json)
139-
test $(openssl dgst -sha256 -binary ${{ matrix.layer }}_x86_64.zip | openssl enc -base64) == $SHA && echo "SHA OK: ${SHA}" || exit 1
141+
test $(openssl dgst -sha256 -binary ${{ matrix.layer }}_x86_64.zip | openssl enc -base64) == $SHA && echo "SHA OK: ${SHA}" || exit 1
140142
- name: Configure AWS Credentials
141143
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
142144
with:
@@ -158,4 +160,4 @@ jobs:
158160
--statement-id 'PublicLayer' \
159161
--action lambda:GetLayerVersion \
160162
--principal '*' \
161-
--version-number
163+
--version-number

Diff for: docs/core/event_handler/api_gateway.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Each dynamic route you set must be part of your function signature. This allows
198198

199199
=== "dynamic_routes.py"
200200

201-
```python hl_lines="14 16"
201+
```python hl_lines="16 18"
202202
--8<-- "examples/event_handler_rest/src/dynamic_routes.py"
203203
```
204204

@@ -640,7 +640,7 @@ matches one of the allowed values.
640640

641641
=== "setting_cors.py"
642642

643-
```python hl_lines="5 11-12 34"
643+
```python hl_lines="7 14-15 38"
644644
--8<-- "examples/event_handler_rest/src/setting_cors.py"
645645
```
646646

@@ -652,7 +652,7 @@ matches one of the allowed values.
652652

653653
=== "setting_cors_extra_origins.py"
654654

655-
```python hl_lines="5 11-12 34"
655+
```python hl_lines="7 14 15 38"
656656
--8<-- "examples/event_handler_rest/src/setting_cors_extra_origins.py"
657657
```
658658

@@ -943,7 +943,7 @@ You can compress with gzip and base64 encode your responses via `compress` param
943943

944944
=== "compressing_responses_using_route.py"
945945

946-
```python hl_lines="17 27"
946+
```python hl_lines="19 29"
947947
--8<-- "examples/event_handler_rest/src/compressing_responses_using_route.py"
948948
```
949949

@@ -1154,7 +1154,7 @@ Let's assume you have `split_route.py` as your Lambda function entrypoint and ro
11541154
!!! info
11551155
This means all methods, including [middleware](#middleware) will work as usual.
11561156

1157-
```python hl_lines="5 13 16 25 28"
1157+
```python hl_lines="7 10 15 18 27 30"
11581158
--8<-- "examples/event_handler_rest/src/split_route_module.py"
11591159
```
11601160

@@ -1186,7 +1186,7 @@ When necessary, you can set a prefix when including a router object. This means
11861186

11871187
=== "split_route_prefix_module.py"
11881188

1189-
```python hl_lines="13 25"
1189+
```python hl_lines="14 26"
11901190
--8<-- "examples/event_handler_rest/src/split_route_prefix_module.py"
11911191
```
11921192

Diff for: docs/utilities/middleware_factory.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
3030
### Middleware with before logic
3131

3232
=== "getting_started_middleware_before_logic_function.py"
33-
```python hl_lines="5 26 27 36 37 39 44 45"
33+
```python hl_lines="5 26 27 35 36 38 41 42"
3434
--8<-- "examples/middleware_factory/src/getting_started_middleware_before_logic_function.py"
3535
```
3636

@@ -58,7 +58,7 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
5858
You can also have your own keyword arguments after the mandatory arguments.
5959

6060
=== "getting_started_middleware_with_params_function.py"
61-
```python hl_lines="6 30 31 41 56 57"
61+
```python hl_lines="6 30 31 41 53 54"
6262
--8<-- "examples/middleware_factory/src/getting_started_middleware_with_params_function.py"
6363
```
6464

Diff for: examples/event_handler_rest/src/compressing_responses_using_route.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24

35
from aws_lambda_powertools import Logger, Tracer
@@ -27,6 +29,7 @@ def get_todos():
2729
@app.get("/todos/<todo_id>", compress=True)
2830
@tracer.capture_method
2931
def get_todo_by_id(todo_id: str): # same example using Response class
32+
todo_id = quote(todo_id, safe="")
3033
todos: requests.Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
3134
todos.raise_for_status()
3235

Diff for: examples/event_handler_rest/src/dynamic_routes.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24
from requests import Response
35

@@ -14,6 +16,7 @@
1416
@app.get("/todos/<todo_id>")
1517
@tracer.capture_method
1618
def get_todo_by_id(todo_id: str): # value come as str
19+
todo_id = quote(todo_id, safe="")
1720
todos: Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
1821
todos.raise_for_status()
1922

Diff for: examples/event_handler_rest/src/setting_cors.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24
from requests import Response
35

@@ -26,6 +28,7 @@ def get_todos():
2628
@app.get("/todos/<todo_id>")
2729
@tracer.capture_method
2830
def get_todo_by_id(todo_id: str): # value come as str
31+
todo_id = quote(todo_id, safe="")
2932
todos: Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
3033
todos.raise_for_status()
3134

Diff for: examples/event_handler_rest/src/setting_cors_extra_origins.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24
from requests import Response
35

@@ -26,6 +28,7 @@ def get_todos():
2628
@app.get("/todos/<todo_id>")
2729
@tracer.capture_method
2830
def get_todo_by_id(todo_id: str): # value come as str
31+
todo_id = quote(todo_id, safe="")
2932
todos: Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
3033
todos.raise_for_status()
3134

Diff for: examples/event_handler_rest/src/split_route_module.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24
from requests import Response
35

@@ -27,6 +29,7 @@ def get_todos():
2729
def get_todo_by_id(todo_id: str): # value come as str
2830
api_key = router.current_event.headers["X-Api-Key"]
2931

32+
todo_id = quote(todo_id, safe="")
3033
todos: Response = requests.get(f"{endpoint}/{todo_id}", headers={"X-Api-Key": api_key})
3134
todos.raise_for_status()
3235

Diff for: examples/event_handler_rest/src/split_route_prefix_module.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import quote
2+
13
import requests
24
from requests import Response
35

@@ -27,6 +29,7 @@ def get_todos():
2729
def get_todo_by_id(todo_id: str): # value come as str
2830
api_key = router.current_event.headers["X-Api-Key"]
2931

32+
todo_id = quote(todo_id, safe="")
3033
todos: Response = requests.get(f"{endpoint}/{todo_id}", headers={"X-Api-Key": api_key})
3134
todos.raise_for_status()
3235

Diff for: examples/middleware_factory/src/combining_powertools_utilities_function.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from typing import Callable
3+
from urllib.parse import quote
34

45
import boto3
56
import combining_powertools_utilities_schema as schemas
@@ -103,19 +104,20 @@ def get_comments():
103104

104105
return {"comments": comments.json()[:10]}
105106
except Exception as exc:
106-
raise InternalServerError(str(exc))
107+
raise InternalServerError(str(exc)) from exc
107108

108109

109110
@app.get("/comments/<comment_id>")
110111
@tracer.capture_method
111112
def get_comments_by_id(comment_id: str):
112113
try:
114+
comment_id = quote(comment_id, safe="")
113115
comments: requests.Response = requests.get(f"https://jsonplaceholder.typicode.com/comments/{comment_id}")
114116
comments.raise_for_status()
115117

116118
return {"comments": comments.json()}
117119
except Exception as exc:
118-
raise InternalServerError(str(exc))
120+
raise InternalServerError(str(exc)) from exc
119121

120122

121123
@middleware_custom

Diff for: examples/middleware_factory/src/getting_started_middleware_before_logic_function.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def middleware_before(
3535
if "status_id" not in detail:
3636
event["detail"]["status_id"] = "pending"
3737

38-
response = handler(event, context)
39-
40-
return response
38+
return handler(event, context)
4139

4240

4341
@middleware_before

Diff for: examples/middleware_factory/src/getting_started_middleware_with_params_function.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ def obfuscate_sensitive_data(
4242
if guest_data.get(guest_field):
4343
event["detail"]["guest"][guest_field] = obfuscate_data(str(guest_data.get(guest_field)))
4444

45-
response = handler(event, context)
46-
47-
return response
45+
return handler(event, context)
4846

4947

5048
def obfuscate_data(value: str) -> bytes:

0 commit comments

Comments
 (0)