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

XOL-6302 Symfony 4.4 upgrade #8

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ php:
- 7.4

env:
- SYMFONY_VERSION="2.1"
- SYMFONY_VERSION="3.0"
- SYMFONY_VERSION="3.4"
- SYMFONY_VERSION="4.0"
- SYMFONY_VERSION="4.4"

before_script:
- composer self-update
- composer --version
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-update
- composer install -n --dev --prefer-source
- composer install -n --prefer-source

script: phpunit --coverage-text --configuration phpunit.xml.dist
18 changes: 9 additions & 9 deletions Controller/ElasticsearchProxyController.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
namespace Xola\ElasticsearchProxyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Xola\ElasticsearchProxyBundle\Event\ElasticsearchProxyEvent;

class ElasticsearchProxyController extends Controller
class ElasticsearchProxyController extends AbstractController
{

public function proxyAction(Request $request, $index, $slug)
public function proxyAction(Request $request, $index, $slug, ContainerInterface $container): ?Response
{
// Check if requested elastic search index is allowed for querying
$config = $this->container->getParameter('xola_elasticsearch_proxy');
$config = $container->getParameter('xola_elasticsearch_proxy');
if (!in_array($index, $config['client']['indexes'])) {
throw new AccessDeniedHttpException();
}
Expand All @@ -31,7 +31,7 @@ public function proxyAction(Request $request, $index, $slug)
$dispatcher->dispatch('elasticsearch_proxy.before_elasticsearch_request', $event);
$data = $event->getQuery();
// Get url for elastic search
$url = $this->getElasticSearchUrl($request->getQueryString(), $index, $slug);
$url = $this->getElasticSearchUrl($request->getQueryString(), $index, $slug, $container);
$response = $this->makeRequestToElasticsearch($url, $request->getMethod(), $data);
$event->setResponse($response);
$dispatcher->dispatch('elasticsearch_proxy.after_elasticsearch_response', $event);
Expand All @@ -48,9 +48,9 @@ public function proxyAction(Request $request, $index, $slug)
*
* @return string
*/
public function getElasticSearchUrl($queryStr, $index, $slug)
public function getElasticSearchUrl($queryStr, $index, $slug, ContainerInterface $container): string
{
$config = $this->container->getParameter('xola_elasticsearch_proxy');
$config = $container->getParameter('xola_elasticsearch_proxy');

// Construct url for making elastic search request
$url = $config['client']['protocol'] . '://' . $config['client']['host'] . ':' . $config['client']['port'] . '/' . $index . '/' . $slug;
Expand All @@ -72,7 +72,7 @@ public function getElasticSearchUrl($queryStr, $index, $slug)
*
* @return Response
*/
public function makeRequestToElasticsearch($url, $method, $data)
public function makeRequestToElasticsearch($url, $method, $data): Response
{
$ch = curl_init();

Expand Down
5 changes: 3 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('xola_elasticsearch_proxy');
Expand All @@ -26,6 +26,7 @@ public function getConfigTreeBuilder()
->arrayNode('roles_skip_auth_filter')->prototype('scalar')->end()
->end()
->end();

return $treeBuilder;
}
}
}
10 changes: 1 addition & 9 deletions DependencyInjection/XolaElasticsearchProxyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@ class XolaElasticsearchProxyExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('config.yml');

$defaultConfig = $container->getParameter($this->getAlias());
array_splice($configs, 0, 0, array($defaultConfig));
$loader->load('services.yml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter($this->getAlias(), $config);
}

public function getAlias()
{
return 'xola_elasticsearch_proxy';
}
}
35 changes: 9 additions & 26 deletions Event/ElasticsearchProxyEvent.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Xola\ElasticsearchProxyBundle\Event;
use Symfony\Component\EventDispatcher\Event as SymfonyEvent;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ElasticsearchProxyEvent extends SymfonyEvent {

class ElasticsearchProxyEvent extends Event
{
// The request object
protected $request;

Expand All @@ -30,49 +31,31 @@ public function __construct(Request $request, $index, $slug, array &$query, Resp
$this->response = $response;
}

/**
* @param mixed $request
*/
public function setRequest($request)
{
$this->request = $request;
}

/**
* @return mixed
*/
public function getRequest()
{
return $this->request;
}

/**
* @param mixed $index
*/
public function setIndex($index)
{
$this->index = $index;
}

/**
* @return mixed
*/
public function getIndex()
{
return $this->index;
}

/**
* @param mixed $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}

/**
* @return mixed
*/
public function getSlug()
{
return $this->slug;
Expand All @@ -95,18 +78,18 @@ public function getQuery()
}

/**
* @param \Symfony\Component\HttpFoundation\Response $response
* @param Response $response
*/
public function setResponse($response)
public function setResponse(Response $response)
{
$this->response = $response;
}

/**
* @return \Symfony\Component\HttpFoundation\Response
* @return Response
*/
public function getResponse()
public function getResponse(): ?Response
{
return $this->response;
}
}
}
9 changes: 0 additions & 9 deletions Resources/config/config.yml

This file was deleted.

8 changes: 8 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true
public: false

Xola\ElasticsearchProxyBundle\Controller\:
resource: '../../Controller'
tags: ['controller.service_arguments']
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

class ElasticsearchProxyControllerTest extends TestCase
{
/** @var ElasticsearchProxyController */
private $controller;

protected function setUp(): void
public function testGetElasticSearchUrl()
{
$container = $this->createMock(ContainerInterface::class);
$config = array(
Expand All @@ -23,19 +20,12 @@ protected function setUp(): void
),
'roles_skip_auth_filter' => array()
);
$container->expects($this->any())->method("getParameter")->with("xola_elasticsearch_proxy")
$container->expects($this->once())->method("getParameter")->with("xola_elasticsearch_proxy")
->willReturn($config);
$controller = new ElasticsearchProxyController();

$this->controller = new ElasticsearchProxyController();
$this->controller->setContainer($container);
}


public function testGetElasticSearchUrl()
{
$url = $this->controller->getElasticSearchUrl('pretty=true', 'logs', '_search');
$url = $controller->getElasticSearchUrl('pretty=true', 'logs', '_search', $container);

$this->assertEquals('http://localhost:9200/logs/_search?pretty=true', $url, 'Should be url built from proxy');
}

}
}
24 changes: 8 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@
"keywords": ["elasticsearch", "symfony", "search", "proxy", "authorization"],
"homepage": "https://github.com/xola/XolaElasticsearchProxyBundle",
"license": "MIT",
"authors": [
{
"name": "Venkata Krishna Kotra",
"email": "[email protected]"
},
{
"name": "Anush Ramani",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1",
"symfony/framework-bundle": ">=2.1 <4.0",
"symfony/framework-bundle": "^3.4|^4.0",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-0": {
"Xola\\ElasticsearchProxyBundle": ""
}
},
"target-dir": "Xola/ElasticsearchProxyBundle"
"psr-4": {
"Xola\\ElasticsearchProxyBundle\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
}
}