|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.aad.msal4j; |
| 5 | + |
| 6 | +import com.nimbusds.oauth2.sdk.ParseException; |
| 7 | +import com.nimbusds.oauth2.sdk.SerializeException; |
| 8 | +import com.nimbusds.oauth2.sdk.http.HTTPRequest; |
| 9 | +import com.nimbusds.oauth2.sdk.http.HTTPResponse; |
| 10 | +import lombok.Getter; |
| 11 | +import lombok.Setter; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import java.beans.Encoder; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.HttpURLConnection; |
| 18 | +import java.net.MalformedURLException; |
| 19 | +import java.net.SocketException; |
| 20 | +import java.net.URISyntaxException; |
| 21 | + |
| 22 | +//base class for all sources that support managed identity |
| 23 | +abstract class AbstractManagedIdentitySource { |
| 24 | + |
| 25 | + protected static final String TIMEOUT_ERROR = "[Managed Identity] Authentication unavailable. The request to the managed identity endpoint timed out."; |
| 26 | + private static final Logger LOG = LoggerFactory.getLogger(AbstractManagedIdentitySource.class); |
| 27 | + private static final String MANAGED_IDENTITY_NO_RESPONSE_RECEIVED = "[Managed Identity] Authentication unavailable. No response received from the managed identity endpoint."; |
| 28 | + |
| 29 | + protected final ManagedIdentityRequest managedIdentityRequest; |
| 30 | + private ServiceBundle serviceBundle; |
| 31 | + private ManagedIdentitySourceType managedIdentitySourceType; |
| 32 | + |
| 33 | + @Getter |
| 34 | + @Setter |
| 35 | + private boolean isUserAssignedManagedIdentity; |
| 36 | + @Getter |
| 37 | + @Setter |
| 38 | + private String managedIdentityUserAssignedClientId; |
| 39 | + @Getter |
| 40 | + @Setter |
| 41 | + private String managedIdentityUserAssignedResourceId; |
| 42 | + |
| 43 | + public AbstractManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, |
| 44 | + ManagedIdentitySourceType sourceType) { |
| 45 | + this.managedIdentityRequest = (ManagedIdentityRequest) msalRequest; |
| 46 | + this.managedIdentitySourceType = sourceType; |
| 47 | + this.serviceBundle = serviceBundle; |
| 48 | + } |
| 49 | + |
| 50 | + public ManagedIdentityResponse getManagedIdentityResponse( |
| 51 | + ManagedIdentityParameters parameters) { |
| 52 | + |
| 53 | + createManagedIdentityRequest(parameters.resource); |
| 54 | + IHttpResponse response; |
| 55 | + |
| 56 | + try { |
| 57 | + HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, managedIdentityRequest.computeURI().toString(), managedIdentityRequest.headers); |
| 58 | + response = HttpHelper.executeHttpRequest(httpRequest, managedIdentityRequest.requestContext(), serviceBundle); |
| 59 | + } catch (URISyntaxException e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } catch (MsalClientException e) { |
| 62 | + if (e.getCause() instanceof SocketException) { |
| 63 | + throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_UNREACHABLE_NETWORK, e.getMessage(), managedIdentitySourceType); |
| 64 | + } |
| 65 | + |
| 66 | + throw e; |
| 67 | + } |
| 68 | + |
| 69 | + return handleResponse(parameters, response); |
| 70 | + } |
| 71 | + |
| 72 | + public ManagedIdentityResponse handleResponse( |
| 73 | + ManagedIdentityParameters parameters, |
| 74 | + IHttpResponse response) { |
| 75 | + |
| 76 | + String message; |
| 77 | + |
| 78 | + try { |
| 79 | + if (response.statusCode() == HttpURLConnection.HTTP_OK) { |
| 80 | + LOG.info("[Managed Identity] Successful response received."); |
| 81 | + return getSuccessfulResponse(response); |
| 82 | + } else { |
| 83 | + message = getMessageFromErrorResponse(response); |
| 84 | + LOG.error( |
| 85 | + String.format("[Managed Identity] request failed, HttpStatusCode: %s Error message: %s", |
| 86 | + response.statusCode(), message)); |
| 87 | + throw new MsalManagedIdentityException(AuthenticationErrorCode.MANAGED_IDENTITY_REQUEST_FAILED, message, managedIdentitySourceType); |
| 88 | + } |
| 89 | + } catch (Exception e) { |
| 90 | + if (!(e instanceof MsalServiceException)) { |
| 91 | + LOG.error( |
| 92 | + String.format("[Managed Identity] Exception: %s Http status code: %s", e.getMessage(), |
| 93 | + response != null ? response.statusCode() : "")); |
| 94 | + message = MsalErrorMessage.MANAGED_IDENTITY_UNEXPECTED_RESPONSE; |
| 95 | + } else { |
| 96 | + throw e; |
| 97 | + } |
| 98 | + throw new MsalManagedIdentityException(AuthenticationErrorCode.MANAGED_IDENTITY_REQUEST_FAILED, message, managedIdentitySourceType); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public abstract void createManagedIdentityRequest(String resource); |
| 103 | + |
| 104 | + protected ManagedIdentityResponse getSuccessfulResponse(IHttpResponse response) { |
| 105 | + |
| 106 | + ManagedIdentityResponse managedIdentityResponse = JsonHelper |
| 107 | + .convertJsonToObject(response.body(), ManagedIdentityResponse.class); |
| 108 | + |
| 109 | + if (managedIdentityResponse == null || managedIdentityResponse.getAccessToken() == null |
| 110 | + || managedIdentityResponse.getAccessToken().isEmpty() || managedIdentityResponse.getExpiresOn() == null |
| 111 | + || managedIdentityResponse.getExpiresOn().isEmpty()) { |
| 112 | + LOG.error("[Managed Identity] Response is either null or insufficient for authentication."); |
| 113 | + throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED, MsalErrorMessage.MANAGED_IDENTITY_UNEXPECTED_RESPONSE, managedIdentitySourceType); |
| 114 | + } |
| 115 | + |
| 116 | + return managedIdentityResponse; |
| 117 | + } |
| 118 | + |
| 119 | + protected String getMessageFromErrorResponse(IHttpResponse response) { |
| 120 | + ManagedIdentityErrorResponse managedIdentityErrorResponse = |
| 121 | + JsonHelper.convertJsonToObject(response.body(), ManagedIdentityErrorResponse.class); |
| 122 | + |
| 123 | + if (managedIdentityErrorResponse == null) { |
| 124 | + return MANAGED_IDENTITY_NO_RESPONSE_RECEIVED; |
| 125 | + } |
| 126 | + |
| 127 | + if (managedIdentityErrorResponse.getMessage() != null && !managedIdentityErrorResponse.getMessage().isEmpty()) { |
| 128 | + return String.format("[Managed Identity] Error Message: %s Managed Identity Correlation ID: %s Use this Correlation ID for further investigation.", |
| 129 | + managedIdentityErrorResponse.getMessage(), managedIdentityErrorResponse.getCorrelationId()); |
| 130 | + } |
| 131 | + |
| 132 | + return String.format("[Managed Identity] Error Code: %s Error Message: %s", |
| 133 | + managedIdentityErrorResponse.getError(), managedIdentityErrorResponse.getErrorDescription()); |
| 134 | + } |
| 135 | + |
| 136 | + protected static IEnvironmentVariables getEnvironmentVariables(ManagedIdentityParameters parameters) { |
| 137 | + return parameters.environmentVariables == null ? new EnvironmentVariables() : parameters.environmentVariables; |
| 138 | + } |
| 139 | +} |
0 commit comments