From 6304ebdbc63605fa152b47966dc64fcab040301a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 8 Feb 2021 16:28:04 +0200 Subject: [PATCH 1/3] Add Amazon SDK gem --- Gemfile | 2 ++ Gemfile.lock | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Gemfile b/Gemfile index dd879a11b..2d156c829 100644 --- a/Gemfile +++ b/Gemfile @@ -92,3 +92,5 @@ group :test do gem 'webdrivers' gem 'webmock' end + +gem "aws-sdk-ses", "~> 1.37" diff --git a/Gemfile.lock b/Gemfile.lock index 51c880c9e..01408ebd7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -148,6 +148,18 @@ GEM attr_required (1.0.1) autoprefixer-rails (10.0.0.2) execjs + aws-eventstream (1.1.0) + aws-partitions (1.424.0) + aws-sdk-core (3.112.0) + aws-eventstream (~> 1, >= 1.0.2) + aws-partitions (~> 1, >= 1.239.0) + aws-sigv4 (~> 1.1) + jmespath (~> 1.0) + aws-sdk-ses (1.37.0) + aws-sdk-core (~> 3, >= 3.112.0) + aws-sigv4 (~> 1.1) + aws-sigv4 (1.2.2) + aws-eventstream (~> 1, >= 1.0.2) bcrypt (3.1.16) bindata (2.4.8) bootsnap (1.4.8) @@ -226,6 +238,7 @@ GEM i18n_data (0.10.0) isikukood (0.1.2) iso8601 (0.12.1) + jmespath (1.4.0) jquery-rails (4.4.0) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) @@ -484,6 +497,7 @@ DEPENDENCIES active_interaction (~> 3.8) activerecord-import airbrake + aws-sdk-ses (~> 1.37) bootsnap (>= 1.1.0) bootstrap-sass (~> 3.4) cancancan From 2af0bfdda04f5427080385507f2faf8a31e7e783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 8 Feb 2021 16:52:31 +0200 Subject: [PATCH 2/3] BouncedMailAddress: Remove from AWS suppression list upon destroy --- Gemfile | 2 +- Gemfile.lock | 4 ++-- app/models/bounced_mail_address.rb | 17 +++++++++++++++++ config/initializers/aws_ses.rb | 4 ++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 config/initializers/aws_ses.rb diff --git a/Gemfile b/Gemfile index 2d156c829..79862bd69 100644 --- a/Gemfile +++ b/Gemfile @@ -93,4 +93,4 @@ group :test do gem 'webmock' end -gem "aws-sdk-ses", "~> 1.37" +gem "aws-sdk-sesv2", "~> 1.16" diff --git a/Gemfile.lock b/Gemfile.lock index 01408ebd7..08daf4226 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -155,7 +155,7 @@ GEM aws-partitions (~> 1, >= 1.239.0) aws-sigv4 (~> 1.1) jmespath (~> 1.0) - aws-sdk-ses (1.37.0) + aws-sdk-sesv2 (1.16.0) aws-sdk-core (~> 3, >= 3.112.0) aws-sigv4 (~> 1.1) aws-sigv4 (1.2.2) @@ -497,7 +497,7 @@ DEPENDENCIES active_interaction (~> 3.8) activerecord-import airbrake - aws-sdk-ses (~> 1.37) + aws-sdk-sesv2 (~> 1.16) bootsnap (>= 1.1.0) bootstrap-sass (~> 3.4) cancancan diff --git a/app/models/bounced_mail_address.rb b/app/models/bounced_mail_address.rb index 73c6a0941..d0411a6c9 100644 --- a/app/models/bounced_mail_address.rb +++ b/app/models/bounced_mail_address.rb @@ -1,5 +1,6 @@ class BouncedMailAddress < ApplicationRecord validates :email, :message_id, :bounce_type, :bounce_subtype, :action, :status, presence: true + after_destroy :destroy_aws_suppression def bounce_reason "#{action} (#{status} #{diagnostic})" @@ -25,4 +26,20 @@ class BouncedMailAddress < ApplicationRecord diagnostic: bounced_record['diagnosticCode'], } end + + def destroy_aws_suppression + return unless BouncedMailAddress.ses_configured? + + res = Aws::SESV2::Client.new.delete_suppressed_destination({ email_address: email }) + res.successful? + rescue Aws::SESV2::Errors::ServiceError => e + logger.warn("Suppression not removed. #{e}") + end + + def self.ses_configured? + ses ||= Aws::SES::Client.new + ses.config.credentials.access_key_id.present? + rescue Aws::Errors::MissingRegionError + false + end end diff --git a/config/initializers/aws_ses.rb b/config/initializers/aws_ses.rb new file mode 100644 index 000000000..baa148e65 --- /dev/null +++ b/config/initializers/aws_ses.rb @@ -0,0 +1,4 @@ +Aws.config.update( + region: ENV['aws_default_region'], + credentials: Aws::Credentials.new(ENV['aws_access_key_id'], ENV['aws_secret_access_key']) +) From f6fd10b01767bb3aa0421200ef9901ad0d387042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 8 Feb 2021 16:57:16 +0200 Subject: [PATCH 3/3] Fix CC issues --- Gemfile | 2 +- app/models/bounced_mail_address.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 79862bd69..82a0b4666 100644 --- a/Gemfile +++ b/Gemfile @@ -93,4 +93,4 @@ group :test do gem 'webmock' end -gem "aws-sdk-sesv2", "~> 1.16" +gem 'aws-sdk-sesv2', '~> 1.16' diff --git a/app/models/bounced_mail_address.rb b/app/models/bounced_mail_address.rb index d0411a6c9..db4413829 100644 --- a/app/models/bounced_mail_address.rb +++ b/app/models/bounced_mail_address.rb @@ -30,14 +30,14 @@ class BouncedMailAddress < ApplicationRecord def destroy_aws_suppression return unless BouncedMailAddress.ses_configured? - res = Aws::SESV2::Client.new.delete_suppressed_destination({ email_address: email }) + res = Aws::SESV2::Client.new.delete_suppressed_destination(email_address: email) res.successful? rescue Aws::SESV2::Errors::ServiceError => e logger.warn("Suppression not removed. #{e}") end def self.ses_configured? - ses ||= Aws::SES::Client.new + ses ||= Aws::SESV2::Client.new ses.config.credentials.access_key_id.present? rescue Aws::Errors::MissingRegionError false