This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #759 from spotify/mattbrown/registry-auth-supplier
add RegistryAuthSupplier interface
- Loading branch information
Showing
19 changed files
with
1,082 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/com/spotify/docker/client/DockerConfigReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/*- | ||
* -\-\- | ||
* docker-client | ||
* -- | ||
* Copyright (C) 2016 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.docker.client; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.common.base.Preconditions; | ||
import com.spotify.docker.client.messages.RegistryAuth; | ||
import com.spotify.docker.client.messages.RegistryConfigs; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DockerConfigReader { | ||
private static final Logger LOG = LoggerFactory.getLogger(DockerConfigReader.class); | ||
|
||
private static final ObjectMapper MAPPER = ObjectMapperProvider.objectMapper(); | ||
|
||
/** Returns all RegistryConfig instances from the configuration file. */ | ||
public RegistryConfigs fromConfig(Path configPath) throws IOException { | ||
return parseDockerConfig(configPath); | ||
} | ||
|
||
public RegistryAuth fromConfig(Path configPath, String serverAddress) throws IOException { | ||
return parseDockerConfig(configPath, serverAddress); | ||
} | ||
|
||
/** | ||
* @deprecated do not use - only exists for backwards compatibility. Use {@link #fromConfig(Path)} | ||
* instead. | ||
*/ | ||
@Deprecated | ||
public RegistryAuth fromFirstConfig(Path configPath) throws IOException { | ||
return parseDockerConfig(configPath, null); | ||
} | ||
|
||
private RegistryAuth parseDockerConfig(final Path configPath, String serverAddress) | ||
throws IOException { | ||
checkNotNull(configPath); | ||
|
||
final Map<String, RegistryAuth> configs = parseDockerConfig(configPath).configs(); | ||
if (serverAddress != null && configs.containsKey(serverAddress) ) { | ||
return configs.get(serverAddress); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"serverAddress=" + serverAddress + " does not appear in config file at " + configPath); | ||
} | ||
|
||
private RegistryConfigs parseDockerConfig(final Path configPath) throws IOException { | ||
checkNotNull(configPath); | ||
return MAPPER.treeToValue(extractAuthJson(configPath), RegistryConfigs.class); | ||
} | ||
|
||
public Path defaultConfigPath() { | ||
final String home = System.getProperty("user.home"); | ||
final Path dockerConfig = Paths.get(home, ".docker", "config.json"); | ||
final Path dockerCfg = Paths.get(home, ".dockercfg"); | ||
|
||
if (Files.exists(dockerConfig)) { | ||
LOG.debug("Using configfile: {}", dockerConfig); | ||
return dockerConfig; | ||
} else { | ||
LOG.debug("Using configfile: {} ", dockerCfg); | ||
return dockerCfg; | ||
} | ||
} | ||
|
||
private ObjectNode extractAuthJson(final Path configPath) throws IOException { | ||
final JsonNode config = MAPPER.readTree(configPath.toFile()); | ||
|
||
Preconditions.checkState(config.isObject(), | ||
"config file contents are not a JSON Object, instead it is a %s", config.getNodeType()); | ||
|
||
if (config.has("auths")) { | ||
final JsonNode auths = config.get("auths"); | ||
Preconditions.checkState(auths.isObject(), | ||
"config file contents are not a JSON Object, instead it is a %s", auths.getNodeType()); | ||
return (ObjectNode) auths; | ||
} | ||
|
||
return (ObjectNode) config; | ||
} | ||
} |
Oops, something went wrong.