DEV Community

qing
qing

Posted on Edited on

Build a Log Aggregation System with Python

Build a Log Aggregation System with Python

tags: python, devops, logging, tutorial


tags: python, devops, logging, tutorial


tags: python, devops, logging, tutorial


tags: python, devops, logging, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, devops, automation, tutorial


tags: python, devops, automation, tutorial


tags: python, redis, architecture, tutorial


tags: python, architecture, tutorial, advanced


tags: python, git, devops, tools


tags: python, git, devops, tools


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, devops, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, redis, architecture, tutorial


tags: python, bot, automation, tutorial


tags: python, security, tutorial, tools


tags: python, security, tutorial, tools


tags: python, security, tutorial, tools


Imagine typing your master key once and instantly unlocking passwords for every site you visit, all stored in a file that looks like gibberish to anyone who tries to peek. That’s the power of building your own password manager in Python: you get total control over your data, learn critical security concepts, and end up with a tool you can actually use today.

Security experts constantly warn against saving passwords in plain text, yet many developers still rely on browser storage or insecure notes. By building a custom manager, you enforce encryption by default and create a CLI tool that fits your workflow perfectly. Let’s dive into creating a secure, command-line password manager that encrypts your credentials using the industry-standard cryptography library.

Why Build Your Own Password Manager?

Before we write code, it’s worth understanding the security mindset behind this project. Most "password manager" tutorials skip the most critical part: encryption. Storing passwords in a .txt file is a security nightmare. If that file gets stolen, every account is compromised.

We’re going to use Fernet, a symmetric encryption scheme provided by the cryptography library. Fernet ensures that your data is authenticated and encrypted, meaning even if someone modifies the file, the system will detect the tampering and refuse to decrypt it. This approach gives you:

  • Confidentiality: Passwords are unreadable without the key.
  • Integrity: Any modification to the file breaks decryption.
  • Simplicity: A single Python script handles everything without complex databases.

Step 1: Install the Necessary Libraries

You’ll need Python 3.11 or later. If you haven’t installed it yet, download it from python.org. Once you’re set up, install the required libraries via pip:

pip install cryptography pyperclip
Enter fullscreen mode Exit fullscreen mode
  • cryptography: Provides the Fernet encryption module.
  • pyperclip: Allows your manager to automatically copy passwords to your clipboard (a handy feature for daily use).

Step 2: Design the Core Class Structure

We’ll build a PasswordManager class that handles key generation, file creation, encryption, and decryption. This keeps our code organized and reusable.

The class will have these main methods:

  • generate_key(): Creates a secure master key.
  • create_password_file(): Initializes an empty encrypted file.
  • add_password(site, password): Encrypts and saves a new password.
  • get_password(site): Decrypts and retrieves a password for a given site.
  • list_passwords(): Shows all stored site names.

Step 3: Write the Working Code

Here’s the complete, runnable script. Save this as password_manager.py and run it in your terminal.

import os
from cryptography.fernet import Fernet
from pyperclip import copy

MASTER_KEY_FILE = "master.key"
PASSWORD_FILE = "passwords.encrypted"

class PasswordManager:
    def __init__(self):
        self.key = self._load_or_generate_key()
        self.cipher = Fernet(self.key)

    def _load_or_generate_key(self):
        if os.path.exists(MASTER_KEY_FILE):
            with open(MASTER_KEY_FILE, "rb") as f:
                return f.read()
        else:
            key = Fernet.generate_key()
            with open(MASTER_KEY_FILE, "wb") as f:
                f.write(key)
            print("Master key generated and saved to master.key")
            return key

    def create_password_file(self):
        if not os.path.exists(PASSWORD_FILE):
            with open(PASSWORD_FILE, "wb") as f:
                f.write(self.cipher.encrypt(b""))
            print("Password file created.")

    def add_password(self, site, password):
        if not os.path.exists(PASSWORD_FILE):
            self.create_password_file()

        with open(PASSWORD_FILE, "rb") as f:
            encrypted_data = f.read()

        decrypted_data = self.cipher.decrypt(encrypted_data).decode()
        entries = {}
        if decrypted_data:
            for line in decrypted_data.splitlines():
                if ":" in line:
                    s, p = line.split(":", 1)
                    entries[s] = p

        entries[site] = password
        new_data = "\n".join(f"{s}:{p}" for s, p in entries.items())

        with open(PASSWORD_FILE, "wb") as f:
            f.write(self.cipher.encrypt(new_data.encode()))

        print(f"Password for {site} added successfully.")

    def get_password(self, site):
        if not os.path.exists(PASSWORD_FILE):
            print("No password file found. Add a password first.")
            return None

        with open(PASSWORD_FILE, "rb") as f:
            encrypted_data = f.read()

        try:
            decrypted_data = self.cipher.decrypt(encrypted_data).decode()
        except Exception:
            print("Error: Password file is corrupted or key is invalid.")
            return None

        if not decrypted_data:
            print("No passwords stored yet.")
            return None

        for line in decrypted_data.splitlines():
            if ":" in line:
                s, p = line.split(":", 1)
                if s == site:
                    print(f"Password for {site}: {p}")
                    copy(p)
                    print("Password copied to clipboard!")
                    return p

        print(f"No password found for {site}.")
        return None

    def list_passwords(self):
        if not os.path.exists(PASSWORD_FILE):
            print("No password file found.")
            return

        with open(PASSWORD_FILE, "rb") as f:
            encrypted_data = f.read()

        try:
            decrypted_data = self.cipher.decrypt(encrypted_data).decode()
        except Exception:
            print("Error: Password file is corrupted.")
            return

        if not decrypted_data:
            print("No passwords stored yet.")
            return

        print("Stored sites:")
        for line in decrypted_data.splitlines():
            if ":" in line:
                site = line.split(":")[0]
                print(f" - {site}")

if __name__ == "__main__":
    pm = PasswordManager()
    pm.create_password_file()

    while True:
        print("\n1. Add Password\n2. Get Password\n3. List Passwords\n4. Exit")
        choice = input("Choose an option: ")

        if choice == "1":
            site = input("Site name: ")
            password = input(