From c3a84ecbfc5759b20147a187d67ef667c0257349 Mon Sep 17 00:00:00 2001 From: jmbardallo Date: Mon, 16 Mar 2026 14:01:42 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20ApiKeyAuthenticator=20para=20acc?= =?UTF-8?q?eso=20seguro=20de=20clientes=20-=20Implementado=20ApiKeyAuthent?= =?UTF-8?q?icator=20para=20proteger=20el=20endpoint=20/commands=20usando?= =?UTF-8?q?=20la=20cabecera=20X-API-KEY.=20-=20A=C3=B1adido=20soporte=20pa?= =?UTF-8?q?ra=20OGCLIENT=5FAPI=5FKEY=20en=20env.json=20para=20centralizar?= =?UTF-8?q?=20su=20gesti=C3=B3n=20desde=20el=20oggui.=20-=20Modificado=20P?= =?UTF-8?q?ostAction=20(PxeBootFile)=20para=20inyectar=20la=20API=20Key=20?= =?UTF-8?q?en=20el=20JSON=20enviado=20a=20ogBoot.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 ++ config/packages/security.yaml | 3 + env.json | 3 +- .../OgBoot/PxeBootFile/PostAction.php | 3 +- src/Security/ApiKeyAuthenticator.php | 77 +++++++++++++++++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/Security/ApiKeyAuthenticator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc644e..82143f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ # Changelog +## [1.2.4] - 2026-03-16 +### Added +- Se ha implementado un nuevo [ApiKeyAuthenticator] para dotar de acceso seguro al endpoint `/commands` mediante la cabecera `X-API-KEY`. +- Incorporación de la variable global de entorno `OGCLIENT_API_KEY` en env.json para gestionar el token de acceso de clientes desde la interfaz gráfica. +### Changed +- Se ha modificado el controlador PostAction de `PxeBootFile` para inyectar y propagar la clave `ogclient_api_key` al payload que se envía a `ogBoot` durante la creación del arranque. + ## [1.2.3] - 2026-03-11 ### Added - Se ha añadido una nueva funcionalidad en el DTO `MenuInput` (campo `menuHtmlContent`) para recibir la plantilla HTML íntegra del menú codificada en Base64. diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 6466847..5a114f1 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -22,6 +22,8 @@ security: success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure jwt: ~ + custom_authenticators: + - App\Security\ApiKeyAuthenticator refresh_jwt: check_path: refresh_token @@ -29,6 +31,7 @@ security: - { path: ^/$, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI docs - { path: ^/auth/login, roles: PUBLIC_ACCESS } + - { path: ^/commands, roles: [ROLE_OGCLIENT_API, ROLE_ADMIN] } - { path: ^/opengnsys/rest, roles: PUBLIC_ACCESS } - { path: ^/og-repository/webhook, roles: PUBLIC_ACCESS } - { path: ^/og-lives/install/webhook, roles: PUBLIC_ACCESS } diff --git a/env.json b/env.json index c40cd36..c62e201 100644 --- a/env.json +++ b/env.json @@ -10,6 +10,7 @@ "SSL_ENABLED": "false", "OG_BOOT_IP": "127.0.0.1", "OG_BOOT_API_PORT": "8082", - "OG_BOOT_PXE_PORT": "8082" + "OG_BOOT_PXE_PORT": "8082", + "OGCLIENT_API_KEY": "ogclient-super-secret-token" } } \ No newline at end of file diff --git a/src/Controller/OgBoot/PxeBootFile/PostAction.php b/src/Controller/OgBoot/PxeBootFile/PostAction.php index 725dad7..46d9a8b 100644 --- a/src/Controller/OgBoot/PxeBootFile/PostAction.php +++ b/src/Controller/OgBoot/PxeBootFile/PostAction.php @@ -58,7 +58,8 @@ class PostAction extends AbstractOgBootController 'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), 'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), 'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), - 'resolution' => $client->getResolution() ?? '791' + 'resolution' => $client->getResolution() ?? '791', + 'ogclient_api_key' => $_ENV['OGCLIENT_API_KEY'] ?? 'ogclient-super-secret-token' ] ]; diff --git a/src/Security/ApiKeyAuthenticator.php b/src/Security/ApiKeyAuthenticator.php new file mode 100644 index 0000000..c954021 --- /dev/null +++ b/src/Security/ApiKeyAuthenticator.php @@ -0,0 +1,77 @@ +projectDir = $kernel->getProjectDir(); + } + + /** + * Called on every request to decide if this authenticator should be used for the request. + * Returning `false` will cause this authenticator to be skipped. + */ + public function supports(Request $request): ?bool + { + return $request->headers->has('X-API-KEY'); + } + + public function authenticate(Request $request): Passport + { + $apiToken = $request->headers->get('X-API-KEY'); + + $envFile = $this->projectDir . DIRECTORY_SEPARATOR . 'env.json'; + if (!is_file($envFile)) { + throw new CustomUserMessageAuthenticationException('Configuration file not found.'); + } + + $config = json_decode(file_get_contents($envFile), true); + $expectedToken = $config['vars']['OGCLIENT_API_KEY'] ?? null; + + if (null === $expectedToken || $apiToken !== $expectedToken) { + throw new CustomUserMessageAuthenticationException('Invalid API Token.'); + } + + // Usamos tu propia entidad User para crear un usuario virtual "al vuelo" + return new SelfValidatingPassport(new UserBadge('ogclient_api_user', function() { + $apiUser = new User(); + $apiUser->setUsername('ogclient_api_user'); + $apiUser->setRoles(['ROLE_OGCLIENT_API']); + $apiUser->setPassword(''); // Evitamos nulos en propiedades requeridas si las hay + + return $apiUser; + })); + } + + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response + { + // on success, let the request continue + return null; + } + + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response + { + $data = [ + 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()) + ]; + + return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); + } +} -- 2.40.1