-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encryption of passwords for remote librarians
- Loading branch information
Showing
10 changed files
with
118 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Functions for encrypting and decrypting data. | ||
""" | ||
|
||
from cryptography.fernet import Fernet | ||
|
||
from .settings import server_settings | ||
|
||
|
||
def encrypt_string(input: str) -> str: | ||
""" | ||
Encrypt the given string. | ||
""" | ||
key = server_settings.encryption_key | ||
|
||
if key is None: | ||
raise ValueError("No encryption key is set!") | ||
|
||
f = Fernet(key=key) | ||
return f.encrypt(input.encode()).decode() | ||
|
||
|
||
def decrypt_string(input: str) -> str: | ||
""" | ||
Decrypt the given string. | ||
""" | ||
key = server_settings.encryption_key | ||
|
||
if key is None: | ||
raise ValueError("No encryption key is set!") | ||
|
||
f = Fernet(key=key) | ||
return f.decrypt(input.encode()).decode() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
Tests for encryption technology. | ||
""" | ||
|
||
import os | ||
|
||
from ..server import server_setup | ||
|
||
|
||
def test_encrypt_decrypt_cycle(tmp_path_factory): | ||
setup = server_setup(tmp_path_factory, name="test_server") | ||
|
||
env_vars = {x: None for x in setup.env.keys()} | ||
|
||
for env_var in list(env_vars.keys()): | ||
env_vars[env_var] = os.environ.get(env_var, None) | ||
if setup.env[env_var] is not None: | ||
os.environ[env_var] = setup.env[env_var] | ||
|
||
from librarian_server.encryption import decrypt_string, encrypt_string | ||
|
||
input = "hello:world" | ||
|
||
encrypted = encrypt_string(input) | ||
|
||
assert encrypted != input | ||
|
||
decrypted = decrypt_string(encrypted) | ||
|
||
assert decrypted == input | ||
|
||
for env_var in list(env_vars.keys()): | ||
if env_vars[env_var] is None: | ||
del os.environ[env_var] | ||
else: | ||
os.environ[env_var] = env_vars[env_var] |