mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
Updated RSA key gen for OIDC
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
from django.core.checks import register, Info
|
||||
from django.conf import settings
|
||||
|
||||
from modoboa.core.utils import generate_rsa_private_key
|
||||
|
||||
|
||||
@register(deploy=True)
|
||||
def generate_rsa_private_key_check(app_configs, **kwargs):
|
||||
if generate_rsa_private_key(settings.BASE_DIR):
|
||||
return Info("Generated the RSA key for OIDC.")
|
||||
@@ -9,8 +9,6 @@ import string
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
import dj_database_url
|
||||
|
||||
import django
|
||||
@@ -19,6 +17,7 @@ from django.template import Context, Template
|
||||
from django.utils.encoding import smart_str
|
||||
|
||||
from modoboa.core.commands import Command
|
||||
from modoboa.core.utils import generate_rsa_private_key
|
||||
from modoboa.lib.api_client import ModoAPIClient
|
||||
from modoboa.lib.sysutils import exec_cmd
|
||||
|
||||
@@ -199,22 +198,6 @@ class DeployCommand(Command):
|
||||
extra_settings.append(extension[1])
|
||||
return extra_settings
|
||||
|
||||
def generate_rsa_private_key(self, storage_path: str):
|
||||
"""Generate RSA private key for OIDC support."""
|
||||
private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096,
|
||||
)
|
||||
pem = private_key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption(),
|
||||
)
|
||||
content = bytes(pem)
|
||||
content = content.replace(b"\n", b"\\n")
|
||||
with open(f"{storage_path}/.env", "wb") as fp:
|
||||
fp.write(b'OIDC_RSA_PRIVATE_KEY="' + content + b'"\n')
|
||||
|
||||
def handle(self, parsed_args):
|
||||
django.setup()
|
||||
management.call_command("startproject", parsed_args.name, verbosity=False)
|
||||
@@ -285,7 +268,7 @@ class DeployCommand(Command):
|
||||
)
|
||||
with open("%s/settings.py" % path, "w") as fp:
|
||||
fp.write(tpl)
|
||||
self.generate_rsa_private_key(parsed_args.name)
|
||||
generate_rsa_private_key(parsed_args.name)
|
||||
|
||||
shutil.copyfile("%s/urls.py.tpl" % self._templates_dir, "%s/urls.py" % path)
|
||||
os.mkdir("%s/media" % parsed_args.name)
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
"""Utility functions."""
|
||||
|
||||
from pkg_resources import parse_version
|
||||
import os
|
||||
import environ
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
@@ -49,3 +54,33 @@ def check_for_updates():
|
||||
update_avail = True
|
||||
break
|
||||
return update_avail, extensions
|
||||
|
||||
|
||||
def generate_rsa_private_key(storage_path: str) -> bool:
|
||||
"""Generate RSA private key for OIDC support.
|
||||
|
||||
:return: False if the key was not generated
|
||||
and True if it was."""
|
||||
|
||||
env_path = os.path.join(storage_path, ".env")
|
||||
|
||||
env = environ.Env(OIDC_RSA_PRIVATE_KEY=(str, "NONE"))
|
||||
environ.Env.read_env(env_path)
|
||||
|
||||
if env("OIDC_RSA_PRIVATE_KEY") != "NONE":
|
||||
return False
|
||||
|
||||
private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096,
|
||||
)
|
||||
pem = private_key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption(),
|
||||
)
|
||||
content = bytes(pem)
|
||||
content = content.replace(b"\n", b"\\n")
|
||||
with open(env_path, "wb") as fp:
|
||||
fp.write(b'OIDC_RSA_PRIVATE_KEY="' + content + b'"\n')
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user