Java源码示例:software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider
示例1
private AwsCredentialsProvider credentialSourceCredentialProvider(CredentialSourceType credentialSource) {
switch (credentialSource) {
case ECS_CONTAINER:
return ContainerCredentialsProvider.builder().build();
case EC2_INSTANCE_METADATA:
return InstanceProfileCredentialsProvider.create();
case ENVIRONMENT:
return AwsCredentialsProviderChain.builder()
.addCredentialsProvider(SystemPropertyCredentialsProvider.create())
.addCredentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
default:
throw noSourceCredentialsException();
}
}
示例2
@Override
public AwsCredentialsProvider deserializeWithType(
JsonParser jsonParser, DeserializationContext context, TypeDeserializer typeDeserializer)
throws IOException {
Map<String, String> asMap =
jsonParser.readValueAs(new TypeReference<Map<String, String>>() {});
String typeNameKey = typeDeserializer.getPropertyName();
String typeName = asMap.get(typeNameKey);
if (typeName == null) {
throw new IOException(
String.format("AWS credentials provider type name key '%s' not found", typeNameKey));
}
if (typeName.equals(StaticCredentialsProvider.class.getSimpleName())) {
return StaticCredentialsProvider.create(
AwsBasicCredentials.create(asMap.get(ACCESS_KEY_ID), asMap.get(SECRET_ACCESS_KEY)));
} else if (typeName.equals(DefaultCredentialsProvider.class.getSimpleName())) {
return DefaultCredentialsProvider.create();
} else if (typeName.equals(EnvironmentVariableCredentialsProvider.class.getSimpleName())) {
return EnvironmentVariableCredentialsProvider.create();
} else if (typeName.equals(SystemPropertyCredentialsProvider.class.getSimpleName())) {
return SystemPropertyCredentialsProvider.create();
} else if (typeName.equals(ProfileCredentialsProvider.class.getSimpleName())) {
return ProfileCredentialsProvider.create();
} else if (typeName.equals(ContainerCredentialsProvider.class.getSimpleName())) {
return ContainerCredentialsProvider.builder().build();
} else {
throw new IOException(
String.format("AWS credential provider type '%s' is not supported", typeName));
}
}
示例3
@Test
public void testAwsCredentialsProviderSerializationDeserialization() throws Exception {
AwsCredentialsProvider credentialsProvider = DefaultCredentialsProvider.create();
String serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
AwsCredentialsProvider deserializedCredentialsProvider =
objectMapper.readValue(serializedCredentialsProvider, DefaultCredentialsProvider.class);
assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
credentialsProvider = EnvironmentVariableCredentialsProvider.create();
serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
deserializedCredentialsProvider =
objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
credentialsProvider = SystemPropertyCredentialsProvider.create();
serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
deserializedCredentialsProvider =
objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
credentialsProvider = ProfileCredentialsProvider.create();
serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
deserializedCredentialsProvider =
objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
credentialsProvider = ContainerCredentialsProvider.builder().build();
serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
deserializedCredentialsProvider =
objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
}