Skip to content

Commit

Permalink
Website: WP Cloud: Relay secrets for error logger (#1337)
Browse files Browse the repository at this point in the history
## What is this PR doing?
This PR updates our WP Cloud hosting setup to provide secrets necessary
for error logging to WP.org Slack.

## Testing Instructions

I tested manually playground-dot-wordpress-dot-net.atomicsites.blog via
dev tools console, since the Playground error dialog is hardcoded to log
to playground.wordpress.net.
```
fd = new FormData;
fd.append('description', 'PLEASE IGNORE: This is a test.');
fd.append('logs', 'test-logs');
fd.append('url', 'https://playground-dot-wordpress-dot-net.atomicsites.blog/');
fd.append('context', 'test-context');
fd.append('blueprint', 'test-blueprint');

fetch( '/logger.php', { method: 'POST', body: fd } ).then(r => (console.log('logging response', r), r.text())).then( t => console.log('text', t) )

// Results:
Promise {<pending>}
logging response Response {type: 'basic', url: 'https://playground-dot-wordpress-dot-net.atomicsites.blog/logger.php', redirected: false, status: 200, ok: true, …}
text {"ok":true}
```
And a message was logged to WP.org Slack here:
https://wordpress.slack.com/archives/C06Q5DCKZ3L/p1714150218128469
  • Loading branch information
brandonpayton authored Apr 26, 2024
1 parent ac6f746 commit 776e8bb
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/playground/website-deployment/custom-redirects-lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function playground_file_needs_special_treatment( $path ) {
return (
!! playground_maybe_rewrite( $path ) ||
!! playground_maybe_redirect( $path ) ||
!! playground_get_custom_response_headers( $path )
!! playground_get_custom_response_headers( basename( $path ) ) ||
!! playground_maybe_set_environment( $path )
);
}

Expand Down Expand Up @@ -127,12 +128,7 @@ function playground_handle_request() {

if ( 'php' === $extension ) {
$log( "Running PHP: '$resolved_path'" );
// TODO: Set these from persistent data for logger.php
/*
# Slack All
SetEnv SLACK_CHANNEL --secret--
SetEnv SLACK_TOKEN --secret--
*/
playground_maybe_set_environment( $requested_path );
require $resolved_path;
} else {
$log( "Reading static file: '$resolved_path'" );
Expand Down Expand Up @@ -177,6 +173,32 @@ function playground_maybe_redirect( $requested_path ) {
return false;
}

function playground_maybe_set_environment( $requested_path ) {
if ( ! str_ends_with( $requested_path, '.php' ) ) {
return false;
}

if ( str_ends_with( $requested_path, 'logger.php' ) ) {
// WORKAROUND: Atomic_Persistent_Data wants the DB_PASSWORD constant
// which is not set yet. But we can force its definition.
__atomic_env_define( 'DB_PASSWORD' );

$secrets = new Atomic_Persistent_Data;
if ( isset(
$secrets->LOGGER_SLACK_CHANNEL,
$secrets->LOGGER_SLACK_TOKEN,
) ) {
putenv( "SLACK_CHANNEL={$secrets->LOGGER_SLACK_CHANNEL}" );
putenv( "SLACK_TOKEN={$secrets->LOGGER_SLACK_TOKEN}" );
} else {
error_log( 'PLAYGROUND: Missing secrets for logger.php' );
}
return true;
}

return false;
}

function playground_get_custom_response_headers( $filename ) {
if ( 'iframe-worker.html' === $filename ) {
return array( 'Origin-Agent-Cluster: ?1' );
Expand Down

0 comments on commit 776e8bb

Please sign in to comment.