Insert the provided source as a record from the provided source name. :param result: The result of a DNS lookup as a JSON object including the fqdn, type, value, zone, and created values. :param source_name: The DNS record source ("ssl","virustotal","s
(self, result, source_name, source_metadata=None)
| 94 | return date.replace(day=d, month=m, year=y) |
| 95 | |
| 96 | def insert_record(self, result, source_name, source_metadata=None): |
| 97 | """ |
| 98 | Insert the provided source as a record from the provided source name. |
| 99 | :param result: The result of a DNS lookup as a JSON object including |
| 100 | the fqdn, type, value, zone, and created values. |
| 101 | :param source_name: The DNS record source ("ssl","virustotal","sonar_dns","common_crawl") |
| 102 | :param source_metadata: An optional record for additional source metadata |
| 103 | [{"key": "foo1", "value", "bar1"}, {"key": "foo2", "value", "bar2"}] |
| 104 | """ |
| 105 | # Ensure all inserted records are lowercase |
| 106 | result["fqdn"] = result["fqdn"].lower() |
| 107 | |
| 108 | query = { |
| 109 | "fqdn": result["fqdn"], |
| 110 | "type": result["type"], |
| 111 | "value": result["value"], |
| 112 | } |
| 113 | check = self._mongo_connector.perform_find_one(self.all_dns_collection, query) |
| 114 | |
| 115 | if check is None: |
| 116 | result["sources"] = [] |
| 117 | result["sources"].append({}) |
| 118 | result["sources"][0]["source"] = source_name |
| 119 | result["sources"][0]["updated"] = datetime.now() |
| 120 | if source_metadata is not None and len(source_metadata) > 0: |
| 121 | for entry in source_metadata: |
| 122 | result["sources"][0][entry["key"]] = entry["value"] |
| 123 | result["updated"] = datetime.now() |
| 124 | self._mongo_connector.perform_insert(self.all_dns_collection, result) |
| 125 | else: |
| 126 | # If the update has accountInfo |
| 127 | if "accountInfo" in result: |
| 128 | # And if the existing record does not have accountInfo |
| 129 | # or the accountInfo is different |
| 130 | # then update the record |
| 131 | account_id_test = False |
| 132 | if "accountInfo" in check: |
| 133 | for c_entry in check["accountInfo"]: |
| 134 | if c_entry["key"] == "accountId": |
| 135 | for r_entry in result["accountInfo"]: |
| 136 | if r_entry["key"] == "accountId": |
| 137 | if c_entry["value"] == r_entry["value"]: |
| 138 | account_id_test = True |
| 139 | break |
| 140 | |
| 141 | if "accountInfo" not in check or not account_id_test: |
| 142 | self.all_dns_collection.update_one( |
| 143 | {"_id": ObjectId(check["_id"])}, |
| 144 | { |
| 145 | "$set": { |
| 146 | "accountInfo": result["accountInfo"], |
| 147 | } |
| 148 | }, |
| 149 | ) |
| 150 | |
| 151 | # Update the record's updated time |
| 152 | self.all_dns_collection.update_one( |
| 153 | {"_id": ObjectId(check["_id"])}, |