28
28
import java .io .IOException ;
29
29
import java .net .URL ;
30
30
import java .nio .charset .StandardCharsets ;
31
+ import java .nio .file .Paths ;
31
32
32
33
import javax .annotation .Nonnull ;
33
34
@@ -54,9 +55,7 @@ public class RegistryKeyMaterialFactory extends KeyMaterialFactory {
54
55
private static final String DOCKER_CONFIG_FILENAME = "config.json" ;
55
56
private static final String BLACKLISTED_PROPERTY_CREDS_STORE = "credsStore" ;
56
57
private static final String BLACKLISTED_PROPERTY_AUTHS = "auths" ;
57
- private static final String BLACKLISTED_PROPERTY_PROXIES = "proxies" ;
58
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 };
60
59
61
60
private final @ Nonnull String username ;
62
61
private final @ Nonnull String password ;
@@ -80,20 +79,25 @@ public RegistryKeyMaterialFactory(@Nonnull String username, @Nonnull String pass
80
79
public KeyMaterial materialize () throws IOException , InterruptedException {
81
80
FilePath dockerConfig = createSecretsDirectory ();
82
81
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)
84
83
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 ());
97
101
}
98
102
99
103
try {
@@ -114,32 +118,19 @@ public KeyMaterial materialize() throws IOException, InterruptedException {
114
118
return new RegistryKeyMaterial (dockerConfig , new EnvVars ("DOCKER_CONFIG" , dockerConfig .getRemote ()));
115
119
}
116
120
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 " );
137
129
}
138
-
139
- dockerConfig .child (DOCKER_CONFIG_FILENAME ).write (json .toString (), StandardCharsets .UTF_8 .name ());
140
130
}
131
+ jsonString = jsonObject .toString ();
141
132
}
142
- return dockerConfig ;
133
+ return jsonString ;
143
134
}
144
135
145
136
private static class RegistryKeyMaterial extends KeyMaterial {
0 commit comments