2424
2525package org .jenkinsci .plugins .docker .commons .impl ;
2626
27+ import java .io .File ;
2728import java .io .IOException ;
2829import java .net .URL ;
2930import java .nio .charset .StandardCharsets ;
5152public class RegistryKeyMaterialFactory extends KeyMaterialFactory {
5253
5354 private static final String DOCKER_CONFIG_FILENAME = "config.json" ;
54- private static final String [] BLACKLISTED_PROPERTIES = { "auths" , "credsStore" };
55+ private static final String BLACKLISTED_PROPERTY_CREDS_STORE = "credsStore" ;
56+ private static final String BLACKLISTED_PROPERTY_AUTHS = "auths" ;
57+ private static final String BLACKLISTED_PROPERTY_PROXIES = "proxies" ;
58+ private static final String [] BLACKLISTED_PROPERTIES = { BLACKLISTED_PROPERTY_AUTHS , BLACKLISTED_PROPERTY_CREDS_STORE };
59+ private static final String [] BLACKLISTED_NESTED_PROPERTIES = { BLACKLISTED_PROPERTY_CREDS_STORE , BLACKLISTED_PROPERTY_PROXIES };
5560
5661 private final @ Nonnull String username ;
5762 private final @ Nonnull String password ;
@@ -77,21 +82,18 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
7782
7883 // read the existing docker config file, which might hold some important settings (e.b. proxies)
7984 FilePath configJsonPath = FilePath .getHomeDirectory (this .launcher .getChannel ()).child (".docker" ).child (DOCKER_CONFIG_FILENAME );
80- if (configJsonPath .exists ()) {
81- String configJson = configJsonPath .readToString ();
82- if (StringUtils .isNotBlank (configJson )) {
83- launcher .getListener ().getLogger ().print ("Using the existing docker config file." );
84-
85- JSONObject json = JSONObject .fromObject (configJson );
86- for (String property : BLACKLISTED_PROPERTIES ) {
87- Object value = json .remove (property );
88- if (value != null ) {
89- launcher .getListener ().getLogger ().print ("Removing blacklisted property: " + property );
90- }
91- }
92-
93- dockerConfig .child (DOCKER_CONFIG_FILENAME ).write (json .toString (), StandardCharsets .UTF_8 .name ());
94- }
85+ dockerConfig = UpdateDockerConfigFromSource (dockerConfig , configJsonPath , BLACKLISTED_PROPERTIES );
86+
87+ // Read the existing docker config file from a nested config block, will probably hold some previous credentials
88+ String existingDockerSecretConfigPath = this .env .get ("DOCKER_CONFIG" );
89+ if (StringUtils .isNotBlank (existingDockerSecretConfigPath )) {
90+ // Can't use FilePath(File) yet as not supported till later versions of jenkins..
91+ //FilePath existingDockerConfig = FilePath(new File(existingDockerSecretConfigPath, DOCKER_CONFIG_FILENAME));
92+ FilePath baseDir = getContext ().getBaseDir ();
93+ // Need to get tmp dir - get base dir length and increase by 1 to include the path separator
94+ String existingTmpConfigDir = existingDockerSecretConfigPath .substring (baseDir .getRemote ().length () + 1 );
95+ FilePath existingDockerConfigPath = baseDir .child (existingTmpConfigDir ).child (DOCKER_CONFIG_FILENAME );
96+ dockerConfig = updateDockerConfigFromSource (dockerConfig , existingDockerConfigPath , BLACKLISTED_NESTED_PROPERTIES );
9597 }
9698
9799 try {
@@ -112,6 +114,34 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
112114 return new RegistryKeyMaterial (dockerConfig , new EnvVars ("DOCKER_CONFIG" , dockerConfig .getRemote ()));
113115 }
114116
117+ /**
118+ * Copy docker config source data to another docker config
119+ * @param dockerConfig
120+ * @param dockerConfigSourcePath
121+ * @param blacklistedProperties
122+ * @return FilePath dockerConfig
123+ */
124+ private FilePath updateDockerConfigFromSource (@ Nonnull FilePath dockerConfig , @ Nonnull FilePath dockerConfigSourcePath , @ Nonnull String [] blacklistedProperties ) throws IOException , InterruptedException {
125+ // Make sure config exists
126+ if (dockerConfigSourcePath .exists ()) {
127+ String configJson = dockerConfigSourcePath .readToString ();
128+ if (StringUtils .isNotBlank (configJson )) {
129+ this .launcher .getListener ().getLogger ().print ("Using the existing docker config file." );
130+
131+ JSONObject json = JSONObject .fromObject (configJson );
132+ for (String property : blacklistedProperties ) {
133+ Object value = json .remove (property );
134+ if (value != null ) {
135+ this .launcher .getListener ().getLogger ().print ("Removing blacklisted property: " + property );
136+ }
137+ }
138+
139+ dockerConfig .child (DOCKER_CONFIG_FILENAME ).write (json .toString (), StandardCharsets .UTF_8 .name ());
140+ }
141+ }
142+ return dockerConfig ;
143+ }
144+
115145 private static class RegistryKeyMaterial extends KeyMaterial {
116146
117147 private final FilePath dockerConfig ;
0 commit comments