Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validity helpers #476

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 +19,25 @@
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)
throws CertificateException {
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()))
.validFor(ValidFor.from(proto.getValidFor()))
Expand Down
18 changes: 14 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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 The Sigstore Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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)));
}
}