-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add url validation when updating recipients. If existing key and url (#…
…717) has changed then fail validation.
- Loading branch information
Showing
4 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
tessera-core/src/main/java/com/quorum/tessera/node/PartyInfoRecipientUpdateCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
package com.quorum.tessera.node; | ||
|
||
import com.quorum.tessera.encryption.PublicKey; | ||
import com.quorum.tessera.node.model.PartyInfo; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class PartyInfoRecipientUpdateCheck { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(PartyInfoRecipientUpdateCheck.class); | ||
|
||
private final PartyInfo existingPartyInfo; | ||
|
||
private final PartyInfo newPartyInfo; | ||
|
||
public PartyInfoRecipientUpdateCheck(PartyInfo existingPartyInfo, PartyInfo newPartyInfo) { | ||
this.existingPartyInfo = Objects.requireNonNull(existingPartyInfo); | ||
this.newPartyInfo = Objects.requireNonNull(newPartyInfo); | ||
} | ||
|
||
public boolean validateKeysToUrls() { | ||
|
||
final Map<PublicKey, String> existingRecipientKeyUrlMap = existingPartyInfo.getRecipients() | ||
.stream() | ||
.collect(Collectors.toMap(r -> r.getKey(), r -> r.getUrl())); | ||
|
||
final Map<PublicKey, String> newRecipientKeyUrlMap = newPartyInfo.getRecipients() | ||
.stream() | ||
.collect(Collectors.toMap(r -> r.getKey(), r -> r.getUrl())); | ||
|
||
for(Map.Entry<PublicKey,String> e : newRecipientKeyUrlMap.entrySet()) { | ||
PublicKey key = e.getKey(); | ||
if(existingRecipientKeyUrlMap.containsKey(key)) { | ||
|
||
String existingUrl = existingRecipientKeyUrlMap.get(key); | ||
String newUrl = newRecipientKeyUrlMap.get(key); | ||
if(!existingUrl.equalsIgnoreCase(newUrl)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tessera-core/src/test/java/com/quorum/tessera/node/PartyInfoRecipientUpdateCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
package com.quorum.tessera.node; | ||
|
||
import com.quorum.tessera.encryption.PublicKey; | ||
import com.quorum.tessera.node.model.PartyInfo; | ||
import com.quorum.tessera.node.model.Recipient; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.junit.Test; | ||
|
||
|
||
public class PartyInfoRecipientUpdateCheckTest { | ||
|
||
|
||
@Test | ||
public void validateEmpty() { | ||
|
||
String url = "http://somedomain.com"; | ||
|
||
Set<Recipient> existingRecipients = new HashSet<>(); | ||
Set<Recipient> newRecipients = new HashSet<>(); | ||
PartyInfo existingPartyInfo = new PartyInfo(url, existingRecipients, Collections.EMPTY_SET); | ||
PartyInfo newPartyInfo = new PartyInfo(url, newRecipients, Collections.EMPTY_SET); | ||
PartyInfoRecipientUpdateCheck check = new PartyInfoRecipientUpdateCheck(existingPartyInfo,newPartyInfo); | ||
|
||
assertThat(check.validateKeysToUrls()).isTrue(); | ||
|
||
} | ||
|
||
@Test | ||
public void attemptToChangeUrl() { | ||
|
||
String url = "http://somedomain.com"; | ||
|
||
PublicKey key = PublicKey.from("ONE".getBytes()); | ||
|
||
Recipient existingRecipient = new Recipient(key,"http://one.com"); | ||
Set<Recipient> existingRecipients = Collections.singleton(existingRecipient); | ||
|
||
Recipient newRecipient = new Recipient(key,"http://two.com"); | ||
Set<Recipient> newRecipients = Collections.singleton(newRecipient); | ||
|
||
|
||
PartyInfo existingPartyInfo = new PartyInfo(url, existingRecipients, Collections.EMPTY_SET); | ||
PartyInfo newPartyInfo = new PartyInfo(url, newRecipients, Collections.EMPTY_SET); | ||
|
||
PartyInfoRecipientUpdateCheck check = new PartyInfoRecipientUpdateCheck(existingPartyInfo,newPartyInfo); | ||
assertThat(check.validateKeysToUrls()).isFalse(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters