Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showcase - compliance #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/deepDiveDemo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Use an official Python runtime as the base image
FROM python:3.9-slim-buster

# Set the working directory in the container
WORKDIR /deepDiveDemo

# Copy the current directory contents into the container
COPY . .

# Set environment variable for Python to enable absolute imports
ENV PYTHONPATH=/deepDiveDemo

# Install required packages using pip
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Expose port 5000 for Flask
EXPOSE 5000

# Define environment variable for Flask to run in production mode
ENV FLASK_ENV=production

# Run app.py using Python when the container launches
CMD ["python", "flask_app/app.py"]
17 changes: 17 additions & 0 deletions examples/deepDiveDemo/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'

services:
web:
build: .
ports:
- "3333:5000"
volumes:
- .:/app
depends_on:
- mongo
environment:
- EXPECTED_CLIENT_HEADER=eGovStack/GOV/90000009/digitalregistries
mongo:
image: "mongo"
ports:
- "27017:27017"
55 changes: 55 additions & 0 deletions examples/deepDiveDemo/flask_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os

from flask import Flask, render_template, jsonify, request, Response
from flask_pymongo import PyMongo

from flask_app.utils import validate_information_mediator_header, JSONEncoderCustom


app = Flask(__name__)


MONGO_URI = os.getenv("MONGO_URI", "mongodb://mongo:27017/mydatabase")
app.config["MONGO_URI"] = MONGO_URI

# Initialize MongoDB with Flask App
mongo = PyMongo(app)
db = mongo.db


@app.route('/')
def index():
return render_template('index.html')


@app.route('/data/<registryname>/<versionnumber>/create', methods=['POST'])
def create_record(registryname, versionnumber):
# Extract header value
client_header = request.headers.get('Information-Mediator-Client')
if not client_header:
return jsonify({"error": "Information-Mediator-Client header missing"}), 400

client_header_valid = validate_information_mediator_header(client_header)
if not client_header_valid['success']:
return jsonify(client_header_valid), 403

# Validate the body
data = request.json.get('write', {}).get('content')
if not data:
return jsonify({"error": "Invalid request body"}), 400
# Save the record to MongoDB
collection = db[registryname] # Using registryname as the collection name for this example
collection.insert_one(data)

data.pop('_id')

response = Response(
JSONEncoderCustom().encode({'content': data}),
mimetype="application/json"
)
response.headers['Content-Type'] = 'application/json; charset=utf-8' # setting the content-type header explicitly
return response


if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
1 change: 1 addition & 0 deletions examples/deepDiveDemo/flask_app/static/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
1 change: 1 addition & 0 deletions examples/deepDiveDemo/flask_app/static/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Add your JS code here
14 changes: 14 additions & 0 deletions examples/deepDiveDemo/flask_app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deep Dive Example Aplication</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
<h1>Welcome to the Demo Application!</h1>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions examples/deepDiveDemo/flask_app/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .informationMediatorAuth import validate_information_mediator_header
from .jsonEncoder import JSONEncoderCustom
10 changes: 10 additions & 0 deletions examples/deepDiveDemo/flask_app/utils/informationMediatorAuth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os


def validate_information_mediator_header(header_value):
EXPECTED_CLIENT_HEADER = os.getenv('EXPECTED_CLIENT_HEADER', None)

if not EXPECTED_CLIENT_HEADER or (header_value != EXPECTED_CLIENT_HEADER):
return {'success': False, "error": "Unauthorized"}

return {'success': True}
8 changes: 8 additions & 0 deletions examples/deepDiveDemo/flask_app/utils/jsonEncoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from bson import ObjectId
from json import JSONEncoder

class JSONEncoderCustom(JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return super().default(o)
3 changes: 3 additions & 0 deletions examples/deepDiveDemo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==2.0.1
pymongo
Flask-PyMongo