Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit routing to agentless nodes with non-UUID metadata.name #50915

Merged
merged 1 commit into from
Jan 9, 2025
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
8 changes: 8 additions & 0 deletions api/utils/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func (m *SSHRouteMatcher) RouteToServerScore(server RouteableServer) (score int)
score = max(score, matchAddr(addr))
}

// Allow a match on name, even though it may not be a UUID or EC2 ID,
// to support agentless hosts that were created without their name being a UUID.
// The score however, is lower than a true direct match, to prevent any
// breaking changes to routing.
if server.GetName() == m.cfg.Host {
score = max(score, indirectMatch)
}

return score
}

Expand Down
15 changes: 14 additions & 1 deletion api/utils/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package utils

import (
"cmp"
"context"
"testing"

Expand Down Expand Up @@ -273,6 +274,7 @@ func TestSSHRouteMatcherScoring(t *testing.T) {
tts := []struct {
desc string
hostname string
name string
addrs []string
score int
}{
Expand Down Expand Up @@ -309,12 +311,23 @@ func TestSSHRouteMatcherScoring(t *testing.T) {
},
score: notMatch,
},
{
desc: "non-uuid name",
name: "foo.example.com",
hostname: "foo.com",
addrs: []string{
"0.0.0.0:0",
"1.1.1.1:0",
},
score: indirectMatch,
},
}

for _, tt := range tts {
t.Run(tt.desc, func(t *testing.T) {
name := cmp.Or(tt.name, uuid.NewString())
score := matcher.RouteToServerScore(mockRouteableServer{
name: uuid.NewString(),
name: name,
hostname: tt.hostname,
publicAddr: tt.addrs,
})
Expand Down
47 changes: 37 additions & 10 deletions lib/proxy/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func TestRouteScoring(t *testing.T) {
hostname: "blue.example.com",
addr: "2.3.4.5:22",
},
{
name: "not-a-uuid",
hostname: "test.example.com",
addr: "3.4.5.6:22",
},
})

// scoring behavior is independent of routing strategy so we just
Expand Down Expand Up @@ -205,6 +210,11 @@ func TestRouteScoring(t *testing.T) {
host: "red.example.com",
expect: "blue.example.com",
},
{
desc: "non-uuid name",
host: "not-a-uuid",
expect: "test.example.com",
},
}

for _, tt := range tts {
Expand Down Expand Up @@ -326,6 +336,21 @@ func TestGetServers(t *testing.T) {
},
})

servers = append(servers,
&types.ServerV2{
Kind: types.KindNode,
SubKind: types.SubKindOpenSSHNode,
Version: types.V2,
Metadata: types.Metadata{
Name: "agentless-node-1",
},
Spec: types.ServerSpecV2{
Addr: "1.2.3.4:22",
Hostname: "agentless-1",
},
},
)

// ensure tests don't have order-dependence
rand.Shuffle(len(servers), func(i, j int) {
servers[i], servers[j] = servers[j], servers[i]
Expand Down Expand Up @@ -432,15 +457,6 @@ func TestGetServers(t *testing.T) {
require.Equal(t, "alpaca", srv.GetHostname())
},
},
{
name: "failure on invalid addresses",
site: testSite{cfg: &unambiguousCfg, nodes: servers},
host: "lion",
errAssertion: require.NoError,
serverAssertion: func(t *testing.T, srv types.Server) {
require.Empty(t, srv)
},
},
{
name: "case-insensitive match",
site: testSite{cfg: &unambiguousInsensitiveCfg, nodes: servers},
Expand All @@ -462,6 +478,17 @@ func TestGetServers(t *testing.T) {
require.Empty(t, srv)
},
},
{
name: "agentless match by non-uuid name",
site: testSite{cfg: &unambiguousCfg, nodes: servers},
host: "agentless-node-1",
errAssertion: require.NoError,
serverAssertion: func(t *testing.T, srv types.Server) {
require.NotNil(t, srv)
require.Equal(t, "agentless-1", srv.GetHostname())
require.True(t, srv.IsOpenSSHNode())
},
},
}

ctx := context.Background()
Expand Down Expand Up @@ -625,7 +652,7 @@ func TestRouter_DialHost(t *testing.T) {
SubKind: types.SubKindOpenSSHNode,
Version: types.V2,
Metadata: types.Metadata{
Name: uuid.NewString(),
Name: "agentless",
},
Spec: types.ServerSpecV2{
Addr: "127.0.0.1:9001",
Expand Down
Loading