diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 5e2315b..e0670c8 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -29,6 +29,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: ^/opengnsys/rest/, roles: PUBLIC_ACCESS } - { path: ^/auth/refresh, roles: PUBLIC_ACCESS } - { path: ^/, roles: IS_AUTHENTICATED_FULLY } diff --git a/src/Controller/OgAgent/OgAdmClientController.php b/src/Controller/OgAgent/OgAdmClientController.php new file mode 100644 index 0000000..0b50352 --- /dev/null +++ b/src/Controller/OgAgent/OgAdmClientController.php @@ -0,0 +1,153 @@ +toArray(); + $requiredFields = ['iph', 'cfg']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + $responseData = [ + 'res' => 1, + 'ido' => $data['ido'] ?? 42, + 'npc' => $data['npc'] ?? 42, + 'che' => 42, + 'exe' => 42, + 'ida' => $data['ida'] ?? 42, + 'idc' => $data['idc'] ?? 42, + ]; + + return new JsonResponse($responseData, Response::HTTP_OK); + } + + + #[Route('/opengnsys/rest/__ogAdmClient/AutoexecCliente', methods: ['POST'])] + public function autoexecCliente(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'exe']; + + $responseData = ['res' => 0]; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + $fileAutoExec = '/tmp/Sautoexec-' . $data['iph']; + try { + $fileExe = fopen($fileAutoExec, 'w'); + fwrite($fileExe, "nfn=popup\x0dtitle=my title\x0dmessage=my message"); + fclose($fileExe); + } catch (\Exception $e) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + + $responseData = [ + 'res' => 1, + 'nfl' => $fileAutoExec + ]; + + return new JsonResponse($responseData, Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/enviaArchivo', methods: ['POST'])] + public function enviaArchivo(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['nfl']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + $nfl = $data['nfl']; + //$nfl = $this->getParameter('kernel.project_dir') . '/file.txt'; + + if (!file_exists($nfl)) { + return new JsonResponse(['message' => 'File not found'], Response::HTTP_NOT_FOUND); + } + + $contents = file_get_contents($nfl); + + return new JsonResponse(['contents' => base64_encode($contents)], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/ComandosPendientes', methods: ['POST'])] + public function comandosPendientes(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'ido']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Simula que el cliente existe + if (!$this->clienteExistente($data['iph'])) { + return new JsonResponse(['message' => 'Client not found'], Response::HTTP_NOT_FOUND); + } + + // Simula obtener comandos pendientes + $param = $this->buscaComandos($data['ido']); + + if (!$param) { + return new JsonResponse(['nfn' => 'NoComandosPtes'], Response::HTTP_OK); + } + + return new JsonResponse($param, Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/__ogAdmClient/DisponibilidadComandos', methods: ['POST'])] + public function disponibilidadComandos(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['iph', 'tpc']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + if (!$this->clienteExistente($data['iph'])) { + return new JsonResponse(['message' => 'Client not found'], Response::HTTP_NOT_FOUND); + } + + return new JsonResponse(((object) null), Response::HTTP_OK); + } + + private function clienteExistente($iph) + { + return true; + } + + private function buscaComandos($ido): null + { + return null; + } +} \ No newline at end of file diff --git a/src/Controller/OgAgent/OgAgentController.php b/src/Controller/OgAgent/OgAgentController.php new file mode 100644 index 0000000..9fc6edd --- /dev/null +++ b/src/Controller/OgAgent/OgAgentController.php @@ -0,0 +1,84 @@ +toArray(); + $requiredFields = ['mac', 'ip', 'secret', 'ostype', 'osversion', 'agent_version']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/stopped', methods: ['POST'])] + public function agentStopped(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['mac', 'ip', 'ostype', 'osversion']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/loggedin', methods: ['POST'])] + public function agentLoggedIn(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['ip', 'user', 'language', 'session', 'ostype', 'osversion']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } + + #[Route('/opengnsys/rest/ogagent/loggedout', methods: ['POST'])] + public function agentLoggedOut(Request $request): JsonResponse + { + $data = $request->toArray(); + $requiredFields = ['ip', 'user']; + + foreach ($requiredFields as $field) { + if (!isset($data[$field])) { + return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST); + } + } + + // Procesar los datos recibidos si es necesario + + return new JsonResponse([], Response::HTTP_OK); + } +} diff --git a/src/OpenApi/OpenApiFactory.php b/src/OpenApi/OpenApiFactory.php index 9e0ae6f..675a6d6 100644 --- a/src/OpenApi/OpenApiFactory.php +++ b/src/OpenApi/OpenApiFactory.php @@ -20,7 +20,7 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface $this->addRefreshToken($openApi); $this->addSearchEndpoint($openApi); - + $this->addOgAgentEndpoints($openApi); return $openApi; } @@ -126,4 +126,580 @@ final readonly class OpenApiFactory implements OpenApiFactoryInterface ]) )); } + + private function addOgAgentEndpoints(OpenApi $openApi): void + { + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/started', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentStarted')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that an agent has started') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of agent start') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'mac' => [ + 'type' => 'string', + 'example' => '00:01:02:03:04:05', + ], + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'secret' => [ + 'type' => 'string', + 'example' => 'nug05l0ipc0lzl578uu5ohm1a074v4t2', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + 'agent_version' => [ + 'type' => 'string', + 'example' => '1.3.0', + ], + ], + 'required' => ['mac', 'ip', 'secret', 'ostype', 'osversion', 'agent_version'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/stopped', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentStopped')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that an agent has stopped') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of agent stop') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'mac' => [ + 'type' => 'string', + 'example' => '00:01:02:03:04:05', + ], + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + ], + 'required' => ['mac', 'ip', 'ostype', 'osversion'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/loggedin', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentLoggedIn')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that a user has logged in') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of user login') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'user' => [ + 'type' => 'string', + 'example' => 'student', + ], + 'language' => [ + 'type' => 'string', + 'example' => 'en_GB', + ], + 'session' => [ + 'type' => 'string', + 'example' => 'x11', + ], + 'ostype' => [ + 'type' => 'string', + 'enum' => ['Linux', 'MacOS', 'Windows'], + ], + 'osversion' => [ + 'type' => 'string', + 'example' => 'Debian GNU/Linux 12 (bookworm)', + ], + ], + 'required' => ['ip', 'user', 'language', 'session', 'ostype', 'osversion'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/ogagent/loggedout', (new Model\PathItem())->withPost( + (new Model\Operation('postAgentLoggedOut')) + ->withTags(['OS agent']) + ->withSummary('Receive notification that a user has logged out') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Notification of user logout') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'ip' => [ + 'type' => 'string', + 'example' => '192.168.2.11', + ], + 'user' => [ + 'type' => 'string', + 'example' => 'student', + ], + ], + 'required' => ['ip', 'user'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/InclusionCliente', (new Model\PathItem())->withPost( + (new Model\Operation('postInclusionCliente')) + ->withTags(['OgLive agent']) + ->withSummary('Add client to the list of known ones') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('Inclusion cliente request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'cfg' => [ + 'type' => 'string', + ], + ], + 'required' => ['iph', 'cfg'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/InclusionClienteRes'], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/AutoexecCliente', (new Model\PathItem())->withPost( + (new Model\Operation('postAutoexecCliente')) + ->withTags(['OgLive agent']) + ->withSummary('Create and return autoexec file for client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('AutoexecCliente request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'exe' => [ + 'type' => 'string', + ], + ], + 'required' => ['iph', 'exe'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, autoexec file created', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'res' => [ + 'type' => 'integer', + 'example' => 1, + ], + 'nfl' => [ + 'type' => 'string', + 'example' => '/tmp/Sautoexec-00:01:02:03:04:05', + ], + ], + 'required' => ['res', 'nfl'], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_INTERNAL_SERVER_ERROR => [ + 'description' => 'Error creating file', + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/enviaArchivo', (new Model\PathItem())->withPost( + (new Model\Operation('postEnviaArchivo')) + ->withTags(['OgLive agent']) + ->withSummary('Send the contents of a file') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('EnviaArchivo request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'nfl' => [ + 'type' => 'string', + ], + ], + 'required' => ['nfl'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, file content returned', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'contents' => [ + 'type' => 'string', + ], + ], + 'required' => ['contents'], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'File not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/ComandosPendientes', (new Model\PathItem())->withPost( + (new Model\Operation('postComandosPendientes')) + ->withTags(['OgLive agent']) + ->withSummary('Retrieve pending commands for a client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('ComandosPendientes request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + ], + 'required' => ['iph', 'ido'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, pending commands returned', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'nfn' => [ + 'type' => 'string', + 'example' => 'NoComandosPtes', + ], + 'comandos' => [ + 'type' => 'array', + 'items' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'Client not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ]) + )); + $openApi + ->getPaths() + ->addPath('/opengnsys/rest/__ogAdmClient/DisponibilidadComandos', (new Model\PathItem())->withPost( + (new Model\Operation('postDisponibilidadComandos')) + ->withTags(['OgLive agent']) + ->withSummary('Check command availability for a client') + ->withRequestBody( + (new Model\RequestBody()) + ->withDescription('DisponibilidadComandos request') + ->withContent(new \ArrayObject([ + 'application/json' => new Model\MediaType(new \ArrayObject(new \ArrayObject([ + 'type' => 'object', + 'properties' => [ + 'iph' => [ + 'type' => 'string', + ], + 'ido' => [ + 'type' => 'integer', + ], + 'npc' => [ + 'type' => 'string', + ], + 'idc' => [ + 'type' => 'integer', + ], + 'ida' => [ + 'type' => 'integer', + ], + 'tpc' => [ + 'type' => 'integer', + ], + ], + 'required' => ['iph', 'tpc'], + ]))) + ])) + ->withRequired(true) + ) + ->withResponses([ + Response::HTTP_OK => [ + 'description' => 'Success, command availability checked', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/EmptyObj'], + ], + ], + ], + Response::HTTP_BAD_REQUEST => [ + 'description' => 'Missing parameter', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + Response::HTTP_NOT_FOUND => [ + 'description' => 'Client not found', + 'content' => [ + 'application/json' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'message' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + ]) + )); + } } \ No newline at end of file