fix(example): log CRON and Command Invocations without the secretUnverified
In prod without Monolog, Symfony's fallback logger writes to stderr from the error level only, so the CRON Invocation line never reached Cockpit. It is lowered to info, and app:greet logs its greeting, as a CRON trigger drops the Function Response holding the command output. POST / logs the argument names instead of the body, which holds the CRON secret when a Command Invocation is rejected.
0ac6fa3 parent: 61ed3cb modified
example/config/services.yaml +6 -0 | @@ -7,3 +7,9 @@ services: | ||
| 7 | 7 | resource: '../src/' |
| 8 | 8 | exclude: |
| 9 | 9 | - '../src/Kernel.php' |
| 10 | + | |
| 11 | + # Symfony's fallback logger writes to stderr, which reaches Cockpit, but from the error level only: | |
| 12 | + # lowered to info so the CRON and Command Invocations show up there, without requiring Monolog. | |
| 13 | + logger: | |
| 14 | + class: Symfony\Component\HttpKernel\Log\Logger | |
| 15 | + arguments: ['info'] | |
| @@ -7,3 +7,9 @@ services: | |||
| 7 | resource: '../src/' | 7 | resource: '../src/' |
| 8 | exclude: | 8 | exclude: |
| 9 | - '../src/Kernel.php' | 9 | - '../src/Kernel.php' |
| 10 | + | ||
| 11 | + # Symfony's fallback logger writes to stderr, which reaches Cockpit, but from the error level only: | ||
| 12 | + # lowered to info so the CRON and Command Invocations show up there, without requiring Monolog. | ||
| 13 | + logger: | ||
| 14 | + class: Symfony\Component\HttpKernel\Log\Logger | ||
| 15 | + arguments: ['info'] | ||
modified
example/src/Command/GreetCommand.php +7 -0 | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace App\Command; |
| 4 | 4 | |
| 5 | +use Psr\Log\LoggerInterface; | |
| 5 | 6 | use Symfony\Component\Console\Attribute\Argument; |
| 6 | 7 | use Symfony\Component\Console\Attribute\AsCommand; |
| 7 | 8 | use Symfony\Component\Console\Attribute\Option; |
| @@ -12,6 +13,10 @@ use Symfony\Component\Console\Style\SymfonyStyle; | ||
| 12 | 13 | #[AsCommand('app:greet', 'Greets someone, or fails on demand to show how a failed command is reported')] |
| 13 | 14 | final class GreetCommand |
| 14 | 15 | { |
| 16 | + public function __construct(private readonly LoggerInterface $logger) | |
| 17 | + { | |
| 18 | + } | |
| 19 | + | |
| 15 | 20 | public function __invoke( |
| 16 | 21 | SymfonyStyle $io, |
| 17 | 22 | #[Argument] string $name = 'world', |
| @@ -20,6 +25,8 @@ final class GreetCommand | ||
| 20 | 25 | ): int { |
| 21 | 26 | $greeting = \sprintf('Hello %s!', $name); |
| 22 | 27 | $io->writeln($yell ? strtoupper($greeting) : $greeting); |
| 28 | + // A CRON trigger drops the Function Response holding the output: the log shows the command ran. | |
| 29 | + $this->logger->info('Command app:greet said: {greeting}', ['greeting' => $greeting]); | |
| 23 | 30 | if ($fail) { |
| 24 | 31 | $io->error('Failing on purpose.'); |
| 25 | 32 | |
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | namespace App\Command; | 3 | namespace App\Command; |
| 4 | 4 | ||
| 5 | +use Psr\Log\LoggerInterface; | ||
| 5 | use Symfony\Component\Console\Attribute\Argument; | 6 | use Symfony\Component\Console\Attribute\Argument; |
| 6 | use Symfony\Component\Console\Attribute\AsCommand; | 7 | use Symfony\Component\Console\Attribute\AsCommand; |
| 7 | use Symfony\Component\Console\Attribute\Option; | 8 | use Symfony\Component\Console\Attribute\Option; |
| @@ -12,6 +13,10 @@ use Symfony\Component\Console\Style\SymfonyStyle; | |||
| 12 | #[AsCommand('app:greet', 'Greets someone, or fails on demand to show how a failed command is reported')] | 13 | #[AsCommand('app:greet', 'Greets someone, or fails on demand to show how a failed command is reported')] |
| 13 | final class GreetCommand | 14 | final class GreetCommand |
| 14 | { | 15 | { |
| 16 | + public function __construct(private readonly LoggerInterface $logger) | ||
| 17 | + { | ||
| 18 | + } | ||
| 19 | + | ||
| 15 | public function __invoke( | 20 | public function __invoke( |
| 16 | SymfonyStyle $io, | 21 | SymfonyStyle $io, |
| 17 | #[Argument] string $name = 'world', | 22 | #[Argument] string $name = 'world', |
| @@ -20,6 +25,8 @@ final class GreetCommand | |||
| 20 | ): int { | 25 | ): int { |
| 21 | $greeting = \sprintf('Hello %s!', $name); | 26 | $greeting = \sprintf('Hello %s!', $name); |
| 22 | $io->writeln($yell ? strtoupper($greeting) : $greeting); | 27 | $io->writeln($yell ? strtoupper($greeting) : $greeting); |
| 28 | + // A CRON trigger drops the Function Response holding the output: the log shows the command ran. | ||
| 29 | + $this->logger->info('Command app:greet said: {greeting}', ['greeting' => $greeting]); | ||
| 23 | if ($fail) { | 30 | if ($fail) { |
| 24 | $io->error('Failing on purpose.'); | 31 | $io->error('Failing on purpose.'); |
| 25 | 32 | ||
modified
example/src/Controller/DemoController.php +4 -2 | @@ -26,9 +26,11 @@ final class DemoController | ||
| 26 | 26 | #[Route('/', methods: ['POST'])] |
| 27 | 27 | public function cron(Request $request, LoggerInterface $logger): JsonResponse |
| 28 | 28 | { |
| 29 | - $logger->info('CRON Invocation received', ['body' => $request->getContent()]); | |
| 29 | + $args = json_decode($request->getContent(), true); | |
| 30 | + // A body meant as a Command Invocation but rejected lands here: its secret must not reach the logs. | |
| 31 | + $logger->info('CRON Invocation received with the arguments {keys}', ['keys' => implode(',', array_keys(\is_array($args) ? $args : []))]); | |
| 30 | 32 | |
| 31 | - return new JsonResponse(['cron' => true, 'args' => json_decode($request->getContent(), true)]); | |
| 33 | + return new JsonResponse(['cron' => true, 'args' => $args]); | |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | #[Route('/hello', methods: ['GET'])] |
| @@ -26,9 +26,11 @@ final class DemoController | |||
| 26 | #[Route('/', methods: ['POST'])] | 26 | #[Route('/', methods: ['POST'])] |
| 27 | public function cron(Request $request, LoggerInterface $logger): JsonResponse | 27 | public function cron(Request $request, LoggerInterface $logger): JsonResponse |
| 28 | { | 28 | { |
| 29 | - $logger->info('CRON Invocation received', ['body' => $request->getContent()]); | 29 | + $args = json_decode($request->getContent(), true); |
| 30 | + // A body meant as a Command Invocation but rejected lands here: its secret must not reach the logs. | ||
| 31 | + $logger->info('CRON Invocation received with the arguments {keys}', ['keys' => implode(',', array_keys(\is_array($args) ? $args : []))]); | ||
| 30 | 32 | ||
| 31 | - return new JsonResponse(['cron' => true, 'args' => json_decode($request->getContent(), true)]); | 33 | + return new JsonResponse(['cron' => true, 'args' => $args]); |
| 32 | } | 34 | } |
| 33 | 35 | ||
| 34 | #[Route('/hello', methods: ['GET'])] | 36 | #[Route('/hello', methods: ['GET'])] |