blob: a715f59f4cb07d5d66376a233a6bea5593071027 [file] [log] [blame]
maximesson98f822c2019-12-11 16:53:33 +01001import connexion
2import fileinput
3import json
4import sys
5
6from flask import Flask, escape, request, make_response
maximesson0aeab562020-01-27 18:05:04 +01007from jsonschema import validate
8from var_declaration import policy_instances, policy_types, policy_status, policy_type_per_instance
maximesson98f822c2019-12-11 16:53:33 +01009
10app = connexion.App(__name__, specification_dir='.')
11
12@app.route('/policytypes/<string:policyTypeId>', methods=['PUT','DELETE'])
13def policy_type(policyTypeId):
14 if request.method == 'PUT':
15 data = request.data.decode("utf-8")
16 data = data.replace("'", "\"")
17 data = json.loads(data)
18 policy_types[policyTypeId] = data
19 return ('The policy type was either created or updated for policy type id: ' + policyTypeId)
20 elif request.method == 'DELETE':
21 if policyTypeId in policy_types.keys():
22 policy_types.pop(policyTypeId)
23 return make_response("policy type successfully deleted for policy type id: " + policyTypeId, 200)
24 else:
25 return make_response("No policy type defined for the specified id", 404)
26
27@app.route('/', methods=['GET'])
28def test():
maximesson0aeab562020-01-27 18:05:04 +010029 return("Everything is fine", 200)
maximesson98f822c2019-12-11 16:53:33 +010030
31@app.route('/deleteinstances', methods=['DELETE'])
32def delete_instances():
33 global policy_instances
34 global policy_status
maximesson0aeab562020-01-27 18:05:04 +010035 global policy_type_per_instance
maximesson98f822c2019-12-11 16:53:33 +010036 policy_instances.clear()
37 policy_status.clear()
maximesson0aeab562020-01-27 18:05:04 +010038 policy_type_per_instance.clear()
maximesson98f822c2019-12-11 16:53:33 +010039 return("All policy instances deleted", 200)
40
41@app.route('/deletetypes', methods=['DELETE'])
42def delete_types():
43 global policy_types
44 policy_types.clear()
45 return("All policy types deleted", 200)
46
47@app.route('/<string:policyId>/<string:enforceStatus>', methods=['PUT'])
48def set_status(policyId, enforceStatus):
49 if policyId in policy_instances.keys():
maximesson0aeab562020-01-27 18:05:04 +010050 if policy_type_per_instance[policyId] == "UNDEFINED":
maximesson98f822c2019-12-11 16:53:33 +010051 ps = {}
52 ps["policyId"] = policyId
53 ps["enforceStatus"] = enforceStatus
maximesson98f822c2019-12-11 16:53:33 +010054 else:
maximesson0aeab562020-01-27 18:05:04 +010055 policy_type_id = policy_type_per_instance[policyId]
56 status_schema = policy_types[policy_type_id]["statusSchema"]
57 ps = {}
58 ps["policyId"] = policyId
59 ps["enforceStatus"] = enforceStatus
60 try:
61 validate(instance=ps, schema=status_schema)
62 except:
63 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
64 policy_status.pop(policyId)
65 policy_status[policyId] = ps
66 return("Status updated for policy: " + policyId, 200)
maximesson98f822c2019-12-11 16:53:33 +010067
68@app.route('/<string:policyId>/<string:enforceStatus>/<string:enforceReason>', methods=['PUT'])
69def set_status_with_reason(policyId, enforceStatus, enforceReason):
70 if policyId in policy_instances.keys():
maximesson0aeab562020-01-27 18:05:04 +010071 if policy_type_per_instance[policyId] == "UNDEFINED":
72 ps = {}
73 ps["policyId"] = policyId
74 ps["enforceStatus"] = enforceStatus
75 ps["enforceReason"] = enforceReason
maximesson98f822c2019-12-11 16:53:33 +010076 else:
maximesson0aeab562020-01-27 18:05:04 +010077 policy_type_id = policy_type_per_instance[policyId]
78 status_schema = policy_types[policy_type_id]["statusSchema"]
79 ps = {}
80 ps["policyId"] = policyId
81 ps["enforceStatus"] = enforceStatus
82 ps["enforceReason"] = enforceReason
83 try:
84 validate(instance=ps, schema=status_schema)
85 except:
86 return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None))
87 policy_status.pop(policyId)
88 policy_status[policyId] = ps
89 return("Status updated for policy: " + policyId, 200)
maximesson98f822c2019-12-11 16:53:33 +010090
BjornMagnussonXA6f1dc482020-03-10 16:32:08 +010091#Metrics function
92
93@app.route('/counter/<string:countername>', methods=['GET'])
94def getCounter(countername):
95 if (countername == "num_instances"):
96 return str(len(policy_instances)),200
97 elif (countername == "num_types"):
98 return str(len(policy_types)),200
99 else:
100 return "Counter name: "+countername+" not found.",404
101
102
maximesson98f822c2019-12-11 16:53:33 +0100103port_number = 8085
104if len(sys.argv) >= 2:
105 if isinstance(sys.argv[1], int):
106 port_number = sys.argv[1]
107
108app.add_api('a1-openapi.yaml')
109app.run(port=port_number)
110