2026-07-08 00:03:14 +03:00

40 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""setup_keyring.py — одноразово кладёт secret и static в keyring из env.
Имя сервиса keyring берётся из общего vpn_config.json (см. launch_vpn_gui.py)."""
import os
import sys
import json
import keyring
CONFIG_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "vpn_config.json")
DEFAULT_SERVICE = "openvpn-totp"
def keyring_service():
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
return data.get("keyring_service", DEFAULT_SERVICE)
except (OSError, ValueError):
pass
return DEFAULT_SERVICE
def main():
secret = os.environ.get("OVPN_TOTP_SECRET")
static = os.environ.get("OVPN_STATIC_PASS")
missing = [name for name, val in
(("OVPN_TOTP_SECRET", secret), ("OVPN_STATIC_PASS", static))
if not val]
if missing:
sys.exit(f"Не заданы переменные окружения: {', '.join(missing)}")
service = keyring_service()
keyring.set_password(service, "secret", secret)
keyring.set_password(service, "static", static)
print(f"Сохранено в keyring (сервис: {service}).")
if __name__ == "__main__":
main()