symfony: protecting the development, integration and production environments
Translation of the original in Catalan. Read it in: CatalàCastellano
In the article symfony: entorns de desenvolupament, integració i producció you can understand the reason for working with these three namespaces (environment) so what this article tries to explain is:
- Prevent access to the applications in the 'dev' and 'int' environments from uncontrolled places. That is, an internet IP must not be able to run myapp_dev.php or myapp_int.php but an IP from the developer's network must be able to.
- Prevent access to backend applications from the production environment. If we have developed some application to do the maintenance of the main application, an administration part or backend, it must not be accessible from uncontrolled IPs.
- Separate the configuration file config/cofnig.php for each of the environments. That way we can define constants or other tasks in a particular way for each of the environments.
In order to achieve these objectives I have created the file config/config.lib.php:
<?php
/**
* Comproba si la IP amb la que s'accedeix a l'aplicació permet connectar en aquest environment
*
* @param string $env nom de l'environment
* @param array $ips ips des de les que podem accedir a l'environment
* @param string $clientip ip del client que vol accedir al recurs
*/
function envCorrecte ($env,$ips,$clientip)
{
if (SF_ENVIRONMENT==$env)
{
$acces=0;
foreach($ips as $ip)
{
$ipclient=substr($clientip,0,strlen($ip));
if ($ipclient==$ip)
{
$acces=1;
}
}
if ($acces==0)
{
echo "You are not in '".$env."' environment! (".$ipclient."<>".$ip.")";
exit;
}
}
}
/**
* Comproba si l'aplicació amb la que s'accedeix al projecte és publica o no
*
* @param array $apps llista d'aplicacions no públiques
* @param array $ips llista d'IP amb les que es pot accedir a les apps no públiques
* @param string $clientip ip del client que vol accedir al recurs
*/
function appCorrecte ($apps,$ips,$clientip)
{
// solucionem problema de la clau '0'
$apps_aux[0]='';
$apps = $apps_aux + $apps;
//
if (array_search(SF_APP,$apps)!=false)
{
$acces=0;
foreach($ips as $ip)
{
$ADR=$HTTP_SERVER_VARS['REMOTE_ADDR'];
$ipclient=substr($clientip,0,strlen($ip));
if ($ipclient==$ip)
{
$acces=1;
}
}
if ($acces==0)
{
echo "Keep away! ".SF_APP." is not a public application!";
exit;
}
}
}
?>
Then from the file config/config.php we have to call this small library of functions that I have created. The rest is more than logical, here you have an example:
<?php
include("config.lib.php");
/**
* Controlem accés als 'environments' de 'env' i 'int'
*/
$ip_dev=array("10.138.0.","192.168.");
$ip_int=array("10.138.0.","192.168.");
envCorrecte('env',$ip_env,$HTTP_SERVER_VARS['REMOTE_ADDR']);
envCorrecte('int',$ip_int,$HTTP_SERVER_VARS['REMOTE_ADDR']);
/**
* Controlem accés a les 'apps' d'ús privat
*/
$ip_apps=array("10.138.0.","192.168.","w.x.y.z");
$apps=array('myapp_1','myapp_2','myapp_n');
appCorrecte($apps,$ip_apps,$HTTP_SERVER_VARS['REMOTE_ADDR']);
/**
* Carreguem el fitxer de configuració propi de l'"environment".
*/
include("config.".SF_ENVIRONMENT.".php");
/**
* Part comú en els 3 entorns
*/
?>
As you can see the code is very simple and its power and dynamism, exceptional.
I have also published this code as a snippet at Access control to environment and applications and custom global config file for an environment.