Skip to content

Commit 10f3e99

Browse files
authored
Apply recommendation for security and reliability (#24)
Apply recommendations in code and documentation - [CI] restrict permissions to `read-all` instead of the default `write-all` - Example `openapi.yaml` : add a note about using `security:` definition when deploying to production - Example `README.md` : add a note about Lambda functions configuration with improved security and scalability changes for production environment
1 parent c72834d commit 10f3e99

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

.github/workflows/pull_request.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
pull_request:
99
types: [opened, reopened, synchronize]
1010

11+
# As per Checkov CKV2_GHA_1
12+
permissions: read-all
13+
1114
jobs:
1215
soundness:
1316
name: Soundness

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ DerivedData/
1111
.swiftpm/config/registries.json
1212
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
1313
*key
14+
.ash

Examples/quoteapi/Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ builder-bot:
4949
docker build -f Dockerfile . -t swift-builder
5050

5151
# prep directories
52+
rm -rf $($@ARTIFACTS_DIR)
5253
mkdir -p $($@BUILD_DIR)/lambda $($@ARTIFACTS_DIR)
5354

5455
# compile application inside Docker image using source code from local project folder
@@ -58,8 +59,9 @@ builder-bot:
5859
# create lambda bootstrap file
5960
docker run --rm -v $($@BUILD_DIR):/build-target -v `pwd`:/build-src -w /build-src swift-builder bash -cl "cd /build-target/lambda && ln -s $($@PRODUCT) /bootstrap"
6061

61-
# copy binary to stage
62-
cp $($@BUILD_DIR)/release/$($@PRODUCT) $($@STAGE)/bootstrap
63-
64-
# copy app from stage to artifacts dir
65-
cp $($@STAGE)/* $($@ARTIFACTS_DIR)
62+
# copy binary to artifacts dir
63+
cp $($@BUILD_DIR)/release/$($@PRODUCT) $($@ARTIFACTS_DIR)/bootstrap
64+
65+
# copy resources to artifacts dir
66+
[ -d "$($@BUILD_DIR)/release/$($@PRODUCT)_$($@PRODUCT).resources" ] && cp $($@BUILD_DIR)/release/$($@PRODUCT)_$($@PRODUCT).resources/* $($@ARTIFACTS_DIR) || true
67+

Examples/quoteapi/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
This application illustrates how to deploy a Server-Side Swift workload on AWS using the [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/) toolkit. The workload is a simple REST API that returns a string from an Amazon API Gateway. Requests to the API Gateway endpoint are handled by an AWS Lambda Function written in Swift.
44

5-
65
## Prerequisites
76

87
To build this sample application, you need:
@@ -81,3 +80,20 @@ When finished with your application, use SAM to delete it from your AWS account.
8180
```bash
8281
sam delete
8382
```
83+
84+
## ⚠️ Security and Reliability Notice
85+
86+
This is an example application for demonstration purposes. When deploying such infrastructure in production environments, we strongly encourage you to follow these best practices for improved security and resiliency:
87+
88+
- Enable access logging on API Gateway ([documentation](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html))
89+
- Ensure that AWS Lambda function is configured for function-level concurrent execution limit ([concurrency documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html), [configuration guide](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html))
90+
- Check encryption settings for Lambda environment variables ([documentation](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html))
91+
- Ensure that AWS Lambda function is configured for a Dead Letter Queue (DLQ) ([documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html#invocation-dlq))
92+
- Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources ([documentation](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html), [code example](https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples/ServiceLifecycle%2BPostgres))
93+
94+
**Note:** The `openapi.yaml` file in this example is not suited for production. In real-world scenarios, you must:
95+
1. Ensure that the global security field has rules defined
96+
2. Ensure that security operations is not empty ([OpenAPI Security Specification](https://learn.openapis.org/specification/security.html))
97+
3. Follow proper authentication, authorization, input validation, and error handling practices
98+
99+
As per Checkov CKV_OPENAPI_4 and CKV_OPENAPI_5 security checks.

Examples/quoteapi/Sources/QuoteAPI/openapi.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
# This is an example API definition not suited for production
2+
#
3+
# In real life scenario, you must
4+
# 1. Ensure that the global security field has rules defined
5+
# 2. Ensure that security operations is not empty.
6+
# https://learn.openapis.org/specification/security.html
7+
#
8+
# As per Checkov CKV_OPENAPI_4 and CKV_OPENAPI_5
9+
110
openapi: 3.1.0
211
info:
312
title: StockQuoteService
413
version: 1.0.0
5-
14+
15+
# security:
16+
# - defaultApiKey: []
17+
618
components:
719
schemas:
820
quote:
@@ -54,3 +66,5 @@ paths:
5466
description: Authentication required
5567
404:
5668
description: Not Found
69+
# security:
70+
# - defaultApiKey: []

Examples/quoteapi/template.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: SAM Template for QuoteService
44

5+
# This is an example SAM template for the purpose of this project.
6+
# When deploying such infrastructure in production environment,
7+
# we strongly encourage you to follow these best practices for improved security and resiliency
8+
# - Enable access loggin on API Gateway
9+
# See: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html)
10+
# - Ensure that AWS Lambda function is configured for function-level concurrent execution limit
11+
# See: https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html
12+
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html
13+
# - Check encryption settings for Lambda environment variable
14+
# See: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html
15+
# - Ensure that AWS Lambda function is configured for a Dead Letter Queue(DLQ)
16+
# See: https://docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html#invocation-dlq
17+
# - Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources
18+
# See: https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html
19+
# Code Example: https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples/ServiceLifecycle%2BPostgres
20+
521
Globals:
622
Function:
723
Timeout: 60

0 commit comments

Comments
 (0)