Skip to content

Commit

Permalink
if user forgets to add password for a database that needs it, ask the…
Browse files Browse the repository at this point in the history
…m instead of throwing error
  • Loading branch information
evangelosmeklis committed Sep 28, 2024
1 parent 6482648 commit 94ff051
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion peepdb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def view(connection_name, table, format, page, page_size):
@cli.command()
@click.argument('connection_name')
@click.option('--db-type', type=click.Choice(['mysql', 'postgres', 'mariadb', 'mongodb', 'sqlite']), required=True, help='Database type')
@click.option('--host', required=True, help='Database host (or file path for SQLite)')
@click.option('--host', required=True, help='Database host or file path for SQLite')
@click.option('--user', required=False, help='Database user (not required for SQLite)')
@click.option('--password', required=False, help='Database password (not required for SQLite)')
@click.option('--database', required=True, help='Database name')
Expand All @@ -99,6 +99,12 @@ def save(connection_name, db_type, host, user, password, database):
"""
if db_type == 'sqlite':
user = password = '' # SQLite doesn't use username and password
else:
if not user:
user = click.prompt('Enter username')
if not password:
password = click.prompt('Enter password', hide_input=True, confirmation_prompt=True)

save_connection(connection_name, db_type, host, user, password, database)
click.echo(f"Connection '{connection_name}' saved successfully.")

Expand Down
11 changes: 8 additions & 3 deletions peepdb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
from dataclasses import dataclass
import logging

import click
import keyring
Expand All @@ -23,7 +24,8 @@ class KeySecurity():
KEYRING_USERNAME = "PEEP_DB_KEY"
KEYRING_SERVICE_NAME = "PEEP_DB"


logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@cached(cache=TTLCache(maxsize=1024, ttl=600))
def generate_key_from_password(salt):
password = click.prompt("Please entry the password to encrpyt/decrpty DB passwords",
Expand Down Expand Up @@ -79,6 +81,8 @@ def decrypt(token: str) -> str:


def save_connection(name, db_type, host, user, password, database):
logger.debug(f"Saving connection: {name}, {db_type}, {host}, {user}, {'*' * len(password) if password else 'None'}, {database}")

if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR)

Expand All @@ -87,11 +91,10 @@ def save_connection(name, db_type, host, user, password, database):
with open(CONFIG_FILE, "r") as f:
config = json.load(f)

# For SQLite, we don't need to encrypt the host (file path)
if db_type == 'sqlite':
config[name] = {
"db_type": db_type,
"host": host,
"host": host, # Store the file path directly for SQLite
"database": database
}
else:
Expand All @@ -105,6 +108,8 @@ def save_connection(name, db_type, host, user, password, database):

with open(CONFIG_FILE, "w") as f:
json.dump(config, f)

logger.debug("Connection saved successfully")


def get_connection(name):
Expand Down

0 comments on commit 94ff051

Please sign in to comment.