debug_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "testing"
  4. "gotest.tools/assert"
  5. is "gotest.tools/assert/cmp"
  6. )
  7. func TestMaskSecretKeys(t *testing.T) {
  8. tests := []struct {
  9. doc string
  10. path string
  11. input map[string]interface{}
  12. expected map[string]interface{}
  13. }{
  14. {
  15. doc: "secret create with API version",
  16. path: "/v1.30/secrets/create",
  17. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  18. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  19. },
  20. {
  21. doc: "secret create with API version and trailing slashes",
  22. path: "/v1.30/secrets/create//",
  23. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  24. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  25. },
  26. {
  27. doc: "secret create with query param",
  28. path: "/secrets/create?key=val",
  29. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  30. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  31. },
  32. {
  33. doc: "other paths with API version",
  34. path: "/v1.30/some/other/path",
  35. input: map[string]interface{}{
  36. "password": "pass",
  37. "secret": "secret",
  38. "jointoken": "jointoken",
  39. "unlockkey": "unlockkey",
  40. "signingcakey": "signingcakey",
  41. "other": map[string]interface{}{
  42. "password": "pass",
  43. "secret": "secret",
  44. "jointoken": "jointoken",
  45. "unlockkey": "unlockkey",
  46. "signingcakey": "signingcakey",
  47. },
  48. },
  49. expected: map[string]interface{}{
  50. "password": "*****",
  51. "secret": "*****",
  52. "jointoken": "*****",
  53. "unlockkey": "*****",
  54. "signingcakey": "*****",
  55. "other": map[string]interface{}{
  56. "password": "*****",
  57. "secret": "*****",
  58. "jointoken": "*****",
  59. "unlockkey": "*****",
  60. "signingcakey": "*****",
  61. },
  62. },
  63. },
  64. {
  65. doc: "other paths with API version case insensitive",
  66. path: "/v1.30/some/other/path",
  67. input: map[string]interface{}{
  68. "PASSWORD": "pass",
  69. "other": map[string]interface{}{
  70. "PASSWORD": "pass",
  71. },
  72. },
  73. expected: map[string]interface{}{
  74. "PASSWORD": "*****",
  75. "other": map[string]interface{}{
  76. "PASSWORD": "*****",
  77. },
  78. },
  79. },
  80. }
  81. for _, testcase := range tests {
  82. t.Run(testcase.doc, func(t *testing.T) {
  83. maskSecretKeys(testcase.input, testcase.path)
  84. assert.Check(t, is.DeepEqual(testcase.expected, testcase.input))
  85. })
  86. }
  87. }