@@ -71,7 +71,7 @@ func Provider() terraform.ResourceProvider {
71
71
"username" : {
72
72
Type : schema .TypeString ,
73
73
Optional : true ,
74
- ConflictsWith : []string {"registry_auth.config_file" },
74
+ ConflictsWith : []string {"registry_auth.config_file" , "registry_auth.config_file_content" },
75
75
DefaultFunc : schema .EnvDefaultFunc ("DOCKER_REGISTRY_USER" , "" ),
76
76
Description : "Username for the registry" ,
77
77
},
@@ -80,18 +80,25 @@ func Provider() terraform.ResourceProvider {
80
80
Type : schema .TypeString ,
81
81
Optional : true ,
82
82
Sensitive : true ,
83
- ConflictsWith : []string {"registry_auth.config_file" },
83
+ ConflictsWith : []string {"registry_auth.config_file" , "registry_auth.config_file_content" },
84
84
DefaultFunc : schema .EnvDefaultFunc ("DOCKER_REGISTRY_PASS" , "" ),
85
85
Description : "Password for the registry" ,
86
86
},
87
87
88
88
"config_file" : {
89
89
Type : schema .TypeString ,
90
90
Optional : true ,
91
- ConflictsWith : []string {"registry_auth.username" , "registry_auth.password" },
91
+ ConflictsWith : []string {"registry_auth.username" , "registry_auth.password" , "registry_auth.config_file_content" },
92
92
DefaultFunc : schema .EnvDefaultFunc ("DOCKER_CONFIG" , "~/.docker/config.json" ),
93
93
Description : "Path to docker json file for registry auth" ,
94
94
},
95
+
96
+ "config_file_content" : {
97
+ Type : schema .TypeString ,
98
+ Optional : true ,
99
+ ConflictsWith : []string {"registry_auth.username" , "registry_auth.password" , "registry_auth.config_file" },
100
+ Description : "Plain content of the docker json file for registry auth" ,
101
+ },
95
102
},
96
103
},
97
104
},
@@ -184,10 +191,43 @@ func providerSetToRegistryAuth(authSet *schema.Set) (*AuthConfigs, error) {
184
191
// For each registry_auth block, generate an AuthConfiguration using either
185
192
// username/password or the given config file
186
193
if username , ok := auth ["username" ]; ok && username .(string ) != "" {
194
+ log .Println ("[DEBUG] Using username for registry auths:" , username )
187
195
authConfig .Username = auth ["username" ].(string )
188
196
authConfig .Password = auth ["password" ].(string )
197
+
198
+ // Note: check for config_file_content first because config_file has a default which would be used
199
+ // neverthelesss config_file_content is set or not. The default has to be kept to check for the
200
+ // environment variable and to be backwards compatible
201
+ } else if configFileContent , ok := auth ["config_file_content" ]; ok && configFileContent .(string ) != "" {
202
+ log .Println ("[DEBUG] Parsing file content for registry auths:" , configFileContent .(string ))
203
+ r := strings .NewReader (configFileContent .(string ))
204
+
205
+ // Parse and set the auth
206
+ auths , err := newAuthConfigurations (r )
207
+ if err != nil {
208
+ return nil , fmt .Errorf ("Error parsing docker registry config json: %v" , err )
209
+ }
210
+
211
+ foundRegistry := false
212
+ for registry , authFileConfig := range auths .Configs {
213
+ if authConfig .ServerAddress == normalizeRegistryAddress (registry ) {
214
+ authConfig .Username = authFileConfig .Username
215
+ authConfig .Password = authFileConfig .Password
216
+ foundRegistry = true
217
+ }
218
+ }
219
+
220
+ if ! foundRegistry {
221
+ return nil , fmt .Errorf ("Couldn't find registry config for '%s' in file content" , authConfig .ServerAddress )
222
+ }
223
+
224
+ // As last step we check if a config file path is given
189
225
} else if configFile , ok := auth ["config_file" ]; ok && configFile .(string ) != "" {
190
226
filePath := configFile .(string )
227
+ log .Println ("[DEBUG] Parsing file for registry auths:" , filePath )
228
+
229
+ // We manually expand the path and do not use the 'pathexpand' interpolation function
230
+ // because in the default of this varable we refer to '~/.docker/config.json'
191
231
if strings .HasPrefix (filePath , "~/" ) {
192
232
usr , err := user .Current ()
193
233
if err != nil {
@@ -201,6 +241,7 @@ func providerSetToRegistryAuth(authSet *schema.Set) (*AuthConfigs, error) {
201
241
return nil , fmt .Errorf ("Error opening docker registry config file: %v" , err )
202
242
}
203
243
244
+ // Parse and set the auth
204
245
auths , err := newAuthConfigurations (r )
205
246
if err != nil {
206
247
return nil , fmt .Errorf ("Error parsing docker registry config json: %v" , err )
0 commit comments