From d62880f4765c183e345cb47a5031f70867d3f31c Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Wed, 11 Jul 2018 11:27:53 +0100
Subject: [PATCH] Adds validation for database name and retention policy in the
 Graph API

---
 src/service/clmcservice/graphapi/tests.py | 5 ++++-
 src/service/clmcservice/graphapi/views.py | 8 ++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/service/clmcservice/graphapi/tests.py b/src/service/clmcservice/graphapi/tests.py
index 566626e..ce56154 100644
--- a/src/service/clmcservice/graphapi/tests.py
+++ b/src/service/clmcservice/graphapi/tests.py
@@ -69,7 +69,10 @@ class TestGraphAPI(object):
          None, "not a timestamp", "A bad request error must have been raised in case of invalid URL parameters."),
         ('{"database": "TestInfluxDB", "retention_policy": "autogen", "service_function_chain_instance": "sfc_1", "service_functions": {"nginx": {"measurement_name": "nginx", "response_time_field": "mean(avg_processing_time)"}, "minio": {"measurement_name": "minio_http", "response_time_field": "mean(total_processing_time)/mean(total_requests_count)"}, "apache": {"measurement_name": "apache", "response_time_field": "mean(avg_processing_time)"}}}',
          2131212, None, "A bad request error must have been raised in case of invalid URL parameters."),
-
+        ('{"database": "DB-not-exists", "retention_policy": "autogen", "service_function_chain_instance": "sfc_1", "service_functions": {"nginx": {"measurement_name": "nginx", "response_time_field": "mean(avg_processing_time)"}, "minio": {"measurement_name": "minio_http", "response_time_field": "mean(total_processing_time)/mean(total_requests_count)"}, "apache": {"measurement_name": "apache", "response_time_field": "mean(avg_processing_time)"}}}',
+         2131212, 2131212, "A bad request error must have been raised in case of a non-existing database."),
+        ('{"database": "TestInfluxDB", "retention_policy-invalid": "autogen", "service_function_chain_instance": "sfc_1", "service_functions": {"nginx": {"measurement_name": "nginx", "response_time_field": "mean(avg_processing_time)"}, "minio": {"measurement_name": "minio_http", "response_time_field": "mean(total_processing_time)/mean(total_requests_count)"}, "apache": {"measurement_name": "apache", "response_time_field": "mean(avg_processing_time)"}}}',
+         2131212, 2131212, "A bad request error must have been raised in case of a non-existing retention policy."),
     ])
     def test_build_error_handling(self, body, from_timestamp, to_timestamp, error_msg):
         """
diff --git a/src/service/clmcservice/graphapi/views.py b/src/service/clmcservice/graphapi/views.py
index 9d7632e..62c312e 100644
--- a/src/service/clmcservice/graphapi/views.py
+++ b/src/service/clmcservice/graphapi/views.py
@@ -74,6 +74,14 @@ class GraphAPI(object):
         graph = Graph(host=self.request.registry.settings['neo4j_host'], password=self.request.registry.settings['neo4j_password'])
         influx_client = InfluxDBClient(host=self.request.registry.settings['influx_host'], port=self.request.registry.settings['influx_port'], timeout=10)
 
+        database_name = json_queries["database"]
+        if database_name not in [db["name"] for db in influx_client.get_list_database()]:
+            raise HTTPBadRequest("Database {0} not found.".format(database_name))
+
+        retention_policy = json_queries["retention_policy"]
+        if retention_policy not in [rp["name"] for rp in influx_client.get_list_retention_policies(database_name)]:
+            raise HTTPBadRequest("Retention policy {0} for database {1} not found.".format(retention_policy, database_name))
+
         from_timestamp = params['from'] * 10**9
         to_timestamp = params['to'] * 10**9
 
-- 
GitLab