Compare commits

...

7 Commits

Author SHA1 Message Date
051fc1ffdd Just updating .gitignore
Changes to be committed:
	modified:   .gitignore
2023-09-21 11:18:45 +03:30
695c4d7b84 Fixed authentication issues
added env vars instead of importing python modules for configuration
 Changes to be committed:
	modified:   server/server_api.py
2023-09-21 11:17:17 +03:30
0263fffb3c new files in new locations
Changes to be committed:
	new file:   Dockerfile
	new file:   scenesense-docker/docker-compose.yml
2023-09-21 11:16:26 +03:30
18ec901518 DB Init script added
Changes to be committed:
	new file:   scenesense-docker/docker-entrypoint-initdb.d/scenesense_db_init.sql
2023-09-21 11:15:40 +03:30
d54a2be28f changed their locations
Changes to be committed:
	deleted:    docker/Dockerfile
	deleted:    docker/docker-compose.yml
2023-09-21 11:14:45 +03:30
bd9ac2f3bf added pymysql 2023-09-21 11:14:11 +03:30
4c33dff400 Deleted server/api_keys.py server/config.py and replaced with ENV VARS 2023-09-21 11:13:52 +03:30
9 changed files with 73 additions and 45 deletions

4
.gitignore vendored
View File

@@ -1 +1,3 @@
docker/.env
scenesense-docker/.env
scenesense-docker/db/
.dockerignore

View File

@@ -1,15 +1,17 @@
# Use the official Python image with Python 3.10
FROM python:3.10
FROM python:3.10-slim-bookworm
# Set the working directory to /app
WORKDIR /app
# Copy the server files and requirements.txt into the container at /app
COPY ../server/ ../requirements.txt /app/
# Copy requirements.txt into the container at /app
COPY requirements.txt /app/
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Copy server file
COPY server/server_api.py /app/
# Expose port 5000 for the Flask app to listen on
EXPOSE 5000

View File

@@ -1,23 +0,0 @@
version: '3'
services:
db:
image: mariadb:latest
container_name: mariadb-container
env_file:
- .env
ports:
- "3306:3306" # Map MariaDB port to host if needed
volumes:
- ./db:/var/lib/mysql # Mount a volume for MariaDB data
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
env_file:
- .env
depends_on:
- db

View File

@@ -1,2 +1,3 @@
flask
flask_sqlalchemy
pymysql

View File

@@ -0,0 +1,27 @@
version: '3'
services:
db:
image: mariadb:11.0
container_name: scenesense-db
env_file:
- .env
volumes:
- ./db:/var/lib/mysql
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
app:
image: scenesense-server:1.0
container_name: scenesense-app
ports:
- "15000:5000"
env_file:
- .env
depends_on:
- db
adminer:
image: adminer:latest
container_name: scenesense-db-adminer
restart: always
ports:
- "18000:8080"

View File

@@ -0,0 +1,5 @@
CREATE TABLE records (
id INT AUTO_INCREMENT PRIMARY KEY,
timestamp VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL
);

View File

@@ -1,7 +0,0 @@
# api_keys.py
api_keys = {
"your_api_key": "user1",
"another_api_key": "user2",
# Add more API keys as needed
}

View File

@@ -1,5 +0,0 @@
import os
class Config:
# Use the environment variable for the database URI
SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI")

View File

@@ -1,17 +1,23 @@
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from api_keys import api_keys
import config
import datetime
import os
import json
# Flask app instance
app = Flask(__name__)
# Configuration for SQLAlchemy
app.config.from_object(config.Config)
app.config[
'SQLALCHEMY_DATABASE_URI'
] = os.environ.get(
"SQLALCHEMY_DATABASE_URI"
)
db = SQLAlchemy(app)
# Database Model for Records
class Record(db.Model):
__tablename__ = 'records'
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
@@ -23,15 +29,35 @@ def parse_timestamp(timestamp_str):
except ValueError:
return None
# Function to validate API keys based on their values and access level
def validate_api_key(api_key, required_access_level):
api_keys_json = os.environ.get('API_KEYS_JSON', '{}')
api_keys_dict = json.loads(api_keys_json)
if api_key in api_keys_dict:
access_level = api_keys_dict[api_key].get("access_level")
return access_level == required_access_level
return False
# API key authentication
@app.before_request
def authenticate():
# Check if the API key provided in the request headers is valid
api_key = request.headers.get("Authorization")
# Allow all requests with RW keys
if validate_api_key(api_key, "RW"):
return None # Allow the request to proceed
# Allow GET requests with RO keys
if request.method == "GET" and validate_api_key(api_key, "RO"):
return None # Allow the request to proceed
# Return a 401 Unauthorized response for invalid API keys or access levels
return jsonify({'error': 'Invalid API key or access level'}), 401
if api_key not in api_keys:
# Return a 401 Unauthorized response for invalid API keys
return jsonify({'error': 'Invalid API key'}), 401
# Route to add a new record
@app.route(