This function extract the IP address ranges from the TXT records and stores them in gcp_ips collection within the database.
(logger=None)
| 170 | |
| 171 | |
| 172 | def main(logger=None): |
| 173 | """ |
| 174 | This function extract the IP address ranges from the TXT records |
| 175 | and stores them in gcp_ips collection within the database. |
| 176 | """ |
| 177 | if logger is None: |
| 178 | logger = LoggingUtil.create_log(__name__) |
| 179 | |
| 180 | now = datetime.now() |
| 181 | print("Starting: " + str(now)) |
| 182 | logger.info("Starting...") |
| 183 | |
| 184 | parser = argparse.ArgumentParser( |
| 185 | description="Retrieve GCP IP ranges from DNS TXT records or the public JSON file." |
| 186 | ) |
| 187 | parser.add_argument( |
| 188 | "--source", |
| 189 | required=False, |
| 190 | default="json", |
| 191 | choices=["json", "dns"], |
| 192 | help="Specify whether to fetch from DNS TXT records or the public JSON file", |
| 193 | ) |
| 194 | |
| 195 | args = parser.parse_args() |
| 196 | |
| 197 | mongo_connector = MongoConnector.MongoConnector() |
| 198 | gcp_collection = mongo_connector.get_gcp_ips_connection() |
| 199 | google_dns = GoogleDNS.GoogleDNS() |
| 200 | jobs_manager = JobsManager.JobsManager(mongo_connector, "get_gcp_ranges") |
| 201 | jobs_manager.record_job_start() |
| 202 | |
| 203 | if args.source == "dns": |
| 204 | ipv4_ranges, ipv6_ranges = dns_search(google_dns, logger) |
| 205 | elif args.source == "json": |
| 206 | ipv4_ranges, ipv6_ranges = json_search(google_dns, logger) |
| 207 | else: |
| 208 | logger.error("Invalid source: " + args.source) |
| 209 | exit(1) |
| 210 | |
| 211 | new_data = {} |
| 212 | new_data["prefixes"] = ipv4_ranges |
| 213 | new_data["ipv6_prefixes"] = ipv6_ranges |
| 214 | new_data["created"] = now |
| 215 | |
| 216 | gcp_collection.delete_many({}) |
| 217 | mongo_connector.perform_insert(gcp_collection, new_data) |
| 218 | |
| 219 | jobs_manager.record_job_complete() |
| 220 | |
| 221 | now = datetime.now() |
| 222 | print("Ending: " + str(now)) |
| 223 | logger.info("Complete.") |
| 224 | |
| 225 | |
| 226 | if __name__ == "__main__": |
no test coverage detected