Search in sources :

Example 1 with ResourceNotFoundException

use of com.azure.core.exception.ResourceNotFoundException in project tessera by ConsenSys.

the class AzureKeyVaultService method getSecret.

@Override
public String getSecret(Map<String, String> azureGetSecretData) {
    final String secretName = azureGetSecretData.get(SECRET_NAME_KEY);
    final String secretVersion = azureGetSecretData.get(SECRET_VERSION_KEY);
    final KeyVaultSecret secret;
    try {
        LOGGER.debug("SecretName : {} , SecretVersion: {}", secretName, secretVersion);
        secret = secretClient.getSecret(secretName, secretVersion);
        LOGGER.debug("secret.id {}", secret.getId());
    } catch (ResourceNotFoundException e) {
        throw new VaultSecretNotFoundException("Azure Key Vault secret " + secretName + " was not found in vault " + secretClient.getVaultUrl());
    }
    return secret.getValue();
}
Also used : KeyVaultSecret(com.azure.security.keyvault.secrets.models.KeyVaultSecret) VaultSecretNotFoundException(com.quorum.tessera.key.vault.VaultSecretNotFoundException) ResourceNotFoundException(com.azure.core.exception.ResourceNotFoundException)

Example 2 with ResourceNotFoundException

use of com.azure.core.exception.ResourceNotFoundException in project tessera by ConsenSys.

the class AzureKeyVaultServiceTest method getSecretThrowsExceptionIfKeyNotFoundInVault.

@Test
public void getSecretThrowsExceptionIfKeyNotFoundInVault() {
    final String secretName = "secret-name";
    final String secretVersion = "secret-version";
    final Map<String, String> getSecretData = Map.of(AzureKeyVaultService.SECRET_NAME_KEY, secretName, AzureKeyVaultService.SECRET_VERSION_KEY, secretVersion);
    final ResourceNotFoundException toThrow = new ResourceNotFoundException("oh no", mock(HttpResponse.class));
    when(secretClient.getSecret(anyString(), anyString())).thenThrow(toThrow);
    when(secretClient.getVaultUrl()).thenReturn("vault-url");
    final Throwable ex = catchThrowable(() -> keyVaultService.getSecret(getSecretData));
    assertThat(ex).isExactlyInstanceOf(VaultSecretNotFoundException.class);
    assertThat(ex).hasMessage("Azure Key Vault secret secret-name was not found in vault vault-url");
    verify(secretClient).getSecret("secret-name", "secret-version");
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ResourceNotFoundException(com.azure.core.exception.ResourceNotFoundException) Test(org.junit.Test)

Example 3 with ResourceNotFoundException

use of com.azure.core.exception.ResourceNotFoundException in project azure-keyvault-plugin by jenkinsci.

the class AzureKeyVaultSecretSource method reveal.

@Override
public Optional<String> reveal(String secret) {
    AzureKeyVaultGlobalConfiguration azureKeyVaultGlobalConfiguration = GlobalConfiguration.all().get(AzureKeyVaultGlobalConfiguration.class);
    if (azureKeyVaultGlobalConfiguration == null) {
        LOGGER.info("No AzureKeyVault url found, skipping jcasc secret resolution");
        return Optional.empty();
    }
    String credentialID = azureKeyVaultGlobalConfiguration.getCredentialID();
    TokenCredential keyVaultCredentials = AzureCredentials.getSystemCredentialById(credentialID);
    if (keyVaultCredentials == null) {
        LOGGER.info("No AzureKeyVault credentials found, skipping jcasc secret resolution");
        return Optional.empty();
    }
    SecretClient client = SecretClientCache.get(credentialID, azureKeyVaultGlobalConfiguration.getKeyVaultURL());
    try {
        KeyVaultSecret secretBundle = client.getSecret(secret);
        return Optional.of(secretBundle.getValue());
    } catch (ResourceNotFoundException ignored) {
        LOGGER.info("Couldn't find secret: " + secret);
        return Optional.empty();
    }
}
Also used : KeyVaultSecret(com.azure.security.keyvault.secrets.models.KeyVaultSecret) ResourceNotFoundException(com.azure.core.exception.ResourceNotFoundException) TokenCredential(com.azure.core.credential.TokenCredential) SecretClient(com.azure.security.keyvault.secrets.SecretClient)

Aggregations

ResourceNotFoundException (com.azure.core.exception.ResourceNotFoundException)3 KeyVaultSecret (com.azure.security.keyvault.secrets.models.KeyVaultSecret)2 TokenCredential (com.azure.core.credential.TokenCredential)1 HttpResponse (com.azure.core.http.HttpResponse)1 SecretClient (com.azure.security.keyvault.secrets.SecretClient)1 VaultSecretNotFoundException (com.quorum.tessera.key.vault.VaultSecretNotFoundException)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Test (org.junit.Test)1