Skip to content

Commit

Permalink
fixed key errors in _process_connectivity_file (#26)
Browse files Browse the repository at this point in the history
* fixed key errors in _process_connectivity_file
* added example connectivity file for validation purposes
* Modified connectivity jsons

Signed-off-by: Aleksandar Andonov <aleksv.andonov@protonmail>
Co-authored-by: Aleksandar Andonov <aleksv.andonov@protonmail>
Co-authored-by: Ziv Nevo <nevo@il.ibm.com>
  • Loading branch information
3 people authored Dec 15, 2022
1 parent 5ab5d1b commit 9da71d8
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 631 deletions.
22 changes: 8 additions & 14 deletions src/netpol_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def _process_connectivity_file(self, connectivity_file):
if not isinstance(element, list):
continue
for connection in element:
src_deploy = self._find_or_add_deployment(connection['source']['Resource'])
used_ports_src = connection['source']['Resource'].get('UsedPorts', [])
tgt_deploy = self._find_or_add_deployment(connection['target']['Resource'])
src_deploy = self._find_or_add_deployment(connection.get('source', {}).get('resource', {}))
used_ports_src = connection.get('source', {}).get('resource', {}).get('UsedPorts', [])
tgt_deploy = self._find_or_add_deployment(connection['target']['resource'])
links = connection['link']['resource']
port_list = self._links_to_port_list(links.get('network'), used_ports_src)
if links.get('type') == 'LoadBalancer':
Expand All @@ -95,13 +95,13 @@ def _find_or_add_deployment(self, resource):
:return: An instance of DeploymentLinks for the given deployment
:rtype: DeploymentLinks
"""
name = resource['name']
name = resource.get('name')
if not name:
return DeploymentLinks('') # empty src

if name not in self.deployments:
namespace = resource.get('namespace', '')
sel = self._selector_array_to_pod_selector(resource.get('selectors', []))
sel = self._selector_array_to_pod_selector(resource.get('labels', []))
labels = resource.get('labels', {})
sa_name = resource.get('serviceaccountname', 'default')
self.deployments[name] = DeploymentLinks(name, namespace, sel, labels, sa_name)
Expand All @@ -115,20 +115,14 @@ def _allowed_by_baseline(self, source_labels, target_labels, port_list):
return None

@staticmethod
def _selector_array_to_pod_selector(sel_array):
res = {}
for sel in sel_array or []:
col_pos = sel.find(':')
key = sel[:col_pos]
val = sel[col_pos + 1:]
res[key] = val
return {'podSelector': {'matchLabels': res}}
def _selector_array_to_pod_selector(sel_map):
return {'podSelector': {'matchLabels': sel_map}}

@staticmethod
def _links_to_port_list(links, used_ports):
if used_ports:
# refer only to relevant ports (not all service ports are in use)
return [{'port': link.get('target_port')} for link in links if link.get('port') in used_ports]
return [{'port': link.get('target_port')} for link in links if link in used_ports]
return [{'port': link.get('target_port')} for link in links]

def _add_must_allow_connections(self):
Expand Down
64 changes: 32 additions & 32 deletions tests/connectivity_jsons/akmebank-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -20,7 +20,7 @@
"target": {
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-command",
"selectors": [
"app:account-command-selector"
Expand Down Expand Up @@ -64,7 +64,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -80,7 +80,7 @@
"target": {
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-command",
"selectors": [
"app:account-command-selector"
Expand Down Expand Up @@ -123,7 +123,7 @@
"source": {
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -145,7 +145,7 @@
"target": {
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -188,7 +188,7 @@
"source": {
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -211,7 +211,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -256,7 +256,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -279,7 +279,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -324,7 +324,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -347,7 +347,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -392,7 +392,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -409,7 +409,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand Down Expand Up @@ -456,7 +456,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -473,7 +473,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand Down Expand Up @@ -520,7 +520,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -543,7 +543,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -588,7 +588,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -611,7 +611,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -656,7 +656,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -679,7 +679,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -724,7 +724,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand All @@ -747,7 +747,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-query",
"selectors": [
"app:account-query-selector"
Expand Down Expand Up @@ -792,7 +792,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -809,7 +809,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-command",
"selectors": [
"app:account-command-selector"
Expand Down Expand Up @@ -854,7 +854,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -871,7 +871,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "account-command",
"selectors": [
"app:account-command-selector"
Expand Down Expand Up @@ -916,7 +916,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -933,7 +933,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand Down Expand Up @@ -980,7 +980,7 @@
"git_url": "",
"git_branch": "",
"commitid": "",
"Resource": {
"resource": {
"name": "",
"selectors": null,
"labels": null,
Expand All @@ -997,7 +997,7 @@
"git_url": "",
"git_branch": "matser",
"commitid": "4e55bcc32dd42f57c2f62f5c160c8dd645684f65",
"Resource": {
"resource": {
"name": "akmebank-ui",
"selectors": [
"app:akmebank-ui-selector"
Expand Down
Loading

0 comments on commit 9da71d8

Please sign in to comment.