[client] Name the resolving reader for what it does, not what it makes

ReadOrGenerateConfig reads the profile config and falls back to the defaults in
memory when there is no file. "Generate" reads as "produces and stores", which
is the opposite of the property the rename it came from was meant to advertise:
the read is pure, writes nothing and mints no identity.

ReadConfigOrDefault says the same without the side effect, and pairs with
GetExistingConfig, which fails where this one falls back. Its doc comment now
states the absence of a write rather than only the fallback.

Pure rename; the two remaining mentions of the pre-branch name ReadConfig in
the tests go with it.

Reported by pappz in review.
This commit is contained in:
riccardom
2026-09-25 13:21:15 +02:00
parent 85c6bf2e27
commit 2885e3771d
7 changed files with 41 additions and 39 deletions
+6 -4
View File
@@ -1330,9 +1330,11 @@ func CreateInMemoryConfig(input ConfigInput) (*Config, error) {
return createProvisionedConfig(input)
}
// ReadOrGenerateConfig reads the profile config at configPath if it exists, or
// generates one in memory from the defaults.
func ReadOrGenerateConfig(configPath string) (*Config, error) {
// ReadConfigOrDefault reads the profile config at configPath, or resolves the
// default config in memory when the file does not exist. It never writes, and
// never mints an identity — EnsureIdentity is where that happens, so the
// caller that provisions is also the one that persists.
func ReadConfigOrDefault(configPath string) (*Config, error) {
return readConfig(configPath, true)
}
@@ -1457,7 +1459,7 @@ func ConfigToJSON(config *Config) (string, error) {
//
// The peer identity is deliberately none of its business, in either direction.
// It does not generate one: a read cannot hand back keys that nothing will
// write down (see ReadOrGenerateConfig). Nor does it refuse a document that
// write down (see ReadConfigOrDefault). Nor does it refuse a document that
// carries none, because a config legitimately has no identity between a logout
// and the next login — mobile logout clears both keys in place — and this is
// also the deserializer the iOS SDK copies a config through. Whoever goes on
@@ -112,8 +112,8 @@ func TestReadsDoNotWriteTheConfigBack(t *testing.T) {
denormalized := []byte(`{"WgIface":"wt0"}`)
for name, read := range map[string]func(string) (*Config, error){
"GetExistingConfig": GetExistingConfig,
"ReadOrGenerateConfig": ReadOrGenerateConfig,
"GetExistingConfig": GetExistingConfig,
"ReadConfigOrDefault": ReadConfigOrDefault,
} {
t.Run(name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "profile.json")
@@ -131,17 +131,17 @@ func TestReadsDoNotWriteTheConfigBack(t *testing.T) {
}
}
// ReadConfig resolves a default config for a profile that has no file yet, and
// that must not create the file either.
// ReadConfigOrDefault resolves a default config for a profile that has no file
// yet, and that must not create the file either.
func TestReadConfigDoesNotCreateTheFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "absent.json")
cfg, err := ReadOrGenerateConfig(path)
cfg, err := ReadConfigOrDefault(path)
require.NoError(t, err)
require.Equal(t, DefaultManagementURL, cfg.ManagementURL.String())
_, err = os.Stat(path)
require.True(t, os.IsNotExist(err), "ReadConfig created the config file")
require.True(t, os.IsNotExist(err), "ReadConfigOrDefault created the config file")
}
// The identity is the one thing a read cannot recompute, so it is provisioned
@@ -310,8 +310,8 @@ func TestWouldChangeIgnoresRestatedCertificatePaths(t *testing.T) {
// A read that lands on a missing file must not hand back keys: nothing would
// write them down, so the caller would connect with an identity that changes on
// the next run and registers a second peer.
func TestReadOrGenerateConfigCarriesNoIdentity(t *testing.T) {
cfg, err := ReadOrGenerateConfig(filepath.Join(t.TempDir(), "absent.json"))
func TestReadConfigOrDefaultCarriesNoIdentity(t *testing.T) {
cfg, err := ReadConfigOrDefault(filepath.Join(t.TempDir(), "absent.json"))
require.NoError(t, err)
require.Empty(t, cfg.PrivateKey, "a read minted a WireGuard key")