Repository: opentofu/opentofu
File: cmd/tofu/provider_source.go (Go)

Code before:
		// cliconfig.ProviderInstallationLocation implementations.
		panic(fmt.Sprintf("unexpected provider source location type %T", loc))
	}
}

func providerDevOverrides(configs []*cliconfig.ProviderInstallation) map[addrs.Provider]getproviders.PackageLocalDir {
	if len(configs) == 0 {
		return nil
	}

	// There should only be zero or one configurations, which is checked by
	// the validation logic in the cliconfig package. Therefore we'll just
	// ignore any additional configurations in here.
	return configs[0].DevOverrides
}

// providerSourceLocationConfig is meant to build a global configuration for the
// remote locations to download a provider from. This is built out of the
// TF_PROVIDER_DOWNLOAD_RETRY env variable and is meant to be passed through
// [getproviders.Source] all the way down to the [getproviders.PackageLocation]
// to be able to tweak the configurations of the http clients used there.
func providerSourceLocationConfig(tofurcCfg int) getproviders.LocationConfig {
	// If there is no configuration for the retries in .tofurc, get the one from env variable
	if tofurcCfg == 0 {
		tofurcCfg = cliconfig.ProviderDownloadRetries()
	}
	return getproviders.LocationConfig{
		ProviderDownloadRetries: tofurcCfg,
	}
}

// providerSourceLocationConfigFromEnv is similar to providerSourceLocationConfig but does not
// take into account the information from the configuration. This is like so because for some
// commands, there is no specific tofurc configuration for the retry, so we want to use the
// env variable if defined and if not, its default.
func providerSourceLocationConfigFromEnv() getproviders.LocationConfig {
	return getproviders.LocationConfig{
		ProviderDownloadRetries: cliconfig.ProviderDownloadRetries(),
	}
}


Review comment (security):
From reading the retryablehttp source code it seems like setting RetryMax to 1 means a maximum of two requests -- one original request and one retry -- and so it seems like zero is a valid value meaning that the client should make only one request and not retry at all. So, my initial instinct was to say that we should allow setting `download_retry_count = 0` to represent "do not retry at all".

However, I also notice that the handling of the environment variable was already treating `TF_PROVIDER_DOWNLOAD_RETRY=0` as meaning "use the default retry setting" (2), so I suppose it's at least _consistent_ that we don't allow completely disabling retries.

I don't know if this is actually important, then -- if it were important to completely disable retries then we might've already heard a request for that related to the environment variable. But since this is entirely new and so we're not constrained by the existing functionality, maybe we should build this so that completely omitting `download_retry_count` is treated differently than setting it to zero, and so explicitly setting it to zero means to make a maximum of one request before returning an error.

From referring to the code that handles the OCI credentials settings, it seems like HCL 1 could support this by decoding into a field of type `*int` and then "unset" would be represented as `nil`:

https://github.com/opentofu/opentofu/blob/78744a23c746b61d44b9812a194481c4a6e053ac/internal/command/cliconfig/oci_credentials.go#L257-L261

https://github.com/opentofu/opentofu/blob/78744a23c746b61d44b9812a194481c4a6e053ac/internal/command/cliconfig/oci_credentials.go#L275-L278



Code after:
		// cliconfig.ProviderInstallationLocation implementations.
		panic(fmt.Sprintf("unexpected provider source location type %T", loc))
	}
}

func providerDevOverrides(configs []*cliconfig.ProviderInstallation) map[addrs.Provider]getproviders.PackageLocalDir {
	if len(configs) == 0 {
		return nil
	}

	// There should only be zero or one configurations, which is checked by
	// the validation logic in the cliconfig package. Therefore we'll just
	// ignore any additional configurations in here.
	return configs[0].DevOverrides
}

// providerSourceLocationConfig is meant to build a global configuration for the
// remote locations to download a provider from. This is built out of the
// TF_PROVIDER_DOWNLOAD_RETRY env variable and is meant to be passed through
// [getproviders.Source] all the way down to the [getproviders.PackageLocation]
// to be able to tweak the configurations of the http clients used there.
func providerSourceLocationConfig(locationRetries cliconfig.ProviderInstallationMethodRetries) getproviders.LocationConfig {
	// If there is no configuration for the retries in .tofurc, get the one from env variable
	retries, configured := locationRetries()
	if !configured {
		retries = cliconfig.ProviderDownloadRetries()
	}
	return getproviders.LocationConfig{
		ProviderDownloadRetries: retries,
	}
}

// providerSourceLocationConfigFromEnv is similar to providerSourceLocationConfig but does not
// take into account the information from the configuration. This is like so because for some
// commands, there is no specific tofurc configuration for the retry, so we want to use the
// env variable if defined and if not, its default.
func providerSourceLocationConfigFromEnv() getproviders.LocationConfig {
	return getproviders.LocationConfig{
		ProviderDownloadRetries: cliconfig.ProviderDownloadRetries(),
	}
}