Skip to content

Commit f3ab4e1

Browse files
authored
chore(security): harden CI, Dockerfile, and bump deps (#22)
CI (push.yaml): - Add top-level permissions: {} and per-job least-privilege (contents: read, packages: write for ghcr push) - SHA-pin all actions with version comments: - actions/checkout v2 -> v4.3.1 - docker/setup-buildx-action v1 -> v3.12.0 - docker/login-action v1 -> v3.7.0 - docker/build-push-action v2 -> v6.19.2 - Add persist-credentials: false to checkout Dockerfile: - Bump python:3.8-slim-buster -> python:3.12-slim (Python 3.8 is EOL; Debian buster is also EOL) - SHA-pin the base image - Add --no-cache-dir to pip install (smaller image) - Run as non-root user (uid 1001) requirements.txt — bump all deps (3.5-4 year old pins, several with CVEs): - apscheduler 3.7.0 -> 3.11.2 - requests 2.25.0 -> 2.34.2 (fixes CVE-2024-35195 among others) - python-decouple 3.3 -> 3.8 - prometheus-client 0.9.0 -> 0.25.0 - pygithub 1.51 -> 2.9.1 github-traffic.py: - Switch to PyGithub 2.x auth API (Github(auth=Auth.Token(token))) to clear DeprecationWarning - De-duplicate redundant imports
1 parent 908408d commit f3ab4e1

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

.github/workflows/push.yaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
name: "push"
33

4+
permissions: {}
5+
46
on:
57
release:
68
types:
@@ -10,22 +12,27 @@ jobs:
1012
build:
1113
name: "Build"
1214
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
1318
steps:
14-
- name: Check Out Repo
15-
uses: actions/checkout@v2
19+
- name: Check Out Repo
20+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
21+
with:
22+
persist-credentials: false
1623

1724
- name: "Setup buildx"
18-
uses: docker/setup-buildx-action@v1
25+
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
1926

2027
- name: "Login into ghcr"
21-
uses: docker/login-action@v1
28+
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
2229
with:
2330
registry: ghcr.io
2431
username: ${{ github.actor }}
2532
password: ${{ secrets.GITHUB_TOKEN }}
2633

2734
- name: "Build image"
28-
uses: docker/build-push-action@v2
35+
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
2936
with:
3037
context: .
3138
push: true

Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
FROM python:3.8-slim-buster
1+
FROM python:3.12-slim@sha256:090ba77e2958f6af52a5341f788b50b032dd4ca28377d2893dcf1ecbdfdfe203
22

33
WORKDIR /app
44

55
COPY requirements.txt requirements.txt
6-
RUN pip3 install -r requirements.txt
6+
RUN pip3 install --no-cache-dir -r requirements.txt
77

88
COPY github-traffic.py /app/github-traffic.py
99

10-
CMD [ "python3", "github-traffic.py"]
10+
RUN useradd -u 1001 -m -s /sbin/nologin app
11+
USER app
12+
13+
CMD [ "python3", "github-traffic.py"]

github-traffic.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from github import Github
1+
from github import Auth, Github
22
from decouple import config
33
from prometheus_client import start_http_server, Gauge
4-
from decouple import config
54
from apscheduler.schedulers.blocking import BlockingScheduler
65
from apscheduler.triggers.cron import CronTrigger
7-
from prometheus_client import start_http_server, Gauge
86
from logfmt_logger import getLogger
97

108
ORG_NAME = config("ORG_NAME", default="")
@@ -14,7 +12,7 @@
1412
CRONTAB_SCHEDULE = config("CRONTAB_SCHEDULE", default="0 * * * *")
1513
GITHUB_TOKEN = config('GITHUB_TOKEN')
1614

17-
github = Github(GITHUB_TOKEN)
15+
github = Github(auth=Auth.Token(GITHUB_TOKEN))
1816

1917
logger = getLogger("github_traffic")
2018

requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
apscheduler==3.7.0
2-
requests==2.25.0
3-
python-decouple==3.3
4-
prometheus-client==0.9.0
5-
pygithub==1.51
6-
logfmt-logger==0.1.2
1+
apscheduler==3.11.2
2+
requests==2.34.2
3+
python-decouple==3.8
4+
prometheus-client==0.25.0
5+
pygithub==2.9.1
6+
logfmt-logger==0.1.2

0 commit comments

Comments
 (0)