Skip to content

Commit e90977c

Browse files
authored
Merge pull request #956 from sigstore/addSingingConfigClasses
Add signing config parsers
2 parents cccd2e0 + d44797f commit e90977c

File tree

7 files changed

+550
-0
lines changed

7 files changed

+550
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
public class SigstoreConfigurationException extends Exception {
19+
public SigstoreConfigurationException(String message) {
20+
super(message);
21+
}
22+
23+
public SigstoreConfigurationException(String message, Throwable cause) {
24+
super(message, cause);
25+
}
26+
27+
public SigstoreConfigurationException(Throwable cause) {
28+
super(cause);
29+
}
30+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 com.google.protobuf.util.JsonFormat;
19+
import dev.sigstore.proto.trustroot.v1.SigningConfig;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
import org.immutables.value.Value.Immutable;
27+
28+
/**
29+
* Sigstore configuration to identify signing infrastructure pieces and the policy for using them
30+
* during a singing event.
31+
*/
32+
@Immutable
33+
public interface SigstoreSigningConfig {
34+
35+
String MEDIA_TYPE = "application/vnd.dev.sigstore.signingconfig.v0.2+json";
36+
37+
List<Service> getCas();
38+
39+
List<Service> getTsas();
40+
41+
List<Service> getTLogs();
42+
43+
// The default sigstore provided oidc providers
44+
List<Service> getOidcProviders();
45+
46+
Service.Config getTsaConfig();
47+
48+
Service.Config getTLogConfig();
49+
50+
static SigstoreSigningConfig from(SigningConfig proto) throws SigstoreConfigurationException {
51+
if (!proto.getMediaType().equals(MEDIA_TYPE)) {
52+
throw new SigstoreConfigurationException(
53+
"Unsupported signing config mediaType: " + proto.getMediaType());
54+
}
55+
56+
return ImmutableSigstoreSigningConfig.builder()
57+
.addAllCas(protoToServiceList(proto.getCaUrlsList()))
58+
.addAllTLogs(protoToServiceList(proto.getRekorTlogUrlsList()))
59+
.addAllOidcProviders(protoToServiceList(proto.getOidcUrlsList()))
60+
.addAllTsas(protoToServiceList(proto.getTsaUrlsList()))
61+
.tsaConfig(Service.Config.from(proto.getTsaConfig()))
62+
.tLogConfig(Service.Config.from(proto.getRekorTlogConfig()))
63+
.build();
64+
}
65+
66+
static SigstoreSigningConfig from(InputStream json) throws SigstoreConfigurationException {
67+
var signingConfigBuilder = SigningConfig.newBuilder();
68+
try (var reader = new InputStreamReader(json, StandardCharsets.UTF_8)) {
69+
JsonFormat.parser().merge(reader, signingConfigBuilder);
70+
} catch (IOException ex) {
71+
throw new SigstoreConfigurationException("Could not parse signing configuration", ex);
72+
}
73+
return from(signingConfigBuilder.build());
74+
}
75+
76+
private static List<Service> protoToServiceList(
77+
List<dev.sigstore.proto.trustroot.v1.Service> serviceList) {
78+
return serviceList.stream().map(Service::from).collect(Collectors.toList());
79+
}
80+
}

0 commit comments

Comments
 (0)