-
Notifications
You must be signed in to change notification settings - Fork 26
http fulcio client #1176
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
Merged
Merged
http fulcio client #1176
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
sigstore-java/src/main/java/dev/sigstore/fulcio/client/FulcioClientHttp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * Copyright 2026 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.fulcio.client; | ||
|
|
||
| import static dev.sigstore.fulcio.v2.SigningCertificate.CertificateCase.SIGNED_CERTIFICATE_DETACHED_SCT; | ||
| import static dev.sigstore.fulcio.v2.SigningCertificate.CertificateCase.SIGNED_CERTIFICATE_EMBEDDED_SCT; | ||
|
|
||
| import com.google.api.client.http.ByteArrayContent; | ||
| import com.google.api.client.http.GenericUrl; | ||
| import com.google.api.client.util.Preconditions; | ||
| import com.google.protobuf.ByteString; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import dev.sigstore.fulcio.v2.CreateSigningCertificateRequest; | ||
| import dev.sigstore.fulcio.v2.PublicKey; | ||
| import dev.sigstore.fulcio.v2.PublicKeyRequest; | ||
| import dev.sigstore.fulcio.v2.SigningCertificate; | ||
| import dev.sigstore.http.HttpClients; | ||
| import dev.sigstore.http.HttpParams; | ||
| import dev.sigstore.json.ProtoJson; | ||
| import dev.sigstore.trustroot.Service; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.security.cert.CertPath; | ||
| import java.security.cert.CertificateException; | ||
| import java.util.Base64; | ||
| import java.util.Locale; | ||
|
|
||
| /** A client to communicate with a fulcio service instance over HTTP. */ | ||
| public class FulcioClientHttp implements FulcioClient { | ||
| public static final String FULCIO_SIGNING_CERT_PATH = "/api/v2/signingCert"; | ||
|
|
||
| private final HttpParams httpParams; | ||
| private final URI uri; | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| private FulcioClientHttp(HttpParams httpParams, URI uri) { | ||
| this.uri = uri; | ||
| this.httpParams = httpParams; | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private HttpParams httpParams = HttpParams.builder().build(); | ||
| private Service service; | ||
|
|
||
| private Builder() {} | ||
|
|
||
| /** Configure the http properties, see {@link HttpParams}. */ | ||
| public Builder setHttpParams(HttpParams httpParams) { | ||
| this.httpParams = httpParams; | ||
| return this; | ||
| } | ||
|
|
||
| /** Service information for a remote fulcio instance. */ | ||
| public Builder setService(Service service) { | ||
| this.service = service; | ||
| return this; | ||
| } | ||
|
|
||
| public FulcioClientHttp build() { | ||
| Preconditions.checkNotNull(service); | ||
| return new FulcioClientHttp(httpParams, service.getUrl()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Request a signing certificate from fulcio over HTTP. | ||
| * | ||
| * @param request certificate request parameters | ||
| * @return a {@link CertPath} from fulcio | ||
| */ | ||
| @Override | ||
| public CertPath signingCertificate(CertificateRequest request) throws CertificateException { | ||
| URI endpoint = uri.resolve(FULCIO_SIGNING_CERT_PATH); | ||
|
|
||
| String pemEncodedPublicKey = | ||
| "-----BEGIN PUBLIC KEY-----\n" | ||
| + Base64.getEncoder().encodeToString(request.getPublicKey().getEncoded()) | ||
| + "\n-----END PUBLIC KEY-----"; | ||
|
|
||
| var createSigningCertificateRequest = | ||
| CreateSigningCertificateRequest.newBuilder() | ||
| .setPublicKeyRequest( | ||
| PublicKeyRequest.newBuilder() | ||
| .setPublicKey( | ||
| PublicKey.newBuilder() | ||
| .setAlgorithm(request.getPublicKeyAlgorithm()) | ||
| .setContent(pemEncodedPublicKey) | ||
| .build()) | ||
| .setProofOfPossession(ByteString.copyFrom(request.getProofOfPossession())) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| String jsonPayload; | ||
| try { | ||
| jsonPayload = JsonFormat.printer().print(createSigningCertificateRequest); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| throw new CertificateException("Failed to serialize certificate request", e); | ||
| } | ||
|
|
||
| String responseJson; | ||
| try { | ||
| var httpRequest = | ||
| HttpClients.newRequestFactory(httpParams) | ||
| .buildPostRequest( | ||
| new GenericUrl(endpoint), | ||
| ByteArrayContent.fromString("application/json", jsonPayload)); | ||
| httpRequest.getHeaders().set("Accept", "application/json"); | ||
| httpRequest.getHeaders().set("Content-Type", "application/json"); | ||
| httpRequest.getHeaders().set("Authorization", "Bearer " + request.getIdToken()); | ||
| httpRequest.setThrowExceptionOnExecuteError(false); | ||
|
|
||
| var resp = httpRequest.execute(); | ||
| responseJson = resp.parseAsString(); | ||
| if (resp.getStatusCode() != 200) { | ||
| throw new CertificateException( | ||
| String.format( | ||
| Locale.ROOT, "bad response from fulcio @ '%s' : %s", endpoint, responseJson)); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new CertificateException("Failed to request signing certificate from fulcio", e); | ||
| } | ||
|
|
||
| var signingCertBuilder = SigningCertificate.newBuilder(); | ||
| try { | ||
| ProtoJson.parser().merge(responseJson, signingCertBuilder); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| throw new CertificateException("Failed to parse signing certificate response from fulcio", e); | ||
| } | ||
| var signingCert = signingCertBuilder.build(); | ||
|
|
||
| if (signingCert.getCertificateCase() == SIGNED_CERTIFICATE_DETACHED_SCT) { | ||
| throw new CertificateException("Detached SCTs are not supported"); | ||
| } | ||
| if (signingCert.getCertificateCase() != SIGNED_CERTIFICATE_EMBEDDED_SCT) { | ||
| throw new CertificateException("No certificate was found in response from fulcio"); | ||
| } | ||
|
|
||
| return FulcioClient.decodeCerts(signingCert.getSignedCertificateEmbeddedSct().getChain()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.