use of com.microsoft.azure.util.AzureBaseCredentials in project azure-credentials-plugin by jenkinsci.
the class AzureResourceManagerRetriever method getClient.
public static AzureResourceManager getClient(String credentialId, @CheckForNull String subscriptionId) {
AzureBaseCredentials credential = AzureCredentialUtil.getCredential(null, credentialId);
String actualSubscriptionId = subscriptionId != null ? subscriptionId : credential.getSubscriptionId();
return getAzureResourceManager(credential, actualSubscriptionId);
}
use of com.microsoft.azure.util.AzureBaseCredentials 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.AzureBaseCredentials 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;
}
use of com.microsoft.azure.util.AzureBaseCredentials in project azure-vm-agents-plugin by jenkinsci.
the class AzureClientUtil method getClient.
/**
* Allows using a different subscription to the one configured on the credential.
*/
public static AzureResourceManager getClient(String credentialId, String subscriptionId) throws AzureCloudException {
boolean validSubscriptionId = AzureUtil.isValidSubscriptionId(credentialId, subscriptionId);
if (!validSubscriptionId) {
throw AzureCloudException.create("The subscription id for gallery image is not valid");
}
AzureBaseCredentials credential = AzureCredentialUtil.getCredential(null, credentialId);
return getAzureResourceManager(credential, subscriptionId);
}
use of com.microsoft.azure.util.AzureBaseCredentials in project azure-keyvault-plugin by jenkinsci.
the class AzureKeyVaultGlobalConfiguration method storeCredential.
private void storeCredential(AzureBaseCredentials credentials) {
SystemCredentialsProvider instance = SystemCredentialsProvider.getInstance();
for (int i = 0; i < instance.getCredentials().size(); i++) {
Credentials cred = instance.getCredentials().get(i);
if (cred instanceof IdCredentials) {
IdCredentials idCredentials = (IdCredentials) cred;
if (idCredentials.getId().equals(credentials.getId())) {
instance.getCredentials().remove(i);
break;
}
}
}
instance.getCredentials().add(credentials);
try {
instance.save();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations