Skip to content

Commit

Permalink
Test cases to verify that encrypted_value records are cleaned up when…
Browse files Browse the repository at this point in the history
… the crednetial is deleted.

- This will cause h2 and postgresql test to fail until the triggers are implemented for them.

[#182121168]
  • Loading branch information
hsinn0 committed Mar 8, 2024
1 parent e4009bb commit f3a4de3
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.cloudfoundry.credhub.entity.CredentialVersionData;
import org.cloudfoundry.credhub.entity.PasswordCredentialVersionData;
import org.cloudfoundry.credhub.entity.SshCredentialVersionData;
import org.cloudfoundry.credhub.entity.UserCredentialVersionData;
import org.cloudfoundry.credhub.entity.ValueCredentialVersionData;
import org.cloudfoundry.credhub.exceptions.MaximumSizeException;
import org.cloudfoundry.credhub.exceptions.ParameterizedValidationException;
import org.cloudfoundry.credhub.repositories.CredentialRepository;
import org.cloudfoundry.credhub.repositories.CredentialVersionRepository;
import org.cloudfoundry.credhub.repositories.EncryptedValueRepository;
import org.cloudfoundry.credhub.util.CurrentTimeProvider;
import org.cloudfoundry.credhub.utils.DatabaseProfileResolver;
import org.cloudfoundry.credhub.utils.DatabaseUtilities;
Expand Down Expand Up @@ -63,6 +65,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@ActiveProfiles(value = "unit-test", resolver = DatabaseProfileResolver.class)
Expand All @@ -76,6 +79,9 @@ public class DefaultCredentialVersionDataServiceTest {
@Autowired
private CredentialRepository credentialRepository;

@Autowired
private EncryptedValueRepository encryptedValueRepository;

@Autowired
private EncryptionKeyCanaryDataService encryptionKeyCanaryDataService;

Expand Down Expand Up @@ -265,13 +271,17 @@ public void save_whenGivenCredentialWithMetadata() {

@Test
public void delete_onAnExistingCredential_returnsTrue() {
long nEncryptedValuesPre = encryptedValueRepository.count();
credentialDataService.save(new Credential("/my-credential"));

assertThat(subject.delete("/my-credential"), equalTo(true));
assertEquals("Associated encryptedValues are deleted when credential is deleted",
nEncryptedValuesPre, encryptedValueRepository.count());
}

@Test
public void delete_onACredentialName_deletesAllCredentialsWithTheName() {
long nEncryptedValuesPre = encryptedValueRepository.count();
final Credential credential = credentialDataService
.save(new Credential("/my-credential"));

Expand Down Expand Up @@ -301,10 +311,13 @@ public void delete_onACredentialName_deletesAllCredentialsWithTheName() {

assertThat(subject.findAllByName("/my-credential"), hasSize(0));
assertNull(credentialDataService.find("/my-credential"));
assertEquals("Associated encryptedValues are deleted when password credential is deleted",
nEncryptedValuesPre, encryptedValueRepository.count());
}

@Test
public void delete_givenACredentialNameCasedDifferentlyFromTheActual_shouldBeCaseInsensitive() {
long nEncryptedValuesPre = encryptedValueRepository.count();
final Credential credentialName = credentialDataService
.save(new Credential("/my-credential"));

Expand Down Expand Up @@ -334,6 +347,36 @@ public void delete_givenACredentialNameCasedDifferentlyFromTheActual_shouldBeCas
subject.delete("/MY-CREDENTIAL");

assertThat(subject.findAllByName("/my-credential"), empty());
assertEquals("Associated encryptedValues are deleted when password credential is deleted",
nEncryptedValuesPre, encryptedValueRepository.count());
}

@Test
public void delete_UserTypeCredential() {
long nEncryptedValuesPre = encryptedValueRepository.count();
final Credential credential = credentialDataService
.save(new Credential("/my-credential"));

final EncryptedValue encryptedValueA = new EncryptedValue();
encryptedValueA.setEncryptionKeyUuid(activeCanaryUuid);
encryptedValueA.setEncryptedValue("credential-password".getBytes(UTF_8));
encryptedValueA.setNonce(new byte[]{});

final UserCredentialVersionData userCredential =
new UserCredentialVersionData("/test-user");
userCredential.setUsername("test-user");
userCredential.setCredential(credential);
userCredential.setEncryptedValueData(encryptedValueA);
userCredential.setSalt("salt");
subject.save(userCredential);

assertEquals("User credential is saved as expected", 1,
subject.findAllByName("/my-credential").size());

assertThat(subject.findAllByName("/my-credential"), hasSize(1));
subject.delete("/my-credential");
assertEquals("Associated encryptedValues are deleted when user credential is deleted",
nEncryptedValuesPre, encryptedValueRepository.count());
}

@Test
Expand Down

0 comments on commit f3a4de3

Please sign in to comment.