1
0

commands_output_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // run dispatches as the ssh exec path does and returns the output and the error, which
  64. // over ssh is exit status 1.
  65. run := func(args ...string) (string, error) {
  66. buf := &bytes.Buffer{}
  67. err := reg.DispatchArgs(args, commands.NewStringWriter(buf))
  68. return buf.String(), err
  69. }
  70. // Seed a pending tunnel so the listing and per-address commands have something to show.
  71. seed, err := run("create-tunnel", "10.222.0.9")
  72. require.NoError(t, err)
  73. require.Equal(t, "Created\n", seed, "seeding a tunnel should succeed before anything else is recorded")
  74. cases := []outputCase{
  75. {"version", nil},
  76. {"device-info", nil},
  77. {"list-hostmap", nil},
  78. {"list-hostmap", []string{"-by-index"}},
  79. {"list-pending-hostmap", nil},
  80. {"list-pending-hostmap", []string{"-by-index"}},
  81. {"list-lighthouse-addrmap", nil},
  82. {"print-cert", nil},
  83. {"print-cert", []string{"-raw"}},
  84. {"print-cert", []string{"10.222.0.9"}},
  85. {"print-cert", []string{"10.222.0.250"}},
  86. {"print-cert", []string{"not-an-address"}},
  87. {"print-tunnel", nil},
  88. {"print-tunnel", []string{"10.222.0.9"}},
  89. {"print-tunnel", []string{"10.222.0.250"}},
  90. {"print-relays", nil},
  91. {"query-lighthouse", nil},
  92. {"query-lighthouse", []string{"10.222.0.9"}},
  93. {"query-lighthouse", []string{"not-an-address"}},
  94. // The test logger cannot change level or format, so these cases record the
  95. // refusal each command prints on it, not a successful change.
  96. {"log-level", nil},
  97. {"log-level", []string{"debug"}},
  98. {"log-level", []string{"bogus"}},
  99. {"log-level", []string{"info"}},
  100. {"log-format", nil},
  101. {"log-format", []string{"json"}},
  102. {"log-format", []string{"text"}},
  103. {"change-remote", nil},
  104. {"change-remote", []string{"10.222.0.9"}},
  105. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.9"}},
  106. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.250"}},
  107. {"create-tunnel", nil},
  108. {"create-tunnel", []string{"10.222.0.9"}},
  109. // A fresh address, so this exercises the handshake start with a remote attached
  110. // rather than the "already handshaking" path above.
  111. {"create-tunnel", []string{"-address", "10.94.0.10:4242", "10.222.0.10"}},
  112. {"create-tunnel", []string{"not-an-address"}},
  113. {"close-tunnel", nil},
  114. {"close-tunnel", []string{"10.222.0.250"}},
  115. {"close-tunnel", []string{"not-an-address"}},
  116. {"help", nil},
  117. {"help", []string{"print-cert"}},
  118. {"help", []string{"logout"}},
  119. {"logout", []string{"-h"}},
  120. {"not-a-command", []string{"10.222.0.9"}},
  121. // reload is last so that whatever a reload does to the daemon cannot leak into an
  122. // earlier case. In this harness it fails: the config was loaded from a string and
  123. // has no path to re-read, which is what the recorded result says.
  124. {"reload", nil},
  125. }
  126. got := &strings.Builder{}
  127. for _, c := range cases {
  128. for _, fmtFlags := range [][]string{nil, {"-json"}, {"-pretty"}} {
  129. args := append([]string{c.name}, fmtFlags...)
  130. args = append(args, c.args...)
  131. fmt.Fprintf(got, "$ %s\n", strings.Join(args, " "))
  132. out, err := run(args...)
  133. out = normalise(out)
  134. got.WriteString(out)
  135. // Output that does not end in a newline is recorded as such, the way diff marks
  136. // it, so the golden sees that byte too and the next prompt still starts a line.
  137. if out != "" && !strings.HasSuffix(out, "\n") {
  138. got.WriteString("\\ No newline at end of output\n")
  139. }
  140. // A failed command is recorded as the exit status an ssh caller sees, so the
  141. // golden pins which outcomes count as failures.
  142. if err != nil {
  143. got.WriteString("exit status 1\n")
  144. }
  145. got.WriteString("\n")
  146. }
  147. }
  148. test.Golden(t, filepath.Join("testdata", "commands_output.golden"), got.String())
  149. }