Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #50 from corona-warn-app/fix/search-and-delete
Browse files Browse the repository at this point in the history
Fix: Search Pending DCC and Cleanup
  • Loading branch information
f11h committed Jan 14, 2022
2 parents 05f9914 + 479976a commit 8fee024
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<liquibase.version>4.6.2</liquibase.version>
<springdoc.version>1.6.4</springdoc.version>
<cbor.version>4.5</cbor.version>
<hibernate-validator.version>7.0.2.Final</hibernate-validator.version>
<shedlock.version>4.30.0</shedlock.version>
<!-- plugins -->
<plugin.checkstyle.version>3.1.2</plugin.checkstyle.version>
Expand Down Expand Up @@ -216,11 +215,6 @@
<version>${spring-security.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public ResponseEntity<Void> uploadPublicKey(
} else if (e.getReason()
== DccRegistrationService.DccRegistrationException.Reason.REGISTRATION_TOKEN_ALREADY_EXISTS) {
throw new DccServerException(HttpStatus.CONFLICT, "RegistrationToken is already assigned with a PublicKey.");
} else if (e.getReason()
== DccRegistrationService.DccRegistrationException.Reason.NO_LAB_ID) {
throw new DccServerException(HttpStatus.FORBIDDEN, "Lab has not provided any LabId for this TestResult.");
} else {
throw new DccServerException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface DccRegistrationRepository extends JpaRepository<DccRegistration

Optional<DccRegistration> findByRegistrationToken(String registrationToken);

List<DccRegistration> findByLabIdAndDccHashIsNull(String labId);
List<DccRegistration> findByLabIdAndDccHashIsNullAndPublicKeyIsNotNull(String labId);

Optional<DccRegistration> findByHashedGuid(String hashedGuid);

Expand All @@ -48,11 +48,12 @@ public interface DccRegistrationRepository extends JpaRepository<DccRegistration
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("UPDATE DccRegistration d SET d.dcc = NULL, d.publicKey = NULL, d.encryptedDataEncryptionKey = NULL,"
+ " d.error = NULL, d.hashedGuid = NULL, d.dccEncryptedPayload = NULL"
+ " WHERE d.updatedAt < :threshold AND d.dcc IS NOT NULL")
+ " WHERE d.updatedAt < :threshold AND d.publicKey IS NOT NULL")
int removeDccDataByUpdatedAtBefore(@Param("threshold") LocalDateTime threshold);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("UPDATE DccRegistration d SET d.registrationToken = NULL"
@Query("UPDATE DccRegistration d SET d.registrationToken = NULL, d.dcc = NULL, d.publicKey = NULL,"
+ " d.encryptedDataEncryptionKey = NULL, d.error = NULL, d.hashedGuid = NULL, d.dccEncryptedPayload = NULL"
+ " WHERE d.createdAt < :threshold AND d.registrationToken IS NOT NULL")
int removeRegistrationTokenByCreatedAtBefore(@Param("threshold") LocalDateTime threshold);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public DccRegistration createDccRegistration(String registrationToken, PublicKey
* @return List of matching DCC Registrations.
*/
public List<DccRegistration> findPendingDccByLabId(String labId) {
return dccRegistrationRepository.findByLabIdAndDccHashIsNull(labId);
return dccRegistrationRepository.findByLabIdAndDccHashIsNullAndPublicKeyIsNotNull(labId);
}

/**
Expand Down Expand Up @@ -194,6 +194,10 @@ private InternalTestResult checkRegistrationTokenIsValid(String registrationToke
throw new DccRegistrationException(DccRegistrationException.Reason.INVALID_REGISTRATION_TOKEN_FORBIDDEN);
}

if (testResult.getLabId() == null) {
throw new DccRegistrationException(DccRegistrationException.Reason.NO_LAB_ID);
}

return testResult;
}

Expand Down Expand Up @@ -247,7 +251,8 @@ public enum Reason {
REGISTRATION_TOKEN_ALREADY_EXISTS,
INVALID_REGISTRATION_TOKEN_NOT_FOUND,
INVALID_REGISTRATION_TOKEN_FORBIDDEN,
VERIFICATION_SERVER_ERROR
VERIFICATION_SERVER_ERROR,
NO_LAB_ID
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static app.coronawarn.dcc.utils.TestValues.registrationTokenValue;
import static app.coronawarn.dcc.utils.TestValues.testId;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -100,9 +99,9 @@ void testUploadPublicKey() throws Exception {
registrationTokenValue, publicKeyBase64, "");

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isCreated());


Expand All @@ -120,9 +119,9 @@ void testUploadPublicKeyFailedInvalidPublicKey() throws Exception {
registrationTokenValue, publicKeyBase64, "");

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isBadRequest());
}

Expand All @@ -140,12 +139,31 @@ void testUploadPublicKeyInvalidRegistrationTokenForbidden() throws Exception {
.when(verificationServerClientMock).result(eq(registrationToken));

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isForbidden());
}

@Test
void testUploadPublicKeyInvalidRegistrationTokenNoLabId() throws Exception {
KeyPair keyPair = TestUtils.generateKeyPair();
String publicKeyBase64 = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());

UploadPublicKeyRequest uploadPublicKeyRequest = new UploadPublicKeyRequest(
registrationTokenValue, publicKeyBase64, "");

reset(verificationServerClientMock);

when(verificationServerClientMock.result(eq(registrationToken)))
.thenReturn(new InternalTestResult(6, null, testId, 0));

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isForbidden());
}

@Test
void testUploadPublicKeyInvalidRegistrationTokenNotFound() throws Exception {
Expand All @@ -161,9 +179,9 @@ void testUploadPublicKeyInvalidRegistrationTokenNotFound() throws Exception {
.when(verificationServerClientMock).result(eq(registrationToken));

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isNotFound());
}

Expand All @@ -182,9 +200,9 @@ void testUploadPublicKeyInvalidRegistrationTokenInternalServerError() throws Exc
.when(verificationServerClientMock).result(eq(registrationToken));

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isInternalServerError());
}

Expand All @@ -199,9 +217,9 @@ void testUploadPublicKeyFailedAlreadyExists() throws Exception {
registrationTokenValue, publicKeyBase64, "");

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isConflict());
}

Expand All @@ -219,9 +237,9 @@ void testUploadPublicKeyFailedTestResultPending() throws Exception {
.thenReturn(new InternalTestResult(0, labId, testId, 0));

mockMvc.perform(post("/version/v1/publicKey")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
)
.andExpect(status().isForbidden());
}

Expand Down

0 comments on commit 8fee024

Please sign in to comment.