client_auth_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright 2019 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 admissionwebhook
  14. import (
  15. "context"
  16. "crypto/tls"
  17. "crypto/x509"
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "net/http"
  22. "net/http/httptest"
  23. "net/url"
  24. "os"
  25. "sync"
  26. "testing"
  27. "time"
  28. "k8s.io/api/admission/v1beta1"
  29. admissionv1beta1 "k8s.io/api/admissionregistration/v1beta1"
  30. corev1 "k8s.io/api/core/v1"
  31. v1 "k8s.io/api/core/v1"
  32. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  33. "k8s.io/apimachinery/pkg/types"
  34. "k8s.io/apimachinery/pkg/util/wait"
  35. clientset "k8s.io/client-go/kubernetes"
  36. "k8s.io/client-go/rest"
  37. kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
  38. "k8s.io/kubernetes/test/integration/framework"
  39. )
  40. const (
  41. testClientAuthClientUsername = "webhook-client-auth-integration-client"
  42. )
  43. // TestWebhookClientAuthWithAggregatorRouting ensures client auth is used for requests to URL backends
  44. func TestWebhookClientAuthWithAggregatorRouting(t *testing.T) {
  45. testWebhookClientAuth(t, true)
  46. }
  47. // TestWebhookClientAuthWithoutAggregatorRouting ensures client auth is used for requests to URL backends
  48. func TestWebhookClientAuthWithoutAggregatorRouting(t *testing.T) {
  49. testWebhookClientAuth(t, false)
  50. }
  51. func testWebhookClientAuth(t *testing.T, enableAggregatorRouting bool) {
  52. roots := x509.NewCertPool()
  53. if !roots.AppendCertsFromPEM(localhostCert) {
  54. t.Fatal("Failed to append Cert from PEM")
  55. }
  56. cert, err := tls.X509KeyPair(localhostCert, localhostKey)
  57. if err != nil {
  58. t.Fatalf("Failed to build cert with error: %+v", err)
  59. }
  60. recorder := &clientAuthRecorder{}
  61. webhookServer := httptest.NewUnstartedServer(newClientAuthWebhookHandler(t, recorder))
  62. webhookServer.TLS = &tls.Config{
  63. RootCAs: roots,
  64. Certificates: []tls.Certificate{cert},
  65. }
  66. webhookServer.StartTLS()
  67. defer webhookServer.Close()
  68. webhookServerURL, err := url.Parse(webhookServer.URL)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. kubeConfigFile, err := ioutil.TempFile("", "admission-config.yaml")
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. defer os.Remove(kubeConfigFile.Name())
  77. if err := ioutil.WriteFile(kubeConfigFile.Name(), []byte(`
  78. apiVersion: v1
  79. kind: Config
  80. users:
  81. - name: "`+webhookServerURL.Host+`"
  82. user:
  83. token: "localhost-match-with-port"
  84. - name: "`+webhookServerURL.Hostname()+`"
  85. user:
  86. token: "localhost-match-without-port"
  87. - name: "*.localhost"
  88. user:
  89. token: "localhost-prefix"
  90. - name: "*"
  91. user:
  92. token: "fallback"
  93. `), os.FileMode(0755)); err != nil {
  94. t.Fatal(err)
  95. }
  96. admissionConfigFile, err := ioutil.TempFile("", "admission-config.yaml")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer os.Remove(admissionConfigFile.Name())
  101. if err := ioutil.WriteFile(admissionConfigFile.Name(), []byte(`
  102. apiVersion: apiserver.k8s.io/v1alpha1
  103. kind: AdmissionConfiguration
  104. plugins:
  105. - name: ValidatingAdmissionWebhook
  106. configuration:
  107. apiVersion: apiserver.config.k8s.io/v1alpha1
  108. kind: WebhookAdmission
  109. kubeConfigFile: "`+kubeConfigFile.Name()+`"
  110. - name: MutatingAdmissionWebhook
  111. configuration:
  112. apiVersion: apiserver.config.k8s.io/v1alpha1
  113. kind: WebhookAdmission
  114. kubeConfigFile: "`+kubeConfigFile.Name()+`"
  115. `), os.FileMode(0755)); err != nil {
  116. t.Fatal(err)
  117. }
  118. s := kubeapiservertesting.StartTestServerOrDie(t, kubeapiservertesting.NewDefaultTestServerOptions(), []string{
  119. "--disable-admission-plugins=ServiceAccount",
  120. fmt.Sprintf("--enable-aggregator-routing=%v", enableAggregatorRouting),
  121. "--admission-control-config-file=" + admissionConfigFile.Name(),
  122. }, framework.SharedEtcd())
  123. defer s.TearDownFn()
  124. // Configure a client with a distinct user name so that it is easy to distinguish requests
  125. // made by the client from requests made by controllers. We use this to filter out requests
  126. // before recording them to ensure we don't accidentally mistake requests from controllers
  127. // as requests made by the client.
  128. clientConfig := rest.CopyConfig(s.ClientConfig)
  129. clientConfig.Impersonate.UserName = testClientAuthClientUsername
  130. clientConfig.Impersonate.Groups = []string{"system:masters", "system:authenticated"}
  131. client, err := clientset.NewForConfig(clientConfig)
  132. if err != nil {
  133. t.Fatalf("unexpected error: %v", err)
  134. }
  135. _, err = client.CoreV1().Pods("default").Create(context.TODO(), clientAuthMarkerFixture, metav1.CreateOptions{})
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. upCh := recorder.Reset()
  140. ns := "load-balance"
  141. _, err = client.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{})
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. fail := admissionv1beta1.Fail
  146. mutatingCfg, err := client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Create(context.TODO(), &admissionv1beta1.MutatingWebhookConfiguration{
  147. ObjectMeta: metav1.ObjectMeta{Name: "admission.integration.test"},
  148. Webhooks: []admissionv1beta1.MutatingWebhook{{
  149. Name: "admission.integration.test",
  150. ClientConfig: admissionv1beta1.WebhookClientConfig{
  151. URL: &webhookServer.URL,
  152. CABundle: localhostCert,
  153. },
  154. Rules: []admissionv1beta1.RuleWithOperations{{
  155. Operations: []admissionv1beta1.OperationType{admissionv1beta1.OperationAll},
  156. Rule: admissionv1beta1.Rule{APIGroups: []string{""}, APIVersions: []string{"v1"}, Resources: []string{"pods"}},
  157. }},
  158. FailurePolicy: &fail,
  159. AdmissionReviewVersions: []string{"v1beta1"},
  160. }},
  161. }, metav1.CreateOptions{})
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. defer func() {
  166. err := client.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Delete(context.TODO(), mutatingCfg.GetName(), &metav1.DeleteOptions{})
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. }()
  171. // wait until new webhook is called
  172. if err := wait.PollImmediate(time.Millisecond*5, wait.ForeverTestTimeout, func() (bool, error) {
  173. _, err = client.CoreV1().Pods("default").Patch(context.TODO(), clientAuthMarkerFixture.Name, types.JSONPatchType, []byte("[]"), metav1.PatchOptions{})
  174. if t.Failed() {
  175. return true, nil
  176. }
  177. select {
  178. case <-upCh:
  179. return true, nil
  180. default:
  181. t.Logf("Waiting for webhook to become effective, getting marker object: %v", err)
  182. return false, nil
  183. }
  184. }); err != nil {
  185. t.Fatal(err)
  186. }
  187. }
  188. type clientAuthRecorder struct {
  189. mu sync.Mutex
  190. upCh chan struct{}
  191. upOnce sync.Once
  192. }
  193. // Reset zeros out all counts and returns a channel that is closed when the first admission of the
  194. // marker object is received.
  195. func (i *clientAuthRecorder) Reset() chan struct{} {
  196. i.mu.Lock()
  197. defer i.mu.Unlock()
  198. i.upCh = make(chan struct{})
  199. i.upOnce = sync.Once{}
  200. return i.upCh
  201. }
  202. func (i *clientAuthRecorder) MarkerReceived() {
  203. i.mu.Lock()
  204. defer i.mu.Unlock()
  205. i.upOnce.Do(func() {
  206. close(i.upCh)
  207. })
  208. }
  209. func newClientAuthWebhookHandler(t *testing.T, recorder *clientAuthRecorder) http.Handler {
  210. allow := func(w http.ResponseWriter) {
  211. w.Header().Set("Content-Type", "application/json")
  212. json.NewEncoder(w).Encode(&v1beta1.AdmissionReview{
  213. Response: &v1beta1.AdmissionResponse{
  214. Allowed: true,
  215. },
  216. })
  217. }
  218. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  219. defer r.Body.Close()
  220. data, err := ioutil.ReadAll(r.Body)
  221. if err != nil {
  222. http.Error(w, err.Error(), http.StatusBadRequest)
  223. }
  224. review := v1beta1.AdmissionReview{}
  225. if err := json.Unmarshal(data, &review); err != nil {
  226. http.Error(w, err.Error(), http.StatusBadRequest)
  227. }
  228. if review.Request.UserInfo.Username != testClientAuthClientUsername {
  229. // skip requests not originating from this integration test's client
  230. allow(w)
  231. return
  232. }
  233. if authz := r.Header.Get("Authorization"); authz != "Bearer localhost-match-with-port" {
  234. t.Errorf("unexpected authz header: %q", authz)
  235. http.Error(w, "Invalid auth", http.StatusUnauthorized)
  236. return
  237. }
  238. if len(review.Request.Object.Raw) == 0 {
  239. http.Error(w, err.Error(), http.StatusBadRequest)
  240. return
  241. }
  242. pod := &corev1.Pod{}
  243. if err := json.Unmarshal(review.Request.Object.Raw, pod); err != nil {
  244. http.Error(w, err.Error(), http.StatusBadRequest)
  245. return
  246. }
  247. // When resetting between tests, a marker object is patched until this webhook
  248. // observes it, at which point it is considered ready.
  249. if pod.Namespace == clientAuthMarkerFixture.Namespace && pod.Name == clientAuthMarkerFixture.Name {
  250. recorder.MarkerReceived()
  251. allow(w)
  252. return
  253. }
  254. })
  255. }
  256. var clientAuthMarkerFixture = &corev1.Pod{
  257. ObjectMeta: metav1.ObjectMeta{
  258. Namespace: "default",
  259. Name: "marker",
  260. },
  261. Spec: corev1.PodSpec{
  262. Containers: []v1.Container{{
  263. Name: "fake-name",
  264. Image: "fakeimage",
  265. }},
  266. },
  267. }