com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException Java Examples

The following examples show how to use com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecretCredentialsManagerImpl.java    From cia with Apache License 2.0 6 votes vote down vote up
private String getSecretField(final Function<SecretData, String> t, final String defaultStr) {
	if (FALSE.equalsIgnoreCase(secretEnabled)) {
		return defaultStr;
	} 

	try {
		if (secretCache == null) {
			secretCache = getSecretCache();
		}
		
    	final ObjectMapper mapper = new ObjectMapper();	   	 
    	return t.apply(mapper.readValue(secretCache.getSecretString(secretName),SecretData.class));	    	
    } catch (AWSSecretsManagerException | IOException e) {
    	LOGGER.error("Problem getting username from secretsmanager using secret:{} :{}:{}", secretName, e.getMessage(),e.getClass().getName());
    	throw new RuntimeException(e);
    }
}
 
Example #2
Source File: TestAWSSecretsManagerCredentialStore.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitIncorrectAWSKeys() throws Exception {
  String region = "us-west-2";
  String awsAccessKey = "access-key";
  String awsSecretKey = "secret-key";

  CredentialStore.Context context = Mockito.mock(CredentialStore.Context.class);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_REGION_PROP)).thenReturn(region);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_ACCESS_KEY_PROP)).thenReturn(awsAccessKey);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_SECRET_KEY_PROP)).thenReturn(awsSecretKey);

  SecretCache secretCache = Mockito.mock(SecretCache.class);
  AWSSecretsManagerException exception = new AWSSecretsManagerException("message");
  exception.setErrorCode("IncompleteSignature");
  AWSSecretsManagerCredentialStore secretManager = createAWSSecretsManagerCredentialStore(secretCache, exception);
  List<CredentialStore.ConfigIssue> issues = secretManager.init(context);
  Assert.assertEquals(1, issues.size());
  Mockito.verify(context, Mockito.times(1)).createConfigIssue(
      Errors.AWS_SECRETS_MANAGER_CRED_STORE_01,
      exception.getMessage(),
      exception
  );
}
 
Example #3
Source File: TestAWSSecretsManagerCredentialStore.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitAccessDeniedException() throws Exception {
  String region = "us-west-2";
  String awsAccessKey = "access-key";
  String awsSecretKey = "secret-key";

  CredentialStore.Context context = Mockito.mock(CredentialStore.Context.class);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_REGION_PROP)).thenReturn(region);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_ACCESS_KEY_PROP)).thenReturn(awsAccessKey);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_SECRET_KEY_PROP)).thenReturn(awsSecretKey);

  SecretCache secretCache = Mockito.mock(SecretCache.class);
  AWSSecretsManagerException exception = new AWSSecretsManagerException("message");
  exception.setErrorCode("AccessDeniedException");
  AWSSecretsManagerCredentialStore secretManager = createAWSSecretsManagerCredentialStore(secretCache, exception);
  List<CredentialStore.ConfigIssue> issues = secretManager.init(context);
  Assert.assertEquals(0, issues.size());  // AccessDeniedException should be ignored at initialization.
}
 
Example #4
Source File: TestAWSSecretsManagerCredentialStore.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitResourceNotFoundException() throws Exception {
  String region = "us-west-2";
  String awsAccessKey = "access-key";
  String awsSecretKey = "secret-key";

  CredentialStore.Context context = Mockito.mock(CredentialStore.Context.class);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_REGION_PROP)).thenReturn(region);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_ACCESS_KEY_PROP)).thenReturn(awsAccessKey);
  Mockito.when(context.getConfig(AWSSecretsManagerCredentialStore.AWS_SECRET_KEY_PROP)).thenReturn(awsSecretKey);

  SecretCache secretCache = Mockito.mock(SecretCache.class);
  AWSSecretsManagerException exception = new ResourceNotFoundException("message");
  exception.setErrorCode("ResourceNotFoundException");
  AWSSecretsManagerCredentialStore secretManager = createAWSSecretsManagerCredentialStore(secretCache, exception);
  List<CredentialStore.ConfigIssue> issues = secretManager.init(context);
  Assert.assertEquals(0, issues.size());  // ResourceNotFoundException should be ignored at initialization.
}
 
Example #5
Source File: SecretsManagerSecretEngine.java    From kork with Apache License 2.0 6 votes vote down vote up
protected GetSecretValueResult getSecretValue(String secretRegion, String secretName) {
  AWSSecretsManager client =
      AWSSecretsManagerClientBuilder.standard().withRegion(secretRegion).build();

  GetSecretValueRequest getSecretValueRequest =
      new GetSecretValueRequest().withSecretId(secretName);

  try {
    return client.getSecretValue(getSecretValueRequest);
  } catch (AWSSecretsManagerException e) {
    throw new SecretException(
        String.format(
            "An error occurred when using AWS Secrets Manager to fetch: [secretName: %s, secretRegion: %s]",
            secretName, secretRegion),
        e);
  }
}
 
Example #6
Source File: AWSSecretsManagerCredentialStore.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public String get() throws StageException {
  if (alwaysRefresh) {
    try {
      LOG.trace("Force refreshing '{}'", name);
      secretCache.refreshNow(name);
    } catch (InterruptedException ie) {
      LOG.warn("Encountered InterruptedException while refreshing credential '{}'", name, ie);
    }
  }
  try {
    String json = secretCache.getSecretString(name);
    if (json == null) {
      throw new StageException(Errors.AWS_SECRETS_MANAGER_CRED_STORE_02, name);
    }
    try {
      String value = parseJSONAndGetValue(json, key);
      if (value == null) {
        throw new StageException(Errors.AWS_SECRETS_MANAGER_CRED_STORE_04, key, name);
      }
      return value;
    } catch (IOException ioe) {
      throw new StageException(Errors.AWS_SECRETS_MANAGER_CRED_STORE_04, key, name, ioe);
    }
  } catch (AWSSecretsManagerException ex) {
    throw new StageException(Errors.AWS_SECRETS_MANAGER_CRED_STORE_03, name, ex);
  }
}