-
Notifications
You must be signed in to change notification settings - Fork 149
Add file based Azure Arc detection #955
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
base: dev
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -26,13 +26,20 @@ class AzureArcManagedIdentitySource extends AbstractManagedIdentitySource{ | |
private static final String FILE_EXTENSION = ".key"; | ||
private static final int MAX_FILE_SIZE_BYTES = 4096; | ||
private static final String WWW_AUTHENTICATE_HEADER = "WWW-Authenticate"; | ||
private static final String FALLBACK_IDENTITY_ENDPOINT = "http://127.0.0.1:40342/metadata/identity/oauth2/token"; | ||
|
||
private final URI MSI_ENDPOINT; | ||
|
||
static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) | ||
{ | ||
IEnvironmentVariables environmentVariables = getEnvironmentVariables(); | ||
String identityEndpoint = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT); | ||
|
||
if (StringHelper.isNullOrBlank(identityEndpoint)) { | ||
LOG.info("[Managed Identity] Azure Arc was detected through file based detection but the environment variables were not found. Defaulting to known azure arc endpoint."); | ||
identityEndpoint = FALLBACK_IDENTITY_ENDPOINT; | ||
} | ||
|
||
String imdsEndpoint = environmentVariables.getEnvironmentVariable(Constants.IMDS_ENDPOINT); | ||
Comment on lines
+37
to
43
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. Don't you also need to check that 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. and if neither exists, also set |
||
|
||
URI validatedUri = validateAndGetUri(identityEndpoint, imdsEndpoint); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,15 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Class to initialize a managed identity and identify the service. | ||
*/ | ||
class ManagedIdentityClient { | ||
private static final Logger LOG = LoggerFactory.getLogger(ManagedIdentityClient.class); | ||
private static final String WINDOWS_HIMDS_FILEPATH = "%Programfiles%\\AzureConnectedMachineAgent\\himds.exe"; | ||
private static final String LINUX_HIMDS_FILEPATH = "/opt/azcmagent/bin/himds"; | ||
|
||
static ManagedIdentitySourceType getManagedIdentitySource() { | ||
IEnvironmentVariables environmentVariables = AbstractManagedIdentitySource.getEnvironmentVariables(); | ||
|
@@ -24,8 +28,7 @@ static ManagedIdentitySourceType getManagedIdentitySource() { | |
} | ||
} else if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.MSI_ENDPOINT))) { | ||
return ManagedIdentitySourceType.CLOUD_SHELL; | ||
} else if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT)) && | ||
!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IMDS_ENDPOINT))) { | ||
} else if (validateAzureArcEnvironment(environmentVariables)) { | ||
return ManagedIdentitySourceType.AZURE_ARC; | ||
} else { | ||
return ManagedIdentitySourceType.DEFAULT_TO_IMDS; | ||
|
@@ -65,4 +68,33 @@ private static AbstractManagedIdentitySource createManagedIdentitySource(MsalReq | |
return new IMDSManagedIdentitySource(msalRequest, serviceBundle); | ||
} | ||
} | ||
|
||
static boolean validateAzureArcEnvironment(IEnvironmentVariables environmentVariables) { | ||
if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT)) && | ||
!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IMDS_ENDPOINT))) { | ||
LOG.debug("[Managed Identity] Azure Arc managed identity is available through environment variables."); | ||
Avery-Dunn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
|
||
String osName = System.getProperty("os.name").toLowerCase(); | ||
|
||
if (osName.contains("windows")) { | ||
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. Can "windows" and "linux" be saved as constants in a constants file? Surely they are used elsewhere, or will be? Otherwise, if you implement my suggestion above, you could get the file path via:
|
||
File windowsFile = new File(WINDOWS_HIMDS_FILEPATH); | ||
if (windowsFile.exists()) { | ||
LOG.debug("[Managed Identity] Azure Arc managed identity is available through file detection."); | ||
return true; | ||
} | ||
} else if (osName.contains("linux")) { | ||
File linuxFile = new File(LINUX_HIMDS_FILEPATH); | ||
if (linuxFile.exists()) { | ||
LOG.debug("[Managed Identity] Azure Arc managed identity is available through file detection."); | ||
return true; | ||
} | ||
} else { | ||
LOG.warn("[Managed Identity] Azure Arc managed identity cannot be configured on a platform other than Windows and Linux."); | ||
} | ||
|
||
LOG.debug("[Managed Identity] Azure Arc managed identity is not available."); | ||
return false; | ||
} | ||
} |
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.
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.
it's a nit, you can choose to ignore