auth_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package server
  14. import (
  15. "net/http"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. "k8s.io/apiserver/pkg/authentication/user"
  20. "k8s.io/apiserver/pkg/authorization/authorizer"
  21. )
  22. func TestIsSubPath(t *testing.T) {
  23. testcases := map[string]struct {
  24. subpath string
  25. path string
  26. expected bool
  27. }{
  28. "empty": {subpath: "", path: "", expected: true},
  29. "match 1": {subpath: "foo", path: "foo", expected: true},
  30. "match 2": {subpath: "/foo", path: "/foo", expected: true},
  31. "match 3": {subpath: "/foo/", path: "/foo/", expected: true},
  32. "match 4": {subpath: "/foo/bar", path: "/foo/bar", expected: true},
  33. "subpath of root 1": {subpath: "/foo", path: "/", expected: true},
  34. "subpath of root 2": {subpath: "/foo/", path: "/", expected: true},
  35. "subpath of root 3": {subpath: "/foo/bar", path: "/", expected: true},
  36. "subpath of path 1": {subpath: "/foo", path: "/foo", expected: true},
  37. "subpath of path 2": {subpath: "/foo/", path: "/foo", expected: true},
  38. "subpath of path 3": {subpath: "/foo/bar", path: "/foo", expected: true},
  39. "mismatch 1": {subpath: "/foo", path: "/bar", expected: false},
  40. "mismatch 2": {subpath: "/foo", path: "/foobar", expected: false},
  41. "mismatch 3": {subpath: "/foobar", path: "/foo", expected: false},
  42. }
  43. for k, tc := range testcases {
  44. result := isSubpath(tc.subpath, tc.path)
  45. if result != tc.expected {
  46. t.Errorf("%s: expected %v, got %v", k, tc.expected, result)
  47. }
  48. }
  49. }
  50. func TestGetRequestAttributes(t *testing.T) {
  51. for _, test := range AuthzTestCases() {
  52. t.Run(test.Method+":"+test.Path, func(t *testing.T) {
  53. getter := NewNodeAuthorizerAttributesGetter(authzTestNodeName)
  54. req, err := http.NewRequest(test.Method, "https://localhost:1234"+test.Path, nil)
  55. require.NoError(t, err)
  56. attrs := getter.GetRequestAttributes(AuthzTestUser(), req)
  57. test.AssertAttributes(t, attrs)
  58. })
  59. }
  60. }
  61. const (
  62. authzTestNodeName = "test"
  63. authzTestUserName = "phibby"
  64. )
  65. type AuthzTestCase struct {
  66. Method, Path string
  67. ExpectedVerb, ExpectedSubresource string
  68. }
  69. func (a *AuthzTestCase) AssertAttributes(t *testing.T, attrs authorizer.Attributes) {
  70. expectedAttributes := authorizer.AttributesRecord{
  71. User: AuthzTestUser(),
  72. APIGroup: "",
  73. APIVersion: "v1",
  74. Verb: a.ExpectedVerb,
  75. Resource: "nodes",
  76. Name: authzTestNodeName,
  77. Subresource: a.ExpectedSubresource,
  78. ResourceRequest: true,
  79. Path: a.Path,
  80. }
  81. assert.Equal(t, expectedAttributes, attrs)
  82. }
  83. func AuthzTestUser() user.Info {
  84. return &user.DefaultInfo{Name: authzTestUserName}
  85. }
  86. func AuthzTestCases() []AuthzTestCase {
  87. // Path -> ExpectedSubresource
  88. testPaths := map[string]string{
  89. "/attach/{podNamespace}/{podID}/{containerName}": "proxy",
  90. "/attach/{podNamespace}/{podID}/{uid}/{containerName}": "proxy",
  91. "/configz": "proxy",
  92. "/containerLogs/{podNamespace}/{podID}/{containerName}": "proxy",
  93. "/cri/": "proxy",
  94. "/cri/foo": "proxy",
  95. "/debug/flags/v": "proxy",
  96. "/debug/pprof/{subpath:*}": "proxy",
  97. "/exec/{podNamespace}/{podID}/{containerName}": "proxy",
  98. "/exec/{podNamespace}/{podID}/{uid}/{containerName}": "proxy",
  99. "/healthz": "proxy",
  100. "/healthz/log": "proxy",
  101. "/healthz/ping": "proxy",
  102. "/healthz/syncloop": "proxy",
  103. "/logs/": "log",
  104. "/logs/{logpath:*}": "log",
  105. "/metrics": "metrics",
  106. "/metrics/cadvisor": "metrics",
  107. "/metrics/probes": "metrics",
  108. "/metrics/resource/v1alpha1": "metrics",
  109. "/metrics/resource": "metrics",
  110. "/pods/": "proxy",
  111. "/portForward/{podNamespace}/{podID}": "proxy",
  112. "/portForward/{podNamespace}/{podID}/{uid}": "proxy",
  113. "/run/{podNamespace}/{podID}/{containerName}": "proxy",
  114. "/run/{podNamespace}/{podID}/{uid}/{containerName}": "proxy",
  115. "/runningpods/": "proxy",
  116. "/spec/": "spec",
  117. "/stats/": "stats",
  118. "/stats/container": "stats",
  119. "/stats/summary": "stats",
  120. "/stats/{namespace}/{podName}/{uid}/{containerName}": "stats",
  121. "/stats/{podName}/{containerName}": "stats",
  122. }
  123. testCases := []AuthzTestCase{}
  124. for path, subresource := range testPaths {
  125. testCases = append(testCases,
  126. AuthzTestCase{"POST", path, "create", subresource},
  127. AuthzTestCase{"GET", path, "get", subresource},
  128. AuthzTestCase{"PUT", path, "update", subresource},
  129. AuthzTestCase{"PATCH", path, "patch", subresource},
  130. AuthzTestCase{"DELETE", path, "delete", subresource})
  131. }
  132. return testCases
  133. }