Skip to content

Proxy host filtering #11

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

Merged
merged 1 commit into from
Jul 14, 2016
Merged
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
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function getConfigTreeBuilder()
})
->end()
->end()
->scalarNode('proxy_host_filter_service')
->defaultNull()
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function transformConfigToContainer(array $config, ContainerBuilder $con
if ($config['enabled']) {
foreach ($config['proxy_urls'] as $id => $proxyUrl) {
$definition = new DefinitionDecorator('phpmentors_proxy_url_rewrite.proxy_url');
$definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url']));
$definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url'], $proxyUrl['proxy_host_filter_service'] === null ? null : new Reference($proxyUrl['proxy_host_filter_service'])));

$serviceId = 'phpmentors_proxy_url_rewrite.proxy_url.'.sha1($id);
$container->setDefinition($serviceId, $definition);
Expand Down
28 changes: 28 additions & 0 deletions src/ProxyUrl/ProxyHostFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* This file is part of PHPMentorsProxyURLRewriteBundle.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/

namespace PHPMentors\ProxyURLRewriteBundle\ProxyUrl;

use PHPMentors\DomainKata\Service\ServiceInterface;

/**
* @since Interface available since Release 1.2.0
*/
interface ProxyHostFilterInterface extends ServiceInterface
{
/**
* @param string
*
* @return string
*/
public function filter($host);
}
13 changes: 9 additions & 4 deletions src/ProxyUrl/ProxyUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
class ProxyUrlFactory implements ServiceInterface
{
/**
* @param int|string $proxyUrlId
* @param string $path
* @param string $proxyUrl
* @param int|string $proxyUrlId
* @param string $path
* @param string $proxyUrl
* @param ProxyHostFilterInterface $hostFilterService
*
* @return ProxyUrl
*/
public function create($proxyUrlId, $path, $proxyUrl)
public function create($proxyUrlId, $path, $proxyUrl, ProxyHostFilterInterface $proxyHostFilter = null)
{
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort) = static::parseUrl($proxyUrl);

if ($proxyHostFilter !== null) {
$proxyUrlHost = $proxyHostFilter->filter($proxyUrlHost);
}

return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Functional/Bundle/TestBundle/ProxyUrl/ProxyHostFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* This file is part of PHPMentorsProxyURLRewriteBundle.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/

namespace PHPMentors\ProxyURLRewriteBundle\Functional\Bundle\TestBundle\ProxyUrl;

use PHPMentors\ProxyURLRewriteBundle\ProxyUrl\ProxyHostFilterInterface;

/**
* @since Class available since Release 1.2.0
*/
class ProxyHostFilter implements ProxyHostFilterInterface
{
/**
* {@inheritdoc}
*/
public function filter($host)
{
if ($host === null) {
return null;
}

return 'baz.'.$host;
}
}
114 changes: 114 additions & 0 deletions tests/Functional/HostFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/*
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* This file is part of PHPMentorsProxyURLRewriteBundle.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/

namespace PHPMentors\ProxyURLRewriteBundle\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @since Class available since Release 1.2.0
*/
class HostFilterTest extends WebTestCase
{
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$_SERVER['KERNEL_DIR'] = __DIR__.'/app';
$_SERVER['SYMFONY__SECRET'] = hash('sha1', uniqid(mt_rand()));

$this->removeCacheDir();
}

/**
* {@inheritdoc}
*/
protected function tearDown()
{
parent::tearDown();

$this->removeCacheDir();
}

/**
* {@inheritdoc}
*/
protected static function createKernel(array $options = array())
{
$kernel = KernelTestCase::createKernel($options);
if (array_key_exists('config', $options)) {
$kernel->setConfig($options['config']);
}

return $kernel;
}

protected function removeCacheDir()
{
$fileSystem = new Filesystem();
$fileSystem->remove($_SERVER['KERNEL_DIR'].'/cache/test');
}

public function filterData()
{
return array(
array('/foo/bar/', true, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'),
array('/foo/bar/', false, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'),
array('//example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
array('//example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'),
array('http://example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
array('http://example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'),
array('https://example.com/foo/bar/', true, 'https://baz.example.com/foo/bar/url-rewriting-in-controllers/'),
array('https://example.com/foo/bar/', false, 'https://example.com/foo/bar/url-rewriting-in-controllers/'),
array('http://example.com:8180/foo/bar/', true, 'http://baz.example.com:8180/foo/bar/url-rewriting-in-controllers/'),
array('http://example.com:8180/foo/bar/', false, 'http://example.com:8180/foo/bar/url-rewriting-in-controllers/'),
);
}

/**
* @test
* @dataProvider filterData
*
* @param string $proxyUrl
* @param bool $proxyHostFilterService
* @param string $rewroteUrl
*/
public function filter($proxyUrl, $proxyHostFilterService, $rewroteUrl)
{
$client = $this->createClient(array('config' => function (ContainerBuilder $container) use ($proxyUrl, $proxyHostFilterService) {
$config = array(
'path' => '!^.*!',
'proxy_url' => $proxyUrl,
);
if ($proxyHostFilterService) {
$config['proxy_host_filter_service'] = 'phpmentors_proxy_url_rewrite_test.proxy_host_filter';
}

$container->loadFromExtension('phpmentors_proxy_url_rewrite', array(
'proxy_urls' => array(
'foo' => $config,
), ));
}));

$client->request('GET', sprintf('http://backend1.example.com:8080/url-rewriting-in-controllers/?referenceType=%s', UrlGeneratorInterface::ABSOLUTE_URL));

$this->assertThat($client->getResponse()->getStatusCode(), $this->equalTo(200), $client->getResponse()->getContent());
$this->assertThat($client->getCrawler()->filterXpath("//*[@id='generateUrl']")->text(), $this->equalTo($rewroteUrl));
}
}
3 changes: 3 additions & 0 deletions tests/Functional/app/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: services.yml }

framework:
secret: "%secret%"
router:
Expand Down
6 changes: 6 additions & 0 deletions tests/Functional/app/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
phpmentors_proxy_url_rewrite_test.proxy_host_filter.class: "PHPMentors\\ProxyURLRewriteBundle\\Functional\\Bundle\\TestBundle\\ProxyUrl\\ProxyHostFilter"

services:
phpmentors_proxy_url_rewrite_test.proxy_host_filter:
class: "%phpmentors_proxy_url_rewrite_test.proxy_host_filter.class%"