Skip to content

Commit

Permalink
Merge pull request #16 from AmazeeLabs/feature/ALCS-46--fastly-cdn
Browse files Browse the repository at this point in the history
Feature/alcs 46  fastly cdn
  • Loading branch information
Tim Clifford authored Jan 12, 2021
2 parents 7c292e6 + 7f4a73e commit 7409e55
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.lock
*.phar
.build
dev-*
drush.alias.yml
14 changes: 14 additions & 0 deletions Policies/cnd_check.policy.yml
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'
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 4 additions & 2 deletions example.drush.alias.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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'
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'
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'
121 changes: 121 additions & 0 deletions src/Audit/CdnCheck.php
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;
}

}
23 changes: 2 additions & 21 deletions src/Audit/SSLChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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().
*
Expand All @@ -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.");
Expand Down
27 changes: 27 additions & 0 deletions src/Utils/Common.php
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;
}
}

0 comments on commit 7409e55

Please sign in to comment.