Somewhat stable VAPID generation

This commit is contained in:
Matthew Raymer
2023-09-29 08:08:06 -04:00
parent ebebd2fce2
commit d2d4071c61
4 changed files with 22 additions and 8 deletions

View File

@@ -3,23 +3,23 @@ FROM python:3.8-alpine3.18 as builder
RUN apk update && apk upgrade RUN apk update && apk upgrade
RUN apk add --no-cache --virtual .build-deps build-base git RUN apk add --no-cache --virtual .build-deps build-base git
RUN apk add bash libffi-dev tzdata --upgrade --no-cache RUN apk add --upgrade --no-cache bash sqlite libffi-dev tzdata
ENV TZ America/New_York ENV TZ America/New_York
# Set the working directory in the container to /app # Set the working directory in the container to /app
WORKDIR /app WORKDIR /app
RUN mkdir -p /app/instance/data
# Copy the current directory contents into the container at /app # Copy the current directory contents into the container at /app
COPY app.py /app COPY app.py /app
COPY requirements.txt /app COPY requirements.txt /app
COPY models.py /app COPY models.py /app
COPY init_db.py /app/init_db.py COPY init_db.py /app/init_db.py
# Install any needed packages specified in requirements.txt # Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
RUN python /app/init_db.py
RUN apk del .build-deps RUN apk del .build-deps
@@ -33,9 +33,13 @@ RUN adduser -D myuser
WORKDIR /app WORKDIR /app
COPY --from=builder /app /app COPY --from=builder /app /app
COPY --from=builder /usr/local /usr/local COPY --from=builder /usr/local /usr/local
COPY --from=builder /usr/bin /usr/bin
RUN chown -R myuser:myuser /app
# Switch to the created user # Switch to the created user
USER myuser USER myuser
RUN python3 init_db.py
# Start gunicorn with the appropriate options # Start gunicorn with the appropriate options
CMD ["gunicorn", "-b", "0.0.0.0:3000", "--log-level=debug", "--workers=3", "app:create_app('default')"] CMD ["gunicorn", "-b", "0.0.0.0:3000", "--log-level=debug", "--workers=3", "app:create_app('default')"]

16
app.py
View File

@@ -8,15 +8,19 @@ import os
def create_app(config_name): def create_app(config_name):
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/webpush.db'
db.init_app(app) db.init_app(app)
def generate_and_save_vapid_keys(): def generate_and_save_vapid_keys():
vapid = Vapid() vapid = Vapid()
vapid.generate_keys() try:
private_key = vapid.get_private_key().to_pem().decode('utf-8').strip() vapid.generate_keys()
public_key = vapid.get_public_key().to_pem().decode('utf-8').strip() private_key = vapid.private_pem().decode('utf-8').strip()
public_key = vapid.public_pem().decode('utf-8').strip()
except Exception as e:
print(f"Error generating VAPID keys: {e}")
key = VAPIDKey(public_key=public_key, private_key=private_key) key = VAPIDKey(public_key=public_key, private_key=private_key)
db.session.add(key) db.session.add(key)
@@ -92,4 +96,8 @@ def create_app(config_name):
else: else:
return jsonify(success=False, error="Subscription not found"), 404 return jsonify(success=False, error="Subscription not found"), 404
with app.app_context():
initialize()
return app return app

View File

@@ -2,7 +2,7 @@ from models import db
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/webpush.db'
db.init_app(app) db.init_app(app)
with app.app_context(): with app.app_context():

View File

@@ -14,3 +14,5 @@ class Subscription(db.Model):
p256dh = db.Column(db.String(255), nullable=False) p256dh = db.Column(db.String(255), nullable=False)
auth = db.Column(db.String(255), nullable=False) auth = db.Column(db.String(255), nullable=False)
vapid_key_id = db.Column(db.Integer, db.ForeignKey('vapid_key.id'), nullable=False) vapid_key_id = db.Column(db.Integer, db.ForeignKey('vapid_key.id'), nullable=False)