-
Notifications
You must be signed in to change notification settings - Fork 4k
feat(xds): Implement CheckRequestBuilder for external authorization #12493
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
Open
sauravzg
wants to merge
3
commits into
grpc:master
Choose a base branch
from
sauravzg:feat/request-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
114 changes: 114 additions & 0 deletions
114
xds/src/main/java/io/grpc/xds/internal/extauthz/CertificateUtils.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,114 @@ | ||
| /* | ||
| * Copyright 2025 The gRPC 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 io.grpc.xds.internal.extauthz; | ||
|
|
||
| import com.google.common.io.BaseEncoding; | ||
| import java.io.UnsupportedEncodingException; | ||
| import java.net.URLEncoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.cert.CertificateEncodingException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * A utility class for certificate-related information. | ||
| */ | ||
| final class CertificateUtils { | ||
| private static final Logger logger = Logger.getLogger(CertificateUtils.class.getName()); | ||
| // From RFC 5280, section 4.2.1.6, Subject Alternative Name | ||
| // dNSName (2) | ||
| // iPAddress (7) | ||
| private static final int SAN_TYPE_DNS_NAME = 2; | ||
| private static final int SAN_TYPE_URI = 6; | ||
|
|
||
| private CertificateUtils() {} | ||
|
|
||
| /** | ||
| * Gets the principal from a certificate. It returns the cert's first URI SAN if set, | ||
| * otherwise the cert's first DNS SAN if set, otherwise the subject field of the certificate in | ||
| * RFC 2253 format. | ||
| * | ||
| * @param cert The certificate. | ||
| * @return The principal. | ||
| */ | ||
| static String getPrincipal(X509Certificate cert) { | ||
| try { | ||
| Collection<List<?>> sans = cert.getSubjectAlternativeNames(); | ||
| if (sans != null) { | ||
| // Look for URI SAN (Priority 1). | ||
| for (List<?> san : sans) { | ||
| if (san.size() == 2 && san.get(0) instanceof Integer | ||
| && san.get(0).equals(SAN_TYPE_URI)) { | ||
| return (String) san.get(1); | ||
| } | ||
| } | ||
| // If no URI SAN, look for DNS SAN (Priority 2). | ||
| for (List<?> san : sans) { | ||
| if (san.size() == 2 && san.get(0) instanceof Integer | ||
| && san.get(0).equals(SAN_TYPE_DNS_NAME)) { | ||
| return (String) san.get(1); | ||
| } | ||
| } | ||
| } | ||
| } catch (java.security.cert.CertificateParsingException e) { | ||
| logger.log(Level.FINE, "Error parsing certificate SANs.", e); | ||
| } | ||
| return cert.getSubjectX500Principal().getName("RFC2253"); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Gets the URL PEM encoded certificate. It Pem encodes first and then urlencodes. | ||
| * | ||
| * @param cert The certificate. | ||
| * @return The URL PEM encoded certificate. | ||
| * @throws CertificateEncodingException If an error occurs while encoding the certificate. | ||
| * @throws UnsupportedEncodingException If an error occurs while encoding the URL. | ||
| */ | ||
| static String getUrlPemEncodedCertificate(X509Certificate cert) | ||
| throws CertificateEncodingException, UnsupportedEncodingException { | ||
| String pemCert = CertPemConverter.toPem(cert); | ||
| return URLEncoder.encode(pemCert, StandardCharsets.UTF_8.name()); | ||
| } | ||
|
|
||
| /** | ||
| * A utility class for PEM encoding. | ||
| */ | ||
| private static final class CertPemConverter { | ||
|
|
||
| private static final String X509_PEM_HEADER = "-----BEGIN CERTIFICATE-----\n"; | ||
| private static final String X509_PEM_FOOTER = "\n-----END CERTIFICATE-----\n"; | ||
|
|
||
| private CertPemConverter() {} | ||
|
|
||
| /** | ||
| * Converts a certificate to a PEM string. | ||
| * | ||
| * @param cert The certificate to convert. | ||
| * @return The PEM encoded certificate. | ||
| * @throws CertificateEncodingException If an error occurs while encoding the certificate. | ||
| */ | ||
| public static String toPem(X509Certificate cert) throws CertificateEncodingException { | ||
| return X509_PEM_HEADER | ||
| + BaseEncoding.base64().withSeparator("\n", 64).encode(cert.getEncoded()) | ||
| + X509_PEM_FOOTER; | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kannanjgithub Would like a closer look at this particular file, since my familiarity with certificates, their specifications and existing battle tested utilities in grpc java or java stdlib is very limited.