Skip to content

Commit

Permalink
Add validity helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Appu Goundan <appu@google.com>
  • Loading branch information
loosebazooka committed Aug 8, 2023
1 parent 59dae85 commit bd41966
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<CertificateAuthority> find(Instant time) {
public CertificateAuthority current() {
var current =
getCertificateAuthorities().stream()
.filter(ca -> ca.getValidFor().getEnd().isEmpty())
.filter(CertificateAuthority::isCurrent)
.collect(Collectors.toList());
if (current.size() == 0) {
throw new IllegalStateException("Trust root contains no current certificate authorities");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@
import java.net.URI;
import java.security.cert.CertPath;
import java.security.cert.CertificateException;
import java.time.Instant;
import org.immutables.value.Value.Immutable;

@Immutable
public interface CertificateAuthority {
CertPath getCertPath();
public abstract class CertificateAuthority {
public abstract CertPath getCertPath();

URI getUri();
public abstract URI getUri();

ValidFor getValidFor();
public abstract ValidFor getValidFor();

Subject getSubject();
public abstract Subject getSubject();

static CertificateAuthority from(dev.sigstore.proto.trustroot.v1.CertificateAuthority proto)
public boolean isCurrent() {
return getValidFor().contains(Instant.now());
}

public static CertificateAuthority from(dev.sigstore.proto.trustroot.v1.CertificateAuthority proto)
throws CertificateException {
return ImmutableCertificateAuthority.builder()
.certPath(ProtoMutators.toCertPath(proto.getCertChain().getCertificatesList()))
Expand Down
19 changes: 15 additions & 4 deletions sigstore-java/src/main/java/dev/sigstore/trustroot/ValidFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@
import org.immutables.value.Value.Immutable;

@Immutable
public interface ValidFor {
Instant getStart();
public abstract class ValidFor {
public abstract Instant getStart();

Optional<Instant> getEnd();
public abstract Optional<Instant> getEnd();

static ValidFor from(TimeRange proto) {
public boolean contains(Instant instant) {
if (!getStart().isBefore(instant)) {
return false;
}
if (getEnd().isEmpty() || getEnd().get().isAfter(instant)) {
return true;
}
return false;
}

public static ValidFor from(TimeRange proto) {
return ImmutableValidFor.builder()
.start(ProtoMutators.toInstant(proto.getStart()))
.end(
Expand All @@ -36,4 +46,5 @@ static ValidFor from(TimeRange proto) {
: Optional.empty())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.sigstore.trustroot;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ValidForTest {

@Test
public void contains_withStartAndEnd() {
var start = Instant.now().minus(10, ChronoUnit.MINUTES);
var end = Instant.now().plus(10, ChronoUnit.MINUTES);
var range = ImmutableValidFor.builder().start(start).end(end).build();

Assertions.assertTrue(range.contains(Instant.now()));

Assertions.assertTrue(range.contains(start.plus(10, ChronoUnit.SECONDS)));
Assertions.assertFalse(range.contains(start));
Assertions.assertFalse(range.contains(start.minus(10, ChronoUnit.SECONDS)));

Assertions.assertTrue(range.contains(end.minus(10, ChronoUnit.SECONDS)));
Assertions.assertFalse(range.contains(end));
Assertions.assertFalse(range.contains(end.plus(10, ChronoUnit.SECONDS)));
}

public void contains_withNoEnd() {
var start = Instant.now().minus(10, ChronoUnit.MINUTES);
var range = ImmutableValidFor.builder().start(start).build();

Assertions.assertTrue(range.contains(Instant.now()));
Assertions.assertTrue(range.contains(Instant.now().plus(10, ChronoUnit.SECONDS)));

Assertions.assertTrue(range.contains(start.plus(10, ChronoUnit.SECONDS)));
Assertions.assertFalse(range.contains(start));
Assertions.assertFalse(range.contains(start.minus(10, ChronoUnit.SECONDS)));
}
}

0 comments on commit bd41966

Please sign in to comment.