Scenario:
I am a security professional working at a healthcare company. As part of my job I am regularly required to update a file that identifies the employees who can access restricted content. Employees are restricted access based on their IP address. My task is to create an algorithm that uses python to check whether the companies allow list contains any IP addresses identified on the remove list and to remove them if so.
Open the file that contains the allow list
For the first part of the algorithm, I opened the “allow_list.txt” file. First I assigned this file name as a string to the import_file variable:
Then, I used a with statement to open the file:
In my algorithm, the with statement is used with the .open() function in read mode to open the allow list file to read it. The reason for opening the file is to allow me to access the IP addresses stored in the allow list file. Using the with keyword will help manage the resources by closing the file after existing the with statement. In the code with the .open() function it has two parameters. The first one Identifies which file to import and the second parameter indicates what I want to do with the file. The “r” indicates that I want to read it. The code above also used the as keyword to assign a variable named file.file stores the output of the .open() function while I work within the with statement.
Read the file contents
Since I used the .open() function with the argument “r” for read, I was able to call the .read() function in the body of the with statement. The .read() method converts the file into a string and allows me to read it. I applied the .read() method to the file variable identified in the with statement. Then, I assigned the string output of this method to the variable ip_addresses. This code reads the contents of the “allow_list.txt” file into string format that allows me to later use the string to extract data in my python program.
Convert the string to a list
To be able to remove specific individual IP addresses from the allow list, It needs to be in list format. This is done using .split() method to convert ip_addresses string into a list.
The .split() function is called by appending it to a string variable. It works by converting the contents of a string into a list. This makes it easier to remove IP addresses from the allow list. By default the .split() function splots the text by whitespace into list elements. In my algorithm, the .split() function takes the data that is stored in ip_addresses and converts it into a list of IP addresses separated by a white space. The list is stored into the variable ip_addresses.
Iterate through the remove list
The for loop in Python repeats code for a specified sequence. The purpose of this for loop in the algorithm is to apply specific code statements to all elements in a sequence. The keyword for starts the for loop. It is followed by the loop variable element and the keyword in. The keyword in indicates to iterate through the sequence ip_addresses and assign each value to the loop variable element.
Remove IP addresses that are on the remove list
My algorithm requires removing any IP address from the allow list (ip_addresses) that is also contained in the remove_list. Since there were no duplicates in ip_addresses I was able to use the following code.
Within the for loop, I created a condition statement that evaluated whether or not the loop variable element was found in the ip_addresses list. If the element was found to be in the ip_addresses list we removed it by using the .remove(element) on ip_addresses. This means that for each IP address that was in remove_list would be removed from Ip_addresses.
Update the file with the revised list of IP addresses
As the final step in my algorithm I needed to update the allow list file with the revised list of IP addresses. To do this, the list needed to be converted back to a string. I used the .join() method for this.
The .join() method combines all items in the iterable into a string. In this algorithm I used the .join() method to create a string from the list ip_addresses so that I could pass it in as an argument to the .write() method when writing to the file “allow_list.txt” I used the string (“\n”) as the separator to instruct python to replace each element on a new line. I then used another with statement and the .write() method to update the file:
In this with statement I used “w” as the second argument with the open() function. This argument shows that I want to open a file to write over its contents. When using the “w” argument it allows me to call the .write() function in the body of the with statement. The .write() function writes string data to a specified file and replaces any existing file content.
In this instance I wanted to write the updated allow list as a string to the file “allow_list.txt”. This way, the restricted content will no longer be accessible to any IP addresses that were removed from the allow list. To rewrite the file, I appended the .write() function to the file object file that identified in the with statement. I passed in the ip_addresses variable as the argument to specify that the contents of the file specified in the with statement should be replaced with the data in this variable.
Summary
I developed an algorithm designed to eliminate IP addresses specified in a remove_list variable from the “allow_list.txt” file containing authorized addresses. The algorithm initiates by accessing the file, reading its contents into a string in read mode, and subsequently transforming this string into a list assigned to the variable ip_addresses. I then implemented an iteration process over the IP addresses within remove_list. During each iteration, the algorithm assesses whether the current element exists within the ip_addresses list. Upon confirmation of its presence, the algorithm employs the .remove() method to excise the element from ip_addresses. Following the completion of this process, I utilized the .join() method to reconvert the updated ip_addresses list back into a string format, enabling me to overwrite the original contents of the “allow_list.txt” file with the revised list of approved IP addresses.
*screenshots were taken from inside google labs Jupyter notebook*