Skip to content

Commit

Permalink
Refactor debug code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ragnarkurmwunder committed Apr 28, 2022
1 parent 8f00dcf commit 55ac7c7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 83 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ composer require wunderio/drupal-ping:^2

### v2.3

- Refactor error messages into JSON for easier automation.
- Refactor error messages into JSON for easier automation
- Refactor debug token generation

### v2.2

Expand All @@ -55,9 +56,9 @@ Breaking Changes!
## Usage

* Visit `/_ping.php` to get a system status
* By using `?debug=token` additional status check table and time profiling information is displayed.
* See [Ping Development & Testing](#ping-development--testing) how to obtain the `token` value.
* Find slow checks and checks errors in logs.
* By using `?debug=token` additional status check table and time profiling information is displayed
* See [Debug Mode](#debug-mode) how to obtain the `token` value
* Find slow checks and checks errors in logs

## Checks

Expand Down Expand Up @@ -132,6 +133,23 @@ Use of `$status->setName()` and `$status->set()` to define the result.
The PHP file does not need to contain functions, just plain PHP is enough.
Check it out how other checks are created in the `_ping.php`.

## Debug Mode

`_ping.php` can be accessed over the web.
For example `https://example.com/_ping.php`.
It can also be accessed from the shell `cd /path/web ; php _ping.php`.
From the shell output the debug token can be attained.
Then visit the ping again with `https://example.com/_ping.php?debug=token`.

The token is generated in one of the following ways.
These methods are listed by precedance.
If earlier fails (is empty), then next one is tried.
* Drupal settings `$settings['ping_token']`
* Environment variable `PING_TOKEN`
* Md5 of the combination of environment variable values where the variable names matches regex `/^(DB|ENVIRONMENT_NAME|GIT|PHP|PROJECT_NAME|S+MTP|VARNISH|WARDEN)/`
* Md5 of Drupal settings `$settings['hash_salt']`
* Md5 of the hostname

## Ping Development & Testing

- `lando install` - Install dev dependencies without Drupal
Expand All @@ -143,11 +161,6 @@ Note: the Lando setup is defined so that D7 and D89 branched can be easily switc
both running their own own setup. Drupal and composer installations wont clash.
They have separate dirs.

`_ping.php` can be accessed over the lando url.
For example `http://localhost:51418/_ping.php`.
It can also be accessed from the shell `cd /app/drupal9/web ; php _ping.php`.
From the shell output the debug token can be attained.

## Maintainers

- [Janne Koponen](https://github.com/tharna)
Expand Down
75 changes: 38 additions & 37 deletions _ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ public function run(): void {
// if mod_php fails and then matching the string.
print "$msg $code" . PHP_EOL;

$code = $this->getDebugCode($settings);
if (!$this->isDebug($code) && !$this->isCli()) {
$token = $this->getToken($settings);
if (!$this->isDebug($token) && !$this->isCli()) {
return;
}

if ($this->isCli()) {
print <<<TXT
<p>Debug code: $code</p>
<p>Debug token: $token</p>
TXT;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ public function logErrors(array $payloads): void {
}

/**
* Compute Debug Code.
* Provide Debug Access Token.
*
* This is needed to limit access to debug info over the web.
* Many methods are tried in sequence to define the code.
Expand All @@ -324,46 +324,47 @@ public function logErrors(array $payloads): void {
* The Drupal settings.
*
* @return string
* Access code.
* Access token.
*/
public function getDebugCode(array $settings): string {
public function getToken(array $settings): string {

// $settings['ping_debug'].
if (!empty($settings['ping_debug'])) {
$code = $settings['ping_debug'];
return $code;
// $settings['ping_token'].
if (!empty($settings['ping_token'])) {
$token = $settings['ping_token'];
return $token;
}

// Echo "$PROJECT_NAME-$ENVIRONMENT_NAME" | md5sum.
if (!empty(getenv('SILTA_CLUSTER'))) {
$proj = getenv('PROJECT_NAME');
$env = getenv('ENVIRONMENT_NAME');
$code = md5("$proj-$env");
return $code;
// Env(PING_TOKEN).
if (!empty(getenv('PING_TOKEN'))) {
$token = (string) getenv('PING_TOKEN');
return $token;
}

// Echo "$DB_HOST_DRUPAL-$DB_NAME_DRUPAL-$DB_PASS_DRUPAL-$DB_PORT_DRUPAL-$DB_USER_DRUPAL"
// | md5sum.
if (!empty(getenv('DB_NAME_DRUPAL'))) {
$host = getenv('DB_HOST_DRUPAL');
$name = getenv('DB_NAME_DRUPAL');
$pass = getenv('DB_PASS_DRUPAL');
$port = getenv('DB_PORT_DRUPAL');
$user = getenv('DB_USER_DRUPAL');
$code = md5("$host-$name-$pass-$port-$user");
return $code;
// Md5(Concatenated-values-of-some-env-variables).
$token = [];
$env = getenv();
ksort($env);
foreach ($env as $key => $value) {
if (preg_match('/^(DB|ENVIRONMENT_NAME|GIT|PHP|PROJECT_NAME|S+MTP|VARNISH|WARDEN)/', $key)) {
$token[] = $value;
}
}
if (!empty($token)) {
$token = implode('-', $token);
$token = md5($token);
return $token;
}

// Md5($settings['hash_salt']).
if (!empty($settings['hash_salt'])) {
$code = md5($settings['hash_salt']);
return $code;
$token = md5($settings['hash_salt']);
return $token;
}

// Hostname | md5sum.
$code = gethostname();
$code = md5($code);
return $code;
// Md5(Hostname).
$token = gethostname();
$token = md5($token);
return $token;
}

/**
Expand All @@ -380,23 +381,23 @@ public function isCli(): bool {
/**
* Detect if debug information should be provided on request.
*
* Currently it is matching '?debug=code'
* Currently it is matching '?debug=token'
*
* @param string $code
* The code to be checked if present in the request.
* @param string $token
* The token to be checked if present in the request.
*
* @return bool
* Return if we need to emit debugging info.
*/
public function isDebug(string $code): bool {
public function isDebug(string $token): bool {

// @codingStandardsIgnoreLine DrupalPractice.Variables.GetRequestData.SuperglobalAccessedWithVar
$debug = $_GET['debug'] ?? NULL;
if (empty($debug)) {
return FALSE;
}

return $debug == $code;
return $debug == $token;
}

}
Expand Down
81 changes: 44 additions & 37 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ public static function setUpBeforeClass(): void {
require_once 'init.php';
}

protected function setUp(): void {
// Cleanup env before every test
foreach (getenv() as $key => $value) {
if (preg_match('/^(DB|ENVIRONMENT_NAME|GIT|PHP|PROJECT_NAME|S+MTP|VARNISH|WARDEN)/', $key)) {
putenv($key);
}
}
}

/**
* @covers ::__construct
*/
Expand Down Expand Up @@ -85,69 +94,67 @@ public function testProfile2logs(): void {
}

/**
* @covers ::getDebugCode
* @covers ::getToken
*/
public function testGetDebugCodePingDebug(): void {
public function testGetTokenSettings(): void {
$a = new App();
$code = '1234';
$settings = ['ping_debug' => $code];
$data = $a->getDebugCode($settings);
$this->assertEquals($code, $data);
$token = '1234';
$settings = ['ping_token' => $token];
$data = $a->getToken($settings);
$this->assertEquals($token, $data);
}

/**
* @covers ::getDebugCode
* @covers ::getToken
*/
public function testGetDebugCodeSilta(): void {
public function testGetTokenEnvToken(): void {
$token = '1234';
putenv("PING_TOKEN=$token");
$a = new App();
putenv('SILTA_CLUSTER=1');
putenv('PROJECT_NAME=a');
putenv('ENVIRONMENT_NAME=b');
$settings = [];
$data = $a->getDebugCode($settings);
putenv('SILTA_CLUSTER');
putenv('PROJECT_NAME');
putenv('ENVIRONMENT_NAME');
$this->assertEquals('8ca2ed590cf2ea2404f2e67641bcdf50', $data);
$data = $a->getToken([]);
$this->assertEquals($token, $data);
}

/**
* @covers ::getDebugCode
* @covers ::getToken
*/
public function testGetDebugCodeVirtualServer(): void {
public function testGetTokenEnv(): void {
$a = new App();
putenv('DB_HOST_DRUPAL=host');
putenv('DB_NAME_DRUPAL=name');
putenv('DB_PASS_DRUPAL=pass');
putenv('DB_PORT_DRUPAL=port');
putenv('DB_USER_DRUPAL=user');
foreach ([
'DB_NAME',
'ENVIRONMENT_NAME',
'GIT_TEST',
'PHP_TEST',
'PROJECT_NAME',
'SMTP',
'SSMTP',
'VARNISH_TEST',
'WARDEN_TEST',
] as $key) {
putenv("$key=test");
}
$settings = [];
$data = $a->getDebugCode($settings);
putenv('DB_HOST_DRUPAL');
putenv('DB_NAME_DRUPAL');
putenv('DB_PASS_DRUPAL');
putenv('DB_PORT_DRUPAL');
putenv('DB_USER_DRUPAL');
$this->assertEquals('9ea396789d54a514eb63e12126c5ae4a', $data);
$data = $a->getToken($settings);
$this->assertEquals('7c3df2116154d33f51c6d77db9aa3dbc', $data);
}

/**
* @covers ::getDebugCode
* @covers ::getToken
*/
public function testGetDebugCodeHashSalt(): void {
public function testGetTokenHashSalt(): void {
$a = new App();
$settings = ['hash_salt' => 'qwertyuiop'];
$data = $a->getDebugCode($settings);
$data = $a->getToken($settings);
$this->assertEquals('6eea9b7ef19179a06954edd0f6c05ceb', $data);
}

/**
* @covers ::getDebugCode
* @covers ::getToken
*/
public function testGetDebugCodeHostName(): void {
public function testGetTokenHostName(): void {
$a = new App();
$settings = [];
$data = $a->getDebugCode($settings);
$data = $a->getToken($settings);
$this->assertEquals(md5(gethostname()), $data);
}

Expand Down

0 comments on commit 55ac7c7

Please sign in to comment.