Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to derive SES SMTP password from existing AWS API keys #348

Merged
merged 2 commits into from
Feb 14, 2025

Conversation

nicksantamaria
Copy link
Contributor

SES can not use the AWS_SECRET_ACCESS_KEY directly via the SMTP interfaces.

Changes

Sets default values for SMTP_USERNAME and SMTP_PASSWORD when none set, and AWS credentials are available.

@nicksantamaria nicksantamaria changed the title Added code to derived SES SMTP password from existing AWS API keys Added code to derive SES SMTP password from existing AWS API keys Feb 13, 2025
@@ -155,7 +155,38 @@
$config['smtp.settings']['smtp_allowhtml'] = 1;

// @see baywatch.module for SMTP_REPLYTO setting.
$config['system.site']['mail'] = getenv('SMTP_FROM') ?: '[email protected]';
$config['system.site']['mail'] = getenv('SMTP_FROM') ?: sprintf("%s.%[email protected]", getenv('LAGOON_ENVIRONMENT'), getenv('LAGOON_PROJECT'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sets the default FROM address to [env].[project]@sdp.delivery. This should be overridden in prod.

Comment on lines +161 to +162
if (empty($config['smtp.settings']['smtp_password']) &&
str_contains($config['smtp.settings']['smtp_host'], "amazonaws.com")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only enter this block if SMTP_PASSWORD is empty, and the host points to SES.

Comment on lines +169 to +186
$config['smtp.settings']['smtp_password'] = (function(string $region, string $awsSecretAccessKey): string {
// Adapted from AWS SDK.
// @see https://github.com/aws/aws-sdk-php/blob/a63e79c15a972c54bf015a16cce3f3572e0c8221/src/Ses/SesClient.php#L195
$date = "11111111";
$service = "ses";
$terminal = "aws4_request";
$message = "SendRawEmail";
$version = 0x04;

$signature = hash_hmac('sha256', $date, "AWS4" . $awsSecretAccessKey, true);
$signature = hash_hmac('sha256', $region, $signature, true);
$signature = hash_hmac('sha256', $service, $signature, true);
$signature = hash_hmac('sha256', $terminal, $signature, true);
$signature = hash_hmac('sha256', $message, $signature, true);
$signatureAndVersion = pack('c', $version) . $signature;

return base64_encode($signatureAndVersion);
})($region, $aws_key);
Copy link
Contributor Author

@nicksantamaria nicksantamaria Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with a closure to prevent the function scope vars leaking out into the rest of settings.php

$signatureAndVersion = pack('c', $version) . $signature;

return base64_encode($signatureAndVersion);
})($region, $aws_key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this passing $region and $aws_key out to the parent scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is passing those vars into the function.

To include parent scope vars the use ($region, $aws_key) syntax is used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the vars set?

@nicksantamaria nicksantamaria merged commit 322f612 into 6.x Feb 14, 2025
1 check passed
@nicksantamaria nicksantamaria deleted the feature/ses-credential-generation branch February 14, 2025 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants