-
Notifications
You must be signed in to change notification settings - Fork 29
[IAS] Dynamically Resolve IAS Host On-Demand #1177
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
MatKuhr
wants to merge
4
commits into
main
Choose a base branch
from
feat/dynamic-ias-url-resolver
base: main
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
Show all changes
4 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
90 changes: 90 additions & 0 deletions
90
...uth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/IasTenantHostResolver.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,90 @@ | ||
| package com.sap.cloud.sdk.cloudplatform.connectivity; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
|
|
||
| import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
| import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
| import org.apache.hc.core5.http.HttpStatus; | ||
| import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
| import org.json.JSONObject; | ||
|
|
||
| import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import lombok.val; | ||
|
|
||
| /** | ||
| * Resolves the IAS tenant host for a given tenant ID by querying the BTP tenant API. | ||
| * <p> | ||
| * The endpoint returns OIDC metadata including a {@code token_endpoint}. The IAS host subdomain is extracted from the | ||
| * first host label of that URL, which identifies the IAS tenant. | ||
| */ | ||
| @Slf4j | ||
| class IasTenantHostResolver | ||
| { | ||
| static final IasTenantHostResolver DEFAULT_INSTANCE = new IasTenantHostResolver(); | ||
| private static final String TENANT_INFO_ENDPOINT_TEMPLATE = "/sap/rest/tenantLoginInfo?id=%s"; | ||
|
|
||
| private final CloseableHttpClient httpClient; | ||
|
|
||
| private IasTenantHostResolver() | ||
| { | ||
| this.httpClient = HttpClients.createDefault(); | ||
| } | ||
|
|
||
| /** | ||
| * Queries {@code btpTenantApiUri} with {@code ?id=<tenantId>} and extracts the IAS tenant subdomain from the | ||
| * {@code token_endpoint} field in the JSON response. | ||
| * | ||
| * @param btpTenantApiUri | ||
| * The full URL of the BTP tenant login-info endpoint. | ||
| * @param tenantId | ||
| * The tenant ID (app_tid/subaccount ID) to look up. | ||
| * @return The subdomain extracted from the {@code token_endpoint} host. | ||
| * @throws DestinationAccessException | ||
| * if the HTTP request fails, the response is not 200, or the subdomain cannot be parsed from the | ||
| * response. | ||
| */ | ||
| @Nonnull | ||
| String resolve( @Nonnull final URI btpTenantApiUri, @Nonnull final String tenantId ) | ||
| { | ||
| val url = btpTenantApiUri.resolve(TENANT_INFO_ENDPOINT_TEMPLATE.formatted(tenantId)); | ||
| log.debug("Dynamically resolving IAS tenant host for tenant '{}' via {}.", tenantId, url); | ||
| val req = new HttpGet(url); | ||
| try { | ||
| return httpClient.execute(req, response -> { | ||
| if( response.getCode() != HttpStatus.SC_OK ) { | ||
| throw new DestinationAccessException( | ||
| "Failed to query BTP tenant API: Server returned status code %d for GET request to '%s'." | ||
| .formatted(response.getCode(), url)); | ||
| } | ||
| val body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); | ||
| return extractSubdomainFromTokenEndpoint(body); | ||
| }); | ||
| } | ||
| catch( IOException e ) { | ||
| throw new DestinationAccessException("Failed to query BTP tenant API: " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @Nonnull | ||
| static String extractSubdomainFromTokenEndpoint( @Nonnull final String responseBody ) | ||
| { | ||
| try { | ||
| final String tokenEndpoint = new JSONObject(responseBody).getString("token_endpoint"); | ||
| final String host = URI.create(tokenEndpoint).getHost(); | ||
| return host.substring(0, host.indexOf('.')); | ||
| } | ||
| catch( final Exception e ) { | ||
| throw new DestinationAccessException( | ||
| "Failed to extract IAS tenant host from the BTP tenant API response. The response did not conform to to the expected format: " | ||
| + responseBody, | ||
| e); | ||
| } | ||
| } | ||
| } |
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
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
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.
(Question)
What's the impact of the additional HTTP request?
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.
Single connection pool since there is only one connection, the URL is fixed for the BTP region. Requests should be fast, but no timeout & caching since this code is intended as temporary workaround.