Skip to content

Commit a4f0cd6

Browse files
authored
Merge pull request #476 from sigstore/validfor-helpers
Add validity helpers
2 parents 8e5c680 + 7d3eabe commit a4f0cd6

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

sigstore-java/src/main/java/dev/sigstore/trustroot/CertificateAuthorities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public List<CertificateAuthority> find(Instant time) {
6363
public CertificateAuthority current() {
6464
var current =
6565
getCertificateAuthorities().stream()
66-
.filter(ca -> ca.getValidFor().getEnd().isEmpty())
66+
.filter(CertificateAuthority::isCurrent)
6767
.collect(Collectors.toList());
6868
if (current.size() == 0) {
6969
throw new IllegalStateException("Trust root contains no current certificate authorities");

sigstore-java/src/main/java/dev/sigstore/trustroot/CertificateAuthority.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@
1919
import java.net.URI;
2020
import java.security.cert.CertPath;
2121
import java.security.cert.CertificateException;
22+
import java.time.Instant;
2223
import org.immutables.value.Value.Immutable;
2324

2425
@Immutable
25-
public interface CertificateAuthority {
26-
CertPath getCertPath();
26+
public abstract class CertificateAuthority {
27+
public abstract CertPath getCertPath();
2728

28-
URI getUri();
29+
public abstract URI getUri();
2930

30-
ValidFor getValidFor();
31+
public abstract ValidFor getValidFor();
3132

32-
Subject getSubject();
33+
public abstract Subject getSubject();
3334

34-
static CertificateAuthority from(dev.sigstore.proto.trustroot.v1.CertificateAuthority proto)
35-
throws CertificateException {
35+
public boolean isCurrent() {
36+
return getValidFor().contains(Instant.now());
37+
}
38+
39+
public static CertificateAuthority from(
40+
dev.sigstore.proto.trustroot.v1.CertificateAuthority proto) throws CertificateException {
3641
return ImmutableCertificateAuthority.builder()
3742
.certPath(ProtoMutators.toCertPath(proto.getCertChain().getCertificatesList()))
3843
.validFor(ValidFor.from(proto.getValidFor()))

sigstore-java/src/main/java/dev/sigstore/trustroot/ValidFor.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@
2222
import org.immutables.value.Value.Immutable;
2323

2424
@Immutable
25-
public interface ValidFor {
26-
Instant getStart();
25+
public abstract class ValidFor {
26+
public abstract Instant getStart();
2727

28-
Optional<Instant> getEnd();
28+
public abstract Optional<Instant> getEnd();
2929

30-
static ValidFor from(TimeRange proto) {
30+
public boolean contains(Instant instant) {
31+
if (!getStart().isBefore(instant)) {
32+
return false;
33+
}
34+
if (getEnd().isEmpty() || getEnd().get().isAfter(instant)) {
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
public static ValidFor from(TimeRange proto) {
3141
return ImmutableValidFor.builder()
3242
.start(ProtoMutators.toInstant(proto.getStart()))
3343
.end(
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 The Sigstore Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.sigstore.trustroot;
17+
18+
import java.time.Instant;
19+
import java.time.temporal.ChronoUnit;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
class ValidForTest {
24+
25+
@Test
26+
public void contains_withStartAndEnd() {
27+
var start = Instant.now().minus(10, ChronoUnit.MINUTES);
28+
var end = Instant.now().plus(10, ChronoUnit.MINUTES);
29+
var range = ImmutableValidFor.builder().start(start).end(end).build();
30+
31+
Assertions.assertTrue(range.contains(Instant.now()));
32+
33+
Assertions.assertTrue(range.contains(start.plus(10, ChronoUnit.SECONDS)));
34+
Assertions.assertFalse(range.contains(start));
35+
Assertions.assertFalse(range.contains(start.minus(10, ChronoUnit.SECONDS)));
36+
37+
Assertions.assertTrue(range.contains(end.minus(10, ChronoUnit.SECONDS)));
38+
Assertions.assertFalse(range.contains(end));
39+
Assertions.assertFalse(range.contains(end.plus(10, ChronoUnit.SECONDS)));
40+
}
41+
42+
public void contains_withNoEnd() {
43+
var start = Instant.now().minus(10, ChronoUnit.MINUTES);
44+
var range = ImmutableValidFor.builder().start(start).build();
45+
46+
Assertions.assertTrue(range.contains(Instant.now()));
47+
Assertions.assertTrue(range.contains(Instant.now().plus(10, ChronoUnit.SECONDS)));
48+
49+
Assertions.assertTrue(range.contains(start.plus(10, ChronoUnit.SECONDS)));
50+
Assertions.assertFalse(range.contains(start));
51+
Assertions.assertFalse(range.contains(start.minus(10, ChronoUnit.SECONDS)));
52+
}
53+
}

0 commit comments

Comments
 (0)