Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions internal/server/add_registry_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func buildRegistrySourceEntry(rawURL, protocol, id, name string) (config.Registr
ErrUnsupportedRegistryProtocol, protocol, defaultRegistryProtocol)
}
if id == "" {
id = deriveRegistryID(u.Host)
id = deriveRegistryID(u.Hostname())
}
if name == "" {
name = id
Expand Down Expand Up @@ -272,13 +272,11 @@ func validateNewRegistrySource(cfg *config.Config, entry config.RegistryEntry) e
return nil
}

// deriveRegistryID slugifies a host into a stable registry id, dropping a
// leading "www." and a trailing port, and replacing non-alphanumerics with "-".
// deriveRegistryID slugifies a hostname (no port, no IPv6 brackets) into a
// stable registry id, dropping a leading "www." and replacing non-alphanumerics
// with "-".
func deriveRegistryID(host string) string {
host = strings.ToLower(host)
if h, _, ok := strings.Cut(host, ":"); ok {
host = h
}
host = strings.TrimPrefix(host, "www.")
var b strings.Builder
for _, r := range host {
Expand Down
25 changes: 25 additions & 0 deletions internal/server/add_registry_source_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package server

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// An IPv6 literal host contains colons. Cutting at the first ':' to drop a port
// reduced every such host to its first group, so distinct registries collided
// on the same derived id and the second one was refused as a duplicate.
func TestBuildRegistrySourceEntry_IPv6HostID(t *testing.T) {
cases := map[string]string{
"https://[2606:4700:4700::1111]/": "2606-4700-4700--1111",
"https://[2606:4700::64]:8443/": "2606-4700--64",
"https://www.Registry.Example:8443/": "registry-example",
"https://registry.acme.example/v0.1/": "registry-acme-example",
}
for raw, want := range cases {
entry, err := buildRegistrySourceEntry(raw, "", "", "")
require.NoError(t, err, raw)
assert.Equal(t, want, entry.ID, raw)
}
}
Loading