Giter VIP home page Giter VIP logo

Comments (3)

xtrasmal avatar xtrasmal commented on July 19, 2024 2

Ok...I am going on with my life. I was accidentally inspired to take a closer look.

Goodluck 😅

from plastic.

xtrasmal avatar xtrasmal commented on July 19, 2024

Hi there,

I do not use this package, but was looking around the web for elasticsearch Laravel packages.

When I looked at you question, I could see that you are trying to connect to an instance that is using token based security(https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-tokens.html). After looking at the code of Plastic, I can tell you that it is not possible to set authentication headers at this moment, due to missing config options. Also even if you had these config options, there is no functionality to set these connection parameters.

Let me explain where to find it and how to fix it:

Take a look at: https://github.com/sleimanx2/plastic/blob/master/src/Connection.php#L247
There you can see this code:

    private function buildClient(array $config)
    {
        $client = ClientBuilder::create()
            ->setHosts($config['hosts']);
        if (isset($config['retries'])) {
            $client->setRetries($config['retries']);
        }
        if (isset($config['logging']) and $config['logging']['enabled'] == true) {
            $logger = ClientBuilder::defaultLogger($config['logging']['path'], $config['logging']['level']);
            $client->setLogger($logger);
        }
        return $client->build();
    }

It configures the Elasticsearch/Client by setting the folllowing:

  • $client->setHosts($config['hosts']);
  • $client->setRetries($config['retries']);
  • $client->setLogger($logger);

This is your problem; Because it needs run another method on $client....which it does not at the moment.

It should call the following as well:

  • $client->setConnectionParams($config['params]);

When you look at the code of the ClientBuilder's build method:
https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/ClientBuilder.php#L475
You can see that it set's the headers at some point.

        if (is_null($this->connectionFactory)) {
            if (is_null($this->connectionParams)) {
                $this->connectionParams = [];
            }
            // Make sure we are setting Content-Type and Accept (unless the user has explicitly
            // overridden it
            if (isset($this->connectionParams['client']['headers']) === false) {
                $this->connectionParams['client']['headers'] = [
                    'Content-Type' => ['application/json'],
                    'Accept' => ['application/json']
                ];
            } else {
                if (isset($this->connectionParams['client']['headers']['Content-Type']) === false) {
                    $this->connectionParams['client']['headers']['Content-Type'] = ['application/json'];
                }
                if (isset($this->connectionParams['client']['headers']['Accept']) === false) {
                    $this->connectionParams['client']['headers']['Accept'] = ['application/json'];
                }
            }
            $this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer);
        }

So I suggest that you do the following:

First Update your config

        /*
        |--------------------------------------------------------------------------
        | Hosts
        |--------------------------------------------------------------------------
        |
        | The most common configuration is telling the client about your cluster: how many nodes, their addresses and ports.
        | If no hosts are specified, the client will attempt to connect to localhost:9200.
        |
        */
        'hosts'   => [
            env('PLASTIC_HOST', '127.0.0.1:9200'),
        ],

       'params' => [
                 'client' => [
                              'headers' => [
                                   'Authentication' => [ 'Bearer <your_token>' ]
                              ]                  
                 ]
      ]

You need to log in and obtain an acces_token, then use laravel to update the config at runtime.
So that the Bearer token gets set.
see: https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-tokens.html

Second change the code

    private function buildClient(array $config)
    {
        $client = ClientBuilder::create()
            ->setHosts($config['hosts']);
        if (isset($config['retries'])) {
            $client->setRetries($config['retries']);
        }

        // ADD THIS
        if (isset($config['params'])) {
            $client->setConnectionParams($config['params']);
        }

        if (isset($config['logging']) and $config['logging']['enabled'] == true) {
            $logger = ClientBuilder::defaultLogger($config['logging']['path'], $config['logging']['level']);
            $client->setLogger($logger);
        }
        return $client->build();
    }

Then you can run the mapping:run with headers set.

@sleimanx2 @dennismende

from plastic.

xtrasmal avatar xtrasmal commented on July 19, 2024

I have made a pull request, which explains it: #172

The only thing you need to do is to set the config headers on runtime.
This might not be possible for existing config. Like this person experienced. https://laracasts.com/discuss/channels/laravel/change-environment-variables-on-the-fly-in-a-command

That person says that it was only possible to set something totally new. If that is the case, you should not update your config, but set it at runtime.

You should than create a new command, artisan make:command CustomRun
And add auth and config stuff:

<?php namespace App\Console\Mapping;

use Illuminate\Console\ConfirmableTrait;
use Sleimanx2\Plastic\Mappings\Mapper;
use Sleimanx2\Plastic\Console\Mapping\BaseCommand;
class CustomRun extends BaseCommand
{
    use ConfirmableTrait;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'mapping:customrun {--index=} {--database=} {--step} {--force}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'run the remaining mappings';

    /**
     * @var Mapper
     */
    private $mapper;


    private $authService;

    /**
     * Reset constructor.
     *
     * @param Mapper $mapper
     */
    public function __construct(Mapper $mapper, $authService)
    {
        parent::__construct();

        $this->mapper = $mapper;
        $this->authService = $authService;
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        if (!$this->confirmToProceed()) {
            return;
        }

        $token = $this->authService->accesstoken();

        Config::set('plastic.connection.params', [
                'client' => [
                    'headers' => [
                        'Authentication' => [ 'Bearer '.$token ]
                    ]
                ]
        ]);

        $this->prepareDatabase();

        $path = $this->getMappingPath();

        $this->mapper->run($path, [
            'step'  => $this->option('step'),
            'index' => $this->option('index'),
        ]);

        // Once the mapper has run we will grab the note output and send it out to
        // the console screen, since the mapper itself functions without having
        // any instances of the OutputInterface contract passed into the class.
        foreach ($this->mapper->getNotes() as $note) {
            $this->output->writeln($note);
        }
    }

    protected function prepareDatabase()
    {
        $this->mapper->setConnection($this->option('database'));

        if (!$this->mapper->repositoryExists()) {
            $options = ['--database' => $this->option('database')];

            $this->call('mapping:install', $options);
        }
    }
}

from plastic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.