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.
Adds support for RegistryAuthSupplier.
Adds support for RegistryAuthSupplier. Client code can consume this using DefaultDockerClient builder. Provides support for authenticating against GCR to allow pushes/pulls from GCR.
- Loading branch information
David Morhovich
authored and
David Morhovich
committed
May 18, 2017
1 parent
835b04c
commit 1af4f57
Showing
15 changed files
with
661 additions
and
99 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
117 changes: 117 additions & 0 deletions
117
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,117 @@ | ||
/*- | ||
* -\-\- | ||
* 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.spotify.docker.client.messages.RegistryAuth; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Iterator; | ||
import org.glassfish.jersey.internal.util.Base64; | ||
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(); | ||
|
||
|
||
public RegistryAuth fromComfig(Path configPath, String serverAddress) throws IOException { | ||
return parseDockerConfig(configPath, serverAddress).build(); | ||
} | ||
|
||
public RegistryAuth.Builder parseDockerConfig(final Path configPath, String serverAddress) | ||
throws IOException { | ||
checkNotNull(configPath); | ||
final RegistryAuth.Builder authBuilder = RegistryAuth.builder(); | ||
final JsonNode authJson = this.extractAuthJson(configPath); | ||
|
||
if (isNullOrEmpty(serverAddress)) { | ||
final Iterator<String> servers = authJson.fieldNames(); | ||
if (servers.hasNext()) { | ||
serverAddress = servers.next(); | ||
} | ||
} else { | ||
if (!authJson.has(serverAddress)) { | ||
LOG.error("Could not find auth config for {}. Returning empty builder", serverAddress); | ||
return RegistryAuth.builder().serverAddress(serverAddress); | ||
} | ||
} | ||
|
||
final JsonNode serverAuth = authJson.get(serverAddress); | ||
if (serverAuth != null && serverAuth.has("auth")) { | ||
authBuilder.serverAddress(serverAddress); | ||
final String authString = serverAuth.get("auth").asText(); | ||
final String[] authParams = Base64.decodeAsString(authString).split(":"); | ||
|
||
if (authParams.length == 2) { | ||
authBuilder.username(authParams[0].trim()); | ||
authBuilder.password(authParams[1].trim()); | ||
} else if (serverAuth.has("identityToken")) { | ||
authBuilder.identityToken(serverAuth.get("identityToken").asText()); | ||
return authBuilder; | ||
} else { | ||
LOG.warn("Failed to parse auth string for {}", serverAddress); | ||
return authBuilder; | ||
} | ||
} else { | ||
LOG.warn("Could not find auth field for {}", serverAddress); | ||
return authBuilder; | ||
} | ||
|
||
if (serverAuth.has("email")) { | ||
authBuilder.email(serverAuth.get("email").asText()); | ||
} | ||
|
||
return authBuilder; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
public JsonNode extractAuthJson(final Path configPath) throws IOException { | ||
final JsonNode config = MAPPER.readTree(configPath.toFile()); | ||
|
||
if (config.has("auths")) { | ||
return config.get("auths"); | ||
} | ||
|
||
return config; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/spotify/docker/client/NoOpRegistryAuthSupplier.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,56 @@ | ||
/*- | ||
* -\-\- | ||
* docker-client | ||
* -- | ||
* Copyright (C) 2016 - 2017 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 com.spotify.docker.client.exceptions.DockerException; | ||
import com.spotify.docker.client.messages.RegistryAuth; | ||
import com.spotify.docker.client.messages.RegistryAuthSupplier; | ||
import java.rmi.registry.Registry; | ||
import sun.reflect.generics.reflectiveObjects.NotImplementedException; | ||
|
||
/** | ||
* Wraps a RegistryAuth with the RegisrtyAuthSupplier interface. | ||
*/ | ||
public class NoOpRegistryAuthSupplier | ||
implements RegistryAuthSupplier { | ||
|
||
private final RegistryAuth registryAuth; | ||
|
||
public NoOpRegistryAuthSupplier(RegistryAuth registryAuth) { | ||
this.registryAuth = registryAuth; | ||
} | ||
|
||
public NoOpRegistryAuthSupplier() { | ||
registryAuth = null; | ||
|
||
} | ||
|
||
@Override | ||
public RegistryAuth authFor(String imageName) throws DockerException { | ||
return registryAuth; | ||
} | ||
|
||
@Override | ||
public RegistryAuth authForSwarm() { | ||
return registryAuth; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/spotify/docker/client/gcr/GCloudProcess.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,31 @@ | ||
/*- | ||
* -\-\- | ||
* 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.gcr; | ||
|
||
import java.io.IOException; | ||
|
||
public class GCloudProcess { | ||
|
||
public Process runGcloudDocker() throws IOException { | ||
return Runtime.getRuntime().exec("gcloud docker -a"); | ||
} | ||
|
||
} |
Oops, something went wrong.