symfony / monolog-bundle

Symfony Monolog Bundle
symfony.com
MIT License
2.89k stars 230 forks source link

Env var not usable with elasticsearch user and password #426

Open fmarchalemisys opened 2 years ago

fmarchalemisys commented 2 years ago

This is not working in monolog.yaml:

monolog:
    handlers:
        elk:
            type: elasticsearch
            formatter: elastica_formatter
            level: INFO
            elasticsearch:
                transport: https
                host: "%env(ELK_HOST)%"
                port: 9243
                user: "%env(ELK_USER)%"
                password: "%env(ELK_PASSWORD)%"

The error message is

Environment variables "ELK_USER", "ELK_PASSWORD" are never used. Please, check your container's configuration.

It works if the user and password are used in plain instead of environment variables.

Dumping the two variables here shows:

"env_1b9e6838d53f59cc_ELK_USER_b09ad074b74ae33fba693bb6c6ba7c05"
"env_1b9e6838d53f59cc_ELK_PASSWORD_bbeec1d8a5b7c8ddd4af0a40ca7c4d8b"

I believe the Authorization string should be build using:

$user = $container->resolveEnvPlaceholders($handler['elasticsearch']['user'], true)
$password = $container->resolveEnvPlaceholders($handler['elasticsearch']['password'], true)
…
'Authorization' => 'Basic ' . base64_encode($user . ':' . $password)

Or am I missing something?

Mokolos commented 2 years ago

this may fix the issue here check on dbrumann solution.

fmarchalemisys commented 2 years ago

As far as I can tell, dbrumann's PR fix the same issue but only for the level.

I'm faced with the issue on the user name and password.

Amunak commented 2 years ago

I have the same issue.

In the meantime you can register a CompilerPass to fix this:

<?php

declare(strict_types=1);

namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use function array_merge;
use function base64_encode;

class MonologElasticFixingCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $configs = $container->getExtensionConfig('monolog');
        $config = array_merge(...$configs);
        if (!isset($config['handlers'])) {
            return;
        }

        foreach ($config['handlers'] as $name => $handler) {
            if (isset($handler['elasticsearch']['id'])) {
                continue;
            }

            if (!isset($handler['elasticsearch']['user'], $handler['elasticsearch']['password'])) {
                continue;
            }

            $definition = $container->getDefinition('monolog.handler.' . $name);
            /** @var Definition $clientDefinition */
            $clientDefinition = $definition->getArgument(0);

            $arg = $container->resolveEnvPlaceholders($clientDefinition->getArgument(0), true);
            /** @noinspection SlowArrayOperationsInLoopInspection */
            $arg = array_merge($arg, [
                'headers' => [
                    'Authorization' => 'Basic ' . base64_encode($container->resolveEnvPlaceholders($handler['elasticsearch']['user'], true) . ':' . $container->resolveEnvPlaceholders($handler['elasticsearch']['password'], true))
                ],
            ]);

            $clientDefinition->setArgument(0, $arg);
        }
    }
}

Note that this will compile all the Elastica Client arguments into your Container, which might change behavior slightly (changing env vars without rebuilding the container will not change the actual connection arguments), and it also exposes the hostname, port (and password, but that is true if you directly use the config as well) in the built container.

(Sorry for posting this for like the third time, I managed to botch the code before).