ACL insert fixes; and eval removed
@@ -91,43 +91,51 @@ async def add_acl(request: web.Request) -> web.Response:
""" Create a new access control list
:Example:
- curl -H "authorization: $AUTH_TOKEN" -sX POST http://localhost:8081/fledge/ACL -d '{"name": "testACL", "service": [{"name": "IEC-104"}, {"type": "notification"}], "url": [{"URL": "/fledge/south/operation"}]}'
+ curl -H "authorization: $AUTH_TOKEN" -sX POST http://localhost:8081/fledge/ACL
+ -d '{"name": "testACL", "service": [{"name": "IEC-104"}, {"type": "notification"}], "url": [ {"URL": "/fledge/south/operation", "ACL": [{"type": "Northbound"}]} ]}'
"""
try:
data = await request.json()
name = data.get('name', None)
service = data.get('service', None)
url = data.get('url', None)
+
if name is None:
- raise ValueError('name param is required')
- if name is not None:
+ raise ValueError('ACL name is required')
+ else:
if not isinstance(name, str):
- raise TypeError('name must be a string')
+ raise TypeError('ACL name must be a string')
name = name.strip()
if name == "":
- raise ValueError('name cannot be empty')
+ raise ValueError('ACL name cannot be empty')
+
if service is None:
raise ValueError('service param is required')
if not isinstance(service, list):
raise TypeError('service must be in list')
+ # check each item in list is an object of, either 'type'|<service type> or 'name'|<service name> value pair
if url is None:
raise ValueError('url param is required')
if not isinstance(url, list):
- raise TypeError('url must be in list')
+ raise TypeError('url must be a list')
+ # check URLs list has objects with URL and a list of ACL where each acl item here is an object of 'type'|<service type> value pair
+
result = {}
storage = connect.get_storage_async()
payload = PayloadBuilder().SELECT("name").WHERE(['name', '=', name]).payload()
get_control_acl_name_result = await storage.query_tbl_with_payload('control_acl', payload)
if get_control_acl_name_result['count'] == 0:
- payload = PayloadBuilder().INSERT(name=name, service=str(service), url=str(url)).payload()
+ services = json.dumps(service)
+ urls = json.dumps(url)
+ payload = PayloadBuilder().INSERT(name=name, service=services, url=urls).payload()
insert_control_acl_result = await storage.insert_into_tbl("control_acl", payload)
if 'response' in insert_control_acl_result:
if insert_control_acl_result['response'] == "inserted":
- result = {"name": name, "service": eval(str(service)), "url": eval(str(url))}
+ result = {"name": name, "service": json.loads(services), "url": json.loads(urls)}
else:
raise StorageServerError(insert_control_acl_result)
else:
- msg = '{} name already exists.'.format(name)
+ msg = 'ACL with name {} already exists.'.format(name)
raise DuplicateNameError(msg)
except StorageServerError as err:
msg = "Storage error: {}".format(str(err))
