Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Encryption to enable future support of other encryption methods #1602

Merged
merged 38 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b3a662f
initial pass through to rework encryption into separate module
wren Sep 24, 2022
c52dcfe
little more cleanup
wren Sep 24, 2022
bb1f263
rename function, fix some linting issues
wren Sep 24, 2022
a76f066
more cleaning
wren Sep 24, 2022
49948c4
fix password bug in encryption
wren Sep 24, 2022
454be33
fix linting issue
wren Sep 24, 2022
a9c7827
more cleanup
wren Sep 24, 2022
dbeb2c2
move prompt into prompt.py
wren Sep 24, 2022
358e6c7
more cleanup
wren Sep 25, 2022
41a5af5
update the upgrade process for new encryption classes
wren Sep 25, 2022
97f3290
general cleanup
wren Sep 26, 2022
ab70c56
turn into enum instead of strings
wren Sep 26, 2022
c6a564e
store status code so tests don't fail
wren Sep 26, 2022
07d1db6
standardize the load and store methods in journals
wren Oct 1, 2022
7c6e34d
get rid of old PlainJournal class
wren Oct 1, 2022
62b7337
typing cleanup
wren Oct 1, 2022
3624bde
more cleanup
wren Oct 1, 2022
46a7aa0
format
wren Oct 1, 2022
44cafe6
Merge branch 'develop' into encryption-rework
wren Oct 1, 2022
fd6bdd7
fix linting issue
wren Oct 1, 2022
ce4df8e
Fix obscure Windows line ending issue with decode
micahellison Oct 1, 2022
e695e7a
fix for python 3.11
wren Oct 1, 2022
0466cda
add more typing
wren Oct 2, 2022
15da50a
don't use class variables because that's not what we want
wren Oct 8, 2022
120ad59
fix more type hints
wren Oct 8, 2022
196cb71
jrnlv1 encryption doesn't support encryption anymore (it's deprecated)
wren Oct 8, 2022
d066a3a
keep logic for password attemps inside the class that uses it
wren Oct 8, 2022
a9f57a0
Merge branch 'develop' into encryption-rework
wren Oct 8, 2022
dda6129
Merge branch 'develop' into encryption-rework
wren Oct 29, 2022
11605af
take out old line of code
wren Oct 29, 2022
02cdf95
add some more logging
wren Oct 29, 2022
c6d52b3
update logging statements
wren Oct 29, 2022
dcdee69
Merge branch 'develop' into encryption-rework
wren Nov 5, 2022
45b05b9
clean up logging statements
wren Nov 5, 2022
ea1b035
fix type checking block
wren Nov 5, 2022
a4c97f3
run linters
wren Nov 5, 2022
12bcc64
fix typo
wren Nov 5, 2022
33b0de8
Fix for new test from develop branch
wren Nov 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 0 additions & 217 deletions jrnl/EncryptedJournal.py

This file was deleted.

55 changes: 31 additions & 24 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from jrnl import Entry
from jrnl import time
from jrnl.config import validate_journal_name
from jrnl.encryption import determine_encryption_method
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand Down Expand Up @@ -47,6 +48,7 @@ def __init__(self, name="default", **kwargs):
self.search_tags = None # Store tags we're highlighting
self.name = name
self.entries = []
self.encryption_method = None

def __len__(self):
"""Returns the number of entries"""
Expand Down Expand Up @@ -78,6 +80,22 @@ def import_(self, other_journal_txt):
self.entries = list(frozenset(self.entries) | frozenset(imported_entries))
self.sort()

def _get_encryption_method(self) -> None:
encryption_method = determine_encryption_method(self.config["encrypt"])
self.encryption_method = encryption_method(self.name, self.config)

def _decrypt(self, text: bytes) -> str:
if self.encryption_method is None:
self._get_encryption_method()

return self.encryption_method.decrypt(text)

def _encrypt(self, text: str) -> bytes:
if self.encryption_method is None:
self._get_encryption_method()

return self.encryption_method.encrypt(text)

def open(self, filename=None):
"""Opens the journal file defined in the config and parses it into a list of Entries.
Entries have the form (date, title, body)."""
Expand Down Expand Up @@ -106,6 +124,7 @@ def open(self, filename=None):
)

text = self._load(filename)
text = self._decrypt(text)
self.entries = self._parse(text)
self.sort()
logging.debug("opened %s with %d entries", self.__class__.__name__, len(self))
Expand All @@ -115,6 +134,7 @@ def write(self, filename=None):
"""Dumps the journal into the config file, overwriting it"""
filename = filename or self.config["journal"]
text = self._to_text()
text = self._encrypt(text)
self._store(filename, text)

def validate_parsing(self):
Expand All @@ -131,11 +151,12 @@ def _to_text(self):
return "\n".join([str(e) for e in self.entries])

def _load(self, filename):
raise NotImplementedError
with open(filename, "rb") as f:
return f.read()

@classmethod
def _store(filename, text):
raise NotImplementedError
def _store(self, filename, text):
with open(filename, "wb") as f:
f.write(text)

def _parse(self, journal_txt):
"""Parses a journal that's stored in a string and returns a list of entries"""
Expand Down Expand Up @@ -342,7 +363,7 @@ def new_entry(self, raw, date=None, sort=True):

def editable_str(self):
"""Turns the journal into a string of entries that can be edited
manually and later be parsed with eslf.parse_editable_str."""
manually and later be parsed with self.parse_editable_str."""
return "\n".join([str(e) for e in self.entries])

def parse_editable_str(self, edited):
Expand All @@ -356,25 +377,11 @@ def parse_editable_str(self, edited):
self.entries = mod_entries


class PlainJournal(Journal):
def _load(self, filename):
with open(filename, "r", encoding="utf-8") as f:
return f.read()

def _store(self, filename, text):
with open(filename, "w", encoding="utf-8") as f:
f.write(text)


class LegacyJournal(Journal):
"""Legacy class to support opening journals formatted with the jrnl 1.x
standard. Main difference here is that in 1.x, timestamps were not cuddled
by square brackets. You'll not be able to save these journals anymore."""

def _load(self, filename):
with open(filename, "r", encoding="utf-8") as f:
return f.read()

def _parse(self, journal_txt):
"""Parses a journal that's stored in a string and returns a list of entries"""
# Entries start with a line that looks like 'date title' - let's figure out how
Expand Down Expand Up @@ -428,6 +435,7 @@ def open_journal(journal_name, config, legacy=False):
If legacy is True, it will open Journals with legacy classes build for
backwards compatibility with jrnl 1.x
"""
logging.debug("open_journal start")
validate_journal_name(journal_name, config)
config = config.copy()
config["journal"] = expand_path(config["journal"])
Expand Down Expand Up @@ -462,10 +470,9 @@ def open_journal(journal_name, config, legacy=False):
from jrnl import FolderJournal

return FolderJournal.Folder(journal_name, **config).open()
return PlainJournal(journal_name, **config).open()

from jrnl import EncryptedJournal
return Journal(journal_name, **config).open()

if legacy:
return EncryptedJournal.LegacyEncryptedJournal(journal_name, **config).open()
return EncryptedJournal.EncryptedJournal(journal_name, **config).open()
config["encrypt"] = "jrnlv1"
return LegacyJournal(journal_name, **config).open()
return Journal(journal_name, **config).open()
1 change: 1 addition & 0 deletions jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def configure_logger(debug: bool = False) -> None:
)
logging.getLogger("parsedatetime").setLevel(logging.INFO)
logging.getLogger("keyring.backend").setLevel(logging.ERROR)
logging.debug("Logging start")


def cli(manual_args: list[str] | None = None) -> int:
Expand Down
Loading