-
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.
- Loading branch information
Showing
4 changed files
with
173 additions
and
115 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,72 @@ | ||
import os | ||
from bitwarden import Bitwarden | ||
from notifier import Notifier | ||
from dotenv import load_dotenv | ||
from datetime import datetime | ||
|
||
|
||
class Backup: | ||
def __init__(self, bitwarden: Bitwarden, notifier: Notifier): | ||
self.bitwarden = bitwarden | ||
self.notifier = notifier | ||
|
||
self.export_timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | ||
|
||
load_dotenv() | ||
|
||
self.backup_path = os.getenv("BACKUP_PATH") | ||
self.backup_format = os.getenv("BACKUP_FORMAT") | ||
self.backup_password = os.getenv("BACKUP_PASSWORD") | ||
|
||
backup_organizations = os.getenv("BACKUP_ORGANIZATIONS").strip() | ||
|
||
if backup_organizations: | ||
self.backup_organizations = backup_organizations.split(",") | ||
else: | ||
self.backup_organizations = [] | ||
|
||
def generate_export_filename(self, organization_id=None): | ||
filename = "bitwarden-encrypted_" + self.export_timestamp | ||
|
||
if organization_id: | ||
filename += "_" + organization_id | ||
|
||
filename += ".json" | ||
|
||
return filename | ||
|
||
def generate_export_path(self, organization_id=None): | ||
return self.backup_path + "/" + self.generate_export_filename(organization_id) | ||
|
||
def start(self): | ||
export_files = [] | ||
|
||
self.notifier.send_start() | ||
|
||
# Logout is needed before configuring the server | ||
self.bitwarden.logout() | ||
|
||
self.bitwarden.configure_server() | ||
self.bitwarden.login() | ||
self.bitwarden.unlock() | ||
|
||
self.bitwarden.export( | ||
self.generate_export_path(), | ||
self.backup_format, | ||
self.backup_password | ||
) | ||
|
||
export_files.append(self.generate_export_filename()) | ||
|
||
for organization_id in self.backup_organizations: | ||
self.bitwarden.export( | ||
self.generate_export_path(organization_id), | ||
self.backup_format, | ||
self.backup_password | ||
) | ||
|
||
export_files.append(self.generate_export_filename(organization_id)) | ||
|
||
self.bitwarden.logout() | ||
|
||
self.notifier.send_success(export_files) |
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,20 @@ | ||
import subprocess | ||
|
||
|
||
class CLI: | ||
def __init__(self, binary_path): | ||
self.binary_path = binary_path | ||
|
||
def execute(self, command, arguments=None, environment=None): | ||
if arguments is None: | ||
arguments = [] | ||
|
||
if environment: | ||
process = subprocess.run([self.binary_path, command] + arguments, env=environment, capture_output=True) | ||
else: | ||
process = subprocess.run([self.binary_path, command] + arguments, capture_output=True) | ||
|
||
stdout = process.stdout.decode('utf-8') | ||
stderr = process.stderr.decode('utf-8') | ||
|
||
return stdout, stderr |
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 |
---|---|---|
@@ -1,104 +1,54 @@ | ||
from dotenv import load_dotenv | ||
from notifier import Notifier | ||
from notifiers.ntfy import NTFY | ||
from notifiers.mock import Mock | ||
from bitwarden import Bitwarden | ||
import os | ||
from datetime import datetime | ||
from backup import Backup | ||
from cli import CLI | ||
|
||
load_dotenv() | ||
|
||
# Path configuration | ||
bw_binary = os.getenv("BW_BINARY") | ||
backup_path = os.getenv("BACKUP_PATH") | ||
|
||
# Bitwarden configuration | ||
# authentication | ||
bw_client_id = os.getenv("BW_CLIENT_ID") | ||
bw_client_secret = os.getenv("BW_CLIENT_SECRET") | ||
bw_master_password = os.getenv("BW_MASTER_PASSWORD") | ||
def inject_cli(): | ||
bw_binary = os.getenv("BW_BINARY") | ||
|
||
# connection | ||
bw_server = os.getenv("BW_SERVER") | ||
return CLI(bw_binary) | ||
|
||
# backups | ||
backup_format = os.getenv("BACKUP_FORMAT") | ||
backup_password = os.getenv("BACKUP_PASSWORD") | ||
backup_organizations = os.getenv("BACKUP_ORGANIZATIONS") | ||
|
||
# NTFY configuration | ||
ntfy_server = os.getenv("NTFY_SERVER") | ||
ntfy_topic = os.getenv("NTFY_TOPIC") | ||
ntfy_username = os.getenv("NTFY_USERNAME") | ||
ntfy_password = os.getenv("NTFY_PASSWORD") | ||
def inject_bitwarden(cli: CLI): | ||
bw_binary = os.getenv("BW_BINARY") | ||
bw_server = os.getenv("BW_SERVER") | ||
bw_client_id = os.getenv("BW_CLIENT_ID") | ||
bw_client_secret = os.getenv("BW_CLIENT_SECRET") | ||
bw_master_password = os.getenv("BW_MASTER_PASSWORD") | ||
|
||
def generate_output_path(path, current_time, organization_id = None): | ||
output_path = backup_path + "/bitwarden-encrypted_" + current_time | ||
return Bitwarden(cli, bw_server, bw_client_id, bw_client_secret, bw_master_password) | ||
|
||
if organization_id: | ||
output_path += "_" + organization_id | ||
|
||
output_path += ".json" | ||
|
||
return output_path | ||
|
||
def inject_notifier(): | ||
ntfy_server = os.getenv("NTFY_SERVER") | ||
ntfy_topic = os.getenv("NTFY_TOPIC") | ||
ntfy_username = os.getenv("NTFY_USERNAME") | ||
ntfy_password = os.getenv("NTFY_PASSWORD") | ||
|
||
if ntfy_server and ntfy_topic: | ||
return NTFY(ntfy_server, ntfy_topic, ntfy_username, ntfy_password) | ||
else: | ||
return Mock() | ||
|
||
def main(notifier: Notifier, bitwarden: Bitwarden, config): | ||
notifier.send_start() | ||
|
||
# Logout is needed before configuring the server | ||
bitwarden.logout() | ||
|
||
bitwarden.configure_server(config["bw_server"]) | ||
|
||
bitwarden.login(config["bw_client_id"], config["bw_client_secret"]) | ||
bitwarden.unlock(config["bw_master_password"]) | ||
|
||
current_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | ||
|
||
output_paths = [] | ||
|
||
vault_output_path = generate_output_path(config["backup_path"], current_time) | ||
|
||
bitwarden.export(vault_output_path, config["backup_format"], config["backup_password"]) | ||
|
||
output_paths.append(vault_output_path) | ||
|
||
if config["backup_organizations"]: | ||
backup_organizations = config["backup_organizations"].split(",") | ||
else: | ||
backup_organizations = [] | ||
|
||
for organization_id in backup_organizations: | ||
organization_output_path = generate_output_path(config["backup_path"], current_time, organization_id) | ||
|
||
bitwarden.export(organization_output_path, config["backup_format"], config["backup_password"], organization_id) | ||
|
||
output_paths.append(organization_output_path) | ||
def main(): | ||
cli = inject_cli() | ||
bitwarden = inject_bitwarden(cli) | ||
notifier = inject_notifier() | ||
|
||
bitwarden.logout() | ||
backup = Backup( | ||
bitwarden, | ||
notifier | ||
) | ||
|
||
notifier.send_success(output_paths) | ||
backup.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
notifier = inject_notifier() | ||
main( | ||
notifier, | ||
Bitwarden(bw_binary), | ||
{ | ||
"bw_server": bw_server, | ||
"bw_client_id": bw_client_id, | ||
"bw_client_secret": bw_client_secret, | ||
"bw_master_password": bw_master_password, | ||
"backup_path": backup_path, | ||
"backup_format": backup_format, | ||
"backup_password": backup_password, | ||
"backup_organizations": backup_organizations | ||
} | ||
) | ||
main() |