authorization.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. run_authorization_tests() {
  19. set -o nounset
  20. set -o errexit
  21. kube::log::status "Testing authorization"
  22. # check remote authorization endpoint, kubectl doesn't actually display the returned object so this isn't super useful
  23. # but it proves that works
  24. kubectl create -f test/fixtures/pkg/kubectl/cmd/create/sar-v1.json --validate=false
  25. kubectl create -f test/fixtures/pkg/kubectl/cmd/create/sar-v1beta1.json --validate=false
  26. SAR_RESULT_FILE="${KUBE_TEMP}/sar-result.json"
  27. curl -k -H "Content-Type:" http://localhost:8080/apis/authorization.k8s.io/v1beta1/subjectaccessreviews -XPOST -d @test/fixtures/pkg/kubectl/cmd/create/sar-v1beta1.json > "${SAR_RESULT_FILE}"
  28. if grep -q '"allowed": true' "${SAR_RESULT_FILE}"; then
  29. kube::log::status "\"authorization.k8s.io/subjectaccessreviews\" returns as expected: $(cat "${SAR_RESULT_FILE}")"
  30. else
  31. kube::log::status "\"authorization.k8s.io/subjectaccessreviews\" does not return as expected: $(cat "${SAR_RESULT_FILE}")"
  32. exit 1
  33. fi
  34. rm "${SAR_RESULT_FILE}"
  35. SAR_RESULT_FILE="${KUBE_TEMP}/sar-result.json"
  36. curl -k -H "Content-Type:" http://localhost:8080/apis/authorization.k8s.io/v1/subjectaccessreviews -XPOST -d @test/fixtures/pkg/kubectl/cmd/create/sar-v1.json > "${SAR_RESULT_FILE}"
  37. if grep -q '"allowed": true' "${SAR_RESULT_FILE}"; then
  38. kube::log::status "\"authorization.k8s.io/subjectaccessreviews\" returns as expected: $(cat "${SAR_RESULT_FILE}")"
  39. else
  40. kube::log::status "\"authorization.k8s.io/subjectaccessreviews\" does not return as expected: $(cat "${SAR_RESULT_FILE}")"
  41. exit 1
  42. fi
  43. rm "${SAR_RESULT_FILE}"
  44. set +o nounset
  45. set +o errexit
  46. }
  47. run_impersonation_tests() {
  48. set -o nounset
  49. set -o errexit
  50. kube::log::status "Testing impersonation"
  51. output_message=$(! kubectl get pods "${kube_flags_with_token[@]:?}" --as-group=foo 2>&1)
  52. kube::test::if_has_string "${output_message}" 'without impersonating a user'
  53. if kube::test::if_supports_resource "${csr:?}" ; then
  54. # --as
  55. kubectl create -f hack/testdata/csr.yml "${kube_flags_with_token[@]:?}" --as=user1
  56. kube::test::get_object_assert 'csr/foo' '{{.spec.username}}' 'user1'
  57. kube::test::get_object_assert 'csr/foo' '{{range .spec.groups}}{{.}}{{end}}' 'system:authenticated'
  58. kubectl delete -f hack/testdata/csr.yml "${kube_flags_with_token[@]:?}"
  59. # --as-group
  60. kubectl create -f hack/testdata/csr.yml "${kube_flags_with_token[@]:?}" --as=user1 --as-group=group2 --as-group=group1 --as-group=,,,chameleon
  61. kube::test::get_object_assert 'csr/foo' '{{len .spec.groups}}' '3'
  62. kube::test::get_object_assert 'csr/foo' '{{range .spec.groups}}{{.}} {{end}}' 'group2 group1 ,,,chameleon '
  63. kubectl delete -f hack/testdata/csr.yml "${kube_flags_with_token[@]:?}"
  64. fi
  65. set +o nounset
  66. set +o errexit
  67. }