From 7633c3b1845624fe2d1d0e8c33fa550676987198 Mon Sep 17 00:00:00 2001 From: jqxin2006 Date: Tue, 5 Mar 2013 22:24:53 -0600 Subject: [PATCH 1/8] add test script for API --- barbican_api_test.py | 115 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 barbican_api_test.py diff --git a/barbican_api_test.py b/barbican_api_test.py new file mode 100644 index 000000000..a378f5fb2 --- /dev/null +++ b/barbican_api_test.py @@ -0,0 +1,115 @@ +# -*- coding: utf-8 -*- +""" + Barbican API testing + ~~~~~~~~~~~~ + + The testing for Barbican API. + + DO NOT USE THIS IN PRODUCTION. IT IS NOT SECURE IN ANY WAY. + YOU HAVE BEEN WARNED. + + :copyright: (c) 2013 by Jarret Raim + :license: Apache 2.0, see LICENSE for details +""" + +import uuid +import datetime +import requests +import json +from models import Event, Tenant, Key, Agent, Policy + + +class BarbicanAPITesting: + def __init__(self, url="http://127.0.0.1:5000/api/"): + self.url = url + + def add_agent(self, tenant_id): + + new_uuid = uuid.uuid4() + self.agent_id = new_uuid + pay_load = {"uuid": str(new_uuid)} + the_url = self.url + str(tenant_id) + "/agents/" + headers = {'content-type': 'application/json'} + response = requests.post(the_url,data=json.dumps(pay_load), headers=headers) + try: + the_uuid = response.json["uuid"] + except Exception as e: + print e + return the_uuid + + def add_policy(self, policy_name, tenant_id): + policy_id = str(uuid.uuid4()) + key_id = str(uuid.uuid4()) + self.key_id = key_id + payload = ''' + { + "policies": [ + { + "uuid": "%s", + "name": "%s", + "directory_name": "my-app-key", + "max_key_accesses": 1, + "time_available_after_reboot": 10, + "keys": [ + { + "uuid": "%s", + "filename": "configuration_key", + "mime_type": "application/aes-256-cbc", + "expiration": "2014-02-28T19:14:44.180394", + "secret": "b7990b786ee9659b43e6b1cd6136de07d9c5aa06513afe5d091c04bde981b280", + "owner": "myapp", + "group": "myapp", + "cacheable": false + } + ] + } + ] +} + ''' % (policy_id, policy_name, key_id) + the_url = self.url + str(tenant_id) + "/policies/" + headers = {'content-type': 'application/json'} + response = requests.post(the_url,data=payload, headers=headers) + return response.text + + def get_policy(self, tenant_id): + the_url = self.url + str(tenant_id) + "/policies/" + response = requests.get(the_url) + return response.text + + + def get_agents(self, tenant_id): + + the_url = self.url + str(tenant_id) + "/agents/" + response = requests.get(the_url) + print response.json + + def add_log(self, tenant_id): + payload = ''' + { + "agent_id": "%s", + "received_on": "%s", + "severity": "INFO", + "key_id": "%s", + "message": "Key accessed by user 'apache'." + } + ''' % (self.agent_id, datetime.datetime.isoformat(datetime.datetime.now()), self.key_id) + the_url = self.url + str(tenant_id) + "/logs/" + headers = {'content-type': 'application/json'} + response = requests.post(the_url,data=payload, headers=headers) + return response.text + + def get_log(self, tenant_id): + the_url = self.url + str(tenant_id) + "/logs/" + response = requests.get(the_url) + return response.text + + +test1 = BarbicanAPITesting() +print test1.add_agent(123) +test1.get_agents(123) + +print test1.add_policy("Before start up", 123) +print test1.get_policy(123) + +print test1.add_log(123) +print test1.get_log(123) \ No newline at end of file From 1bf19a91f2fef8e70757b100317e1a049df09360 Mon Sep 17 00:00:00 2001 From: jqxin2006 Date: Tue, 5 Mar 2013 22:55:57 -0600 Subject: [PATCH 2/8] add tenant api --- barbican_api.py | 18 ++++++++++++++++++ barbican_api_test.py | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/barbican_api.py b/barbican_api.py index b4b308ccb..9843b34fd 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -25,6 +25,24 @@ api = Blueprint('api', __name__, url_prefix="/api") def root(): return jsonify(hello='World') +@api.route('//', methods=['GET', 'POST']) +def tenant(tenant_id): + if request.method == 'POST': + tenant = Tenant.query.filter_by(id=tenant_id).first() + if tenant is None: + tenant = Tenant(id=tenant_id) + db_session.add(tenant) + db_session.commit() + return Response("Tenant created!", status=201) + else: + return Response("Tenant already exists!", status=200) + return Response(status=201) + else: + tenant = Tenant.query.filter_by(id=tenant_id).first() + if tenant is None: + return Response("No tenant found!", status=404) + else: + return Response("Tenant found!", status=200) @api.route('//policies/', methods=['GET', 'POST']) def policies(tenant_id): diff --git a/barbican_api_test.py b/barbican_api_test.py index a378f5fb2..af5ee66cf 100644 --- a/barbican_api_test.py +++ b/barbican_api_test.py @@ -103,8 +103,22 @@ class BarbicanAPITesting: response = requests.get(the_url) return response.text + def get_tenant(self, tenant_id): + the_url = self.url + str(tenant_id) + "/" + response = requests.get(the_url) + return response.text + + def create_tenant(self, tenant_id): + the_url = self.url + str(tenant_id) + "/" + response = requests.post(the_url) + return response.text + test1 = BarbicanAPITesting() +print test1.get_tenant(123) +print test1.get_tenant(1234) +print test1.create_tenant(12345) +print test1.create_tenant(234) print test1.add_agent(123) test1.get_agents(123) From 06ad7b408b38b0631f3bc1bd5cf72454975aab7c Mon Sep 17 00:00:00 2001 From: jqxin2006 Date: Wed, 6 Mar 2013 07:14:26 -0600 Subject: [PATCH 3/8] Updated tenant API and API testing --- barbican_api.py | 9 +++++---- barbican_api_test.py | 14 +++++++------- models.py | 7 +++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/barbican_api.py b/barbican_api.py index 9843b34fd..cf0bcb9b7 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -33,16 +33,17 @@ def tenant(tenant_id): tenant = Tenant(id=tenant_id) db_session.add(tenant) db_session.commit() - return Response("Tenant created!", status=201) + return jsonify(tenant.as_dict()), 201 else: - return Response("Tenant already exists!", status=200) - return Response(status=201) + return jsonify(tenant.as_dict()) + + else: tenant = Tenant.query.filter_by(id=tenant_id).first() if tenant is None: return Response("No tenant found!", status=404) else: - return Response("Tenant found!", status=200) + return jsonify(tenant.as_dict()) @api.route('//policies/', methods=['GET', 'POST']) def policies(tenant_id): diff --git a/barbican_api_test.py b/barbican_api_test.py index af5ee66cf..b3dc6d8a2 100644 --- a/barbican_api_test.py +++ b/barbican_api_test.py @@ -117,13 +117,13 @@ class BarbicanAPITesting: test1 = BarbicanAPITesting() print test1.get_tenant(123) print test1.get_tenant(1234) -print test1.create_tenant(12345) -print test1.create_tenant(234) -print test1.add_agent(123) -test1.get_agents(123) +print test1.create_tenant(123456) +print test1.create_tenant(2349) +print test1.add_agent(1234) +test1.get_agents(1234) -print test1.add_policy("Before start up", 123) -print test1.get_policy(123) +print test1.add_policy("Before start up", 1234) +print test1.get_policy(1234) -print test1.add_log(123) +print test1.add_log(1234) print test1.get_log(123) \ No newline at end of file diff --git a/models.py b/models.py index a31bc99f1..45f5c0362 100644 --- a/models.py +++ b/models.py @@ -57,6 +57,13 @@ class Tenant(Base): def __repr__(self): return '' % self.uuid + + def as_dict(self): + json = { + 'id': self.id, + 'uuid': self.uuid + } + return json class Key(Base): From 40060ba944aa3cb6d62304dc41368ffa6e9cb4fa Mon Sep 17 00:00:00 2001 From: jqxin2006 Date: Fri, 8 Mar 2013 20:18:12 -0600 Subject: [PATCH 4/8] Update Web UI for event display --- barbican.py | 6 +- barbican_api.py | 29 +- barbican_api_test.py | 21 +- static/css/jquery.dataTables.css | 221 + static/css/jquery.dataTables_themeroller.css | 244 + static/img/Sorting icons.psd | Bin 0 -> 27490 bytes static/img/back_disabled.png | Bin 0 -> 1361 bytes static/img/back_enabled.png | Bin 0 -> 1379 bytes static/img/back_enabled_hover.png | Bin 0 -> 1375 bytes static/img/favicon.ico | Bin 0 -> 894 bytes static/img/forward_disabled.png | Bin 0 -> 1363 bytes static/img/forward_enabled.png | Bin 0 -> 1380 bytes static/img/forward_enabled_hover.png | Bin 0 -> 1379 bytes static/img/sort_asc.png | Bin 0 -> 1118 bytes static/img/sort_asc_disabled.png | Bin 0 -> 1050 bytes static/img/sort_both.png | Bin 0 -> 1136 bytes static/img/sort_desc.png | Bin 0 -> 1127 bytes static/img/sort_desc_disabled.png | Bin 0 -> 1045 bytes static/js/jquery.dataTables.js | 12099 +++++++++++++++++ static/js/jquery.dataTables.min.js | 155 + templates/events.html | 29 + templates/layout.html | 7 +- 22 files changed, 12801 insertions(+), 10 deletions(-) create mode 100644 static/css/jquery.dataTables.css create mode 100644 static/css/jquery.dataTables_themeroller.css create mode 100644 static/img/Sorting icons.psd create mode 100644 static/img/back_disabled.png create mode 100644 static/img/back_enabled.png create mode 100644 static/img/back_enabled_hover.png create mode 100644 static/img/favicon.ico create mode 100644 static/img/forward_disabled.png create mode 100644 static/img/forward_enabled.png create mode 100644 static/img/forward_enabled_hover.png create mode 100644 static/img/sort_asc.png create mode 100644 static/img/sort_asc_disabled.png create mode 100644 static/img/sort_both.png create mode 100644 static/img/sort_desc.png create mode 100644 static/img/sort_desc_disabled.png create mode 100644 static/js/jquery.dataTables.js create mode 100644 static/js/jquery.dataTables.min.js create mode 100644 templates/events.html diff --git a/barbican.py b/barbican.py index 689977cb4..16318eee0 100644 --- a/barbican.py +++ b/barbican.py @@ -44,6 +44,10 @@ login_manager.login_view = 'login' def hello(): return render_template("index.html") +@app.route("/events") +def events(): + return render_template("events.html") + # # Login forms @@ -87,4 +91,4 @@ if __name__ == '__main__': if not os.path.exists('/tmp/barbican.db'): app.logger.info('No database detected at /tmp/barbican.db. Creating one and the admin user.') init_db() - app.run(debug=True) \ No newline at end of file + app.run(debug=True) diff --git a/barbican_api.py b/barbican_api.py index cf0bcb9b7..02661cc68 100644 --- a/barbican_api.py +++ b/barbican_api.py @@ -116,10 +116,37 @@ def logs(tenant_id): events_dicts = map(Event.as_dict, events.all()) return Response(json.dumps(events_dicts, cls=DateTimeJsonEncoder), mimetype='application/json') +@api.route('/alllogs/', methods=['GET']) +def alllogs(timestamp=None): + events = Event.query.order_by(Event.received_on) + helper = Helper() + json_str = '''{ + "aaData":[ + ''' + for event in events.all(): + json_str += '''["%s","%s","%s","%s","%s","%s", "%s" + ],''' % (event.id,event.received_on, event.tenant_id, event.key_id, event.agent_id, event.severity, helper.html_escape(event.message)) + json_str = json_str[:-1] + json_str += '''] + }''' + return Response(json_str, mimetype='application/json') class DateTimeJsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime.datetime): return obj.isoformat() else: - return super(DateTimeJsonEncoder, self).default(obj) \ No newline at end of file + return super(DateTimeJsonEncoder, self).default(obj) + +class Helper: + def __init__(self): + self.html_escape_table = { + "&": "&", + '"': """, + "'": "'", + ">": ">", + "<": "<", + } + + def html_escape(self,text): + return "".join(self.html_escape_table.get(c,c) for c in text) diff --git a/barbican_api_test.py b/barbican_api_test.py index b3dc6d8a2..974193efb 100644 --- a/barbican_api_test.py +++ b/barbican_api_test.py @@ -83,16 +83,16 @@ class BarbicanAPITesting: response = requests.get(the_url) print response.json - def add_log(self, tenant_id): + def add_log(self, tenant_id=123, severity="INFO", message="Key accessed by user 'apache'." ): payload = ''' { "agent_id": "%s", "received_on": "%s", - "severity": "INFO", + "severity": "%s", "key_id": "%s", - "message": "Key accessed by user 'apache'." + "message": "%s" } - ''' % (self.agent_id, datetime.datetime.isoformat(datetime.datetime.now()), self.key_id) + ''' % (self.agent_id, datetime.datetime.isoformat(datetime.datetime.now()), severity, self.key_id, message) the_url = self.url + str(tenant_id) + "/logs/" headers = {'content-type': 'application/json'} response = requests.post(the_url,data=payload, headers=headers) @@ -103,6 +103,11 @@ class BarbicanAPITesting: response = requests.get(the_url) return response.text + def get_alllogs(self): + the_url = self.url + "alllogs/" + response = requests.get(the_url) + return response.text + def get_tenant(self, tenant_id): the_url = self.url + str(tenant_id) + "/" response = requests.get(the_url) @@ -125,5 +130,11 @@ test1.get_agents(1234) print test1.add_policy("Before start up", 1234) print test1.get_policy(1234) +for k in range(1,50): + test1.create_tenant(k) + #test1.add_log(k, severity="INFO", message="Access from agent") + print test1.add_log(k, severity="INFO", message='Error while trying to +

Event logs

+
+
+{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 2609a113d..0fb22b669 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -11,6 +11,7 @@ +