UserSpecificDockerRegistryCredentialsProvider { @Nullable public DockerAuthConfigs getCredentials() { try { String encodedCredentials = preferenceManager .find( EnvironmentContext.getCurrent().getSubject().getUserId(), DOCKER_REGISTRY_CREDENTIALS_KEY) .get(DOCKER_REGISTRY_CREDENTIALS_KEY); String credentials = encodedCredentials != null ? new String(Base64.getDecoder().decode(encodedCredentials), "UTF-8") : "{}"; return DtoFactory.newDto(DockerAuthConfigs.class) .withConfigs( DtoFactory.getInstance().createMapDtoFromJson(credentials, DockerAuthConfig.class)); } catch (Exception e) { LOG.warn(e.getLocalizedMessage()); return null; } } @Inject  UserSpecificDockerRegistryCredentialsProvider(PreferenceManager preferenceManager); @Nullable DockerAuthConfigs getCredentials();  }
@Test public void shouldParseCredentialsFromUserPreferences() throws ServerException { String base64encodedCredentials = "eyJyZWdpc3RyeS5jb206NTAwMCI6eyJ1c2VybmFtZSI6ImxvZ2luIiwicGFzc3dvcmQiOiJwYXNzIn19"; setCredentialsIntoPreferences(base64encodedCredentials); String registry = "registry.com:5000"; DockerAuthConfig dockerAuthConfig = DtoFactory.newDto(DockerAuthConfig.class).withUsername("login").withPassword("pass"); DockerAuthConfigs parsedDockerAuthConfigs = dockerCredentials.getCredentials(); DockerAuthConfig parsedDockerAuthConfig = parsedDockerAuthConfigs.getConfigs().get(registry); assertNotNull(parsedDockerAuthConfig); assertEquals(parsedDockerAuthConfig.getUsername(), dockerAuthConfig.getUsername()); assertEquals(parsedDockerAuthConfig.getPassword(), dockerAuthConfig.getPassword()); }
@Test public void shouldReturnNullIfDataFormatIsCorruptedInPreferences() throws ServerException { String base64encodedCredentials = "sdJfpwJwkek59kafj239lFfkHjhek5l1"; setCredentialsIntoPreferences(base64encodedCredentials); DockerAuthConfigs parsedDockerAuthConfigs = dockerCredentials.getCredentials(); assertNull(parsedDockerAuthConfigs); }
@Test public void shouldReturnNullIfDataFormatIsWrong() throws ServerException { String base64encodedCredentials = "eyJpbnZhbGlkIjoianNvbiJ9"; setCredentialsIntoPreferences(base64encodedCredentials); DockerAuthConfigs parsedDockerAuthConfigs = dockerCredentials.getCredentials(); assertNull(parsedDockerAuthConfigs); }
@Test public void shouldReturnConfigsWithEmptyMapIfNoCredentialsDataInUserPreferences() throws ServerException { String base64encodedCredentials = "e30="; setCredentialsIntoPreferences(base64encodedCredentials); DockerAuthConfigs parsedDockerAuthConfigs = dockerCredentials.getCredentials(); assertEquals(parsedDockerAuthConfigs.getConfigs().size(), 0); }