@Test public void getSecretThrowsExceptionIfAWSException() { String secretName = "secret"; AWSGetSecretData getSecretData = mock(AWSGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); when(secretsManager.getSecretValue(Mockito.any(GetSecretValueRequest.class))) .thenThrow(InvalidParameterException.builder().build()); Throwable throwable = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(throwable).isInstanceOf(AWSSecretsManagerException.class); }
@Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); }
AWSKeyVaultService implements KeyVaultService<AWSSetSecretData, AWSGetSecretData> { @Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } }
AWSKeyVaultService implements KeyVaultService<AWSSetSecretData, AWSGetSecretData> { @Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } AWSKeyVaultService(SecretsManagerClient secretsManager); }
AWSKeyVaultService implements KeyVaultService<AWSSetSecretData, AWSGetSecretData> { @Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } AWSKeyVaultService(SecretsManagerClient secretsManager); @Override String getSecret(AWSGetSecretData getSecretData); @Override Object setSecret(AWSSetSecretData setSecretData); }
AWSKeyVaultService implements KeyVaultService<AWSSetSecretData, AWSGetSecretData> { @Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } AWSKeyVaultService(SecretsManagerClient secretsManager); @Override String getSecret(AWSGetSecretData getSecretData); @Override Object setSecret(AWSSetSecretData setSecretData);  }