Return HTTP/429 whenever somebody tries to get contacts from the old directory system.
This commit is contained in:
		
							parent
							
								
									e440eb1733
								
							
						
					
					
						commit
						9412a7424c
					
				| 
						 | 
					@ -138,18 +138,7 @@ public class DirectoryController {
 | 
				
			||||||
  public Response getTokenPresence(@Auth Account account, @PathParam("token") String token)
 | 
					  public Response getTokenPresence(@Auth Account account, @PathParam("token") String token)
 | 
				
			||||||
      throws RateLimitExceededException
 | 
					      throws RateLimitExceededException
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    rateLimiters.getContactsLimiter().validate(account.getNumber());
 | 
					    return Response.status(429).build();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
      Optional<ClientContact> contact = directory.get(decodeToken(token));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (contact.isPresent()) return Response.ok().entity(contact.get()).build();
 | 
					 | 
				
			||||||
      else                     return Response.status(404).build();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    } catch (IOException e) {
 | 
					 | 
				
			||||||
      logger.info("Bad token", e);
 | 
					 | 
				
			||||||
      return Response.status(404).build();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Timed
 | 
					  @Timed
 | 
				
			||||||
| 
						 | 
					@ -157,37 +146,12 @@ public class DirectoryController {
 | 
				
			||||||
  @Path("/tokens")
 | 
					  @Path("/tokens")
 | 
				
			||||||
  @Produces(MediaType.APPLICATION_JSON)
 | 
					  @Produces(MediaType.APPLICATION_JSON)
 | 
				
			||||||
  @Consumes(MediaType.APPLICATION_JSON)
 | 
					  @Consumes(MediaType.APPLICATION_JSON)
 | 
				
			||||||
  public ClientContacts getContactIntersection(@Auth Account account,
 | 
					  public Response getContactIntersection(@Auth Account account,
 | 
				
			||||||
                                               @HeaderParam("X-Forwarded-For") String forwardedFor,
 | 
					                                               @HeaderParam("X-Forwarded-For") String forwardedFor,
 | 
				
			||||||
                                               @Valid ClientContactTokens contacts)
 | 
					                                               @Valid ClientContactTokens contacts)
 | 
				
			||||||
      throws RateLimitExceededException
 | 
					      throws RateLimitExceededException
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    String requester = Arrays.stream(forwardedFor.split(","))
 | 
					    return Response.status(429).build();
 | 
				
			||||||
                             .map(String::trim)
 | 
					 | 
				
			||||||
                             .reduce((a, b) -> b)
 | 
					 | 
				
			||||||
                             .orElseThrow();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (Stream.of(FRONTED_REGIONS).noneMatch(region -> account.getNumber().startsWith(region))) {
 | 
					 | 
				
			||||||
      rateLimiters.getContactsIpLimiter().validate(requester);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    rateLimiters.getContactsLimiter().validate(account.getNumber(), contacts.getContacts().size());
 | 
					 | 
				
			||||||
    contactsHistogram.update(contacts.getContacts().size());
 | 
					 | 
				
			||||||
    contactsMeter.mark(contacts.getContacts().size());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
      List<byte[]> tokens = new LinkedList<>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      for (String encodedContact : contacts.getContacts()) {
 | 
					 | 
				
			||||||
        tokens.add(decodeToken(encodedContact));
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      List<ClientContact> intersection = directory.get(tokens);
 | 
					 | 
				
			||||||
      return new ClientContacts(intersection);
 | 
					 | 
				
			||||||
    } catch (IOException e) {
 | 
					 | 
				
			||||||
      logger.info("Bad token", e);
 | 
					 | 
				
			||||||
      throw new WebApplicationException(Response.status(400).build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private byte[] decodeToken(String encoded) throws IOException {
 | 
					  private byte[] decodeToken(String encoded) throws IOException {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -190,9 +190,6 @@ public class DirectoryControllerTest {
 | 
				
			||||||
                 .put(Entity.entity(new ClientContactTokens(tokens), MediaType.APPLICATION_JSON_TYPE));
 | 
					                 .put(Entity.entity(new ClientContactTokens(tokens), MediaType.APPLICATION_JSON_TYPE));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    assertThat(response.getStatus()).isEqualTo(200);
 | 
					    assertThat(response.getStatus()).isEqualTo(429);
 | 
				
			||||||
    assertThat(response.readEntity(ClientContactTokens.class).getContacts()).isEqualTo(expectedResponse);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    verify(ipLimiter).validate("1.1.1.1");
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue