authorization_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2018 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 options
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
  17. )
  18. func TestAuthzValidate(t *testing.T) {
  19. examplePolicyFile := "../../auth/authorizer/abac/example_policy_file.jsonl"
  20. testCases := []struct {
  21. name string
  22. modes []string
  23. policyFile string
  24. webhookConfigFile string
  25. expectErr bool
  26. }{
  27. {
  28. name: "Unknown modes should return errors",
  29. modes: []string{"DoesNotExist"},
  30. expectErr: true,
  31. },
  32. {
  33. name: "At least one authorizationMode is necessary",
  34. modes: []string{},
  35. expectErr: true,
  36. },
  37. {
  38. name: "ModeAlwaysAllow and ModeAlwaysDeny should return without authorizationPolicyFile",
  39. modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny},
  40. expectErr: false,
  41. },
  42. {
  43. name: "ModeABAC requires a policy file",
  44. modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC},
  45. expectErr: true,
  46. },
  47. {
  48. name: "Authorization Policy file cannot be used without ModeABAC",
  49. modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny},
  50. policyFile: examplePolicyFile,
  51. webhookConfigFile: "",
  52. expectErr: true,
  53. },
  54. {
  55. name: "ModeABAC should not error if a valid policy path is provided",
  56. modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC},
  57. policyFile: examplePolicyFile,
  58. webhookConfigFile: "",
  59. expectErr: false,
  60. },
  61. {
  62. name: "ModeWebhook requires a config file",
  63. modes: []string{modes.ModeWebhook},
  64. expectErr: true,
  65. },
  66. {
  67. name: "Cannot provide webhook config file without ModeWebhook",
  68. modes: []string{modes.ModeAlwaysAllow},
  69. webhookConfigFile: "authz_webhook_config.yaml",
  70. expectErr: true,
  71. },
  72. {
  73. name: "ModeWebhook should not error if a valid config file is provided",
  74. modes: []string{modes.ModeWebhook},
  75. webhookConfigFile: "authz_webhook_config.yaml",
  76. expectErr: false,
  77. },
  78. }
  79. for _, testcase := range testCases {
  80. t.Run(testcase.name, func(t *testing.T) {
  81. options := NewBuiltInAuthorizationOptions()
  82. options.Modes = testcase.modes
  83. options.WebhookConfigFile = testcase.webhookConfigFile
  84. options.PolicyFile = testcase.policyFile
  85. errs := options.Validate()
  86. if len(errs) > 0 && !testcase.expectErr {
  87. t.Errorf("got unexpected err %v", errs)
  88. }
  89. if testcase.expectErr && len(errs) == 0 {
  90. t.Errorf("should return an error")
  91. }
  92. })
  93. }
  94. }