64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
# tenant/utils/common.py
|
|
import getpass
|
|
import re
|
|
from OpenSSL import crypto
|
|
|
|
TYPE_RSA = crypto.TYPE_RSA
|
|
TYPE_DSA = crypto.TYPE_DSA
|
|
key = crypto.PKey()
|
|
|
|
|
|
def get_secure_password(prompt="Enter password: "):
|
|
# Use getpass to securely input a password without displaying it
|
|
return getpass.getpass(prompt=prompt)
|
|
|
|
|
|
def generate_key(keyfile):
|
|
print("Generating Key: ")
|
|
key.generate_key(TYPE_RSA, 4096)
|
|
f = open(keyfile, "wb")
|
|
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
|
|
|
|
|
|
def generate_csr(csrfile, ingress):
|
|
req = crypto.X509Req()
|
|
req.get_subject().CN = ingress
|
|
req.get_subject().C = "DE"
|
|
req.get_subject().ST = "Nordrhein-Westfalen"
|
|
req.get_subject().L = "Bonn"
|
|
req.get_subject().O = "Informationstechnikzentrum Bund (ITZBund)"
|
|
req.get_subject().OU = "Laas/DaaS"
|
|
req.get_subject().emailAddress = "logging@itzbund.de"
|
|
req.set_pubkey(key)
|
|
req.sign(key, "sha256")
|
|
f = open(csrfile, "wb")
|
|
f.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))
|
|
f.close()
|
|
print("CSR file generated")
|
|
|
|
|
|
def calculate_java_settings(memory_limit):
|
|
# Use regex to extract the value and unit
|
|
match = re.match(r"(\d*\.?\d+)([GgMm])i?", memory_limit)
|
|
|
|
if match:
|
|
value, unit = match.groups()
|
|
|
|
# Convert the value to an integer
|
|
value = float(value)
|
|
|
|
if unit.lower() == "g":
|
|
# Convert GiB to MiB
|
|
value *= 1024
|
|
|
|
# Calculate Xmx value
|
|
xmx_value = value * 0.75
|
|
|
|
# Build the Java Xmx settings string without decimal part if it's ".0"
|
|
value = f"{int(xmx_value) if xmx_value.is_integer() else xmx_value}m"
|
|
java_settings = "-Xmx" + value + " " + "-Xms" + value
|
|
return java_settings
|
|
else:
|
|
# If the unit is neither "Gi" nor "m", return an error message or handle accordingly
|
|
return "Unsupported memory unit"
|