11package com .github .dockerjava .core ;
22
3- import static com .google .common .base .Preconditions .checkNotNull ;
4- import static org .apache .commons .lang .BooleanUtils .isTrue ;
3+ import com .github .dockerjava .api .exception .DockerClientException ;
4+ import com .github .dockerjava .api .model .AuthConfig ;
5+ import com .github .dockerjava .api .model .AuthConfigurations ;
6+ import com .github .dockerjava .core .NameParser .HostnameReposName ;
7+ import com .github .dockerjava .core .NameParser .ReposTag ;
8+ import org .apache .commons .lang .StringUtils ;
9+ import org .apache .commons .lang .builder .EqualsBuilder ;
10+ import org .apache .commons .lang .builder .HashCodeBuilder ;
11+ import org .apache .commons .lang .builder .ToStringBuilder ;
12+ import org .apache .commons .lang .builder .ToStringStyle ;
513
614import java .io .File ;
715import java .io .FileInputStream ;
1422import java .util .Properties ;
1523import java .util .Set ;
1624
17- import org .apache .commons .lang .StringUtils ;
18- import org .apache .commons .lang .builder .EqualsBuilder ;
19- import org .apache .commons .lang .builder .HashCodeBuilder ;
20- import org .apache .commons .lang .builder .ToStringBuilder ;
21- import org .apache .commons .lang .builder .ToStringStyle ;
22-
23- import com .github .dockerjava .api .exception .DockerClientException ;
24- import com .github .dockerjava .api .model .AuthConfig ;
25- import com .github .dockerjava .api .model .AuthConfigurations ;
26- import com .github .dockerjava .core .NameParser .HostnameReposName ;
27- import com .github .dockerjava .core .NameParser .ReposTag ;
25+ import static com .google .common .base .Preconditions .checkNotNull ;
26+ import static org .apache .commons .lang .BooleanUtils .isTrue ;
2827
2928/**
3029 * Respects some of the docker CLI options. See https://docs.docker.com/engine/reference/commandline/cli/#environment-variables
@@ -53,8 +52,6 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
5352
5453 private static final String DOCKER_JAVA_PROPERTIES = "docker-java.properties" ;
5554
56- private static final String DOCKER_CFG = ".dockercfg" ;
57-
5855 private static final Set <String > CONFIG_KEYS = new HashSet <String >();
5956
6057 static {
@@ -71,16 +68,18 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
7168
7269 private final URI dockerHost ;
7370
74- private final String registryUsername , registryPassword , registryEmail , registryUrl , dockerConfig ;
71+ private final String registryUsername , registryPassword , registryEmail , registryUrl , dockerConfigPath ;
7572
7673 private final SSLConfig sslConfig ;
7774
7875 private final RemoteApiVersion apiVersion ;
7976
80- DefaultDockerClientConfig (URI dockerHost , String dockerConfig , String apiVersion , String registryUrl ,
81- String registryUsername , String registryPassword , String registryEmail , SSLConfig sslConfig ) {
77+ private DockerConfigFile dockerConfig = null ;
78+
79+ DefaultDockerClientConfig (URI dockerHost , String dockerConfigPath , String apiVersion , String registryUrl ,
80+ String registryUsername , String registryPassword , String registryEmail , SSLConfig sslConfig ) {
8281 this .dockerHost = checkDockerHostScheme (dockerHost );
83- this .dockerConfig = dockerConfig ;
82+ this .dockerConfigPath = dockerConfigPath ;
8483 this .apiVersion = RemoteApiVersion .parseConfigWithDefault (apiVersion );
8584 this .sslConfig = sslConfig ;
8685 this .registryUsername = registryUsername ;
@@ -232,7 +231,18 @@ public String getRegistryUrl() {
232231 return registryUrl ;
233232 }
234233
235- public String getDockerConfig () {
234+ public String getDockerConfigPath () {
235+ return dockerConfigPath ;
236+ }
237+
238+ public DockerConfigFile getDockerConfig () {
239+ if (dockerConfig == null ) {
240+ try {
241+ dockerConfig = DockerConfigFile .loadConfig (new File (getDockerConfigPath ()));
242+ } catch (IOException e ) {
243+ throw new DockerClientException ("Failed to parse docker configuration file" , e );
244+ }
245+ }
236246 return dockerConfig ;
237247 }
238248
@@ -251,47 +261,23 @@ && getRegistryUrl() != null) {
251261
252262 @ Override
253263 public AuthConfig effectiveAuthConfig (String imageName ) {
254- AuthConfig authConfig = null ;
255-
256- File dockerCfgFile = new File (getDockerConfig () + File .separator + DOCKER_CFG );
264+ AuthConfig authConfig = getAuthConfig ();
257265
258- if (dockerCfgFile .exists () && dockerCfgFile .isFile () && imageName != null ) {
259- AuthConfigFile authConfigFile ;
260- try {
261- authConfigFile = AuthConfigFile .loadConfig (dockerCfgFile );
262- } catch (IOException e ) {
263- throw new DockerClientException ("Failed to parse dockerCfgFile" , e );
264- }
265- ReposTag reposTag = NameParser .parseRepositoryTag (imageName );
266- HostnameReposName hostnameReposName = NameParser .resolveRepositoryName (reposTag .repos );
267-
268- authConfig = authConfigFile .resolveAuthConfig (hostnameReposName .hostname );
266+ if (authConfig != null ) {
267+ return authConfig ;
269268 }
270269
271- AuthConfig otherAuthConfig = getAuthConfig ();
270+ DockerConfigFile dockerCfg = getDockerConfig ();
272271
273- if (otherAuthConfig != null ) {
274- authConfig = otherAuthConfig ;
275- }
272+ ReposTag reposTag = NameParser .parseRepositoryTag (imageName );
273+ HostnameReposName hostnameReposName = NameParser .resolveRepositoryName (reposTag .repos );
276274
277- return authConfig ;
275+ return dockerCfg . resolveAuthConfig ( hostnameReposName . hostname ) ;
278276 }
279277
280278 @ Override
281279 public AuthConfigurations getAuthConfigurations () {
282- File dockerCfgFile = new File (getDockerConfig () + File .separator + DOCKER_CFG );
283- if (dockerCfgFile .exists () && dockerCfgFile .isFile ()) {
284- AuthConfigFile authConfigFile ;
285- try {
286- authConfigFile = AuthConfigFile .loadConfig (dockerCfgFile );
287- } catch (IOException e ) {
288- throw new DockerClientException ("Failed to parse dockerCfgFile" , e );
289- }
290-
291- return authConfigFile .getAuthConfigurations ();
292- }
293-
294- return new AuthConfigurations ();
280+ return getDockerConfig ().getAuthConfigurations ();
295281 }
296282
297283 @ Override
0 commit comments