-
Notifications
You must be signed in to change notification settings - Fork 30
[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
Changes from all commits
7748883
259b391
fc61c72
f829354
ca9e710
1c7e56a
004e35f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 the expected format: " | ||
| + responseBody, | ||
| e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,10 @@ class OAuth2Service | |
| private final ResilienceConfiguration resilienceConfiguration; | ||
| @Nonnull | ||
| private final TokenCacheParameters tokenCacheParameters; | ||
| @Nullable | ||
| private final URI btpTenantApiUri; | ||
| @Nonnull | ||
| private IasTenantHostResolver iasTenantHostResolver; | ||
|
|
||
| // package-private for testing | ||
| @Nonnull | ||
|
|
@@ -249,16 +253,16 @@ private String getTenantSubdomainOrNull( @Nullable final Tenant tenant ) | |
| return null; | ||
| } | ||
|
|
||
| if( !(tenant instanceof TenantWithSubdomain tenantWithSubdomain) ) { | ||
| final String msg = "Unable to get subdomain of tenant '%s' because the instance is not an instance of %s."; | ||
| throw new DestinationAccessException(msg.formatted(tenant, TenantWithSubdomain.class.getSimpleName())); | ||
| if( tenant instanceof TenantWithSubdomain tenantWithSubdomain && tenantWithSubdomain.getSubdomain() != null ) { | ||
| return tenantWithSubdomain.getSubdomain(); | ||
| } | ||
| final var subdomain = tenantWithSubdomain.getSubdomain(); | ||
| if( subdomain == null ) { | ||
| log.debug("IAS tenant host is unknown for tenant {}. Performing IAS host lookup.", tenant.getTenantId()); | ||
| if( btpTenantApiUri == null ) { | ||
| throw new DestinationAccessException( | ||
| "The given tenant '%s' does not have a subdomain defined.".formatted(tenant)); | ||
| "Failed to dynamically resolve IAS tenant host: The BTP API URL is not given. " | ||
| + "Ensure your IAS service binding contains the BTP tenant API URL in the property 'btp-tenant-api'."); | ||
| } | ||
| return subdomain; | ||
| return iasTenantHostResolver.resolve(btpTenantApiUri, tenant.getTenantId()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Question) What's the impact of the additional HTTP request?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| @Nullable | ||
|
|
@@ -341,6 +345,10 @@ static class Builder | |
| private final Map<String, String> additionalParameters = new HashMap<>(); | ||
| private ResilienceConfiguration.TimeLimiterConfiguration timeLimiter = OAuth2Options.DEFAULT_TIMEOUT; | ||
| private TokenCacheParameters tokenCacheParameters = OAuth2Options.DEFAULT_TOKEN_CACHE_PARAMETERS; | ||
| @Nullable | ||
| private URI btpTenantApiUri; | ||
| @Nullable | ||
| private IasTenantHostResolver iasTenantHostResolver; | ||
|
|
||
| @Nonnull | ||
| Builder withTokenUri( @Nonnull final String tokenUri ) | ||
|
|
@@ -425,6 +433,20 @@ Builder withTokenCacheParameters( @Nonnull final TokenCacheParameters tokenCache | |
| return this; | ||
| } | ||
|
|
||
| @Nonnull | ||
| Builder withBtpTenantApiUri( @Nullable final URI btpTenantApiBaseUri ) | ||
| { | ||
| this.btpTenantApiUri = btpTenantApiBaseUri; | ||
| return this; | ||
| } | ||
|
|
||
| @Nonnull | ||
| Builder withIasTenantHostResolver( @Nullable final IasTenantHostResolver iasTenantHostResolver ) | ||
| { | ||
| this.iasTenantHostResolver = iasTenantHostResolver; | ||
| return this; | ||
| } | ||
|
|
||
| @Nonnull | ||
| OAuth2Service build() | ||
| { | ||
|
|
@@ -448,14 +470,19 @@ OAuth2Service build() | |
| // copy the additional parameters to prevent accidental manipulation after the `OAuth2Service` instance has been created. | ||
| final Map<String, String> additionalParameters = new HashMap<>(this.additionalParameters); | ||
|
|
||
| final var resolver = | ||
| iasTenantHostResolver != null ? iasTenantHostResolver : IasTenantHostResolver.DEFAULT_INSTANCE; | ||
|
|
||
| return new OAuth2Service( | ||
| tokenUri, | ||
| identity, | ||
| onBehalfOf, | ||
| tenantPropagationStrategy, | ||
| additionalParameters, | ||
| resilienceConfig, | ||
| tokenCacheParameters); | ||
| tokenCacheParameters, | ||
| btpTenantApiUri, | ||
| resolver); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.