use of software.amazon.awssdk.services.ecr.model.EcrException in project aws-greengrass-nucleus by aws-greengrass.
the class EcrAccessor method getCredentials.
/**
* Get credentials(auth token) for a private docker registry in ECR.
*
* @param registryId Registry id
* @return Registry.Credentials - Registry's authorization information
* @throws RegistryAuthException When authentication fails
*/
@SuppressWarnings("PMD.AvoidRethrowingException")
public Registry.Credentials getCredentials(String registryId) throws RegistryAuthException {
try (EcrClient client = getClient()) {
AuthorizationData authorizationData = client.getAuthorizationToken(GetAuthorizationTokenRequest.builder().registryIds(Collections.singletonList(registryId)).build()).authorizationData().get(0);
// Decoded auth token is of the format <username>:<password>
String[] authTokenParts = new String(Base64.getDecoder().decode(authorizationData.authorizationToken()), StandardCharsets.UTF_8).split(":");
return new Registry.Credentials(authTokenParts[0], authTokenParts[1], authorizationData.expiresAt());
} catch (ServerException | SdkClientException e) {
// Errors we can retry on
throw e;
} catch (EcrException e) {
throw new RegistryAuthException(String.format("Failed to get credentials for ECR registry - %s", registryId), e);
}
}
use of software.amazon.awssdk.services.ecr.model.EcrException in project aws-greengrass-nucleus by aws-greengrass.
the class EcrAccessorTest method GIVEN_ecr_accessor_WHEN_get_credentials_failure_THEN_throw_auth_error.
@Test
void GIVEN_ecr_accessor_WHEN_get_credentials_failure_THEN_throw_auth_error() throws Exception {
EcrException ecrException = (EcrException) EcrException.builder().message("Something went wrong").build();
when(ecrClient.getAuthorizationToken(any(GetAuthorizationTokenRequest.class))).thenThrow(ecrException);
Throwable err = assertThrows(RegistryAuthException.class, () -> ecrAccessor.getCredentials("some_registry_id"));
assertThat(err.getMessage(), containsString("Failed to get credentials for ECR registry - some_registry_id"));
assertThat(err.getCause(), is(instanceOf(EcrException.class)));
verify(ecrClient).getAuthorizationToken(any(GetAuthorizationTokenRequest.class));
}
Aggregations