CUBE configuration tool for INOX

This commit is contained in:
Quentin WEPHRE
2025-03-26 13:37:49 +01:00
parent 21c8c300c0
commit 62cb98f732
3 changed files with 189 additions and 35 deletions

View File

@@ -9,19 +9,17 @@ import io
load_dotenv(override=True)
def authenticate(base_url, username, password, certificate_path, verify_ssl=True):
def authenticate(base_url):
"""
Authenticate with the CUBE API using username, password and certificate.
Returns the JWT token if successful.
"""
auth_url = f"{base_url}/api/auth"
# Verify certificate file exists
# if not os.path.isfile(certificate_path):
# print(f"Error: Certificate file not found at: {certificate_path}")
# sys.exit(1)
# print(os.getenv("DEFAULT_CERTIFICATE").encode("utf-8"))
username = os.getenv("DEFAULT_CUBE_WEB_ADMIN_USER")
password = os.getenv("DEFAULT_CUBE_WEB_ADMIN_PASSWORD")
certificate = os.getenv("DEFAULT_CERTIFICATE").encode("utf-8")
# Prepare the multipart form data
auth_params = {
"login": username,
@@ -29,13 +27,12 @@ def authenticate(base_url, username, password, certificate_path, verify_ssl=True
}
files = {
"params": (None, json.dumps(auth_params), "application/json"),
"certificate": ("certificate.pem", os.getenv("DEFAULT_CERTIFICATE").encode("utf-8"), "application/octet-stream")
"certificate": ("certificate.pem", certificate, "application/octet-stream")
}
# print(files)
try:
print(f"Authenticating as {username}...")
response = requests.post(auth_url, files=files, verify=verify_ssl)
response = requests.post(auth_url, files=files, verify=False)
response.raise_for_status() # Raise exception for 4XX/5XX responses
# Extract token from response
@@ -44,7 +41,6 @@ def authenticate(base_url, username, password, certificate_path, verify_ssl=True
if not token:
print("Error: No token received in authentication response")
sys.exit(1)
print("Authentication successful.")
return token
@@ -53,9 +49,9 @@ def authenticate(base_url, username, password, certificate_path, verify_ssl=True
print(f"Authentication failed: {e}")
if hasattr(e, 'response') and e.response:
print(f"Response: {e.response.text}")
sys.exit(1)
def set_ssh_status(base_url, token, verify_ssl=True):
raise
def set_ssh_status(base_url, token):
"""
Set SSH status (enable) using the provided JWT token.
"""
@@ -71,7 +67,7 @@ def set_ssh_status(base_url, token, verify_ssl=True):
try:
print(f"Sending request to enable SSH...")
response = requests.post(ssh_url, headers=headers, json=payload, verify=verify_ssl)
response = requests.post(ssh_url, headers=headers, json=payload, verify=False)
response.raise_for_status()
print(f"SSH enabled successfully!")
@@ -84,21 +80,10 @@ def set_ssh_status(base_url, token, verify_ssl=True):
print(f"Response: {e.response.text}")
return False
def main():
parser = argparse.ArgumentParser(description="Manage SSH on CUBE application")
parser.add_argument("--url", help="Base URL of the CUBE API (e.g., https://cube-04fe12:9080)",
default="https://cube-04fe12:9080")
parser.add_argument("--username", help="Admin username with ROLE_SAFT_ADMIN permissions",
default=os.getenv("DEFAULT_CUBE_WEB_ADMIN_USER"))
parser.add_argument("--password", help="Admin password",
default=os.getenv("DEFAULT_CUBE_WEB_ADMIN_PASSWORD"))
parser.add_argument("--certificate", help="Path to mission certificate file",
default=os.getenv("DEFAULT_CERTIFICATE"))
args = parser.parse_args()
def activate_ssh(ip_address):
# Ensure the URL uses HTTPS
url = args.url
url = ip_address
if not url.startswith("https://"):
# Convert http:// to https:// or add https:// if no protocol specified
if url.startswith("http://"):
@@ -107,16 +92,15 @@ def main():
else:
url = "https://" + url
print(f"Adding HTTPS protocol: {url}")
if not url.endswith(":9080"):
url = url + ":9080"
verify_ssl = False
if not verify_ssl:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
token = authenticate(url, args.username, args.password, args.certificate, verify_ssl)
token = authenticate(url)
if not token:
return
set_ssh_status(url, token, verify_ssl)
if __name__ == "__main__":
main()
set_ssh_status(url, token)