Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Add ses module #669

Merged
merged 6 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/_configprops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
|cloud.aws.stack.auto | true | Enables the automatic stack name detection for the application.
|cloud.aws.stack.enabled | true | Enables Stack integration.
|cloud.aws.stack.name | | The name of the manually configured stack name that will be used to retrieve the resources.
|spring.cloud.aws.ses.region || The specific region for SES integration.

|===
15 changes: 15 additions & 0 deletions docs/src/main/asciidoc/ses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ an SES service where the client is overridden to use a valid region (EU-WEST-1).
</beans>
----

Since 2.3, if using spring-boot, the existing auto-configuration offers a specific configuration property to set the
region for the SES client.

[source,properties,indent=0]
----
cloud.aws.mail.region=eu-west-1
----

Also, there is a new starter `spring-cloud-starter-aws-ses` and it offers a specific configuration property.

[source,properties,indent=0]
----
spring.cloud.aws.ses.region=eu-west-1
----

=== Authenticating e-mails
To avoid any spam attacks on the Amazon SES mail service, applications without production access must
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-email-addresses.html[verify] each
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
<module>spring-cloud-aws-autoconfigure</module>
<module>spring-cloud-aws-parameter-store-config</module>
<module>spring-cloud-aws-secrets-manager-config</module>
<module>spring-cloud-aws-ses</module>
<module>spring-cloud-starter-aws</module>
<module>spring-cloud-starter-aws-jdbc</module>
<module>spring-cloud-starter-aws-messaging</module>
<module>spring-cloud-starter-aws-parameter-store-config</module>
<module>spring-cloud-starter-aws-secrets-manager-config</module>
<module>spring-cloud-starter-aws-ses</module>
<module>spring-cloud-aws-integration-test</module>
<module>docs</module>
</modules>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-aws-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>spring-cloud-aws-parameter-store-config</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-ses</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.aws.autoconfigure.mail;

import javax.mail.Session;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration;
import org.springframework.cloud.aws.context.annotation.ConditionalOnMissingAmazonClient;
import org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean;
import org.springframework.cloud.aws.core.region.RegionProvider;
import org.springframework.cloud.aws.core.region.StaticRegionProvider;
import org.springframework.cloud.aws.ses.SimpleEmailServiceJavaMailSender;
import org.springframework.cloud.aws.ses.SimpleEmailServiceMailSender;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;

/**
* {@link EnableAutoConfiguration Auto-configuration} for AWS Simple Email Service
* support.
*
* @author Agim Emruli
* @author Eddú Meléndez
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(MailSenderAutoConfiguration.class)
@AutoConfigureBefore(SimpleEmailAutoConfiguration.class)
@ConditionalOnClass({ AmazonSimpleEmailService.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Import(ContextCredentialsAutoConfiguration.class)
@EnableConfigurationProperties(SesProperties.class)
@ConditionalOnProperty(name = "spring.cloud.aws.ses.enabled", havingValue = "true", matchIfMissing = true)
public class SesAutoConfiguration {

private final AWSCredentialsProvider credentialsProvider;

private final RegionProvider regionProvider;

public SesAutoConfiguration(ObjectProvider<RegionProvider> regionProvider,
ObjectProvider<AWSCredentialsProvider> credentialsProvider, SesProperties properties) {
this.credentialsProvider = credentialsProvider.getIfAvailable();
this.regionProvider = properties.getRegion() == null ? regionProvider.getIfAvailable()
: new StaticRegionProvider(properties.getRegion());
}

@Bean
@ConditionalOnMissingAmazonClient(AmazonSimpleEmailService.class)
public AmazonWebserviceClientFactoryBean<AmazonSimpleEmailServiceClient> amazonSimpleEmailService() {
return new AmazonWebserviceClientFactoryBean<>(AmazonSimpleEmailServiceClient.class, this.credentialsProvider,
this.regionProvider);
}

@Bean
@ConditionalOnMissingClass("javax.mail.Session")
public MailSender simpleMailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
return new SimpleEmailServiceMailSender(amazonSimpleEmailService);
}

@Bean
@ConditionalOnClass(Session.class)
public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.aws.autoconfigure.mail;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for AWS Simple Email Service.
*
* @author Eddú Meléndez
*/
@ConfigurationProperties(prefix = "spring.cloud.aws.ses")
public class SesProperties {

/**
* Overrides the default region.
*/
private String region;

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
"defaultValue": true,
"name": "cloud.aws.mail.enabled",
"description": "Enables Mail integration.",
"type": "java.lang.Boolean"
"type": "java.lang.Boolean",
"deprecation": {
"level": "warning",
"replacement": "spring.cloud.aws.ses.enabled"
}
},
{
"defaultValue": true,
Expand All @@ -63,6 +67,12 @@
"name": "spring.cloud.aws.security.cognito.enabled",
"description": "Enables Cognito integration.",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "spring.cloud.aws.ses.enabled",
"description": "Enables Simple Email Service integration.",
"type": "java.lang.Boolean"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ org.springframework.cloud.aws.autoconfigure.context.ContextResourceLoaderAutoCon
org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.mail.SimpleEmailAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.cache.ElastiCacheAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.mail.SesAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.messaging.SqsAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.messaging.SnsAutoConfiguration,\
org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.aws.autoconfigure.mail;

import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;

class SesAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SesAutoConfiguration.class));

@Test
void mailSenderWithJavaMail() {
this.contextRunner.run(context -> {
assertThat(context.getBean(MailSender.class)).isNotNull();
assertThat(context.getBean(JavaMailSender.class)).isNotNull();
assertThat(context.getBean(JavaMailSender.class)).isSameAs(context.getBean(MailSender.class));
});
}

@Test
void mailSenderWithSimpleEmail() {
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> {
assertThat(context.getBean(MailSender.class)).isNotNull();
assertThat(context.getBean("simpleMailSender")).isNotNull();
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class));
});
}

@Test
void mailSenderWithDefaultRegion() {
this.contextRunner.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> {
assertThat(context.getBean(MailSender.class)).isNotNull();
assertThat(context.getBean("simpleMailSender")).isNotNull();
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class));

AmazonSimpleEmailServiceClient client = context.getBean(AmazonSimpleEmailServiceClient.class);
Object region = ReflectionTestUtils.getField(client, "signingRegion");
assertThat(region).isEqualTo("us-west-2");
});
}

@Test
void mailSenderWithCustomRegion() {
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.region:us-east-1")
.withClassLoader(new FilteredClassLoader("javax.mail.Session")).run(context -> {
assertThat(context.getBean(MailSender.class)).isNotNull();
assertThat(context.getBean("simpleMailSender")).isNotNull();
assertThat(context.getBean("simpleMailSender")).isSameAs(context.getBean(MailSender.class));

AmazonSimpleEmailServiceClient client = context.getBean(AmazonSimpleEmailServiceClient.class);
Object region = ReflectionTestUtils.getField(client, "signingRegion");
assertThat(region).isEqualTo("us-east-1");
});
}

@Test
void sesAutoConfigurationIsDisabled() {
this.contextRunner.withPropertyValues("spring.cloud.aws.ses.enabled:false").run(context -> {
assertThat(context).doesNotHaveBean(MailSender.class);
assertThat(context).doesNotHaveBean(JavaMailSender.class);
});
}

@Test
void sesAutoConfigurationIsDisabledButSimpleEmailAutoConfigurationIsEnabled() {
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(SesAutoConfiguration.class, SimpleEmailAutoConfiguration.class))
.withPropertyValues("spring.cloud.aws.ses.enabled:false").run(context -> {
assertThat(context).hasSingleBean(MailSender.class);
assertThat(context).hasSingleBean(JavaMailSender.class);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*
* @author Agim Emruli
* @since 1.0
* @deprecated Use `spring-cloud-starter-aws-ses`
*/
public class SimpleEmailServiceJavaMailSender extends SimpleEmailServiceMailSender implements JavaMailSender {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* text body.
*
* @author Agim Emruli
* @deprecated Use `spring-cloud-starter-aws-ses`
*/
public class SimpleEmailServiceMailSender implements MailSender, DisposableBean {

Expand Down
10 changes: 10 additions & 0 deletions spring-cloud-aws-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-ses</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-context</artifactId>
Expand Down Expand Up @@ -139,6 +144,11 @@
<artifactId>spring-cloud-aws-secrets-manager-config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-ses</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
Expand Down
Loading