import requests import json import argparse import sys import os import urllib3 from dotenv import load_dotenv import io import time load_dotenv(override=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" 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, "password": password } files = { "params": (None, json.dumps(auth_params), "application/json"), "certificate": ("certificate.pem", certificate, "application/octet-stream") } print(f"Authenticating against {auth_url} with {username} {password} {certificate}") try: response = requests.post(auth_url, files=files, verify=False, timeout=10) response.raise_for_status() # Raise exception for 4XX/5XX responses # Extract token from response auth_data = response.json() token = auth_data.get("token") if not token: print("Authentication failure!") print("Authentication success!", end = " ") return token except requests.exceptions.RequestException as e: print(f"Authentication failed: {e}") if hasattr(e, 'response') and e.response: print(f"Response: {e.response.text}") raise def set_ssh_status(base_url, token): """ Set SSH status (enable) using the provided JWT token. """ ssh_url = f"{base_url}/api/ssh" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {token}" } # Set new SSH status payload = { "currentStatus": True } try: response = requests.post(ssh_url, headers=headers, json=payload, verify=False, timeout=10) response.raise_for_status() print(f"SSH activation success!") return True except requests.exceptions.RequestException as e: print("SSH activation failure!") print(f"Exception: {e}") if hasattr(e, 'response') and e.response: print(f"Response: {e.response.text}") return False def activate_ssh(ip_address): # Ensure the URL uses HTTPS url = ip_address if not url.startswith("https://"): # Convert http:// to https:// or add https:// if no protocol specified if url.startswith("http://"): url = "https://" + url[7:] else: url = "https://" + url if not url.endswith(":9080"): url = url + ":9080" print(f"Target address: {url}") verify_ssl = False if not verify_ssl: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) token = authenticate(url) if not token: return time.sleep(3) set_ssh_status(url, token)