diff --git a/api/repo_api.py b/api/repo_api.py index e1dc4bc..c2827fc 100644 --- a/api/repo_api.py +++ b/api/repo_api.py @@ -2281,6 +2281,38 @@ def git_backup_repository_task(repo, params, job_id): recall_ogcore(data) +def git_add_sshkey_task(oglive, description, job_id): + module = _load_installer() + print(f"Got {module}") + OpengnsysGitInstaller = getattr(module, 'OpengnsysGitInstaller') + installer = OpengnsysGitInstaller() + + results = installer.add_ssh_key_from_squashfs(oglive_file = oglive) + keys_added = 0 + keys_existed = 0 + keys_failed = 0 + + for status, message in results: + if status == 200 or status == 201: + keys_added = keys_added + 1 + elif status == 422: + keys_existed = keys_existed + 1 + else: + keys_failed = keys_failed + 1 + journal.send(f"Unrecognized reply from forgejo: code {status}, content {message}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + + data = { + 'job_id': job_id, + 'keys_added' : keys_added, + 'keys_failed' : keys_failed, + 'keys_existed' : keys_existed, + 'output' : message + } + + journal.send(f"Calling function 'recall_ogcore' (JOB_ID: {job_id}, SUCCESS: True)", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + recall_ogcore(data) + @app.route("/ogrepository/v1/git/repositories", methods=['GET']) def git_list_repositories(): @@ -2737,6 +2769,75 @@ def git_delete_tag(repo, tag): +@app.route("/ogrepository/v1/git/ssh_key", methods=['POST']) +def git_add_sshkey(): + """Add a SSH key + + Args: + ssh_key (str): The SSH key + oglive (str): URL to an oglive image from which to extract the key. May be a local file or HTTP. + description (str): Description for the SSH key + + Returns: + Response: A JSON response containing a list of tag names or an error message if the key can't be added. + - 200: A JSON object with a "status" key containing "added" + + """ + + data = request.json + + if data is None: + journal.send(f"Can't add SSH keys, POST data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if not "description" in data: + data["description"] = "" + + if not ("ssh_key" in data or "oglive" in data): + journal.send(f"Can't add SSH keys, either ssh_key or oglive is required", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing, specify ssh_key or oglive"}), 400 + + + + module = _load_installer() + print(f"Got {module}") + OpengnsysGitInstaller = getattr(module, 'OpengnsysGitInstaller') + installer = OpengnsysGitInstaller() + + if "oglive" in data: + job_id = f"GitSshKey_{''.join(random.choice('0123456789abcdef') for char in range(8))}" + threading.Thread(target=git_add_sshkey_task, args=(data["oglive"], data["description"], job_id)).start() + return jsonify({ + "success": True, + "output": "Extracting key from ogLive...", + "job_id": job_id + }), 200 + else: + status, content = installer.add_forgejo_sshkey(data["ssh_key"], data["description"]) + message = "Result unrecognized" + success = False + httpcode = 500 + + if status == 200 or status == 201: + message = "SSH key added" + success = True + httpcode = 200 + elif status == 422: + message = "SSH key already existed" + success = True + httpcode = 200 + else: + message = "Unrecognized reply from forgejo" + success = False + httpcode = status + journal.send(f"Unrecognized reply from forgejo: code {status}, content {content}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + return jsonify({ + "success": success, + "output": message, + }), httpcode + + # -------------------------------------------------------------------------------------------- diff --git a/api/swagger.yaml b/api/swagger.yaml index a619772..18e1ba9 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -1971,6 +1971,64 @@ paths: # # ----------------------------------------------------------- + /ogrepository/v1/git/ssh_key: + post: + summary: "Agregar clave de SSH de ogLive" + description: | + Agrega una clave de SSH que va a usarse desde el ogLive + para interactuar con Git. + + Es necesario especificar **ssh_key** o **oglive**. + + Especificando **ssh_key** se agrega la cclave especificada directamente. + + Especificando **oglive** se descarga si es necesario el .iso, se monta, y se extrae la clave. + Esta acción se hace en el fondo, y se devuelve un job_id. + + tags: + - "Git" + parameters: + - name: JSON + in: body + required: true + description: | + * **ssh_key** - Clave de SSH (opcional) + * **oglive** - URL a ogLive (opcional, NO USAR DE MOMENTO) + * **description** - Descripcion (opcional) + schema: + type: object + properties: + ssh_key: + type: string + example: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINEOttwhJ+9THRZ1Zv/6QUwPUDq1X7opG9V7EFLVWxQV" + required: False + description: + type: string + example: "OgLive r20250518" + required: False + oglive: + type: string + example: "https://ognproject.evlt.uma.es/oglive/ogLive-noble-6.8.0-31-generic-amd64-r20250518.cf13a6d_20250519.iso" + required: False + responses: + "200": + description: "Exito" + schema: + type: object + properties: + success: + type: boolean + example: True + required: True + output: + type: string + example: "SSH key added" + required: True + job_id: + type: string + example: "GitSshKey_873f353f" + required: False + /ogrepository/v1/git/repositories: get: summary: "Obtener lista de repositorios"