def test_scheduler_job(self): # Temporarily pause the scheduler. self.scheduler.pause_job("delete_expired_links") # Add a dummy secret in database with an expired expiry date. slug = "z6HNg2dCcvvaOXli1z3x" encrypted_text = ( b"nKir73XhgyXxjwYyCG-QHQABhqCAAAAAAF6rPvPYX7OYFZRTzy" b"PdIwvdo2SFwAN0VXrfosL54nGHr0MN1YtyoNjx4t5Y6058lFvDH" b"zsnv_Q1KaGFL6adJgLLVreOZ9kt5HpwnEe_Lod5Or85Ig==") now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") expired_date = datetime.strptime( now, "%Y-%m-%d %H:%M:%S") - timedelta(days=1) Entries.create(slug_link=slug, encrypted_text=encrypted_text, date_created=now, date_expires=expired_date) # Run scheduler task. tasks.delete_expired_links() # Check that the secret has been deleted from the database. link = Entries.query.filter_by(slug_link=slug).first() self.assertIsNone(link) # Resume the scheduler. self.scheduler.resume_job("delete_expired_links")
def create_secret(passphrase: str, secret: str, expire: int, tries: int, haveibeenpwned: bool) -> Tuple[Dict, int]: """Create a secret. Args: passphrase (str): Passphrase needed to encrypt the secret. secret (str): Secret to encrypt. expire (int): Number of days the secret will be stored. tries (int): Number of tries to read the secret before it gets deleted. haveibeenpwned (bool): Passphrase has been checked with haveibeenpwned. """ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") expiration_date = datetime.strptime( now, "%Y-%m-%d %H:%M:%S") + timedelta(days=expire) slug = _generate_unique_slug() Entries.create(slug_link=slug, encrypted_text=Secret(secret.encode(), passphrase).encrypt(), date_created=now, date_expires=expiration_date, tries=tries, haveibeenpwned=haveibeenpwned) app.logger.info(f"{slug} created and expires on {expiration_date}") timez = datetime.now(timezone.utc).astimezone().tzname() return dict( status=Status.CREATED.value, details="Secret successfully created.", slug=slug, link=f"{request.url_root}r/{slug}", expires_on=f"{expiration_date.strftime('%Y-%m-%d at %H:%M')} {timez}" ), 201
def write_secret( passphrase: str, secret: str, expire: str, tries: int, haveibeenpwned: bool ) -> Tuple[WriteResponse, HTTPStatus]: """Write a secret. Args: passphrase (str): Passphrase needed to encrypt the secret. secret (str): Secret to encrypt. expire (int): How long the secret will be stored. tries (int): Number of tries to read the secret before it gets deleted. haveibeenpwned (bool): Passphrase has been checked with haveibeenpwned. """ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") exp_date = _build_expiry_date(now, expire) slug = _generate_unique_slug() Entries.create( slug_link=slug, encrypted_text=Secret(secret.encode(), passphrase).encrypt(), date_created=now, date_expires=exp_date, tries=tries, haveibeenpwned=haveibeenpwned, ) app.logger.info(f"{slug} created and expires on {exp_date}") timez = datetime.now(timezone.utc).astimezone().tzname() expires_on = f"{exp_date.strftime('%B %d, %Y at %H:%M')} {timez}" return (WriteResponse(slug, expires_on), HTTPStatus.CREATED)
def create_secret(passphrase: str, secret: str, expire: int, tries: int, haveibeenpwned: bool) -> Tuple[Dict, int]: """Create a secret. Args: passphrase (str): Passphrase needed to encrypt the secret. secret (str): Secret to encrypt. expire (int): Number of days the secret will be stored. tries (int): Number of tries to read the secret before it gets deleted. haveibeenpwned (bool): Passphrase has been checked with haveibeenpwned. """ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") exp_date = datetime.strptime(now, "%Y-%m-%d %H:%M:%S") + timedelta(days=expire) slug = _generate_unique_slug() Entries.create( slug_link=slug, encrypted_text=Secret(secret.encode(), passphrase).encrypt(), date_created=now, date_expires=exp_date, tries=tries, haveibeenpwned=haveibeenpwned, ) app.logger.info(f"{slug} created and expires on {exp_date}") timez = datetime.now(timezone.utc).astimezone().tzname() hostname = request.url_root if app.config["SHHH_HOST"]: hostname = f"{app.config['SHHH_HOST']}/" response = { "status": Status.CREATED.value, "details": "Secret successfully created.", "slug": slug, "link": f"{hostname}r/{slug}", "expires_on": f"{exp_date.strftime('%Y-%m-%d at %H:%M')} {timez}", } return (response, HTTPStatus.CREATED.value)
def test_repr_entry(self): slug = "z6HNg2dCcvvaOXli1z3x" encrypted_text = ( b"nKir73XhgyXxjwYyCG-QHQABhqCAAAAAAF6rPvPYX7OYFZRTzy" b"PdIwvdo2SFwAN0VXrfosL54nGHr0MN1YtyoNjx4t5Y6058lFvDH" b"zsnv_Q1KaGFL6adJgLLVreOZ9kt5HpwnEe_Lod5Or85Ig==") now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") expired_date = datetime.strptime( now, "%Y-%m-%d %H:%M:%S") - timedelta(days=1) entry = Entries.create( slug_link=slug, encrypted_text=encrypted_text, date_created=now, date_expires=expired_date, ) self.assertEqual(repr(entry), f"<Entry {slug}>")