1
0

commands_output_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //go:build e2e_testing
  2. package e2e
  3. import (
  4. "bytes"
  5. "fmt"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/slackhq/nebula/cert_test"
  13. "github.com/slackhq/nebula/commands"
  14. "github.com/slackhq/nebula/sshd"
  15. "github.com/slackhq/nebula/test"
  16. "github.com/stretchr/testify/require"
  17. )
  18. // volatile lists everything in command output that legitimately differs between runs.
  19. // Each run mints fresh certificates and indexes, so these are replaced with a marker
  20. // before comparison. Keep the list short: anything added here is something the golden
  21. // stops guarding.
  22. var volatile = []struct {
  23. re *regexp.Regexp
  24. with string
  25. }{
  26. {regexp.MustCompile(`[0-9a-f]{40,}`), "<hex>"}, // fingerprints, keys, signatures
  27. {regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})`), "<time>"}, // RFC 3339
  28. {regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? [+-]\d{4} \w+`), "<time>"}, // time.Time.String()
  29. {regexp.MustCompile(`(?s)(-----BEGIN [A-Z ]+-----\n).*?(\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // print-cert -raw
  30. {regexp.MustCompile(`(-----BEGIN [A-Z ]+-----\\n).*?(\\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // the same, inside a JSON string
  31. {regexp.MustCompile(`"(localIndex|remoteIndex|messageCounter)":\d+`), `"$1":<n>`},
  32. {regexp.MustCompile(`(?m)^(\s*"?(?:LocalIndex|RemoteIndex|localIndex|remoteIndex|messageCounter)"?:?\s*)\d+`), "${1}<n>"}, // and the -pretty form
  33. }
  34. func normalise(s string) string {
  35. for _, v := range volatile {
  36. s = v.re.ReplaceAllString(s, v.with)
  37. }
  38. return s
  39. }
  40. // outputCase is one command line to run through the ssh dispatch path.
  41. type outputCase struct {
  42. name string
  43. args []string
  44. }
  45. // TestCommandOutput pins the bytes every command produces, in every format, through the
  46. // ssh dispatch path. It is the safety net for moving rendering out of the commands: a
  47. // change here is a change someone at an ssh prompt would see. Regenerate with:
  48. //
  49. // go test -tags e2e_testing ./e2e -run TestCommandOutput -update
  50. func TestCommandOutput(t *testing.T) {
  51. ca, _, caKey, _ := cert_test.NewTestCaCert(
  52. cert.Version1, cert.Curve_CURVE25519,
  53. time.Now(), time.Now().Add(10*time.Minute),
  54. nil, nil, []string{},
  55. )
  56. control, _, _, _ := newSimpleServer(cert.Version1, ca, caKey, "output-golden", "10.222.0.1/24", nil)
  57. control.Start()
  58. t.Cleanup(func() { control.Stop() })
  59. // As an ssh session does: a clone of the daemon's registry with logout added, which
  60. // help on the clone lists and describes. There is no session to end here.
  61. reg := control.Commands().Clone()
  62. reg.Register(sshd.LogoutCommand(func() {}))
  63. // Dispatch with ssh's default-format policy, so the no-flag column records what an
  64. // operator at an ssh prompt sees.
  65. reg.DefaultFormat = sshd.DefaultFormat
  66. // run dispatches as the ssh exec path does and returns the output and the error, which
  67. // over ssh is exit status 1.
  68. run := func(args ...string) (string, error) {
  69. buf := &bytes.Buffer{}
  70. err := reg.DispatchArgs(args, commands.MergedOutput(buf))
  71. return buf.String(), err
  72. }
  73. // Seed a pending tunnel so the listing and per-address commands have something to show.
  74. seed, err := run("create-tunnel", "10.222.0.9")
  75. require.NoError(t, err)
  76. require.Equal(t, "Created\n", seed, "seeding a tunnel should succeed before anything else is recorded")
  77. cases := []outputCase{
  78. {"version", nil},
  79. {"device-info", nil},
  80. {"list-hostmap", nil},
  81. {"list-hostmap", []string{"-by-index"}},
  82. {"list-pending-hostmap", nil},
  83. {"list-pending-hostmap", []string{"-by-index"}},
  84. {"list-lighthouse-addrmap", nil},
  85. {"print-cert", nil},
  86. {"print-cert", []string{"-raw"}},
  87. {"print-cert", []string{"10.222.0.9"}},
  88. // A flag after the operand is still a flag, on both surfaces.
  89. {"print-cert", []string{"10.222.0.9", "-json"}},
  90. {"print-cert", []string{"10.222.0.250"}},
  91. {"print-cert", []string{"not-an-address"}},
  92. {"print-tunnel", nil},
  93. {"print-tunnel", []string{"10.222.0.9"}},
  94. {"print-tunnel", []string{"10.222.0.250"}},
  95. {"print-relays", nil},
  96. {"query-lighthouse", nil},
  97. {"query-lighthouse", []string{"10.222.0.9"}},
  98. {"query-lighthouse", []string{"not-an-address"}},
  99. // The test logger cannot change level or format, so these cases record the
  100. // refusal each command prints on it, not a successful change.
  101. {"log-level", nil},
  102. {"log-level", []string{"debug"}},
  103. {"log-level", []string{"bogus"}},
  104. {"log-level", []string{"info"}},
  105. {"log-format", nil},
  106. {"log-format", []string{"json"}},
  107. {"log-format", []string{"text"}},
  108. {"change-remote", nil},
  109. {"change-remote", []string{"10.222.0.9"}},
  110. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.9"}},
  111. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.250"}},
  112. {"create-tunnel", nil},
  113. {"create-tunnel", []string{"10.222.0.9"}},
  114. // A fresh address, so this exercises the handshake start with a remote attached
  115. // rather than the "already handshaking" path above.
  116. {"create-tunnel", []string{"-address", "10.94.0.10:4242", "10.222.0.10"}},
  117. {"create-tunnel", []string{"not-an-address"}},
  118. {"close-tunnel", nil},
  119. {"close-tunnel", []string{"10.222.0.250"}},
  120. {"close-tunnel", []string{"not-an-address"}},
  121. {"help", nil},
  122. {"help", []string{"print-cert"}},
  123. {"help", []string{"logout"}},
  124. {"logout", []string{"-h"}},
  125. {"not-a-command", []string{"10.222.0.9"}},
  126. // reload is last so that whatever a reload does to the daemon cannot leak into an
  127. // earlier case. In this harness it fails: the config was loaded from a string and
  128. // has no path to re-read, which is what the recorded result says.
  129. {"reload", nil},
  130. }
  131. got := &strings.Builder{}
  132. for _, c := range cases {
  133. for _, fmtFlags := range [][]string{nil, {"-json"}, {"-pretty"}} {
  134. args := append([]string{c.name}, fmtFlags...)
  135. args = append(args, c.args...)
  136. fmt.Fprintf(got, "$ %s\n", strings.Join(args, " "))
  137. out, err := run(args...)
  138. out = normalise(out)
  139. got.WriteString(out)
  140. // Output that does not end in a newline is recorded as such, the way diff marks
  141. // it, so the golden sees that byte too and the next prompt still starts a line.
  142. if out != "" && !strings.HasSuffix(out, "\n") {
  143. got.WriteString("\\ No newline at end of output\n")
  144. }
  145. // A failed command is recorded as the exit status an ssh caller sees, so the
  146. // golden pins which outcomes count as failures.
  147. if err != nil {
  148. got.WriteString("exit status 1\n")
  149. }
  150. got.WriteString("\n")
  151. }
  152. }
  153. test.Golden(t, filepath.Join("testdata", "commands_output.golden"), got.String())
  154. }