def rescan_subdomains() -> int: current_time = time_source.timestamp() targets_to_rescan = (db_models.db.session.query( db_models.SubdomainRescanTarget ).filter( current_time - db_models.SubdomainRescanTarget.subdomain_last_scan >= config.SubdomainRescanConfig.SUBDOMAIN_RESCAN_INTERVAL).order_by( db_models.SubdomainRescanTarget.subdomain_last_scan.asc()).limit( config.SubdomainRescanConfig.SUBDOMAIN_BATCH_SIZE).all()) for target in targets_to_rescan: add_subdomains(target.subdomain_scan_target_id, target.subdomain_scan_user_id) target.subdomain_last_scan = time_source.timestamp() db_models.db.session.commit() return len(targets_to_rescan) # for testing
def add_subdomains(target_id: int, user_id: int, data: dict = None) -> Tuple[str, int]: target = actions.get_target_from_id_if_user_can_see(target_id, user_id) if target is None: return "You do not have permission to track this target.", 400 if data is None: # dummy data for searching and enqueuing new subdomains data = get_dummy_target_data(target.hostname) existing_subdomain_rescan_target = db_models.db.session. \ query(db_models.SubdomainRescanTarget). \ filter(db_models.SubdomainRescanTarget.subdomain_scan_target_id == target_id). \ filter(db_models.SubdomainRescanTarget.subdomain_scan_user_id == user_id). \ all() if not existing_subdomain_rescan_target: new = db_models.SubdomainRescanTarget() new.subdomain_scan_target_id = target_id new.subdomain_scan_user_id = user_id new.subdomain_last_scan = time_source.timestamp() db_models.db.session.add(new) db_models.db.session.commit() subdomains = subdomain_lookup(target, user_id) subdomain_ids = add_targets(list(subdomains), user_id, data) return f"Successfully added {len(subdomain_ids)} subdomains", 200
def validate_code(db_code: str, activity: ActivityType, user_id=None)\ -> Union[Tuple[bool, str], Tuple[bool, db_models.TmpRandomCodes]]: query = db_models.db.session \ .query(db_models.TmpRandomCodes) \ .filter(db_models.TmpRandomCodes.code == db_code) \ .filter(db_models.TmpRandomCodes.activity == activity.name) \ .filter(db_models.TmpRandomCodes.expires >= time_source.timestamp()) if user_id: query = query.\ filter(db_models.TmpRandomCodes.user_id == user_id) res: db_models.TmpRandomCodes = query.first() db_models.logger.warning(f'TmpRandomCode obj {res}') if res is None: msg = """Code either: - doesn't exist - has expired - is being used for different purpose than it was originally issued """ if user_id: msg += "- belongs to different user than is currently signed in\n" return False, msg return True, res
class SentNotificationsLog(Base, UniqueModel): __tablename__ = 'notificationssentlog' __uniqueColumns__ = ['sent_notification_id', 'channel'] __table_args__ = (db.UniqueConstraint(*__uniqueColumns__, name=f'_uq_{__tablename__}'),) id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(NumericTimestamp, default=time_source.timestamp()) sent_notification_id = db.Column(db.String, nullable=False) channel = db.Column(db.String, nullable=False)
class ScanResultsHistory(Base, UniqueModel): __tablename__ = 'scanresultshistory' __uniqueColumns__ = ['target_id', 'scanresult_id', 'timestamp'] __table_args__ = (db.UniqueConstraint(*__uniqueColumns__, name=f'_uq_{__tablename__}'),) id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(NumericTimestamp, default=time_source.timestamp()) target_id = db.Column(db.Integer, db.ForeignKey('targets.id'), nullable=False) target = db.relationship("Target") scanresult_id = db.Column(db.Integer, db.ForeignKey('scanresults.id'), nullable=False) scanresult = db.relationship("ScanResults")
def update_references_to_scan_result_single_target(target_id: int, scanresult_id: int): db_utils_advanced.generic_get_create_edit_from_data(db_schemas.ScanResultsHistorySchema, {'target_id': target_id, 'scanresult_id': scanresult_id, 'timestamp': time_source.timestamp(), }) # ls = db_utils_advanced.generic_get_create_edit_from_data(db_schemas.LastScanSchema, # {'target_id': target_id, # 'last_scanned': time_source.timestamp(), # 'scanresult_id': scanresult_id, # # 'last_enqueued': # previous value # }) ls = db_utils_advanced.generic_get_create_edit_from_data(db_schemas.LastScanSchema, {'target_id': target_id}, get_only=True) if ls is None: logger.error(f"SC0001 Scan result for a target which doesn't have Last Scan record. {target_id}") return ls.last_scanned = time_source.timestamp() ls.result_id = scanresult_id db_utils_advanced.generic_get_create_edit_from_transient(db_schemas.LastScanSchema, ls)
class TmpRandomCodes(Base, UniqueModel): __tablename__ = 'tmprandomcodes' __uniqueColumns__ = ['code'] __table_args__ = (db.UniqueConstraint(*__uniqueColumns__, name=f'_uq_{__tablename__}'),) id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(NumericTimestamp, default=time_source.timestamp()) expires = db.Column(NumericTimestamp) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship("User") code = db.Column(db.String, unique=True, index=True) activity = db.Column(db.String) params = db.Column(db.String)
def current_timestamp(): return str(time_source.timestamp()), 200
def test_timestamp(self): time_source.mock(True) for i in range(5): mock_time = datetime.fromtimestamp(i) time_source.set_time(mock_time) assert time_source.timestamp() == i