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

Adding owner support to ClientDAO #1167

Merged
merged 4 commits into from Dec 6, 2022
Merged

Conversation

mmontgomery-square
Copy link
Contributor

No description provided.

@mmontgomery-square mmontgomery-square requested a review from a team as a code owner December 5, 2022 07:09
@coveralls
Copy link

coveralls commented Dec 5, 2022

Coverage Status

Coverage increased (+0.008%) to 77.253% when pulling c0651c5 on mmontgomery/datasec-766 into 62f0575 on master.

Comment on lines 74 to +75
@JsonProperty
private final String owner;
private String owner;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the last PR: making the owner field writable simplifies the DAO layer code.

Comment on lines 180 to 187
private Optional<Client> getClient(Condition condition) {
Record record = dslContext
.select(CLIENTS.fields())
.select(CLIENT_OWNERS.NAME)
.from(CLIENTS)
.leftJoin(CLIENT_OWNERS)
.on(CLIENTS.OWNER.eq(CLIENT_OWNERS.ID))
.where(condition)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a little fancier than the GroupDAO PR by parameterizing the where clause.

Comment on lines +156 to +171
@Test public void deleteSetsClientOwnerToNull() {
String ownerName = randomName();
long ownerId = createGroup(ownerName, NO_OWNER);

String clientName = randomName();
long clientId = createClient(clientName, ownerId);

Client beforeWithOwner = getClientById(clientId);
assertEquals(ownerName, beforeWithOwner.getOwner());

Group owner = getGroupById(ownerId);
groupDAO.deleteGroup(owner);

Client afterWithoutOwner = getClientById(clientId);
assertNull(afterWithoutOwner.getOwner());
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that this test is unnecessary. Because we do a left join on the owning group and then only populate the owner name if the owner is present, a "dangling" owner ID field doesn't cause a problem. Of course, that's bad, because it masks an underlying issue. I think the solution is to add a sanity check in the record conversion code that will barf if the owner ID is present but the owning group is not.

Comment on lines +201 to +208
boolean danglingOwner = clientRecord.getOwner() != null && ownerRecord.getId() == null;
if (danglingOwner) {
throw new IllegalStateException(
String.format(
"Owner %s for client %s is missing.",
clientRecord.getOwner(),
clientRecord.getName()));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic fixes a bug I'd discussed in another comment: the left join will present a null owner name if the owner is missing regardless of whether the owner ID in the client is populated or not. Because we don't enforce referential integrity in the database, we have to hand-manage it here.

Copy link
Contributor

@spennymac spennymac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I left a few suggestions.

server/src/main/java/keywhiz/service/daos/ClientDAO.java Outdated Show resolved Hide resolved
server/src/main/java/keywhiz/service/daos/ClientDAO.java Outdated Show resolved Hide resolved
mmontgomery-square and others added 2 commits December 5, 2022 15:51
Co-authored-by: Spencer McCreary <spencer_mccreary@compulsivesoftware.com>
Co-authored-by: Spencer McCreary <spencer_mccreary@compulsivesoftware.com>
@mmontgomery-square mmontgomery-square merged commit 2aae402 into master Dec 6, 2022
@mmontgomery-square mmontgomery-square deleted the mmontgomery/datasec-766 branch December 6, 2022 04:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants