AwsEcrAuthResolver implements DockerRegistryDynamicAuthResolver { @Override @Nullable public AuthConfig getXRegistryAuth(@Nullable String registry) { if (registry != null) { AwsAccountCredentials awsAccountCredentials = awsEcrInitialAuthConfig.getAuthConfigs().get(registry); if (awsAccountCredentials != null) { try { String authorizationToken = getAwsAuthorizationToken( awsAccountCredentials.getAccessKeyId(), awsAccountCredentials.getSecretAccessKey()); if (authorizationToken != null) { String decodedAuthorizationToken = new String(Base64.getDecoder().decode(authorizationToken)); int colonIndex = decodedAuthorizationToken.indexOf(':'); if (colonIndex != -1) { return newDto(AuthConfig.class) .withUsername(decodedAuthorizationToken.substring(0, colonIndex)) .withPassword(decodedAuthorizationToken.substring(colonIndex + 1)); } else { LOG.error("Cannot retrieve ECR credentials from token for {} registry", registry); } } } catch (IllegalArgumentException e) { LOG.error( "Retrieved AWS ECR authorization token for {} registry has invalid format", registry); } } } return null; } @Inject  AwsEcrAuthResolver(AwsEcrInitialAuthConfig awsInitialAuthConfig); @Override @Nullable AuthConfig getXRegistryAuth(@Nullable String registry); @Override Map<String, AuthConfig> getXRegistryConfig();  }
@Test public void shouldReturnNullWhenGetDynamicXRegistryAuthIfRegistryIsNull() { assertNull(awsEcrAuthResolver.getXRegistryAuth(null)); }
@Test public void shouldReturnNullWhenGetDynamicXRegistryAuthIfCredentialsForSpecifiedRegistryIsNotConfigured() { assertNull(awsEcrAuthResolver.getXRegistryAuth("my.registry.com")); }
@Test public void shouldReturnNullWhenGetDynamicXRegistryAuthIfCredentialsIsNotConfigured() { when(awsEcrInitialAuthConfig.getAuthConfigs()).thenReturn(new HashMap<>()); assertNull(awsEcrAuthResolver.getXRegistryAuth(AWS_ECR)); }