24
24
25
25
package org .jenkinsci .plugins .docker .commons .impl ;
26
26
27
+ import java .io .File ;
27
28
import java .io .IOException ;
28
29
import java .net .URL ;
29
30
import java .nio .charset .StandardCharsets ;
51
52
public class RegistryKeyMaterialFactory extends KeyMaterialFactory {
52
53
53
54
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 };
55
60
56
61
private final @ Nonnull String username ;
57
62
private final @ Nonnull String password ;
@@ -77,21 +82,18 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
77
82
78
83
// read the existing docker config file, which might hold some important settings (e.b. proxies)
79
84
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 );
95
97
}
96
98
97
99
try {
@@ -112,6 +114,34 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
112
114
return new RegistryKeyMaterial (dockerConfig , new EnvVars ("DOCKER_CONFIG" , dockerConfig .getRemote ()));
113
115
}
114
116
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
+
115
145
private static class RegistryKeyMaterial extends KeyMaterial {
116
146
117
147
private final FilePath dockerConfig ;
0 commit comments