This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit fa35a53ef80501aca7b38bd23011afaec9c1460f Author: Gautier DI FOLCO <[email protected]> AuthorDate: Mon Mar 16 14:27:48 2020 +0100 JAMES-3118 Be more explicit when an alias creation fails --- .../apache/james/webadmin/routes/AliasRoutes.java | 22 +++++++++++++++--- .../james/webadmin/routes/AliasRoutesTest.java | 27 +++++++++++++++++++--- src/site/markdown/server/manage-webadmin.md | 2 +- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java index 5cd31ac..8905b3b 100644 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java @@ -32,8 +32,11 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import org.apache.james.core.Domain; import org.apache.james.core.MailAddress; import org.apache.james.core.Username; +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.domainlist.api.DomainListException; import org.apache.james.rrt.api.MappingAlreadyExistsException; import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTableException; @@ -83,13 +86,15 @@ public class AliasRoutes implements Routes { private static final String ADDRESS_TYPE = "alias"; private final UsersRepository usersRepository; + private final DomainList domainList; private final JsonTransformer jsonTransformer; private final RecipientRewriteTable recipientRewriteTable; @Inject @VisibleForTesting - AliasRoutes(RecipientRewriteTable recipientRewriteTable, UsersRepository usersRepository, JsonTransformer jsonTransformer) { + AliasRoutes(RecipientRewriteTable recipientRewriteTable, UsersRepository usersRepository, DomainList domainList, JsonTransformer jsonTransformer) { this.usersRepository = usersRepository; + this.domainList = domainList; this.jsonTransformer = jsonTransformer; this.recipientRewriteTable = recipientRewriteTable; } @@ -140,14 +145,15 @@ public class AliasRoutes implements Routes { @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = ALIAS_DESTINATION_ADDRESS + " or alias structure format is not valid"), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The alias source exists as an user already"), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Source and destination can't be the same!"), - @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain in the source is not managed by the DomainList"), + @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain in the destination or source is not managed by the DomainList"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) - public HaltException addAlias(Request request, Response response) throws UsersRepositoryException, RecipientRewriteTableException { + public HaltException addAlias(Request request, Response response) throws UsersRepositoryException, RecipientRewriteTableException, DomainListException { MailAddress aliasSourceAddress = MailAddressParser.parseMailAddress(request.params(ALIAS_SOURCE_ADDRESS), ADDRESS_TYPE); ensureUserDoesNotExist(aliasSourceAddress); MailAddress destinationAddress = MailAddressParser.parseMailAddress(request.params(ALIAS_DESTINATION_ADDRESS), ADDRESS_TYPE); + ensureDomainIsSupported(destinationAddress.getDomain()); MappingSource source = MappingSource.fromUser(Username.fromMailAddress(aliasSourceAddress)); addAlias(source, destinationAddress); return halt(HttpStatus.NO_CONTENT_204); @@ -167,6 +173,16 @@ public class AliasRoutes implements Routes { } } + private void ensureDomainIsSupported(Domain domain) throws DomainListException { + if (!domainList.containsDomain(domain)) { + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message("Domain in the destination is not managed by the DomainList") + .haltError(); + } + } + private void ensureUserDoesNotExist(MailAddress mailAddress) throws UsersRepositoryException { Username username = usersRepository.getUser(mailAddress); diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java index 64ebf18..2ef976e 100644 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java @@ -123,7 +123,7 @@ class AliasRoutesTest { usersRepository.addUser(Username.of(BOB_WITH_SLASH), BOB_WITH_SLASH_PASSWORD); usersRepository.addUser(Username.of(ALICE), ALICE_PASSWORD); - createServer(new AliasRoutes(memoryRecipientRewriteTable, usersRepository, new JsonTransformer(module))); + createServer(new AliasRoutes(memoryRecipientRewriteTable, usersRepository, domainList, new JsonTransformer(module))); } @Test @@ -424,13 +424,13 @@ class AliasRoutesTest { domainList = mock(DomainList.class); memoryRecipientRewriteTable.setDomainList(domainList); Mockito.when(domainList.containsDomain(any())).thenReturn(true); - createServer(new AliasRoutes(memoryRecipientRewriteTable, userRepository, new JsonTransformer())); + createServer(new AliasRoutes(memoryRecipientRewriteTable, userRepository, domainList, new JsonTransformer())); } @Test void putAliasSourceContainingNotManagedDomainShouldReturnBadRequest() throws Exception { Mockito.when(domainList.containsDomain(any())) - .thenReturn(false); + .thenAnswer(invocation -> invocation.getArgument(0, Domain.class).equals(DOMAIN)); Map<String, Object> errors = when() .put(BOB + SEPARATOR + "sources" + SEPARATOR + "[email protected]") @@ -449,6 +449,27 @@ class AliasRoutesTest { } @Test + void putAliasDestinationContainingNotManagedDomainShouldReturnBadRequest() throws Exception { + Mockito.when(domainList.containsDomain(any())) + .thenAnswer(invocation -> invocation.getArgument(0, Domain.class).equals(DOMAIN)); + + Map<String, Object> errors = when() + .put("[email protected]" + SEPARATOR + "sources" + SEPARATOR + BOB) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .contentType(ContentType.JSON) + .extract() + .body() + .jsonPath() + .getMap("."); + + assertThat(errors) + .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) + .containsEntry("type", "InvalidArgument") + .containsEntry("message", "Domain in the destination is not managed by the DomainList"); + } + + @Test void putMalformedUserDestinationShouldReturnBadRequest() { Map<String, Object> errors = when() .put("not-an-address" + SEPARATOR + "sources" + SEPARATOR + BOB_ALIAS) diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md index 0b84b2f..0a937fe 100644 --- a/src/site/markdown/server/manage-webadmin.md +++ b/src/site/markdown/server/manage-webadmin.md @@ -1859,7 +1859,7 @@ Response codes: - 400: Alias structure or member is not valid - 400: The alias source exists as an user already - 400: Source and destination can't be the same! - - 400: Domain in the source is not managed by the DomainList + - 400: Domain in the destination or source is not managed by the DomainList ### Removing an alias of an user --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
