|
| 1 | +/* |
| 2 | + * Copyright 2025 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 dev.sigstore.proto.trustroot.v1.ServiceConfiguration; |
| 19 | +import java.net.URI; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.OptionalInt; |
| 25 | +import org.immutables.value.Value.Immutable; |
| 26 | + |
| 27 | +@Immutable |
| 28 | +public interface Service { |
| 29 | + |
| 30 | + /** The URL of the remote service infrastructure piece. */ |
| 31 | + URI getUrl(); |
| 32 | + |
| 33 | + /** The API version of the service. */ |
| 34 | + int getApiVersion(); |
| 35 | + |
| 36 | + /** The validity window in which this service may be used for signing. */ |
| 37 | + ValidFor getValidFor(); |
| 38 | + |
| 39 | + static Service from(dev.sigstore.proto.trustroot.v1.Service service) { |
| 40 | + return ImmutableService.builder() |
| 41 | + .apiVersion(service.getMajorApiVersion()) |
| 42 | + .validFor(ValidFor.from(service.getValidFor())) |
| 43 | + .url(URI.create(service.getUrl())) |
| 44 | + .build(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * INTERNAL ONLY: Returns a default Service object for a url that is valid forever. Only used |
| 49 | + * transitionally as we adopt SigningConfig throughout the ecosystem. |
| 50 | + */ |
| 51 | + static Service of(URI url, int apiVersion) { |
| 52 | + return ImmutableService.builder() |
| 53 | + .apiVersion(apiVersion) |
| 54 | + .validFor(ImmutableValidFor.builder().start(Instant.now()).build()) |
| 55 | + .url(url) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Return a single service that is currently valid, that also exposes an api version supported by |
| 61 | + * this client. If multiple services match that criteria, filter by those services with the |
| 62 | + * highest apiVersion and further sort by services that were started most recently. |
| 63 | + * |
| 64 | + * @param services the service list |
| 65 | + * @param apiVersions an list of api version this clients supports |
| 66 | + * @return A service if found |
| 67 | + */ |
| 68 | + static Optional<Service> select(List<Service> services, List<Integer> apiVersions) { |
| 69 | + OptionalInt maxApiVersionMaybe = |
| 70 | + services.stream().mapToInt(Service::getApiVersion).filter(apiVersions::contains).max(); |
| 71 | + |
| 72 | + if (maxApiVersionMaybe.isEmpty()) { |
| 73 | + return Optional.empty(); |
| 74 | + } |
| 75 | + |
| 76 | + int maxApiVersion = maxApiVersionMaybe.getAsInt(); |
| 77 | + |
| 78 | + return services.stream() |
| 79 | + .filter(s -> s.getValidFor().contains(Instant.now())) |
| 80 | + .filter(s -> s.getApiVersion() == maxApiVersion) |
| 81 | + .max(Comparator.comparingLong(s -> s.getValidFor().getStart().toEpochMilli())); |
| 82 | + } |
| 83 | + |
| 84 | + @Immutable |
| 85 | + interface Config { |
| 86 | + enum Selector { |
| 87 | + ANY, |
| 88 | + EXACT, |
| 89 | + ALL |
| 90 | + } |
| 91 | + |
| 92 | + // the number to select when selector is EXACT |
| 93 | + OptionalInt getCount(); |
| 94 | + |
| 95 | + // the selector type |
| 96 | + Selector getSelector(); |
| 97 | + |
| 98 | + static Config from(ServiceConfiguration config) throws SigstoreConfigurationException { |
| 99 | + switch (config.getSelector()) { |
| 100 | + case ANY: |
| 101 | + return ImmutableConfig.builder().selector(Selector.ANY).build(); |
| 102 | + case EXACT: |
| 103 | + return ImmutableConfig.builder() |
| 104 | + .selector(Selector.EXACT) |
| 105 | + .count(config.getCount()) |
| 106 | + .build(); |
| 107 | + case ALL: |
| 108 | + return ImmutableConfig.builder().selector(Selector.ALL).build(); |
| 109 | + default: |
| 110 | + throw new SigstoreConfigurationException( |
| 111 | + "Cannot parse signing configuration selector: " + config.getSelector()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * INTERNAL ONLY: Returns the default config of ANY, only used transitionally as we adopt |
| 117 | + * SigningConfig throughout the ecosystem. |
| 118 | + */ |
| 119 | + static Config ofAny() { |
| 120 | + return ImmutableConfig.builder().selector(Selector.ANY).build(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments