Early learning task completed in Google Cyber Security course
You are a security professional working at a health care company. As part of your job, you're required to regularly update a file that identifies the employees who can access restricted content. The contents of the file are based on who is working with personal patient records. Employees are restricted access based on their IP address. There is an allow list for IP addresses permitted to sign into the restricted subnetwork. There's also a remove list that identifies which employees you must remove from this allow list. Your task is to create an algorithm that uses Python code to check whether the allow list contains any IP addresses identified on the remove list. If so, you should remove those IP addresses from the file containing the allow list.
The file containing the permitted employee IP addresses (allow_list.txt) must be opened in read mode ("r") to access its contents:
The entire contents of the allow list file are read into a single string variable (ip_addresses):
The string containing all IP addresses is converted into a list of individual strings using the .split() method:
A for loop is initiated to iterate over the items in the ip_addresses list. To safely remove elements within the loop, the iteration is performed over a copy of the list:
A conditional if statement checks if the current element (IP address) exists within the remove_list. If a match is found, the IP address is removed from the original ip_addresses list using the .remove() method:
To finalize the update, the filtered list is converted back into a single, space-separated string using the .join() method. The original file is then opened in write mode ("w") to overwrite its entire content with the revised list.
The entire logic is encapsulated into a reusable Python function, This function automates the security task of ensuring only currently authorized employees have IP access to the restricted subnetwork containing personal patient records.