Add db_password

This commit is contained in:
2022-12-26 12:33:09 +08:00
parent a2972f355b
commit 136b70ccf8
3 changed files with 16 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ compress_method: "bzip2" # Optional. Default value: null. Supported value: "bzi
# zstd support 0-22 (Default: 3). brotli support 0-11 (Default: unset).
compress_level: 6
encrypt_db: false # Optional. Default value: false. Encrypt the database. Warning: The default python sqlite library don't support encrypt, it just ignore encrypt phases.
db_password: "Password" # Specify the password of the encryped database.
encrypt_files: false # Optional. Default value: false. Encrypt backup files. The key information will stored in database.
programs:
- name: Your program name # This name is used to identify different application.

View File

@@ -452,6 +452,7 @@ class Program(BasicOption, NFBasicOption):
class Config(BasicOption, NFBasicOption):
dest = ''
encrypt_db = False
db_password = None
progs = []
progs_name = []
@@ -471,6 +472,10 @@ class Config(BasicOption, NFBasicOption):
self.encrypt_db = t['encrypt_db']
else:
self.encrypt_db = False
if 'db_password' in t:
if not isinstance(t['db_password'], str):
raise ValueError('db_password should be a string.')
self.db_password = t['db_password']
if 'programs' not in t:
raise ValueError("No programs found.")
self.parse_all(t)

View File

@@ -1,4 +1,4 @@
from getpass import getpass
from getpass import getpass as _getpass
from os import close
from os.path import join
from shutil import move
@@ -45,6 +45,12 @@ PRIMARY KEY(id)
);'''
def getpass(prompt, cfg: Config) -> str:
if cfg.db_password is not None:
return cfg.db_password
return _getpass(prompt)
class Db:
VERSION = [1, 0, 0, 2]
@@ -83,7 +89,7 @@ class Db:
hydrate_file_if_needed(fn)
self.db = connect(fn, check_same_thread=False)
if config.encrypt_db:
passpharse = getpass('Please input the password of the database:')
passpharse = getpass('Please input the password of the database:', config) # noqa: E501
if not self.encrypted:
tfn = mkstemp()
close(tfn[0])
@@ -98,7 +104,7 @@ class Db:
self.db = connect(fn, check_same_thread=False)
elif opts.change_key:
self.__set_encrypt_key(passpharse)
passpharse = getpass('Please input new password of the database:') # noqa: E501
passpharse = getpass('Please input new password of the database:', config) # noqa: E501
tfn = mkstemp()
close(tfn[0])
tfn = tfn[1]
@@ -113,7 +119,7 @@ class Db:
self.__set_encrypt_key(passpharse)
else:
if self.encrypted:
passpharse = getpass('Please input the password of the database:') # noqa: E501
passpharse = getpass('Please input the password of the database:', config) # noqa: E501
self.__set_encrypt_key(passpharse)
tfn = mkstemp()
close(tfn[0])