Skip to content

Commit 778dcb3

Browse files
authored
Incorporated review comments (#22)
* Incorporated review comments * review comments
1 parent 6514c5c commit 778dcb3

File tree

1 file changed

+117
-1
lines changed

1 file changed

+117
-1
lines changed

github_discovery.py

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,36 @@ def get_sc_product_id(product_id):
141141
except Exception as e:
142142
log.error(f"Error getting product ID from SC: {e}")
143143
return False
144-
144+
# This method is to find the values defined for allowlist in values*.yaml files under helm_deploy folder of each project.
145+
# This methods read all the values files under helm_deploy folder and create a dictionary object of allowlist for each environment
146+
# including the default values.
147+
def fetch_values_for_allowlist_key(yaml_data, key):
148+
values = {}
149+
if isinstance(yaml_data, dict):
150+
if key in yaml_data:
151+
values.update(yaml_data[key])
152+
for k, v in yaml_data.items():
153+
if isinstance(v, dict) or isinstance(v, list):
154+
child_values = fetch_values_for_allowlist_key(v, key)
155+
if child_values:
156+
values.update({k: child_values})
157+
elif isinstance(yaml_data, list):
158+
for item in yaml_data:
159+
child_values = fetch_values_for_allowlist_key(item, key)
160+
if child_values:
161+
values.update(child_values)
162+
return values
163+
164+
# This method read the value stored in dictionary passed to it checks if the ip allow list is present or not and returns boolean
165+
166+
def is_ipallowList_enabled(yaml_data):
167+
ip_allow_list_enabled = False
168+
if len(yaml_data) !=0:
169+
ip_allow_list_enabled = True
170+
return ip_allow_list_enabled
145171
def process_repo(**component):
172+
173+
allow_list_key = "allowlist"
146174
c_name = component["attributes"]["name"]
147175
c_id = component["id"]
148176
github_repo = component["attributes"]["github_repo"]
@@ -175,6 +203,10 @@ def process_repo(**component):
175203
teams_write = []
176204
teams_admin = []
177205
teams_maintain = []
206+
#variables used for implemenmtation of findind IP allowlist in helm values files
207+
ip_allow_list_data={}
208+
ip_allow_list = {}
209+
ip_allow_list_default={}
178210

179211
try:
180212
branch_protection = default_branch.get_protection()
@@ -249,8 +281,17 @@ def process_repo(**component):
249281
if file.name.startswith('values-'):
250282
env = re.match('values-([a-z0-9-]+)\\.y[a]?ml', file.name)[1]
251283
helm_environments.append(env)
284+
285+
# HEAT-223 Start : Read and collate data for IPallowlist from all environment specific values.yaml files.
286+
ip_allow_list[file] = fetch_values_for_allowlist_key(get_file_yaml(repo, f"{monorepo_dir_suffix}helm_deploy/{file.name}"), allow_list_key)
287+
ip_allow_list_data.update({file.name: ip_allow_list[file]})
288+
# HEAT-223 End : Read and collate data for IPallowlist from all environment specific values.yaml files.
289+
252290
helm_default_values = get_file_yaml(repo, f"{monorepo_dir_suffix}helm_deploy/{c_name}/values.yaml")
253291
if helm_default_values:
292+
293+
ip_allow_list_default = fetch_values_for_allowlist_key(helm_default_values, allow_list_key)
294+
254295
# Try to get the container image
255296
try:
256297
container_image = helm_default_values['image']['repository']
@@ -323,12 +364,30 @@ def process_repo(**component):
323364
if 'circleci_project_k8s_namespace' in p:
324365
dev_namespace = p['circleci_project_k8s_namespace']
325366
e={'namespace': dev_namespace, 'type': 'dev'}
367+
allow_list_values_for_prj_ns={}
326368
if 'dev' in helm_envs:
327369
dev_url = f"https://{helm_envs['dev']['host']}"
328370
e.update({'name': 'dev', 'type': 'dev', 'url': dev_url})
371+
372+
try:
373+
ip_allow_list_env=ip_allow_list_data['values-dev.yaml']
374+
allow_list_values_for_prj_ns.update({'values-dev.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
375+
except KeyError:
376+
pass
377+
378+
e.update({'ip_allow_list': allow_list_values_for_prj_ns, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values_for_prj_ns)})
379+
329380
elif 'development' in helm_envs:
330381
dev_url = f"https://{helm_envs['development']['host']}"
331382
e.update({'name': 'development', 'type': 'dev', 'url': dev_url})
383+
384+
try:
385+
ip_allow_list_env=ip_allow_list_data['values-development.yaml']
386+
allow_list_values_for_prj_ns.update({'values-development.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
387+
except KeyError:
388+
pass
389+
e.update({'ip_allow_list': allow_list_values_for_prj_ns, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values_for_prj_ns)})
390+
332391
else:
333392
dev_url = False
334393

@@ -361,6 +420,7 @@ def process_repo(**component):
361420
if 'circleci_context_k8s_namespaces' in p:
362421
for c in p['circleci_context_k8s_namespaces']:
363422
e = {}
423+
allow_list_values={}
364424
env_name=c['env_name']
365425
env_type=c['env_type']
366426

@@ -369,24 +429,80 @@ def process_repo(**component):
369429
if env_name in helm_envs:
370430
env_url=f"https://{helm_envs[env_name]['host']}"
371431
e.update({'name': env_name, 'url': env_url})
432+
try:
433+
ip_allow_list_env=ip_allow_list_data[f'values-{env_name}.yaml']
434+
allow_list_values.update({f'values-{env_name}.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
435+
except KeyError:
436+
pass
437+
438+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
439+
372440
elif 'developement' in helm_envs:
373441
env_url=f"https://{helm_envs['developement']['host']}"
374442
e.update({'type': 'dev', 'name': 'developement', 'url': env_url})
443+
try:
444+
ip_allow_list_env=ip_allow_list_data[f'values-{env_name}.yaml']
445+
allow_list_values.update({f'values-{env_name}.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
446+
except KeyError:
447+
pass
448+
449+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
450+
375451
elif 'test' in helm_envs:
376452
env_url=f"https://{helm_envs['test']['host']}"
377453
e.update({'type': 'test', 'name': 'test', 'url': env_url})
454+
try:
455+
ip_allow_list_env=ip_allow_list_data['values-test.yaml']
456+
allow_list_values.update({'values-test.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
457+
except KeyError:
458+
pass
459+
460+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
461+
378462
elif 'testing' in helm_envs:
379463
env_url=f"https://{helm_envs['testing']['host']}"
380464
e.update({'type': 'test', 'name': 'testing', 'url': env_url})
465+
try:
466+
ip_allow_list_env=ip_allow_list_data['values-testing.yaml']
467+
allow_list_values.update({'values-testing.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
468+
except KeyError:
469+
pass
470+
471+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
472+
381473
elif 'staging' in helm_envs:
382474
env_url=f"https://{helm_envs['staging']['host']}"
383475
e.update({'type': 'stage', 'name': 'staging', 'url': env_url})
476+
try:
477+
ip_allow_list_env=ip_allow_list_data['values-staging.yaml']
478+
allow_list_values.update({'values-staging.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
479+
except KeyError:
480+
pass
481+
482+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
483+
384484
elif 'qa' in helm_envs:
385485
env_url=f"https://{helm_envs['qa']['host']}"
386486
e.update({'type': 'preprod', 'name': 'qa', 'url': env_url})
487+
try:
488+
ip_allow_list_env=ip_allow_list_data['values-qa.yaml']
489+
allow_list_values.update({'values-qa.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
490+
except KeyError:
491+
pass
492+
493+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
494+
387495
elif 'production' in helm_envs:
388496
env_url=f"https://{helm_envs['production']['host']}"
389497
e.update({'type': 'prod', 'name': 'production', 'url': env_url})
498+
try:
499+
ip_allow_list_env=ip_allow_list_data['values-production.yaml']
500+
allow_list_values.update({'values-production.yaml' : ip_allow_list_env, 'values.yaml':ip_allow_list_default})
501+
except KeyError:
502+
pass
503+
504+
e.update({'ip_allow_list': allow_list_values, 'ip_allow_list_enabled': is_ipallowList_enabled(allow_list_values)})
505+
390506
else:
391507
env_url = False
392508

0 commit comments

Comments
 (0)