Skip to content

Commit 762f9ec

Browse files
authored
Initial commit
0 parents  commit 762f9ec

File tree

9 files changed

+585
-0
lines changed

9 files changed

+585
-0
lines changed

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# ---> Python
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
7+
# C extensions
8+
*.so
9+
10+
# Distribution / packaging
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
cover/
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Django stuff:
60+
*.log
61+
local_settings.py
62+
db.sqlite3
63+
db.sqlite3-journal
64+
65+
# Flask stuff:
66+
instance/
67+
.webassets-cache
68+
69+
# Scrapy stuff:
70+
.scrapy
71+
72+
# Sphinx documentation
73+
docs/_build/
74+
75+
# PyBuilder
76+
.pybuilder/
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
# For a library or package, you might want to ignore these files since the code is
88+
# intended to run in multiple environments; otherwise, check them in:
89+
# .python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# poetry
99+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100+
# This is especially recommended for binary packages to ensure reproducibility, and is more
101+
# commonly ignored for libraries.
102+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103+
#poetry.lock
104+
105+
# pdm
106+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107+
#pdm.lock
108+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109+
# in version control.
110+
# https://pdm.fming.dev/#use-with-ide
111+
.pdm.toml
112+
113+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
114+
__pypackages__/
115+
116+
# Celery stuff
117+
celerybeat-schedule
118+
celerybeat.pid
119+
120+
# SageMath parsed files
121+
*.sage.py
122+
123+
# Environments
124+
.env
125+
.venv
126+
env/
127+
venv/
128+
ENV/
129+
env.bak/
130+
venv.bak/
131+
132+
# Spyder project settings
133+
.spyderproject
134+
.spyproject
135+
136+
# Rope project settings
137+
.ropeproject
138+
139+
# mkdocs documentation
140+
/site
141+
142+
# mypy
143+
.mypy_cache/
144+
.dmypy.json
145+
dmypy.json
146+
147+
# Pyre type checker
148+
.pyre/
149+
150+
# pytype static type analyzer
151+
.pytype/
152+
153+
# Cython debug symbols
154+
cython_debug/
155+
156+
# PyCharm
157+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159+
# and can be added to the global gitignore or merged into this file. For a more nuclear
160+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
161+
#.idea/
162+

.pylintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[main]
2+
3+
ignore=tests,.env

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.12.5-bullseye
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Install system dependencies if needed,
8+
# not supported by Nexus unfortunately
9+
RUN apt-get update -y
10+
RUN apt upgrade -y
11+
12+
# Install Python dependencies
13+
COPY requirements.txt .
14+
RUN pip install --no-cache-dir -r requirements.txt
15+
16+
# Copy the current directory contents into the container
17+
COPY . .
18+
19+
# Expose port 8080 to the outside world
20+
EXPOSE 8080
21+
22+
# Run app.py when the container launches
23+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024 damien
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# GCP Python FastAPI
2+
3+
This project sets up a simple FastAPI application (with some vulnerabilites) within a Docker container. It uses the official Python runtime and includes all necessary configurations to deploy a FastAPI app with Docker. The container will expose the app on port 80 and automatically run the FastAPI app on startup.
4+
5+
## Requirements
6+
7+
- Docker
8+
- Python 3.12+
9+
- FastAPI
10+
- Uvicorn
11+
12+
## Features
13+
14+
- **Dockerized FastAPI application**: A containerized setup for easy deployment.
15+
- **Python 3.12.5 runtime**: Uses the latest stable Python version as a base.
16+
- **Efficient package installation**: Installs required dependencies via `requirements.txt`.
17+
18+
## Project Structure
19+
20+
```bash
21+
gcp-python-fastapi/
22+
├── Dockerfile
23+
├── requirements.txt
24+
├── main.py # FastAPI app entry point
25+
└── ...
26+
```
27+
28+
- **Dockerfile**: Configures the Docker container for the FastAPI app.
29+
- **requirements.txt**: Specifies the required Python dependencies for the application.
30+
- **main.py**: The entry point for the FastAPI application (Make sure to include this file in the project structure).
31+
32+
## Setup and Installation
33+
34+
### 1. Clone the repository
35+
36+
If you haven't cloned the project yet, use the following command:
37+
38+
```bash
39+
git clone https://github.com/your-username/awesome-fastapi.git
40+
cd awesome-fastapi
41+
```
42+
43+
### 2. Build the Docker image
44+
45+
To build the Docker image, run the following command in the root of the project directory:
46+
47+
```bash
48+
docker build -t awesome-fastapi .
49+
```
50+
51+
### 3. Run the Docker container
52+
53+
After the image is built, run the container:
54+
55+
```bash
56+
docker run -d -p 80:80 awesome-fastapi
57+
```
58+
59+
This command will run the FastAPI app on port 80 of your localhost.
60+
61+
### 4. Access the app
62+
63+
Once the container is running, you can access the FastAPI application by navigating to:
64+
65+
```
66+
http://localhost:80
67+
```
68+
69+
## Dependencies
70+
71+
The project uses the following Python packages, which are listed in the `requirements.txt` file:
72+
73+
- `fastapi`: The core framework for building the API.
74+
- `uvicorn`: ASGI server to run the FastAPI application.
75+
76+
To install dependencies locally, use the following:
77+
78+
```bash
79+
pip install -r requirements.txt
80+
```
81+
82+
## Notes
83+
84+
- This setup assumes that your FastAPI app’s entry point is `main.py`, and the FastAPI application instance is named `app`. If your app is structured differently, you may need to modify the `CMD` directive in the Dockerfile accordingly.
85+
- The container will expose the application on port 80. If you want to use a different port, adjust the `EXPOSE` and `CMD` directives in the Dockerfile.

cloudbuild.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
steps:
2+
# Step 1: Build Docker Image
3+
- name: "gcr.io/cloud-builders/docker"
4+
args: ["build", "-t", "gcr.io/$PROJECT_ID/my-app:latest", "."]
5+
6+
# Step 2: Push Docker Image to Container Registry
7+
- name: "gcr.io/cloud-builders/docker"
8+
args: ["push", "gcr.io/$PROJECT_ID/my-app:latest"]
9+
10+
# Step 3: Run Unit Tests
11+
- name: "python"
12+
entrypoint: "bash"
13+
args:
14+
- "-c"
15+
- |
16+
pip install -r requirements.txt
17+
pytest tests/
18+
19+
# Step 4: Scan Docker Image with Trivy
20+
- name: "aquasec/trivy"
21+
args: ["image", "--exit-code", "0", "gcr.io/$PROJECT_ID/my-app:latest"]
22+
23+
# Step 5: Snyk Code and Dependency Scanning
24+
- name: "node"
25+
entrypoint: "bash"
26+
secretEnv: ["SNYK_TOKEN"]
27+
args:
28+
- "-c"
29+
- |
30+
npm install -g snyk
31+
snyk auth $$SNYK_TOKEN
32+
snyk test || true
33+
snyk code test || true
34+
35+
# Step 6: Deploy to Cloud Run
36+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
37+
entrypoint: "bash"
38+
args:
39+
- "-c"
40+
- |
41+
gcloud run deploy gcp-python-fastapi-service \
42+
--image gcr.io/$PROJECT_ID/my-app:latest \
43+
--region us-central1 \
44+
--platform managed
45+
46+
gcloud run services add-iam-policy-binding \
47+
gcp-python-fastapi-service \
48+
--platform=managed \
49+
--region=us-central1 \
50+
--member="allUsers" \
51+
--role="roles/run.invoker" \
52+
53+
options:
54+
defaultLogsBucketBehavior: REGIONAL_USER_OWNED_BUCKET
55+
artifacts:
56+
objects:
57+
location: "gs://dsb-devsecops-lab-bucket"
58+
paths: ["*/**"]
59+
availableSecrets:
60+
secretManager:
61+
- versionName: projects/724455289756/secrets/cloudbuild-snyk-token/versions/2
62+
env: "SNYK_TOKEN"

0 commit comments

Comments
 (0)