Skip to content

Commit 89e66dd

Browse files
feat: Add several demos (#10)
1 parent e4dcca1 commit 89e66dd

File tree

101 files changed

+10370
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+10370
-0
lines changed

NGINX-API-Steering/README.md

+954
Large diffs are not rendered by default.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
COPY requirements.txt .
5+
RUN pip install --no-cache-dir -r requirements.txt
6+
COPY apiserver.py .
7+
8+
EXPOSE 5000
9+
CMD ["python", "apiserver.py"]
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/python3
2+
3+
from flask import Flask, request, jsonify
4+
from datetime import datetime
5+
import socket
6+
7+
app = Flask(__name__)
8+
9+
# curl -ks -X GET https://127.0.0.1:5000/echo_data | jq
10+
@app.route("/get_data", methods=["GET"])
11+
def get_data():
12+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
13+
hostname = socket.gethostname()
14+
15+
data = {
16+
"timestamp": timestamp,
17+
"hostname": hostname
18+
}
19+
20+
return jsonify(data)
21+
22+
# curl -ks -X POST https://127.0.0.1:5000/echo_data -d '{"var":123}' -H "Content-Type: application/json"
23+
@app.route("/echo_data", methods=["POST"])
24+
def echo_data():
25+
payload = request.get_json() if request.get_json() != None else ''
26+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
27+
hostname = socket.gethostname()
28+
29+
return jsonify({"payload": payload, "hostname": hostname, "timestamp": timestamp})
30+
31+
if __name__ == "__main__":
32+
app.run(ssl_context="adhoc",host="0.0.0.0", port=5000)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask
2+
cryptography

NGINX-API-Steering/backend/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
COPY requirements.txt .
5+
RUN pip install --no-cache-dir -r requirements.txt
6+
COPY backend.py .
7+
8+
EXPOSE 5000
9+
CMD ["python", "backend.py"]

NGINX-API-Steering/backend/backend.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
from flask import Flask, jsonify, abort, make_response, request
5+
6+
app = Flask(__name__)
7+
8+
with open('db.json') as db:
9+
rules = json.load(db)
10+
11+
@app.route('/backend/fetchkey/<path:uri>', methods=['GET'])
12+
def get_key(uri):
13+
rule = [rule for rule in rules if rule['uri'] == uri]
14+
if len(rule) == 0:
15+
abort(404)
16+
return jsonify({'rule': rule[0]})
17+
18+
@app.route('/backend/fetchallkeys', methods=['GET'])
19+
def get_all_keys():
20+
return jsonify({'rules': rules})
21+
22+
@app.route('/jwks.json', methods=['GET'])
23+
def get_jwks():
24+
return jsonify({"keys": [{ "k":"ZmFudGFzdGljand0", "kty":"oct", "kid":"0001" }]})
25+
26+
@app.errorhandler(404)
27+
def not_found(error):
28+
return make_response(jsonify({'error': 'Not found'}), 404)
29+
30+
if __name__ == '__main__':
31+
app.run(host='0.0.0.0')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[
2+
{
3+
"id": 1,
4+
"enabled": true,
5+
"uri": "v1.0/api_get",
6+
"matchRules": {
7+
"method": "GET",
8+
"roles": "guest"
9+
},
10+
"operation": {
11+
"url": "https://api-server-1:5000/get_data"
12+
}
13+
},
14+
{
15+
"id": 2,
16+
"enabled": true,
17+
"uri": "v1.0/api_post",
18+
"matchRules": {
19+
"method": "POST",
20+
"roles": "devops"
21+
},
22+
"operation": {
23+
"url": "https://api-server-2:5000/echo_data"
24+
},
25+
"json": {
26+
"to_server": {
27+
"set": [
28+
{
29+
"field1": "value1"
30+
},
31+
{
32+
"field2": "value2"
33+
}
34+
],
35+
"del": [
36+
"group"
37+
]
38+
},
39+
"to_client": {
40+
"set": [
41+
{
42+
"new_response_field": "ADDED"
43+
}
44+
],
45+
"del": [
46+
"hostname"
47+
]
48+
}
49+
}
50+
},
51+
{
52+
"id": 3,
53+
"enabled": true,
54+
"uri": "v1.0/api_post_no_change",
55+
"matchRules": {
56+
"method": "POST",
57+
"roles": "devops"
58+
},
59+
"operation": {
60+
"url": "https://api-server-2:5000/echo_data"
61+
}
62+
},
63+
{
64+
"id": 4,
65+
"enabled": true,
66+
"uri": "v1.0/template_test",
67+
"matchRules": {
68+
"method": "POST",
69+
"roles": "guest"
70+
},
71+
"operation": {
72+
"url": "https://api-server-2:5000/echo_data"
73+
},
74+
"template": {
75+
"name": "",
76+
"age": 0,
77+
"address": {
78+
"street": "",
79+
"city": ""
80+
}
81+
}
82+
}
83+
]
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[
2+
{
3+
"id": 1,
4+
"enabled": true,
5+
"uri": "v1.0/api_get",
6+
"matchRules": {
7+
"method": "GET",
8+
"roles": "guest"
9+
},
10+
"operation": {
11+
"url": "https://api-server-1.nginx-api-steering.svc.cluster.local:5000/get_data"
12+
}
13+
},
14+
{
15+
"id": 2,
16+
"enabled": true,
17+
"uri": "v1.0/api_post",
18+
"matchRules": {
19+
"method": "POST",
20+
"roles": "devops"
21+
},
22+
"operation": {
23+
"url": "https://api-server-2.nginx-api-steering.svc.cluster.local:5000/echo_data"
24+
},
25+
"json": {
26+
"to_server": {
27+
"set": [
28+
{
29+
"field1": "value1"
30+
},
31+
{
32+
"field2": "value2"
33+
}
34+
],
35+
"del": [
36+
"group"
37+
]
38+
},
39+
"to_client": {
40+
"set": [
41+
{
42+
"new_response_field": "ADDED"
43+
}
44+
],
45+
"del": [
46+
"hostname"
47+
]
48+
}
49+
}
50+
},
51+
{
52+
"id": 3,
53+
"enabled": true,
54+
"uri": "v1.0/api_post_no_change",
55+
"matchRules": {
56+
"method": "POST",
57+
"roles": "devops"
58+
},
59+
"operation": {
60+
"url": "https://api-server-2.nginx-api-steering.svc.cluster.local:5000/echo_data"
61+
}
62+
},
63+
{
64+
"id": 4,
65+
"enabled": true,
66+
"uri": "v1.0/template_test",
67+
"matchRules": {
68+
"method": "POST",
69+
"roles": "guest"
70+
},
71+
"operation": {
72+
"url": "https://api-server-2.nginx-api-steering.svc.cluster.local:5000/echo_data"
73+
},
74+
"template": {
75+
"name": "",
76+
"age": 0,
77+
"address": {
78+
"street": "",
79+
"city": ""
80+
}
81+
}
82+
}
83+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
version: "3.9"
2+
3+
services:
4+
backend:
5+
container_name: backend
6+
image: backend
7+
build:
8+
context: ./backend
9+
dockerfile: Dockerfile
10+
ports:
11+
- 10000:5000
12+
networks:
13+
lab-network:
14+
ipv4_address: 10.5.0.10
15+
volumes:
16+
- ./backend/db-dockercompose.json:/app/db.json:ro
17+
18+
api-server-1:
19+
container_name: api-server-1
20+
image: api-server
21+
build:
22+
context: ./apiserver
23+
dockerfile: Dockerfile
24+
ports:
25+
- 5001:5000
26+
networks:
27+
lab-network:
28+
ipv4_address: 10.5.0.11
29+
30+
api-server-2:
31+
container_name: api-server-2
32+
image: api-server
33+
build:
34+
context: ./apiserver
35+
dockerfile: Dockerfile
36+
ports:
37+
- 5002:5000
38+
networks:
39+
lab-network:
40+
ipv4_address: 10.5.0.12
41+
42+
nginx:
43+
container_name: nginx
44+
image: nginx-api-steering
45+
build:
46+
context: ./nginx
47+
dockerfile: Dockerfile
48+
secrets:
49+
- nginx-crt
50+
- nginx-key
51+
ports:
52+
# Clients access to published REST API
53+
- 10080:80
54+
# Admin access to NGINX Plus API and Dashboard
55+
- 20080:8080
56+
networks:
57+
lab-network:
58+
ipv4_address: 10.5.0.20
59+
volumes:
60+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
61+
- ./nginx/api.conf:/etc/nginx/conf.d/api.conf:ro
62+
- ./nginx/steering.js:/etc/nginx/conf.d/steering.js:ro
63+
- ./nginx/steering.conf-dockercompose:/etc/nginx/conf.d/steering.conf:ro
64+
- /dev/null:/etc/nginx/conf.d/default.conf:ro
65+
66+
secrets:
67+
nginx-crt:
68+
name: nginx-crt
69+
file: ${NGINX_CERT}
70+
nginx-key:
71+
name: nginx-key
72+
file: ${NGINX_KEY}
73+
74+
networks:
75+
lab-network:
76+
driver: bridge
77+
ipam:
78+
config:
79+
- subnet: 10.5.0.0/24
80+
gateway: 10.5.0.1

NGINX-API-Steering/jwt/jwks.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"keys": [
3+
{
4+
"k":"ZmFudGFzdGljand0",
5+
"kty":"oct",
6+
"kid":"0001"
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)