Skip to content

Commit 2e64d99

Browse files
authored
Merge pull request #34 from gematik/add-basic-facade-poc
Add a basic facade PoC
2 parents 78e22d1 + 433e8e5 commit 2e64d99

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

facade-poc/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Motivation
2+
This set up shows a simple facade implementation with re-authentication.
3+
4+
## Components
5+
* server
6+
* nginx
7+
* client call
8+
9+
# Requirements
10+
* python (tested with version 3.12.2)
11+
* nginx (tested with version 1.25.4)
12+
13+
# Server
14+
1) Create a Virtual Environment for Python
15+
16+
`python3 -m venv venv`
17+
18+
2) Install Flask
19+
20+
`pip3 install flask`
21+
22+
3) Activate the virtual environment for python
23+
24+
`source venv/bin/activate`
25+
26+
4) Create Private Key
27+
28+
`openssl genrsa -aes256 -out server.key 2048`
29+
30+
5) Create Certificate Signing Request
31+
32+
`openssl req -new -key server.key -out server.csr`
33+
34+
6) Create the Certificate
35+
36+
`openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt`
37+
38+
7) Start the server
39+
40+
`python server.py`
41+
42+
8) Test the server
43+
44+
`curl https://127.0.0.1:5000/unsecured --insecure`
45+
46+
9) Test passing an API Key
47+
48+
`curl https://localhost:5000/secured -H "X-API-Key:TEST-API-KEY" --insecure`
49+
50+
# NGINX
51+
Apply the right configuration, found in this repository in `nginx.conf`
52+
On MacOS it is located in `/usr/local/etc/nginx`
53+
54+
## Create Certificates and Adjust NGINX Configuration
55+
56+
1) Create Private Key
57+
58+
`openssl genrsa -aes256 -out server.key 2048`
59+
60+
2) Create Certificate Signing Request
61+
62+
`openssl req -new -key nginx.key -out nginx.csr`
63+
64+
3) Create the Certificate
65+
66+
`openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt`
67+
68+
4) Adjust paths in nginx.conf
69+
70+
71+
5) Start nginx
72+
73+
`nginx`
74+
75+
# Start Testing
76+
77+
Test for forbidden on unsecured endpoint
78+
79+
`curl https://localhost/unsecured --insecure`
80+
81+
Test for successful request on secured endpoint with enriched credentials
82+
83+
`curl https://localhost/secured --insecure`
84+
85+
# Troubleshooting
86+
87+
Beware of the nginx state. Sometimes, a reload via `nginx -s reload` is not enough. If it behaves not as expected, try `nginx -s quit` and restart using `nginx`.

facade-poc/nginx.conf

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#user nobody;
3+
worker_processes 1;
4+
5+
error_log /var/log/nginx/error.log;
6+
error_log /var/log/nginx/error.log notice;
7+
error_log /var/log/nginx/error.log info;
8+
9+
#pid logs/nginx.pid;
10+
11+
events {
12+
worker_connections 1024;
13+
}
14+
15+
16+
http {
17+
include mime.types;
18+
default_type application/octet-stream;
19+
20+
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
21+
# '$status $body_bytes_sent "$http_referer" '
22+
# '"$http_user_agent" "$http_x_forwarded_for"';
23+
24+
#access_log logs/access.log main;
25+
26+
sendfile on;
27+
#tcp_nopush on;
28+
29+
#keepalive_timeout 0;
30+
keepalive_timeout 65;
31+
32+
#gzip on;
33+
34+
# HTTPS server
35+
36+
server {
37+
listen 443 ssl;
38+
server_name localhost;
39+
ssl_certificate "<ABSOLUTE-PATH-TO-YOUR-PROJECT>/certificates/nginx.crt";
40+
ssl_certificate_key "<ABSOLUTE-PATH-TO-YOUR-PROJECT>/certificates/nginx.key";
41+
ssl_session_cache shared:SSL:1m;
42+
ssl_session_timeout 5m;
43+
ssl_ciphers HIGH:!aNULL:!MD5;
44+
ssl_prefer_server_ciphers on;
45+
46+
47+
location /secured {
48+
# Hardcoded API key
49+
set $api_key "TEST-API-KEY";
50+
51+
# Add the API key as a custom HTTP header
52+
proxy_set_header X-API-Key $api_key;
53+
54+
proxy_pass https://127.0.0.1:5000;
55+
proxy_set_header Host $host;
56+
proxy_set_header X-Real-IP $remote_addr;
57+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
58+
proxy_set_header X-Forwarded-Proto $scheme;
59+
60+
proxy_ssl_verify off;
61+
}
62+
63+
# Allow access to other locations
64+
location / {
65+
# Define your access permissions here
66+
deny all;
67+
# Additional configurations for other locations if needed
68+
}
69+
70+
71+
}
72+
include servers/*;
73+
}

facade-poc/server.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask, jsonify, request
2+
import ssl
3+
4+
app = Flask(__name__)
5+
6+
# Hardcoded API key
7+
API_KEY = "TEST-API-KEY"
8+
HEADER_NAME = "X-API-Key"
9+
10+
# Unsecured endpoint
11+
@app.route('/unsecured', methods=['GET'])
12+
def unsecured():
13+
data = {'message': 'This is an unsecured endpoint.'}
14+
return jsonify(data)
15+
16+
# Secured endpoint
17+
@app.route('/secured', methods=['GET'])
18+
def endpoint2():
19+
# Check if API key is provided in the request headers
20+
provided_api_key = request.headers.get(HEADER_NAME)
21+
22+
# API key is invalid, return send unauthorized
23+
if provided_api_key != API_KEY:
24+
return jsonify({'error': 'Unauthorized access. Invalid API key.'}), 401
25+
26+
27+
data = {'message': 'This is a secured endpoint.'}
28+
return jsonify(data)
29+
30+
if __name__ == '__main__':
31+
# Generate SSL context
32+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
33+
context.load_cert_chain('certificates/server.crt', 'certificates/server.key') # Provide paths to your certificate and private key files
34+
35+
# Run Flask app with TLS/SSL enabled
36+
app.run(debug=True, ssl_context=context)

0 commit comments

Comments
 (0)