diff --git a/.gitignore b/.gitignore index a73c09a..079865b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock *.phar .build dev-* +drush.alias.yml diff --git a/Policies/cnd_check.policy.yml b/Policies/cnd_check.policy.yml new file mode 100644 index 0000000..3bb7865 --- /dev/null +++ b/Policies/cnd_check.policy.yml @@ -0,0 +1,14 @@ +title: "CDN Check" +class: \Drutiny\algm\Audit\CdnCheck +name: algm:CdnCheck +tags: + - Speed +description: | + Runs to check if the site is using the a CDN. +success: | + {{ status }} +failure: | + {{ status }} +parameters: + cdn: + default: 'fastly' diff --git a/composer.json b/composer.json index 2672de2..9ddf304 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "require": { "drutiny/plugin-drupal-8": "^2.3", "drutiny/plugin-drupal-7": "^2.3", - "spatie/ssl-certificate": "^1.20" + "spatie/ssl-certificate": "^1.20", + "rlanvin/php-ip": "^2.1" }, "autoload": { "psr-4": { diff --git a/example.drush.alias.yml b/example.drush.alias.yml index 7dab0a7..6cf05ec 100644 --- a/example.drush.alias.yml +++ b/example.drush.alias.yml @@ -1,8 +1,8 @@ prod: host: ssh.lagoon.amazeeio.cloud + user: site-name-ch-prod root: /app/web uri: https://www.site-name.ch - user: site-name-ch-prod remote-host: ssh.lagoon.amazeeio.cloud remote-user: site-name-ch-prod ssh-options: '-o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222' @@ -10,6 +10,7 @@ staging: host: ssh.lagoon.amazeeio.cloud user: site-name-ch-pre-prod root: /app/web + uri: https://www.site-name.ch remote-host: ssh.lagoon.amazeeio.cloud remote-user: site-name-ch-pre-prod ssh-options: '-o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222' @@ -17,6 +18,7 @@ dev: host: ssh.lagoon.amazeeio.cloud user: site-name-ch-dev root: /app/web + uri: https://www.site-name.ch remote-host: ssh.lagoon.amazeeio.cloud remote-user: site-name-ch-dev - ssh-options: '-o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222' + ssh-options: '-o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222' \ No newline at end of file diff --git a/src/Audit/CdnCheck.php b/src/Audit/CdnCheck.php new file mode 100644 index 0000000..744996f --- /dev/null +++ b/src/Audit/CdnCheck.php @@ -0,0 +1,121 @@ + [ + "23.235.32.0/20", + "43.249.72.0/22", + "103.244.50.0/24", + "103.245.222.0/23", + "103.245.224.0/24", + "104.156.80.0/20", + "146.75.0.0/16", + "151.101.0.0/16", + "157.52.64.0/18", + "167.82.0.0/17", + "167.82.128.0/20", + "167.82.160.0/20", + "167.82.224.0/20", + "172.111.64.0/18", + "185.31.16.0/22", + "199.27.72.0/21", + "199.232.0.0/16", + "2a04:4e40::/32", + "2a04:4e42::/32", + ], + ]; + return $cdns; + } + + /** + * Extract the host from a url. + * + * @param $url + * + * @return mixed + */ + private function getHost($url){ + $parse = parse_url($url); + return $parse['host']; + } + + + /** + * @inheritdoc + */ + public function audit(Sandbox $sandbox) { + $cdn = $sandbox->getParameter('cdn'); + + $command = "printenv"; + $output = $sandbox->exec($command); + $env = Common::envStringToAssociativeArray($output); + + if (!$env) { + throw new \Exception("Could not fetch environment variables."); + return Audit::ERROR; + } + + $url = $env['LAGOON_ROUTE']; + if (!$url) { + throw new \Exception("The route could not be found."); + return Audit::ERROR; + } + + $host = $this->getHost($url); + $hostIp = gethostbyname($host); + $cdnIpAddresses = $this->cdnIpAddresses(); + if ($selectedCdn = $cdnIpAddresses[$cdn]) { + foreach ($selectedCdn as $ip) { + $block = IPBlock::create($ip); + if ($block->contains($hostIp)) { + $msg = sprintf('The domain %s (%s) has been found in the ip range of %s which matches the %s CDN', $url, $hostIp, $ip, ucfirst($cdn)); + $sandbox->setParameter('status', $msg); + return Audit::PASS; + } + } + } + else { + throw new \Exception(sprintf("Could not find any ip addresses matching the CDN named %s", $cdn)); + return Audit::ERROR; + } + + $msg = sprintf('The domain %s (%s) has not been found using the %s CDN ', $url, $hostIp, ucfirst($cdn)); + $sandbox->setParameter('status', $msg); + return Audit::FAILURE; + } + +} diff --git a/src/Audit/SSLChecker.php b/src/Audit/SSLChecker.php index 88bc298..84ddc71 100644 --- a/src/Audit/SSLChecker.php +++ b/src/Audit/SSLChecker.php @@ -2,12 +2,11 @@ namespace Drutiny\algm\Audit; +use Drutiny\algm\Utils\Common; use Drutiny\Audit; use Drutiny\Sandbox\Sandbox; use Drutiny\Annotation\Token; use Drutiny\Annotation\Param; -use Drutiny\Target\DrushTarget; -use Drutiny\RemediableInterface; use Spatie\SslCertificate\SslCertificate; @@ -29,24 +28,6 @@ */ class SSLChecker extends Audit { - /** - * Converts string from printenv to associative array - * - * @param string $input - * @return array | null - */ - private function envStringToAssociativeArray($input) { - $env=[]; - $lines = explode(PHP_EOL, $input); - foreach ($lines as $line) { - $split = explode("=", $line, 2); - if ($split[0]) { - $env[$split[0]] = $split[1]; - } - } - return count($env) ? $env : NULL; - } - /** * This will be called before audit(). * @@ -67,7 +48,7 @@ public function audit(Sandbox $sandbox) { // Execute and clean the output into usable data. $command = "printenv"; $output = $sandbox->exec($command); - $env = $this->envStringToAssociativeArray($output); + $env = Common::envStringToAssociativeArray($output); if (!$env) { throw new \Exception("Could not fetch environment variables."); diff --git a/src/Utils/Common.php b/src/Utils/Common.php new file mode 100644 index 0000000..e554823 --- /dev/null +++ b/src/Utils/Common.php @@ -0,0 +1,27 @@ +