Skip to content

Commit 507ed55

Browse files
committed
Better document a bad SHA256 invocation
This code uses sha256.New().Sum instead of sha256.Sum256. See https://go.dev/play/p/vSW0U3Hq4qk for a demonstration of the differences. SHA256 is used to generate identifiers that map server names to settings. I think these IDs have been persisted to external sources (setting.go mentions S3 buckets), so moving this to a good invocation is hard and probably not worth it. Instead, document the exact behavior of what's happening with the bad invocation, make it more obvious, and enshrine it within a helper function.
1 parent 3a56067 commit 507ed55

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

provider/aws/registries.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ func (p *Provider) RegistryAdd(server, username, password string) (*structs.Regi
7575
return nil, log.Error(err)
7676
}
7777

78-
id := fmt.Sprintf("%x", sha256.New().Sum([]byte(server)))
78+
id := legacyServerIdentifier(server)
7979

80-
if err := p.SettingPut(fmt.Sprintf("system/registries/%s", id), string(data)); err != nil {
80+
if err := p.SettingPut("system/registries/%s"+id, string(data)); err != nil {
8181
return nil, log.Error(err)
8282
}
8383

@@ -93,7 +93,8 @@ func (p *Provider) RegistryAdd(server, username, password string) (*structs.Regi
9393
func (p *Provider) RegistryRemove(server string) error {
9494
log := Logger.At("RegistryRemove").Namespace("server=%q", server).Start()
9595

96-
key := fmt.Sprintf("system/registries/%x", sha256.New().Sum([]byte(server)))
96+
id := legacyServerIdentifier(server)
97+
key := "system/registries/%s" + id
9798

9899
if _, err := p.SettingExists(key); err != nil {
99100
return log.Error(errorNotFound(fmt.Sprintf("registry not found: %s", server)))
@@ -135,3 +136,13 @@ func (p *Provider) RegistryList() (structs.Registries, error) {
135136

136137
return registries, log.Success()
137138
}
139+
140+
var hashOfNothing = sha256.New().Sum(nil)
141+
142+
// legacyServerIdentifier generates a hex string from a server.
143+
// This format is suboptimal, but it must be preserved for compatibility reasons
144+
// as deviation from this format would orphan registry settings.
145+
// This function exist to make the behavior more apparent.
146+
func legacyServerIdentifier(server string) string {
147+
return fmt.Sprintf("%x", append([]byte(server), hashOfNothing[:]...))
148+
}

0 commit comments

Comments
 (0)