From bb67999808a6074d0753d4532bdd7106ca140d33 Mon Sep 17 00:00:00 2001 From: Ross Zurowski Date: Tue, 7 Jun 2022 14:50:08 -0400 Subject: [PATCH] tailcfg: add TailscaleSSHEnabled helper check (#4812) This commit adds a helper to check if Tailscale SSH is enabled. We're currently checking the SSH_HostKeys field in a few places, but later plan to add an explicit bool. This helper makes the check and any future changes easier. Signed-off-by: Ross Zurowski --- tailcfg/tailcfg.go | 8 ++++++++ tailcfg/tailcfg_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index d32f8edf9..111ae1cf9 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -475,6 +475,14 @@ type Hostinfo struct { // require changes to Hostinfo.Equal. } +// TailscaleSSHEnabled reports whether or not this node is acting as a +// Tailscale SSH server. +func (hi *Hostinfo) TailscaleSSHEnabled() bool { + // Currently, we use `SSH_HostKeys` as a proxy for this. However, we may later + // include non-Tailscale host keys, and will add a separate flag to rely on. + return hi != nil && len(hi.SSH_HostKeys) > 0 +} + // NetInfo contains information about the host's network state. type NetInfo struct { // MappingVariesByDestIP says whether the host's NAT mappings diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go index e5135a407..10ef8800e 100644 --- a/tailcfg/tailcfg_test.go +++ b/tailcfg/tailcfg_test.go @@ -271,6 +271,33 @@ func TestHostinfoHowEqual(t *testing.T) { } } +func TestHostinfoTailscaleSSHEnabled(t *testing.T) { + tests := []struct { + hi *Hostinfo + want bool + }{ + { + nil, + false, + }, + { + &Hostinfo{}, + false, + }, + { + &Hostinfo{SSH_HostKeys: []string{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO.... root@bar"}}, + true, + }, + } + + for i, tt := range tests { + got := tt.hi.TailscaleSSHEnabled() + if got != tt.want { + t.Errorf("%d. got %v; want %v", i, got, tt.want) + } + } +} + func TestNodeEqual(t *testing.T) { nodeHandles := []string{ "ID", "StableID", "Name", "User", "Sharer",