Skip to content

Commit

Permalink
Rework of how the CMS responds to public file base header. (#18424)
Browse files Browse the repository at this point in the history
* Rework of how the CMS responds to public file base header.

* Ensure header check is case insensitive.

* Variable consistency.
  • Loading branch information
timcosgrove authored Jul 10, 2024
1 parent 4b78a60 commit 2b4b7e1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 124 deletions.
31 changes: 31 additions & 0 deletions docroot/sites/default/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@
$deploy_service->run($GLOBALS['request'], $app_root, $site_path);
}

// Because Jenkins can't resolve e.g. prod.cms.va.gov DNS, and only the
// internal*elb addresses. And sometimes the host would return data with
// "http://default/..." hostnames for files so we set the host here and pass it
// to the `file_public_base_url` setting to fix that.
if (!empty($webhost_on_cli)) {
if (PHP_SAPI === 'cli') {
// This is running from drush so set the webhost.
// Var $webhost_on_cli is set in <settings.<environment>.php.
$webhost = $webhost_on_cli;
}
else {
// This is running from an HTTP request.
$webhost = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
}

$settings['file_public_base_url'] = "{$webhost}/sites/default/files";
}

// Look for an incoming request header to indicate we should use the public
// asset S3 location for file rather than the Drupal-internal location.
if (!empty($public_asset_s3_base_url)) {
$headerArray = function_exists('apache_request_headers') ? apache_request_headers() : [];
$targetHeader = 'File-Public-Base-Url-Check';
foreach ($headerArray as $header => $value) {
if (strtolower($header) == strtolower($targetHeader) && $value === 'true') {
// Point the file base url to the public asset S3 bucket.
$settings['file_public_base_url'] = $public_asset_s3_base_url;
}
}
}

// Monolog
$settings['container_yamls'][] = __DIR__ . '/services/services.monolog.yml';

Expand Down
3 changes: 3 additions & 0 deletions docroot/sites/default/settings/settings.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@
$settings['va_gov_frontend_url'] = 'https://dev.va.gov';
$settings['va_gov_frontend_build_type'] = 'brd';
$settings['github_actions_deploy_env'] = 'dev';

// Public asset S3 location
$public_asset_s3_base_url = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
33 changes: 2 additions & 31 deletions docroot/sites/default/settings/settings.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,5 @@

$settings['va_cms_bot_github_auth_token'] = getenv('GITHUB_TOKEN') ?: 'fake-token';

// Because Jenkins can't resolve e.g. prod.cms.va.gov DNS, and only the
// internal*elb addresses. And sometimes the host would return data with
// "http://default/..." hostnames for files so we set the host here and pass it
// to the `file_public_base_url` setting to fix that.
if (!empty($webhost_on_cli)) {
if (PHP_SAPI === 'cli') {
// This is running from drush so set the webhost.
// Var $webhost_on_cli is set in <settings.<environment>.php.
$webhost = $webhost_on_cli;
}
else {
// This is running from an HTTP request.
$webhost = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
}

// Setting the custom header to switch the file base URL in next-build
// Uncomment this next line to test that the header is being set
// header('File-Public-Base-Url-Check: true');

// Get all headers
$headersArray = headers_list();

// If the header is set in the response headers
if (in_array('File-Public-Base-Url-Check: true', $headersArray, true)) {
// Make the file base url point to staging
$settings['file_public_base_url'] = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
} else {
// Otherwise use the default webhost
$settings['file_public_base_url'] = "{$webhost}/sites/default/files";
}
}
// Public asset S3 location
$public_asset_s3_base_url = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
33 changes: 2 additions & 31 deletions docroot/sites/default/settings/settings.prod.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,5 @@
// Settings supporting broken link report import.
$settings['broken_link_report_import_enabled'] = TRUE;

// Because Jenkins can't resolve e.g. prod.cms.va.gov DNS, and only the
// internal*elb addresses. And sometimes the host would return data with
// "http://default/..." hostnames for files so we set the host here and pass it
// to the `file_public_base_url` setting to fix that.
if (!empty($webhost_on_cli)) {
if (PHP_SAPI === 'cli') {
// This is running from drush so set the webhost.
// Var $webhost_on_cli is set in <settings.<environment>.php.
$webhost = $webhost_on_cli;
}
else {
// This is running from an HTTP request.
$webhost = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
}

// Setting the custom header to switch the file base URL in next-build
// Uncomment this next line to test that the header is being set
// header('File-Public-Base-Url-Check: true');

// Get all headers
$headersArray = headers_list();

// If the header is set in the response headers
if (in_array('File-Public-Base-Url-Check: true', $headersArray, true)) {
// Make the file base url point to prod
$settings['file_public_base_url'] = "https://dsva-vagov-prod-cms-files.s3.us-gov-west-1.amazonaws.com";
} else {
// Otherwise use the default webhost
$settings['file_public_base_url'] = "{$webhost}/sites/default/files";
}
}
// Public asset S3 location
$public_asset_s3_base_url = "https://dsva-vagov-prod-cms-files.s3.us-gov-west-1.amazonaws.com";
33 changes: 2 additions & 31 deletions docroot/sites/default/settings/settings.staging.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,5 @@
$settings['va_gov_frontend_build_type'] = 'brd';
$settings['github_actions_deploy_env'] = 'staging';

// Because Jenkins can't resolve e.g. prod.cms.va.gov DNS, and only the
// internal*elb addresses. And sometimes the host would return data with
// "http://default/..." hostnames for files so we set the host here and pass it
// to the `file_public_base_url` setting to fix that.
if (!empty($webhost_on_cli)) {
if (PHP_SAPI === 'cli') {
// This is running from drush so set the webhost.
// Var $webhost_on_cli is set in <settings.<environment>.php.
$webhost = $webhost_on_cli;
}
else {
// This is running from an HTTP request.
$webhost = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
}

// Setting the custom header to switch the file base URL in next-build
// Uncomment this next line to test that the header is being set
// header('File-Public-Base-Url-Check: true');

// Get all headers
$headersArray = headers_list();

// If the header is set in the response headers
if (in_array('File-Public-Base-Url-Check: true', $headersArray, true)) {
// Make the file base url point to staging
$settings['file_public_base_url'] = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
} else {
// Otherwise use the default webhost
$settings['file_public_base_url'] = "{$webhost}/sites/default/files";
}
}
// Public asset S3 location
$public_asset_s3_base_url = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
33 changes: 2 additions & 31 deletions docroot/sites/default/settings/settings.tugboat.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,5 @@
$settings['broken_link_report_import_enabled'] = TRUE;
$settings['broken_link_report_location'] = '/var/lib/tugboat/docroot/vendor/va-gov/content-build/logs/vagovdev-broken-links.json';

// Because Jenkins can't resolve e.g. prod.cms.va.gov DNS, and only the
// internal*elb addresses. And sometimes the host would return data with
// "http://default/..." hostnames for files so we set the host here and pass it
// to the `file_public_base_url` setting to fix that.
if (!empty($webhost_on_cli)) {
if (PHP_SAPI === 'cli') {
// This is running from drush so set the webhost.
// Var $webhost_on_cli is set in <settings.<environment>.php.
$webhost = $webhost_on_cli;
}
else {
// This is running from an HTTP request.
$webhost = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}";
}

// Setting the custom header to switch the file base URL in next-build
// Uncomment this next line to test that the header is being set
// header('File-Public-Base-Url-Check: true');

// Get all headers
$headersArray = headers_list();

// If the header is set in the response headers
if (in_array('File-Public-Base-Url-Check: true', $headersArray, true)) {
// Make the file base url point to staging
$settings['file_public_base_url'] = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";
} else {
// Otherwise use the default webhost
$settings['file_public_base_url'] = "{$webhost}/sites/default/files";
}
}
// Public asset S3 location
$public_asset_s3_base_url = "https://dsva-vagov-staging-cms-files.s3.us-gov-west-1.amazonaws.com";

0 comments on commit 2b4b7e1

Please sign in to comment.