debug_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: "secret update with API version",
  34. path: "/v1.30/secrets/mysecret/update",
  35. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  36. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  37. },
  38. {
  39. doc: "secret update with API version and trailing slashes",
  40. path: "/v1.30/secrets/mysecret/update//",
  41. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  42. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  43. },
  44. {
  45. doc: "secret update with query parameter",
  46. path: "/secrets/mysecret/update?version=34",
  47. input: map[string]interface{}{"Data": "foo", "Name": "name", "Labels": map[string]interface{}{}},
  48. expected: map[string]interface{}{"Data": "*****", "Name": "name", "Labels": map[string]interface{}{}},
  49. },
  50. {
  51. doc: "other paths with API version",
  52. path: "/v1.30/some/other/path",
  53. input: map[string]interface{}{
  54. "password": "pass",
  55. "secret": "secret",
  56. "jointoken": "jointoken",
  57. "unlockkey": "unlockkey",
  58. "signingcakey": "signingcakey",
  59. "other": map[string]interface{}{
  60. "password": "pass",
  61. "secret": "secret",
  62. "jointoken": "jointoken",
  63. "unlockkey": "unlockkey",
  64. "signingcakey": "signingcakey",
  65. },
  66. },
  67. expected: map[string]interface{}{
  68. "password": "*****",
  69. "secret": "*****",
  70. "jointoken": "*****",
  71. "unlockkey": "*****",
  72. "signingcakey": "*****",
  73. "other": map[string]interface{}{
  74. "password": "*****",
  75. "secret": "*****",
  76. "jointoken": "*****",
  77. "unlockkey": "*****",
  78. "signingcakey": "*****",
  79. },
  80. },
  81. },
  82. {
  83. doc: "other paths with API version case insensitive",
  84. path: "/v1.30/some/other/path",
  85. input: map[string]interface{}{
  86. "PASSWORD": "pass",
  87. "other": map[string]interface{}{
  88. "PASSWORD": "pass",
  89. },
  90. },
  91. expected: map[string]interface{}{
  92. "PASSWORD": "*****",
  93. "other": map[string]interface{}{
  94. "PASSWORD": "*****",
  95. },
  96. },
  97. },
  98. }
  99. for _, testcase := range tests {
  100. t.Run(testcase.doc, func(t *testing.T) {
  101. maskSecretKeys(testcase.input, testcase.path)
  102. assert.Check(t, is.DeepEqual(testcase.expected, testcase.input))
  103. })
  104. }
  105. }