Files
nebula/identity_test.go
T
Matt RichardsonandClaude Fable 5.1 613c7e3061 Add the identity command and serve it on an opt-in HTTP info API
Build slackhq/nebula#1821 ("who is this vpn address?" over a local
HTTP+JSON socket) on the command registry, so the lookup is one command
on every surface and the socket is one more local API.

identity
- `identity [-address <vpn addr>[:port]]`: no address reports this node;
  with one, this node or the peer behind an established tunnel
- Certificate fields only: name, vpn addresses, networks, unsafe
  networks, groups, fingerprint, issuer, validity, cert version; a peer's
  fingerprint is the one nebula computed when it verified the cert
- Port accepted and stripped, 4in6 unmapped, so a RemoteAddr passes
  straight through
- No tunnel is a commands.NotFound; an empty -address is refused
- Lighthouse TXT lookup shares the owner and peer lookups, keeps answering
  with the initiating certificate, and no longer panics without a default cert
- Served on ssh and nebula ctl too

Info API
- Off until `local_api.info.enabled` with `socket` and optional quoted
  octal `socket_mode` (default "0600"); nothing derived
- Serves commands declaring Command.Identity plus help; others 403 naming
  their surfaces. TestInfoAPISurface pins the set to identity
- GET /v1 is the schema; GET /v1/<cmd>?<flag>=<value> runs one; body is
  the -json rendering via commands.Render; commands.PresentationFormat
  reads json/pretty from the query the way a line is read
- 400 commands.ParamError, 403 commands.WithheldError (what Resolve now
  returns for a withheld name, so help and dispatch type-check it too),
  404 NotFound or unknown, 500 Run error, refusal with no reason, or
  recovered panic with no detail; no redirects; every miss is JSON
- listen takes a mode: bind under umask 0077 then chmod; "0660" plus a
  searchable dir opens it to a group
- The chmod, at bind and on a mode-only reload, goes through chmodSocket,
  which never follows a link: a user who can write the socket's directory
  could otherwise swap the node for a link and have a root daemon put
  0660 or 0666 on any file. Only a socket we own is chmoded. On linux the
  node is held open O_PATH|O_NOFOLLOW, checked by fstat, and chmoded
  through /proc/self/fd, so the check and the chmod land on one inode;
  fchmodat2 is the fallback without /proc. On darwin and the BSDs it is
  an Lstat check and fchmodat(AT_SYMLINK_NOFOLLOW). clearStale Lstats
  too, so a dangling link is refused rather than followed by bind on
  darwin
- One endpoint type owns bind under mutex, bind-before-release move,
  context watch and stop/unlink; each server supplies what it serves
- Reloadable through the same reconcile shape as the control socket:
  mode applied in place, unparsable section keeps what runs, control
  socket path refused by name
- Wire returns Endpoints{Control, Info} with one Stop
- Windows unsupported for now (a pipe has no mode); warned once

Tests: identity lookups and shapes; API filter, status codes, panic
recovery, socket mode, rebind, cancellation; config parsing incl. bare
0660; Wire transitions; e2e of two nebulas with the info API resolving
the peer by address and port, through shared test.HTTPDo/HTTPGet helpers.
Goldens gain identity.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-21 12:26:09 -04:00

224 lines
8.7 KiB
Go

package nebula
import (
"bytes"
"encoding/json"
"net/netip"
"testing"
"time"
"github.com/slackhq/nebula/cert"
"github.com/slackhq/nebula/cert_test"
"github.com/slackhq/nebula/commands"
"github.com/slackhq/nebula/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// identityFixture is an Interface with our own certificate loaded and one peer behind an
// established tunnel, which is all identity reads.
func identityFixture(t *testing.T) (*Interface, cert.Certificate) {
t.Helper()
l := test.NewLogger()
me := netip.MustParseAddr("10.0.0.1")
peer := netip.MustParsePrefix("10.0.0.2/24")
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
peerCert, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, "peer", time.Time{}, time.Time{},
[]netip.Prefix{peer}, []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")}, []string{"web", "db"})
hm := newHostMap(l)
hm.Hosts[peer.Addr()] = &HostInfo{
vpnAddrs: []netip.Addr{peer.Addr()},
ConnectionState: &ConnectionState{peerCert: &cert.CachedCertificate{Certificate: peerCert}},
}
// A tunnel still handshaking has no certificate yet, and must not be reported.
hm.Hosts[netip.MustParseAddr("10.0.0.3")] = &HostInfo{
vpnAddrs: []netip.Addr{netip.MustParseAddr("10.0.0.3")},
ConnectionState: &ConnectionState{},
}
return &Interface{pki: newTestPKI(t, "me", []netip.Addr{me}), hostMap: hm}, peerCert
}
// identity runs the command with -address given as address.
func identity(t *testing.T, f *Interface, address string, operands ...string) commands.Result {
t.Helper()
fs := &identityFlags{}
require.NoError(t, fs.setAddress(address))
r, err := runIdentity(f, fs, operands)
require.NoError(t, err)
return r
}
// whoami runs the whoami command.
func whoami(t *testing.T, f *Interface) commands.Result {
t.Helper()
r, err := runWhoami(f)
require.NoError(t, err)
return r
}
// TestWhoamiWithoutACertificateRefuses: no certificate is a refusal with the reason, as
// print-cert answers it, not an error a surface collapses to "command failed".
func TestWhoamiWithoutACertificateRefuses(t *testing.T) {
r := whoami(t, &Interface{pki: &PKI{}})
assert.False(t, r.OK)
assert.Equal(t, "No certificate is loaded", r.Reason)
}
func TestWhoamiReportsThisNode(t *testing.T) {
f, _ := identityFixture(t)
r := whoami(t, f)
require.True(t, r.OK)
id, ok := r.Value.(hostIdentity)
require.True(t, ok, "got %T", r.Value)
assert.Equal(t, "me", id.Name)
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.0.0.1")}, id.VpnAddrs)
assert.Equal(t, 2, id.CertVersion)
assert.NotEmpty(t, id.Fingerprint)
assert.NotEmpty(t, id.Issuer)
// Our own address answers the same way: the hostmap never holds ourselves.
self := identity(t, f, "10.0.0.1")
assert.Equal(t, id, self.Value)
}
func TestIdentityReportsAPeerFromItsCertificate(t *testing.T) {
f, peerCert := identityFixture(t)
for _, addr := range []string{
"10.0.0.2",
"10.0.0.2:54321", // a connection's RemoteAddr, as a server has it
"::ffff:10.0.0.2", // the same peer seen through a dual stack listener
"[::ffff:10.0.0.2]:8", // and with a port
} {
r := identity(t, f, addr)
require.True(t, r.OK, "%s: %s", addr, r.Reason)
id := r.Value.(hostIdentity)
assert.Equal(t, "peer", id.Name, addr)
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.0.0.2")}, id.VpnAddrs, addr)
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")}, id.Networks, addr)
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")}, id.UnsafeNetworks, addr)
assert.Equal(t, []string{"web", "db"}, id.Groups, addr)
assert.Equal(t, peerCert.Issuer(), id.Issuer, addr)
assert.Equal(t, peerCert.NotAfter(), id.NotAfter, addr)
}
}
// TestIdentityRefusals: the ways a lookup fails are told apart, because a program
// deciding whether to let a connection in treats them differently.
func TestIdentityRefusals(t *testing.T) {
f, _ := identityFixture(t)
unknown := identity(t, f, "10.0.0.9")
assert.False(t, unknown.OK)
assert.True(t, unknown.NotFound, "no tunnel is a not found")
assert.Equal(t, "No active tunnel for vpn addr: 10.0.0.9", unknown.Reason)
pending := identity(t, f, "10.0.0.3:1")
assert.True(t, pending.NotFound, "a tunnel without a certificate yet is not active")
assert.Equal(t, "No active tunnel for vpn addr: 10.0.0.3", pending.Reason, "the reason names the address, not the port")
garbage := identity(t, f, "not-an-address")
assert.False(t, garbage.OK)
assert.False(t, garbage.NotFound, "an address that does not parse is the caller's mistake")
assert.Equal(t, "The provided vpn addr could not be parsed: not-an-address", garbage.Reason)
// An address that was given and is empty is a caller that failed to fill it in, and
// must not be answered with our own identity.
empty := identity(t, f, "")
assert.False(t, empty.OK)
assert.False(t, empty.NotFound)
assert.Equal(t, "The provided vpn addr could not be parsed: ", empty.Reason)
operand := identity(t, f, "", "10.0.0.2")
assert.False(t, operand.OK)
assert.Contains(t, operand.Reason, "identity -address 10.0.0.2")
// No address at all is a caller that forgot the parameter, and is pointed at whoami
// rather than handed this node's identity.
omitted, err := runIdentity(f, &identityFlags{}, nil)
require.NoError(t, err)
assert.False(t, omitted.OK)
assert.False(t, omitted.NotFound)
assert.Contains(t, omitted.Reason, "-address")
assert.Contains(t, omitted.Reason, "whoami")
}
// TestOwnCertificatePrefersV2: with both versions loaded, the v2 certificate is the one
// myVpnAddrsTable is built from and may carry addresses the v1 does not, so it is the one
// whoami and an identity lookup of our own address report, whichever version handshakes
// are initiated with.
func TestOwnCertificatePrefersV2(t *testing.T) {
v4 := netip.MustParsePrefix("10.0.0.1/24")
ca1, _, caKey1, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
v1, _, _, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca1, caKey1, "me", time.Time{}, time.Time{}, []netip.Prefix{v4}, nil, nil)
ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
v2, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca2, caKey2, "me", time.Time{}, time.Time{},
[]netip.Prefix{v4, netip.MustParsePrefix("fd00::1/64")}, nil, nil)
assert.Nil(t, ownCertificate(nil))
assert.Equal(t, v1, ownCertificate(&CertState{v1Cert: v1, initiatingVersion: cert.Version1}))
assert.Equal(t, v2, ownCertificate(&CertState{v2Cert: v2, initiatingVersion: cert.Version2}))
assert.Equal(t, v2, ownCertificate(&CertState{v1Cert: v1, v2Cert: v2, initiatingVersion: cert.Version1}),
"the initiating version is v1, the reported certificate is still the v2")
}
// TestIdentityShapes pins the JSON a program reads and the text a person reads. The
// field names are an API served to programs not built against nebula.
func TestIdentityShapes(t *testing.T) {
f, _ := identityFixture(t)
r := identity(t, f, "10.0.0.2")
b, err := json.Marshal(r.Payload())
require.NoError(t, err)
var got map[string]any
require.NoError(t, json.Unmarshal(b, &got))
var keys []string
for k := range got {
keys = append(keys, k)
}
assert.ElementsMatch(t, []string{
"name", "vpnAddrs", "networks", "unsafeNetworks", "groups",
"fingerprint", "issuer", "notBefore", "notAfter", "certVersion",
}, keys)
// Empty lists marshal as [], never null.
self := whoami(t, f)
b, err = json.Marshal(self.Payload())
require.NoError(t, err)
assert.Contains(t, string(b), `"groups":[]`)
assert.Contains(t, string(b), `"unsafeNetworks":[]`)
out := &bytes.Buffer{}
require.NoError(t, commands.Render(&commands.Command{Name: commands.Identity, Text: commands.Typed(textIdentity)}, r, commands.FormatText, out))
assert.Contains(t, out.String(), "name: peer\n")
assert.Contains(t, out.String(), "vpn addrs: 10.0.0.2\n")
assert.Contains(t, out.String(), "groups: web, db\n")
assert.Contains(t, out.String(), "cert version: 2\n")
}
func TestParseVpnAddrWithOptionalPort(t *testing.T) {
for in, want := range map[string]string{
"10.0.0.2": "10.0.0.2",
"10.0.0.2:443": "10.0.0.2",
"fd00::1": "fd00::1",
"[fd00::1]:443": "fd00::1",
"::ffff:10.0.0.2": "10.0.0.2",
"[::ffff:10.0.0.2]:1": "10.0.0.2",
"fe80::1%eth0": "fe80::1",
} {
got, err := parseVpnAddrWithOptionalPort(in)
require.NoError(t, err, in)
assert.Equal(t, netip.MustParseAddr(want), got, in)
}
for _, in := range []string{"", "10.0.0.2:", ":443", "10.0.0", "example.com:80"} {
_, err := parseVpnAddrWithOptionalPort(in)
assert.Error(t, err, in)
}
}