use of com.spotify.docker.client.messages.RegistryAuth in project docker-client by spotify.
the class MultiRegistryAuthSupplierTest method testAuthForSwarm.
@Test
public void testAuthForSwarm() throws Exception {
final RegistryAuth auth1 = RegistryAuth.builder().email("a@b.com").build();
when(supplier1.authForSwarm()).thenReturn(auth1);
assertThat(multiSupplier.authForSwarm(), is(auth1));
verify(supplier2, never()).authForSwarm();
// test fallback
final RegistryAuth auth2 = RegistryAuth.builder().email("foo@biz.com").build();
when(supplier1.authForSwarm()).thenReturn(null);
when(supplier2.authForSwarm()).thenReturn(auth2);
assertThat(multiSupplier.authForSwarm(), is(auth2));
}
use of com.spotify.docker.client.messages.RegistryAuth in project docker-client by spotify.
the class DockerConfigReader method parseDockerConfig.
private RegistryConfigs parseDockerConfig(final Path configPath) throws IOException {
checkNotNull(configPath);
ObjectNode authJson = extractAuthJson(configPath);
if (authJson.has(CREDS_STORE) && authJson.has(AUTHS_ENTRY)) {
String credsStore = authJson.get(CREDS_STORE).textValue();
Map<String, RegistryAuth> registryAuthMap = new HashMap<>();
ObjectNode auths = (ObjectNode) authJson.get(AUTHS_ENTRY);
Iterator<String> serverIterator = auths.fieldNames();
while (serverIterator.hasNext()) {
String serverAddress = serverIterator.next();
Process process = Runtime.getRuntime().exec("docker-credential-" + credsStore + " get");
try (Writer outStreamWriter = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)) {
try (BufferedWriter writer = new BufferedWriter(outStreamWriter)) {
writer.write(serverAddress + "\n");
writer.flush();
}
}
try (InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) {
try (BufferedReader input = new BufferedReader(reader)) {
String serverAuthDetails = input.readLine();
JsonNode serverAuthNode = MAPPER.readTree(serverAuthDetails);
RegistryAuthV2 serverAuth = new RegistryAuthV2(serverAuthNode.get("Username").textValue(), serverAuthNode.get("Secret").textValue(), serverAuthNode.get("ServerURL").textValue());
registryAuthMap.put(serverAddress, serverAuth);
}
}
}
return RegistryConfigs.create(registryAuthMap);
} else if (authJson.has(AUTHS_ENTRY)) {
authJson = (ObjectNode) authJson.get(AUTHS_ENTRY);
}
return MAPPER.treeToValue(authJson, RegistryConfigs.class);
}
use of com.spotify.docker.client.messages.RegistryAuth in project docker-client by spotify.
the class DockerConfigReader method parseDockerConfig.
private RegistryAuth parseDockerConfig(final Path configPath, final String serverAddress) throws IOException {
checkNotNull(configPath);
final Map<String, RegistryAuth> configs = parseDockerConfig(configPath).configs();
if (isNullOrEmpty(serverAddress)) {
if (configs.isEmpty()) {
return RegistryAuth.builder().build();
}
LOG.warn("Returning first entry from docker config file - use fromConfig(Path) instead, " + "this behavior is deprecated and will soon be removed");
return configs.values().iterator().next();
}
if (configs.containsKey(serverAddress)) {
return configs.get(serverAddress);
}
// auth tokens to config.json.
try {
final URI serverAddressUri = new URI(serverAddress);
if (serverAddressUri.getScheme() == null) {
for (String proto : Arrays.asList("https://", "http://")) {
final String addrWithProto = proto + serverAddress;
if (configs.containsKey(addrWithProto)) {
return configs.get(addrWithProto);
}
}
}
} catch (URISyntaxException e) {
// Nothing to do, just let this fall through below
}
throw new IllegalArgumentException("serverAddress=" + serverAddress + " does not appear in config file at " + configPath);
}
Aggregations