-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ALCS-46 ✨ added CDN checker policy and created common utils for commo…
…n methods.
- Loading branch information
Showing
5 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Drutiny\algm\Audit; | ||
|
||
use Drutiny\algm\Utils\Common; | ||
use Drutiny\Annotation\Param; | ||
use Drutiny\Audit; | ||
use Drutiny\Sandbox\Sandbox; | ||
use Drutiny\Annotation\Token; | ||
use PhpIP\IPBlock; | ||
|
||
|
||
/** | ||
* Simple Drush Status test | ||
* | ||
* @Param( | ||
* name = "cdn", | ||
* description = "Set which CDN we are checking for.", | ||
* type = "string" | ||
* ) | ||
* @Token( | ||
* name = "status", | ||
* type = "string", | ||
* description = "Results from Drush status" | ||
* ) | ||
*/ | ||
class CdnCheck extends Audit { | ||
|
||
/** | ||
* Returns a list of ip address for CDNs | ||
* | ||
* @return \string[][] | ||
*/ | ||
private function cdnIpAddresses() { | ||
$cdns = [ | ||
// you get a list of fastly IPs from here | ||
// I have hardcoded them encase the sever we use cannot ping out. | ||
// https://api.fastly.com/public-ip-list | ||
"fastly" => [ | ||
"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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Drutiny\algm\Utils; | ||
|
||
/** | ||
* Generate markdown table as output from php array | ||
*/ | ||
class Common { | ||
|
||
/** | ||
* Converts string from printenv to associative array | ||
* | ||
* @param string $input | ||
* @return array | null | ||
*/ | ||
public static 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; | ||
} | ||
} |