-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from phenobarbital/new-cypher-capabilities
New cypher capabilities
- Loading branch information
Showing
9 changed files
with
185 additions
and
8 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
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,40 @@ | ||
import os | ||
import asyncio | ||
from navconfig import ( | ||
SITE_ROOT, | ||
config, | ||
DEBUG | ||
) | ||
import logging | ||
from navconfig.cyphers import FileCypher | ||
from dotenv import load_dotenv, dotenv_values | ||
|
||
async def cypher(): | ||
path = SITE_ROOT.joinpath('env') | ||
fc = FileCypher(path) | ||
# first: create the key: | ||
await fc.create_key() | ||
# then, encrypt the file: | ||
file = await fc.encrypt(name = '.env') | ||
print(f'Encrypted ENV was saved to {file}') | ||
|
||
async def test_cypher(): | ||
path = SITE_ROOT.joinpath('env') | ||
fc = FileCypher(path) | ||
file = await fc.decrypt(name = 'env.crypt') | ||
print(file) | ||
|
||
async def test_env(): | ||
path = SITE_ROOT.joinpath('env') | ||
fc = FileCypher(path) | ||
file = await fc.decrypt(name = 'env.crypt') | ||
print(file) | ||
load_dotenv( | ||
stream=file | ||
) | ||
print(os.getenv('ADFS_SERVER')) | ||
|
||
if __name__ == '__main__': | ||
asyncio.run(cypher()) | ||
asyncio.run(test_cypher()) | ||
asyncio.run(test_env()) |
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,26 @@ | ||
# Import Config Class | ||
from navconfig import ( | ||
BASE_DIR, | ||
config, | ||
DEBUG | ||
) | ||
from navconfig.logging import ( | ||
logging | ||
) | ||
|
||
""" | ||
Routes | ||
""" | ||
APP_NAME = config.get('APP_NAME', fallback='Navigator') | ||
APP_DIR = BASE_DIR.joinpath("apps") | ||
|
||
logging.debug(f'::: STARTING APP: {APP_NAME} in path: {APP_DIR} ::: ') | ||
print(f'STARTING WITH DEBUG: {DEBUG}') | ||
|
||
PRODUCTION = config.get('PRODUCTION') | ||
ALLOWED_HOSTS = [ | ||
e.strip() | ||
for e in list(config.get("ALLOWED_HOSTS", section="auth", fallback="localhost*").split(",")) | ||
] | ||
print(f'Allowed HOSTS: {ALLOWED_HOSTS}') | ||
print(f'Production: {PRODUCTION}') |
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,3 @@ | ||
from .fernet import FileCypher | ||
|
||
__all__ = ('FileCypher', ) |
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,78 @@ | ||
import hashlib | ||
import aiofiles | ||
from cryptography.fernet import Fernet | ||
from pathlib import PurePath, PosixPath | ||
from io import StringIO | ||
|
||
|
||
class FileCypher(object): | ||
def __init__(self, directory: PurePath): | ||
self.path = directory | ||
|
||
async def create_key(self): | ||
#generate the key | ||
key = Fernet.generate_key() | ||
file = self.path.joinpath('unlock.key') | ||
#string the key into a file | ||
async with aiofiles.open(file, 'wb') as unlock: | ||
await unlock.write(key) | ||
return file | ||
|
||
async def open_file(self, path: PurePath): | ||
content = None | ||
if not path.exists(): | ||
raise FileNotFoundError( | ||
f'File {path} does not exist' | ||
) | ||
try: | ||
async with aiofiles.open(path) as f: | ||
content = await f.read() | ||
except IOError: | ||
raise Exception( | ||
f'NavConfig: Error loading Environment File {path}' | ||
) | ||
return content | ||
|
||
async def save_file(self, path: PurePath, content): | ||
async with aiofiles.open(path, 'wb') as file: | ||
await file.write(content) | ||
|
||
async def get_key(self): | ||
fkey = self.path.joinpath('unlock.key') | ||
key = None | ||
async with aiofiles.open(fkey) as f: | ||
key = await f.read() | ||
if not key: | ||
raise Exception( | ||
f'Missing the Unlock Key: {fkey!s}' | ||
) | ||
#use the generated key | ||
f = Fernet(key) | ||
return f | ||
|
||
async def encrypt(self, name: str = '.env'): | ||
#use the generated key | ||
f = await self.get_key() | ||
file = self.path.joinpath(name) | ||
# original content | ||
original = await self.open_file(file) | ||
#encrypt the file | ||
encrypted = f.encrypt(original.encode()) | ||
# at now, save it into the same directory | ||
file = self.path.joinpath('env.crypt') | ||
await self.save_file(file, encrypted) | ||
return file | ||
|
||
async def decrypt(self, name: str = 'env.crypt'): | ||
#use the generated key | ||
f = await self.get_key() | ||
#open the original file to encrypt | ||
file = self.path.joinpath(name) | ||
content = await self.open_file(file) | ||
#decrypt the file | ||
decrypted = f.decrypt(content.encode()) | ||
s = StringIO() | ||
s.write(decrypted.decode()) | ||
s.seek(0) | ||
# returned a StringIO | ||
return s |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
__title__ = 'navconfig' | ||
__description__ = ('Configuration tool for all Navigator Services ' | ||
'Tool for accessing Config info from different sources.') | ||
__version__ = '0.7.8' | ||
__version__ = '0.8.0' | ||
__author__ = 'Jesus Lara' | ||
__author_email__ = '[email protected]' | ||
__license__ = 'BSD' |
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