Skip to content

Commit de5a8aa

Browse files
committed
Apply httpparams more universally
- add config to forbid users from using transport directly - make tuf/oidc clients use requestfactory when possible Signed-off-by: Appu Goundan <[email protected]>
1 parent c053464 commit de5a8aa

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

config/forbiddenApis.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
com.google.protobuf.util.JsonFormat#parser() @ Use dev.sigstore.json.ProtoJson#parser() instead
2+
dev.sigstore.http.HttpClients#newHttpTransport(dev.sigstore.http.HttpParams) @ Use dev.sigstore.http.HttpClients#newRequestFactory(...) instead

sigstore-java/src/main/java/dev/sigstore/http/HttpClients.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
import com.google.api.client.http.HttpTransport;
2121
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
2222
import com.google.api.client.util.ExponentialBackOff;
23+
import com.google.api.client.util.ObjectParser;
24+
import dev.sigstore.forbidden.SuppressForbidden;
2325
import java.io.IOException;
26+
import javax.annotation.Nullable;
2427
import org.apache.http.conn.ssl.NoopHostnameVerifier;
2528
import org.apache.http.impl.client.HttpClientBuilder;
2629

2730
/** HttpClients generates Google Http Client objects from configuration. */
2831
public class HttpClients {
2932

3033
/**
31-
* Build a transport, you probably want to use {@link #newRequestFactory} to instantiate GET and
32-
* POST requests.
34+
* Build a transport, you probably want to use {@link #newRequestFactory(HttpParams)} to
35+
* instantiate GET and POST requests or use {@link #newRequestFactory(HttpParams, ObjectParser) if
36+
* you need to also configure the response parser}.
3337
*/
3438
public static HttpTransport newHttpTransport(HttpParams httpParams) {
3539
HttpClientBuilder hcb =
@@ -41,7 +45,15 @@ public static HttpTransport newHttpTransport(HttpParams httpParams) {
4145
}
4246

4347
/** Create a new get requests with the httpParams applied and retries. */
48+
@SuppressForbidden(reason = "HttpClients#newHttpTransport(HttpParams)")
4449
public static HttpRequestFactory newRequestFactory(HttpParams httpParams) throws IOException {
50+
return newRequestFactory(httpParams, null);
51+
}
52+
53+
/** Create a new get requests with the httpParams applied, retries and a response parser. */
54+
@SuppressForbidden(reason = "HttpClients#newHttpTransport(HttpParams)")
55+
public static HttpRequestFactory newRequestFactory(
56+
HttpParams httpParams, @Nullable ObjectParser responseParser) throws IOException {
4557
return HttpClients.newHttpTransport(httpParams)
4658
.createRequestFactory(
4759
request -> {
@@ -52,6 +64,9 @@ public static HttpRequestFactory newRequestFactory(HttpParams httpParams) throws
5264
UnsuccessfulResponseHandler.newUnsuccessfulResponseHandler());
5365
request.setIOExceptionHandler(
5466
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
67+
if (responseParser != null) {
68+
request.setParser(responseParser);
69+
}
5570
});
5671
}
5772
}

sigstore-java/src/main/java/dev/sigstore/oidc/client/WebOidcClient.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
2525
import com.google.api.client.http.GenericUrl;
2626
import com.google.api.client.http.HttpRequest;
27-
import com.google.api.client.http.HttpRequestFactory;
2827
import com.google.api.client.http.HttpTransport;
2928
import com.google.api.client.json.GenericJson;
3029
import com.google.api.client.json.JsonFactory;
@@ -33,6 +32,7 @@
3332
import com.google.api.client.util.Preconditions;
3433
import com.google.api.client.util.store.DataStoreFactory;
3534
import com.google.api.client.util.store.MemoryDataStoreFactory;
35+
import dev.sigstore.forbidden.SuppressForbidden;
3636
import dev.sigstore.http.HttpClients;
3737
import dev.sigstore.http.HttpParams;
3838
import dev.sigstore.trustroot.Service;
@@ -140,13 +140,14 @@ public boolean isEnabled(Map<String, String> env) {
140140
* @throws OidcException if an error occurs doing the authorization flow
141141
*/
142142
@Override
143+
@SuppressForbidden(reason = "HttpClients#newHttpTransport(HttpParams)")
143144
public OidcToken getIDToken(Map<String, String> env) throws OidcException {
144145
JsonFactory jsonFactory = new GsonFactory();
145146
HttpTransport httpTransport = HttpClients.newHttpTransport(httpParams);
146147
DataStoreFactory memStoreFactory = new MemoryDataStoreFactory();
147148
OIDCEndpoints endpoints;
148149
try {
149-
endpoints = parseDiscoveryDocument(jsonFactory, httpTransport);
150+
endpoints = parseDiscoveryDocument(jsonFactory);
150151
} catch (IOException e) {
151152
// TODO: maybe a more descriptive exception message
152153
throw new OidcException(
@@ -209,15 +210,12 @@ public OidcToken getIDToken(Map<String, String> env) throws OidcException {
209210
.build();
210211
}
211212

212-
// Parses a oidc discovery document to discover other endpoints. This method does not
213+
// Parses an oidc discovery document to discover other endpoints. This method does not
213214
// parse all the values, only the endpoints we care about.
214-
OIDCEndpoints parseDiscoveryDocument(JsonFactory jsonFactory, HttpTransport httpTransport)
215-
throws IOException {
216-
HttpRequestFactory requestFactory =
217-
httpTransport.createRequestFactory(
218-
request -> {
219-
request.setParser(jsonFactory.createJsonObjectParser());
220-
});
215+
OIDCEndpoints parseDiscoveryDocument(JsonFactory jsonFactory) throws IOException {
216+
var requestFactory =
217+
HttpClients.newRequestFactory(
218+
HttpParams.builder().build(), jsonFactory.createJsonObjectParser());
221219
GenericUrl wellKnownConfig = new GenericUrl(issuer);
222220
wellKnownConfig.appendRawPath(WELL_KNOWN_CONFIG);
223221
HttpRequest request = requestFactory.buildGetRequest(wellKnownConfig);

sigstore-java/src/main/java/dev/sigstore/tuf/HttpFetcher.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,33 @@
1616
package dev.sigstore.tuf;
1717

1818
import com.google.api.client.http.GenericUrl;
19+
import com.google.api.client.http.HttpRequestFactory;
1920
import com.google.api.client.json.gson.GsonFactory;
2021
import dev.sigstore.http.HttpClients;
2122
import dev.sigstore.http.HttpParams;
2223
import java.io.IOException;
23-
import java.net.MalformedURLException;
2424
import java.net.URL;
2525
import java.util.Locale;
2626

2727
public class HttpFetcher implements Fetcher {
2828

2929
private final URL mirror;
30+
private final HttpRequestFactory requestFactory;
3031

31-
private HttpFetcher(URL mirror) {
32+
private HttpFetcher(URL mirror, HttpRequestFactory requestFactory) {
3233
this.mirror = mirror;
34+
this.requestFactory = requestFactory;
3335
}
3436

35-
public static HttpFetcher newFetcher(URL mirror) throws MalformedURLException {
37+
public static HttpFetcher newFetcher(URL mirror) throws IOException {
38+
var requestFactory =
39+
HttpClients.newRequestFactory(
40+
HttpParams.builder().build(),
41+
GsonFactory.getDefaultInstance().createJsonObjectParser());
3642
if (mirror.toString().endsWith("/")) {
37-
return new HttpFetcher(mirror);
43+
return new HttpFetcher(mirror, requestFactory);
3844
}
39-
return new HttpFetcher(new URL(mirror.toExternalForm() + "/"));
45+
return new HttpFetcher(new URL(mirror.toExternalForm() + "/"), requestFactory);
4046
}
4147

4248
@Override
@@ -48,12 +54,7 @@ public String getSource() {
4854
public byte[] fetchResource(String filename, int maxLength)
4955
throws IOException, FileExceedsMaxLengthException {
5056
GenericUrl fileUrl = new GenericUrl(mirror + filename);
51-
var req =
52-
HttpClients.newHttpTransport(HttpParams.builder().build())
53-
.createRequestFactory(
54-
request ->
55-
request.setParser(GsonFactory.getDefaultInstance().createJsonObjectParser()))
56-
.buildGetRequest(fileUrl);
57+
var req = requestFactory.buildGetRequest(fileUrl);
5758
req.getHeaders().setAccept("application/json; api-version=2.0");
5859
req.getHeaders().setContentType("application/json");
5960
req.setThrowExceptionOnExecuteError(false);

0 commit comments

Comments
 (0)