use of com.microsoft.azure.util.AzureImdsCredentials in project azure-keyvault-plugin by jenkinsci.
the class AzureKeyVaultGlobalConfiguration method resolveCredentialIdFromEnvironment.
private Optional<String> resolveCredentialIdFromEnvironment() {
// directly lookup the credential so that we don't get a stackoverflow due to credential provider
Optional<Credentials> optionalCredentials = SystemCredentialsProvider.getInstance().getCredentials().stream().filter(credentials -> (credentials instanceof AzureCredentials || credentials instanceof AzureImdsCredentials) && ((IdCredentials) credentials).getId().equals(GENERATED_ID)).findAny();
String uami = getPropertyByEnvOrSystemProperty("AZURE_KEYVAULT_UAMI_ENABLED", "jenkins.azure-keyvault.uami.enabled").orElse("false");
AzureBaseCredentials credentials;
if (uami.equals("true")) {
if (optionalCredentials.isPresent() && optionalCredentials.get() instanceof AzureImdsCredentials) {
// don't overwrite the credential if it matches what we currently have so as we don't save to disk all the time
return Optional.empty();
}
credentials = new AzureImdsCredentials(CredentialsScope.GLOBAL, GENERATED_ID, GENERATED_DESCRIPTION);
storeCredential(credentials);
return Optional.of(credentials.getId());
}
String clientId = getPropertyByEnvOrSystemProperty("AZURE_KEYVAULT_SP_CLIENT_ID", "jenkins.azure-keyvault.sp.client_id").orElse("false");
if (clientId.equals("false")) {
return Optional.empty();
}
String clientSecret = getPropertyByEnvOrSystemProperty("AZURE_KEYVAULT_SP_CLIENT_SECRET", "jenkins.azure-keyvault.sp.client_secret").orElseThrow(IllegalArgumentException::new);
String subscriptionId = getPropertyByEnvOrSystemProperty("AZURE_KEYVAULT_SP_SUBSCRIPTION_ID", "jenkins.azure-keyvault.sp.subscription_id").orElseThrow(IllegalArgumentException::new);
String tenantId = getPropertyByEnvOrSystemProperty("AZURE_KEYVAULT_SP_TENANT_ID", "jenkins.azure-keyvault.sp.tenant_id").orElseThrow(IllegalArgumentException::new);
if (optionalCredentials.isPresent() && optionalCredentials.get() instanceof AzureCredentials && azureCredentialIsEqual((AzureCredentials) optionalCredentials.get(), clientId, clientSecret, subscriptionId, tenantId)) {
// don't overwrite the credential if it matches what we currently have so as we don't save to disk all the time
return Optional.empty();
}
AzureCredentials azureCredentials = new AzureCredentials(CredentialsScope.GLOBAL, GENERATED_ID, GENERATED_DESCRIPTION, subscriptionId, clientId, clientSecret);
azureCredentials.setTenant(tenantId);
storeCredential(azureCredentials);
return Optional.of(azureCredentials.getId());
}
use of com.microsoft.azure.util.AzureImdsCredentials in project azure-keyvault-plugin by jenkinsci.
the class AzureKeyVaultCredentialRetriever method getCredentialById.
@CheckForNull
public static TokenCredential getCredentialById(String credentialID, Run<?, ?> build) {
TokenCredential credential;
AzureBaseCredentials cred = CredentialsProvider.findCredentialById(credentialID, AzureBaseCredentials.class, build);
if (cred == null) {
throw new AzureKeyVaultException(String.format("Credential: %s was not found", credentialID));
}
if (cred instanceof AzureCredentials) {
LOGGER.log(Level.FINE, format("Fetched %s as AzureCredentials", credentialID));
CredentialsProvider.track(build, cred);
AzureCredentials azureCredentials = (AzureCredentials) cred;
credential = new ClientSecretCredentialBuilder().clientId(azureCredentials.getClientId()).clientSecret(azureCredentials.getPlainClientSecret()).httpClient(HttpClientRetriever.get()).tenantId(azureCredentials.getTenant()).build();
} else if (cred instanceof AzureImdsCredentials) {
credential = new ManagedIdentityCredentialBuilder().build();
} else {
throw new AzureKeyVaultException("Could not determine the type for Secret id " + credentialID + " only 'Azure Service Principal' and 'Azure Managed Identity' are supported");
}
return credential;
}
Aggregations