Search in sources :

Example 26 with RegistryAuth

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));
}
Also used : RegistryAuth(com.spotify.docker.client.messages.RegistryAuth) Test(org.junit.Test)

Example 27 with RegistryAuth

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) RegistryAuthV2(com.spotify.docker.client.messages.RegistryAuthV2) RegistryAuth(com.spotify.docker.client.messages.RegistryAuth) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 28 with RegistryAuth

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);
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RegistryAuth(com.spotify.docker.client.messages.RegistryAuth)

Aggregations

RegistryAuth (com.spotify.docker.client.messages.RegistryAuth)28 Test (org.junit.Test)22 FixedRegistryAuthSupplier (com.spotify.docker.client.auth.FixedRegistryAuthSupplier)7 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)4 DockerClient (com.spotify.docker.client.DockerClient)4 Path (java.nio.file.Path)4 Endpoint (com.spotify.docker.client.messages.swarm.Endpoint)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AccessToken (com.google.auth.oauth2.AccessToken)1 DockerException (com.spotify.docker.client.exceptions.DockerException)1 ProgressMessage (com.spotify.docker.client.messages.ProgressMessage)1 RegistryAuthV2 (com.spotify.docker.client.messages.RegistryAuthV2)1 RegistryConfigs (com.spotify.docker.client.messages.RegistryConfigs)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1