-
Notifications
You must be signed in to change notification settings - Fork 148
Service Fabric MSI #729
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
Merged
Merged
Service Fabric MSI #729
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6382bad
Support for service fabric, most tests working
Avery-Dunn 6ca6fdb
TODOs and sonarlint recommendations
Avery-Dunn 0912291
Merge branch 'nebharg/MsiAzureArc' of https://github.com/AzureAD/micr…
Avery-Dunn 5c0b320
Address PR comments
Avery-Dunn c872211
Merge branch 'nebharg/MSI' of https://github.com/AzureAD/microsoft-au…
Avery-Dunn b3265af
Merge branch 'nebharg/MsiAzureArc' of https://github.com/AzureAD/micr…
Avery-Dunn 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
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
89 changes: 89 additions & 0 deletions
89
msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ServiceFabricManagedIdentitySource.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,89 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.aad.msal4j; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
|
||
class ServiceFabricManagedIdentitySource extends AbstractManagedIdentitySource { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ServiceFabricManagedIdentitySource.class); | ||
|
||
private static final String SERVICE_FABRIC_MSI_API_VERSION = "2019-07-01-preview"; | ||
|
||
private final URI msiEndpoint; | ||
private final String identityHeader; | ||
private final ManagedIdentityIdType idType; | ||
private final String userAssignedId; | ||
|
||
@Override | ||
public void createManagedIdentityRequest(String resource) { | ||
managedIdentityRequest.baseEndpoint = msiEndpoint; | ||
managedIdentityRequest.method = HttpMethod.GET; | ||
|
||
managedIdentityRequest.headers = new HashMap<>(); | ||
managedIdentityRequest.headers.put("secret", identityHeader); | ||
|
||
managedIdentityRequest.queryParameters = new HashMap<>(); | ||
managedIdentityRequest.queryParameters.put("resource", Collections.singletonList(resource)); | ||
managedIdentityRequest.queryParameters.put("api-version", Collections.singletonList(SERVICE_FABRIC_MSI_API_VERSION)); | ||
|
||
if (idType == ManagedIdentityIdType.CLIENT_ID) { | ||
LOG.info("[Managed Identity] Adding user assigned client id to the request for Service Fabric Managed Identity."); | ||
managedIdentityRequest.queryParameters.put(Constants.MANAGED_IDENTITY_CLIENT_ID, Collections.singletonList(userAssignedId)); | ||
} else if (idType == ManagedIdentityIdType.RESOURCE_ID) { | ||
LOG.info("[Managed Identity] Adding user assigned resource id to the request for Service Fabric Managed Identity."); | ||
managedIdentityRequest.queryParameters.put(Constants.MANAGED_IDENTITY_RESOURCE_ID, Collections.singletonList(userAssignedId)); | ||
} | ||
} | ||
|
||
private ServiceFabricManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI msiEndpoint, String identityHeader) | ||
{ | ||
super(msalRequest, serviceBundle, ManagedIdentitySourceType.SERVICE_FABRIC); | ||
this.msiEndpoint = msiEndpoint; | ||
this.identityHeader = identityHeader; | ||
|
||
this.idType = ((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getIdType(); | ||
this.userAssignedId = ((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getUserAssignedId(); | ||
} | ||
|
||
static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) { | ||
|
||
IEnvironmentVariables environmentVariables = getEnvironmentVariables((ManagedIdentityParameters) msalRequest.requestContext().apiParameters()); | ||
String msiEndpoint = environmentVariables.getEnvironmentVariable(Constants.MSI_ENDPOINT); | ||
String identityHeader = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT); | ||
String identityServerThumbprint = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_SERVER_THUMBPRINT); | ||
|
||
|
||
if (StringHelper.isNullOrBlank(msiEndpoint) || StringHelper.isNullOrBlank(identityHeader) || StringHelper.isNullOrBlank(identityServerThumbprint)) | ||
{ | ||
LOG.info("[Managed Identity] Service fabric managed identity is unavailable."); | ||
return null; | ||
} | ||
|
||
return new ServiceFabricManagedIdentitySource(msalRequest, serviceBundle, validateAndGetUri(msiEndpoint), identityHeader); | ||
} | ||
|
||
private static URI validateAndGetUri(String msiEndpoint) | ||
{ | ||
try | ||
{ | ||
URI endpointUri = new URI(msiEndpoint); | ||
LOG.info("[Managed Identity] Environment variables validation passed for Service Fabric Managed Identity. Endpoint URI: " + endpointUri); | ||
return endpointUri; | ||
} | ||
catch (URISyntaxException ex) | ||
{ | ||
throw new MsalManagedIdentityException(MsalError.INVALID_MANAGED_IDENTITY_ENDPOINT, String.format( | ||
MsalErrorMessage.MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR, "MSI_ENDPOINT", msiEndpoint, "Service Fabric"), | ||
ManagedIdentitySourceType.SERVICE_FABRIC); | ||
} | ||
} | ||
|
||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.