Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env var not usable with elasticsearch user and password #426

Open
fmarchalemisys opened this issue Dec 1, 2021 · 3 comments
Open

Env var not usable with elasticsearch user and password #426

fmarchalemisys opened this issue Dec 1, 2021 · 3 comments

Comments

@fmarchalemisys
Copy link

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
Copy link

Mokolos commented Dec 2, 2021

this may fix the issue here check on dbrumann solution.

@fmarchalemisys
Copy link
Author

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
Copy link

Amunak commented Dec 2, 2021

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants