1
0

commands_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package nebula
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/netip"
  7. "testing"
  8. "time"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/cert_test"
  11. "github.com/slackhq/nebula/commands"
  12. "github.com/slackhq/nebula/logging"
  13. "github.com/slackhq/nebula/test"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. // TestLogFormatRefusalIsShown: a format the logger does not know is a refusal the user
  18. // sees, rather than a bare error, which over ssh was silence.
  19. func TestLogFormatRefusalIsShown(t *testing.T) {
  20. l := logging.NewLogger(io.Discard)
  21. res, err := runLogFormat(l, []string{"bogus"})
  22. require.NoError(t, err)
  23. assert.False(t, res.OK)
  24. assert.Equal(t, "Unable to set log format: unknown log format `bogus`. possible formats: [text json]", res.Reason)
  25. res, err = runLogFormat(l, []string{"JSON"})
  26. require.NoError(t, err)
  27. out := &bytes.Buffer{}
  28. require.NoError(t, commands.Typed(textLogFormat)(res.Value, out))
  29. assert.Equal(t, "Log format is: json\n", out.String())
  30. }
  31. // TestPrintRelaysReportsErrorsAsText: the Error field was an error value, which encodes
  32. // as {} and lost the message. It is the message now, and absent when there is none.
  33. func TestPrintRelaysReportsErrorsAsText(t *testing.T) {
  34. l := test.NewLogger()
  35. hm := newHostMap(l)
  36. relay := netip.MustParseAddr("10.0.0.1")
  37. peer := netip.MustParseAddr("10.0.0.2")
  38. // A relay host that is in Relays but not in Hosts, so its hostinfo cannot be found.
  39. hm.Relays[1] = &HostInfo{vpnAddrs: []netip.Addr{relay}}
  40. ifce := &Interface{hostMap: hm}
  41. marshal := func() string {
  42. b, err := json.Marshal(runPrintRelays(ifce))
  43. require.NoError(t, err)
  44. return string(b)
  45. }
  46. assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Error":"could not find hostinfo","Type":"","State":"","PeerAddr":"","LocalIndex":0,"RemoteIndex":0,"RelayedThrough":null}]}]}`, marshal())
  47. // The same relay host, found, relaying for a peer whose index matches: no Error key.
  48. hi := hm.Relays[1]
  49. r := &Relay{Type: ForwardingType, State: Established, PeerAddr: peer, LocalIndex: 1, RemoteIndex: 2}
  50. hi.relayState = RelayState{
  51. relayForByAddr: map[netip.Addr]*Relay{peer: r},
  52. relayForByIdx: map[uint32]*Relay{r.LocalIndex: r},
  53. }
  54. hm.Hosts[relay] = hi
  55. assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Type":"forwarding","State":"established","PeerAddr":"10.0.0.2","LocalIndex":1,"RemoteIndex":2,"RelayedThrough":null}]}]}`, marshal())
  56. // And an index mismatch names it.
  57. hm.Relays[7] = hi
  58. delete(hm.Relays, 1)
  59. assert.Contains(t, marshal(), `"Error":"hostmap LocalIndex '7' does not match RelayState LocalIndex"`)
  60. }
  61. // TestPrintCertOfATunnelWithoutACertificate: a tunnel still handshaking, or one being
  62. // torn down, has no certificate, and print-cert refuses rather than panicking.
  63. func TestPrintCertOfATunnelWithoutACertificate(t *testing.T) {
  64. f, _ := identityFixture(t)
  65. r, err := runPrintCert(f, &printCertFlags{}, []string{"10.0.0.3"})
  66. require.NoError(t, err)
  67. assert.False(t, r.OK)
  68. assert.Equal(t, "Could not find tunnel for vpn addr: 10.0.0.3", r.Reason)
  69. r, err = runPrintCert(f, &printCertFlags{}, []string{"10.0.0.2"})
  70. require.NoError(t, err)
  71. assert.True(t, r.OK, "a tunnel with a certificate is printed")
  72. }
  73. // TestPrintCertByAddressFlag: -address is the info API's spelling of the operand, takes a
  74. // port and a 4in6 form the way identity does, answers our own address with our own
  75. // certificate, and is refused together with an operand.
  76. func TestPrintCertByAddressFlag(t *testing.T) {
  77. f, peerCert := identityFixture(t)
  78. for _, addr := range []string{"10.0.0.2", "10.0.0.2:4242", "[::ffff:10.0.0.2]:4242"} {
  79. fl := &printCertFlags{}
  80. require.NoError(t, fl.setAddress(addr))
  81. r, err := runPrintCert(f, fl, nil)
  82. require.NoError(t, err)
  83. require.True(t, r.OK, addr)
  84. assert.Equal(t, peerCert, r.Value, addr)
  85. }
  86. fl := &printCertFlags{}
  87. require.NoError(t, fl.setAddress("10.0.0.1"))
  88. r, err := runPrintCert(f, fl, nil)
  89. require.NoError(t, err)
  90. require.True(t, r.OK)
  91. assert.Equal(t, ownCertificate(f.pki.getCertState()), r.Value, "our own address is our own certificate")
  92. fl = &printCertFlags{}
  93. require.NoError(t, fl.setAddress("10.0.0.3"))
  94. r, err = runPrintCert(f, fl, nil)
  95. require.NoError(t, err)
  96. assert.False(t, r.OK)
  97. assert.True(t, r.NotFound)
  98. fl = &printCertFlags{}
  99. require.NoError(t, fl.setAddress("not-an-address"))
  100. r, err = runPrintCert(f, fl, nil)
  101. require.NoError(t, err)
  102. assert.Equal(t, "The provided vpn addr could not be parsed: not-an-address", r.Reason)
  103. fl = &printCertFlags{}
  104. require.NoError(t, fl.setAddress("10.0.0.2"))
  105. r, err = runPrintCert(f, fl, []string{"10.0.0.2"})
  106. require.NoError(t, err)
  107. assert.Equal(t, "print-cert takes one vpn addr, as -address or as an operand, not both", r.Reason)
  108. }
  109. // TestPrintCertPrefersOwnV2: with no address print-cert reports the certificate whoami
  110. // does, the v2 when both versions are loaded, rather than the one handshakes start with.
  111. func TestPrintCertPrefersOwnV2(t *testing.T) {
  112. v4 := netip.MustParsePrefix("10.0.0.1/24")
  113. ca1, _, caKey1, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
  114. v1, _, _, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca1, caKey1, "me", time.Time{}, time.Time{}, []netip.Prefix{v4}, nil, nil)
  115. ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
  116. v2, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca2, caKey2, "me", time.Time{}, time.Time{}, []netip.Prefix{v4}, nil, nil)
  117. f := &Interface{pki: &PKI{}, hostMap: newHostMap(test.NewLogger())}
  118. f.pki.cs.Store(&CertState{v1Cert: v1, v2Cert: v2, initiatingVersion: cert.Version1})
  119. r, err := runPrintCert(f, &printCertFlags{}, nil)
  120. require.NoError(t, err)
  121. require.True(t, r.OK)
  122. assert.Equal(t, v2, r.Value)
  123. }