File size: 2,929 Bytes
3a1c55b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import shutil
import logging


from blossomtune_gradio.tls import TLSGenerator
from blossomtune_gradio import config as cfg


# Configure basic logging for the script
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def generate_dev_cert():
    """Generates a self-signed certificate for localhost development."""
    try:
        print("\n--- Generating self-signed certificate for localhost ---")
        cert_dir = "certificates_localhost"
        if os.path.exists(cert_dir):
            shutil.rmtree(cert_dir)

        generator = TLSGenerator(cert_dir=cert_dir)
        # Note: No existing CA is passed, so a new one will be created.
        generator.generate_server_certificate(
            common_name="localhost", sans=["localhost", "127.0.0.1"]
        )
        print(f"\n✅ Success! Self-signed CA and server cert created in '{cert_dir}'.")
    except Exception as e:
        print(f"\n❌ An error occurred: {e}")


def generate_prod_cert():
    """Generates a server certificate signed by the CA specified in config."""
    if not cfg.TLS_CA_KEY_PATH or not cfg.TLS_CA_CERT_PATH:
        print(
            "\n❌ Error: TLS_CA_KEY_PATH and TLS_CA_CERT_PATH are not set in your config."
        )
        print("Please configure the paths to your main CA certificate and key.")
        return

    try:
        print(
            f"\n--- Generating production certificate signed by {cfg.TLS_CA_CERT_PATH} ---"
        )
        common_name = input(
            "Enter the primary domain name for the server (e.g., fl.mydomain.com): "
        ).strip()
        if not common_name:
            print("Error: Domain name cannot be empty.")
            return

        generator = TLSGenerator(cert_dir=cfg.TLS_CERT_DIR)
        generator.generate_server_certificate(
            common_name=common_name,
            ca_key_path=cfg.TLS_CA_KEY_PATH,
            ca_cert_path=cfg.TLS_CA_CERT_PATH,
        )
        print(
            f"\n✅ Success! Server certificate and key created in '{cfg.TLS_CERT_DIR}'."
        )
    except Exception as e:
        print(f"\n❌ An error occurred: {e}")


def main():
    """Main function to run the interactive menu."""
    while True:
        print("\n===== BlossomTune TLS Certificate Generator =====")
        print("Select an option:")
        print("  1. Generate a self-signed 'localhost' certificate (for Development)")
        print("  2. Generate a server certificate using the main CA (for Production)")
        print("  3. Exit")

        choice = input("Enter your choice [1]: ").strip() or "1"

        if choice == "1":
            generate_dev_cert()
        elif choice == "2":
            generate_prod_cert()
        elif choice == "3":
            print("Exiting.")
            break
        else:
            print("Invalid choice. Please try again.")


if __name__ == "__main__":
    main()