@@ -71,7 +71,7 @@ func Provider() terraform.ResourceProvider {
7171 "username" : {
7272 Type : schema .TypeString ,
7373 Optional : true ,
74- ConflictsWith : []string {"registry_auth.config_file" },
74+ ConflictsWith : []string {"registry_auth.config_file" , "registry_auth.config_file_content" },
7575 DefaultFunc : schema .EnvDefaultFunc ("DOCKER_REGISTRY_USER" , "" ),
7676 Description : "Username for the registry" ,
7777 },
@@ -80,18 +80,25 @@ func Provider() terraform.ResourceProvider {
8080 Type : schema .TypeString ,
8181 Optional : true ,
8282 Sensitive : true ,
83- ConflictsWith : []string {"registry_auth.config_file" },
83+ ConflictsWith : []string {"registry_auth.config_file" , "registry_auth.config_file_content" },
8484 DefaultFunc : schema .EnvDefaultFunc ("DOCKER_REGISTRY_PASS" , "" ),
8585 Description : "Password for the registry" ,
8686 },
8787
8888 "config_file" : {
8989 Type : schema .TypeString ,
9090 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" },
9292 DefaultFunc : schema .EnvDefaultFunc ("DOCKER_CONFIG" , "~/.docker/config.json" ),
9393 Description : "Path to docker json file for registry auth" ,
9494 },
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+ },
95102 },
96103 },
97104 },
@@ -184,10 +191,43 @@ func providerSetToRegistryAuth(authSet *schema.Set) (*AuthConfigs, error) {
184191 // For each registry_auth block, generate an AuthConfiguration using either
185192 // username/password or the given config file
186193 if username , ok := auth ["username" ]; ok && username .(string ) != "" {
194+ log .Println ("[DEBUG] Using username for registry auths:" , username )
187195 authConfig .Username = auth ["username" ].(string )
188196 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
189225 } else if configFile , ok := auth ["config_file" ]; ok && configFile .(string ) != "" {
190226 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'
191231 if strings .HasPrefix (filePath , "~/" ) {
192232 usr , err := user .Current ()
193233 if err != nil {
@@ -201,6 +241,7 @@ func providerSetToRegistryAuth(authSet *schema.Set) (*AuthConfigs, error) {
201241 return nil , fmt .Errorf ("Error opening docker registry config file: %v" , err )
202242 }
203243
244+ // Parse and set the auth
204245 auths , err := newAuthConfigurations (r )
205246 if err != nil {
206247 return nil , fmt .Errorf ("Error parsing docker registry config json: %v" , err )
0 commit comments