-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(deps): update dependency org.postgresql:postgresql to v42.7.2 [79c1ed1b0] * Fix after rebase [df830cb78] * Simplify [6e853d9de] * Code smells [1815eb9eb] * Fixes [f4b703da4] * Code smells [236ad94f0] * Refactor duplicate ROA validations [76fb40114] * chore(deps): update dependency gradle to v8.6 [2e2addc0c]
- Loading branch information
RPKI Team at RIPE NCC
committed
Mar 8, 2024
1 parent
50451ac
commit 28aeef1
Showing
11 changed files
with
180 additions
and
198 deletions.
There are no files selected for viewing
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
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
Binary file not shown.
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
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
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
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,66 @@ | ||
package net.ripe.rpki.rest.service; | ||
|
||
import com.google.common.collect.Streams; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Value; | ||
import net.ripe.ipresource.Asn; | ||
import net.ripe.ipresource.IpRange; | ||
import net.ripe.rpki.commons.validation.roa.AllowedRoute; | ||
import net.ripe.rpki.commons.validation.roa.AnnouncedRoute; | ||
import net.ripe.rpki.rest.pojo.PublishSet; | ||
import net.ripe.rpki.rest.pojo.ROA; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Roas { | ||
|
||
public static Optional<String> validateUniqueROAs(String prefix, Map<AnnouncedRoute, List<Integer>> newOnes) { | ||
for (var e : newOnes.entrySet()) { | ||
var maxLengths = e.getValue(); | ||
if (maxLengths.size() > 1) { | ||
var sorted = maxLengths.stream().sorted().collect(Collectors.toList()); | ||
return Optional.of(String.format("%s: there are more than one pair (%s, %s), max lengths: %s", | ||
prefix, e.getKey().getOriginAsn(), e.getKey().getPrefix(), sorted)); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static RoaDiff toDiff(PublishSet publishSet) { | ||
return new RoaDiff( | ||
publishSet.getAdded().stream().map(Roas::toAllowedRoute).collect(Collectors.toSet()), | ||
publishSet.getDeleted().stream().map(Roas::toAllowedRoute).collect(Collectors.toSet()) | ||
); | ||
} | ||
|
||
private static AllowedRoute toAllowedRoute(ROA roa) { | ||
var roaIpRange = IpRange.parse(roa.getPrefix()); | ||
var maxLength = roa.getMaxLength() != null ? roa.getMaxLength() : roaIpRange.getPrefixLength(); | ||
return new AllowedRoute(Asn.parse(roa.getAsn()), roaIpRange, maxLength); | ||
} | ||
|
||
public static Set<AllowedRoute> applyDiff(Set<AllowedRoute> currentRoas, RoaDiff diff) { | ||
var futureRoas = new HashSet<>(currentRoas); | ||
diff.getDeleted().forEach(futureRoas::remove); | ||
futureRoas.addAll(diff.getAdded()); | ||
return futureRoas; | ||
} | ||
|
||
public static Optional<String> validateRoaUpdate(Set<AllowedRoute> futureRoutes) { | ||
var futureMap = futureRoutes.stream().collect(Collectors.toMap( | ||
r -> new AnnouncedRoute(r.getAsn(), r.getPrefix()), | ||
r -> Collections.singletonList(r.getMaximumLength()), | ||
(a, b) -> Streams.concat(a.stream(), b.stream()).collect(Collectors.toList()))); | ||
|
||
return validateUniqueROAs("Error in future ROAs", futureMap); | ||
} | ||
|
||
@Value | ||
public static class RoaDiff { | ||
Set<AllowedRoute> added; | ||
Set<AllowedRoute> deleted; | ||
} | ||
} |
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
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
Oops, something went wrong.