|
| 1 | +package com.sap.cloud.sdk.cloudplatform.connectivity; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | + |
| 7 | +import javax.annotation.Nonnull; |
| 8 | + |
| 9 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 10 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 11 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 12 | +import org.apache.hc.core5.http.HttpStatus; |
| 13 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 14 | +import org.json.JSONObject; |
| 15 | + |
| 16 | +import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; |
| 17 | + |
| 18 | +import lombok.extern.slf4j.Slf4j; |
| 19 | +import lombok.val; |
| 20 | + |
| 21 | +/** |
| 22 | + * Resolves the IAS tenant host for a given tenant ID by querying the BTP tenant API. |
| 23 | + * <p> |
| 24 | + * The endpoint returns OIDC metadata including a {@code token_endpoint}. The IAS host subdomain is extracted from the |
| 25 | + * first host label of that URL, which identifies the IAS tenant. |
| 26 | + */ |
| 27 | +@Slf4j |
| 28 | +class IasTenantHostResolver |
| 29 | +{ |
| 30 | + static final IasTenantHostResolver DEFAULT_INSTANCE = new IasTenantHostResolver(); |
| 31 | + private static final String TENANT_INFO_ENDPOINT_TEMPLATE = "/sap/rest/tenantLoginInfo?id=%s"; |
| 32 | + |
| 33 | + private final CloseableHttpClient httpClient; |
| 34 | + |
| 35 | + private IasTenantHostResolver() |
| 36 | + { |
| 37 | + this.httpClient = HttpClients.createDefault(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Queries {@code btpTenantApiUri} with {@code ?id=<tenantId>} and extracts the IAS tenant subdomain from the |
| 42 | + * {@code token_endpoint} field in the JSON response. |
| 43 | + * |
| 44 | + * @param btpTenantApiUri |
| 45 | + * The full URL of the BTP tenant login-info endpoint. |
| 46 | + * @param tenantId |
| 47 | + * The tenant ID (app_tid/subaccount ID) to look up. |
| 48 | + * @return The subdomain extracted from the {@code token_endpoint} host. |
| 49 | + * @throws DestinationAccessException |
| 50 | + * if the HTTP request fails, the response is not 200, or the subdomain cannot be parsed from the |
| 51 | + * response. |
| 52 | + */ |
| 53 | + @Nonnull |
| 54 | + String resolve( @Nonnull final URI btpTenantApiUri, @Nonnull final String tenantId ) |
| 55 | + { |
| 56 | + val url = btpTenantApiUri.resolve(TENANT_INFO_ENDPOINT_TEMPLATE.formatted(tenantId)); |
| 57 | + log.debug("Dynamically resolving IAS tenant host for tenant '{}' via {}.", tenantId, url); |
| 58 | + val req = new HttpGet(url); |
| 59 | + try { |
| 60 | + return httpClient.execute(req, response -> { |
| 61 | + if( response.getCode() != HttpStatus.SC_OK ) { |
| 62 | + throw new DestinationAccessException( |
| 63 | + "Failed to query BTP tenant API: Server returned status code %d for GET request to '%s'." |
| 64 | + .formatted(response.getCode(), url)); |
| 65 | + } |
| 66 | + val body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
| 67 | + return extractSubdomainFromTokenEndpoint(body); |
| 68 | + }); |
| 69 | + } |
| 70 | + catch( IOException e ) { |
| 71 | + throw new DestinationAccessException("Failed to query BTP tenant API: " + e.getMessage(), e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Nonnull |
| 76 | + static String extractSubdomainFromTokenEndpoint( @Nonnull final String responseBody ) |
| 77 | + { |
| 78 | + try { |
| 79 | + final String tokenEndpoint = new JSONObject(responseBody).getString("token_endpoint"); |
| 80 | + final String host = URI.create(tokenEndpoint).getHost(); |
| 81 | + return host.substring(0, host.indexOf('.')); |
| 82 | + } |
| 83 | + catch( final Exception e ) { |
| 84 | + throw new DestinationAccessException( |
| 85 | + "Failed to extract IAS tenant host from the BTP tenant API response. The response did not conform to the expected format: " |
| 86 | + + responseBody, |
| 87 | + e); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments