2828import java .io .IOException ;
2929import java .net .URL ;
3030import java .nio .charset .StandardCharsets ;
31+ import java .nio .file .Paths ;
3132
3233import javax .annotation .Nonnull ;
3334
@@ -54,9 +55,7 @@ public class RegistryKeyMaterialFactory extends KeyMaterialFactory {
5455 private static final String DOCKER_CONFIG_FILENAME = "config.json" ;
5556 private static final String BLACKLISTED_PROPERTY_CREDS_STORE = "credsStore" ;
5657 private static final String BLACKLISTED_PROPERTY_AUTHS = "auths" ;
57- private static final String BLACKLISTED_PROPERTY_PROXIES = "proxies" ;
5858 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 };
6059
6160 private final @ Nonnull String username ;
6261 private final @ Nonnull String password ;
@@ -80,20 +79,25 @@ public RegistryKeyMaterialFactory(@Nonnull String username, @Nonnull String pass
8079 public KeyMaterial materialize () throws IOException , InterruptedException {
8180 FilePath dockerConfig = createSecretsDirectory ();
8281
83- // read the existing docker config file, which might hold some important settings (e.b. proxies)
82+ // read the user's home dir docker config file, which might hold some important settings (e.b. proxies)
8483 FilePath configJsonPath = FilePath .getHomeDirectory (this .launcher .getChannel ()).child (".docker" ).child (DOCKER_CONFIG_FILENAME );
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 );
84+ // read the current docker config which might hold some existing settings (e.b. credentials)
85+ FilePath existingDockerConfigPath = new FilePath (this .launcher .getChannel (),
86+ Paths .get (this .env .get ("DOCKER_CONFIG" ), DOCKER_CONFIG_FILENAME ).toString ());
87+
88+ String dockerConfigJson = "" ;
89+ if (existingDockerConfigPath .exists ()) {
90+ this .launcher .getListener ().getLogger ().print ("Reading the existing DOCKER_CONFIG '" +
91+ existingDockerConfigPath + "' docker config file.\n " );
92+ dockerConfigJson = existingDockerConfigPath .readToString ();
93+ } else if (configJsonPath .exists ()) {
94+ this .launcher .getListener ().getLogger ().print ("Reading the existing user's home '" +
95+ configJsonPath + "' docker config file.\n " );
96+ dockerConfigJson = removeBlacklistedProperties (configJsonPath .readToString (), BLACKLISTED_PROPERTIES );
97+ }
98+
99+ if (StringUtils .isNotBlank (dockerConfigJson )) {
100+ dockerConfig .child (DOCKER_CONFIG_FILENAME ).write (dockerConfigJson , StandardCharsets .UTF_8 .name ());
97101 }
98102
99103 try {
@@ -114,32 +118,19 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
114118 return new RegistryKeyMaterial (dockerConfig , new EnvVars ("DOCKER_CONFIG" , dockerConfig .getRemote ()));
115119 }
116120
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- }
121+ private String removeBlacklistedProperties (@ Nonnull String json , @ Nonnull String [] blacklistedProperties ) {
122+ String jsonString = "" ;
123+ if (StringUtils .isNotBlank (json )) {
124+ JSONObject jsonObject = JSONObject .fromObject (json );
125+ for (String property : blacklistedProperties ) {
126+ Object value = jsonObject .remove (property );
127+ if (value != null ) {
128+ this .launcher .getListener ().getLogger ().print ("Removing blacklisted property: " + property + "\n " );
137129 }
138-
139- dockerConfig .child (DOCKER_CONFIG_FILENAME ).write (json .toString (), StandardCharsets .UTF_8 .name ());
140130 }
131+ jsonString = jsonObject .toString ();
141132 }
142- return dockerConfig ;
133+ return jsonString ;
143134 }
144135
145136 private static class RegistryKeyMaterial extends KeyMaterial {
0 commit comments