|
13 | 13 | import org.elasticsearch.xpack.core.watcher.common.secret.Secret;
|
14 | 14 | import org.junit.Before;
|
15 | 15 |
|
| 16 | +import java.io.UnsupportedEncodingException; |
| 17 | +import java.time.ZonedDateTime; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
16 | 20 | import java.util.HashSet;
|
| 21 | +import java.util.List; |
17 | 22 | import java.util.Properties;
|
| 23 | +import java.util.Set; |
18 | 24 |
|
| 25 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 26 | +import static org.hamcrest.Matchers.containsString; |
19 | 27 | import static org.hamcrest.Matchers.hasEntry;
|
20 | 28 | import static org.hamcrest.Matchers.hasKey;
|
21 | 29 | import static org.hamcrest.Matchers.is;
|
@@ -113,4 +121,155 @@ public void testAccountSmtpPropertyConfiguration() {
|
113 | 121 | assertThat(properties5, hasEntry("mail.smtp.quitwait", "true"));
|
114 | 122 | assertThat(properties5, hasEntry("mail.smtp.ssl.trust", "host1,host2,host3"));
|
115 | 123 | }
|
| 124 | + |
| 125 | + public void testExtractDomains() throws Exception { |
| 126 | + Email email = new Email( |
| 127 | + "id", |
| 128 | + |
| 129 | + |
| 130 | + randomFrom(Email.Priority.values()), |
| 131 | + ZonedDateTime.now(), |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + "subject", |
| 136 | + "body", |
| 137 | + "htmlbody", |
| 138 | + Collections.emptyMap() |
| 139 | + ); |
| 140 | + assertThat( |
| 141 | + EmailService.getRecipientDomains(email), |
| 142 | + containsInAnyOrder("bar.com", "eggplant.com", "example.com", "another.com", "bcc.com") |
| 143 | + ); |
| 144 | + |
| 145 | + email = new Email( |
| 146 | + "id", |
| 147 | + |
| 148 | + |
| 149 | + randomFrom(Email.Priority.values()), |
| 150 | + ZonedDateTime.now(), |
| 151 | + |
| 152 | + null, |
| 153 | + null, |
| 154 | + "subject", |
| 155 | + "body", |
| 156 | + "htmlbody", |
| 157 | + Collections.emptyMap() |
| 158 | + ); |
| 159 | + assertThat(EmailService.getRecipientDomains(email), containsInAnyOrder("bar.com", "eggplant.com", "example.com")); |
| 160 | + } |
| 161 | + |
| 162 | + public void testAllowedDomain() throws Exception { |
| 163 | + Email email = new Email( |
| 164 | + "id", |
| 165 | + new Email. Address( "[email protected]", "Mr. Foo Man"), |
| 166 | + |
| 167 | + randomFrom(Email.Priority.values()), |
| 168 | + ZonedDateTime.now(), |
| 169 | + createAddressList( "[email protected]"), |
| 170 | + null, |
| 171 | + null, |
| 172 | + "subject", |
| 173 | + "body", |
| 174 | + "htmlbody", |
| 175 | + Collections.emptyMap() |
| 176 | + ); |
| 177 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("*"))); |
| 178 | + assertFalse(EmailService.recipientDomainsInAllowList(email, Set.of())); |
| 179 | + assertFalse(EmailService.recipientDomainsInAllowList(email, Set.of(""))); |
| 180 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("other.com", "bar.com"))); |
| 181 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("other.com", "*.com"))); |
| 182 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("*.CoM"))); |
| 183 | + |
| 184 | + // Invalid email in CC doesn't blow up |
| 185 | + email = new Email( |
| 186 | + "id", |
| 187 | + new Email. Address( "[email protected]", "Mr. Foo Man"), |
| 188 | + |
| 189 | + randomFrom(Email.Priority.values()), |
| 190 | + ZonedDateTime.now(), |
| 191 | + createAddressList( "[email protected]"), |
| 192 | + createAddressList("badEmail"), |
| 193 | + null, |
| 194 | + "subject", |
| 195 | + "body", |
| 196 | + "htmlbody", |
| 197 | + Collections.emptyMap() |
| 198 | + ); |
| 199 | + assertFalse(EmailService.recipientDomainsInAllowList(email, Set.of("other.com", "bar.com"))); |
| 200 | + |
| 201 | + // Check CC |
| 202 | + email = new Email( |
| 203 | + "id", |
| 204 | + new Email. Address( "[email protected]", "Mr. Foo Man"), |
| 205 | + |
| 206 | + randomFrom(Email.Priority.values()), |
| 207 | + ZonedDateTime.now(), |
| 208 | + createAddressList( "[email protected]"), |
| 209 | + createAddressList( "[email protected]"), |
| 210 | + null, |
| 211 | + "subject", |
| 212 | + "body", |
| 213 | + "htmlbody", |
| 214 | + Collections.emptyMap() |
| 215 | + ); |
| 216 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("other.com", "bar.com"))); |
| 217 | + assertFalse(EmailService.recipientDomainsInAllowList(email, Set.of("bar.com"))); |
| 218 | + |
| 219 | + // Check BCC |
| 220 | + email = new Email( |
| 221 | + "id", |
| 222 | + new Email. Address( "[email protected]", "Mr. Foo Man"), |
| 223 | + |
| 224 | + randomFrom(Email.Priority.values()), |
| 225 | + ZonedDateTime.now(), |
| 226 | + createAddressList( "[email protected]"), |
| 227 | + null, |
| 228 | + createAddressList( "[email protected]"), |
| 229 | + "subject", |
| 230 | + "body", |
| 231 | + "htmlbody", |
| 232 | + Collections.emptyMap() |
| 233 | + ); |
| 234 | + assertTrue(EmailService.recipientDomainsInAllowList(email, Set.of("other.com", "bar.com"))); |
| 235 | + assertFalse(EmailService.recipientDomainsInAllowList(email, Set.of("bar.com"))); |
| 236 | + } |
| 237 | + |
| 238 | + public void testSendEmailWithDomainNotInAllowList() throws Exception { |
| 239 | + service.updateAllowedDomains(Collections.singletonList(randomFrom("bar.*", "bar.com", "b*"))); |
| 240 | + Email email = new Email( |
| 241 | + "id", |
| 242 | + new Email. Address( "[email protected]", "Mr. Foo Man"), |
| 243 | + |
| 244 | + randomFrom(Email.Priority.values()), |
| 245 | + ZonedDateTime.now(), |
| 246 | + |
| 247 | + null, |
| 248 | + null, |
| 249 | + "subject", |
| 250 | + "body", |
| 251 | + "htmlbody", |
| 252 | + Collections.emptyMap() |
| 253 | + ); |
| 254 | + when(account.name()).thenReturn("account1"); |
| 255 | + Authentication auth = new Authentication("user", new Secret("passwd".toCharArray())); |
| 256 | + Profile profile = randomFrom(Profile.values()); |
| 257 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.send(email, auth, profile, "account1")); |
| 258 | + assertThat( |
| 259 | + e.getMessage(), |
| 260 | + containsString( |
| 261 | + "failed to send email with subject [subject] and recipient domains " |
| 262 | + + "[bar.com, invalid.com], one or more recipients is not specified in the domain allow list setting " |
| 263 | + + "[xpack.notification.email.account.domain_allowlist]." |
| 264 | + ) |
| 265 | + ); |
| 266 | + } |
| 267 | + |
| 268 | + private static Email.AddressList createAddressList(String... emails) throws UnsupportedEncodingException { |
| 269 | + List<Email.Address> addresses = new ArrayList<>(); |
| 270 | + for (String email : emails) { |
| 271 | + addresses.add(new Email.Address(email, randomAlphaOfLength(10))); |
| 272 | + } |
| 273 | + return new Email.AddressList(addresses); |
| 274 | + } |
116 | 275 | }
|
0 commit comments