abac_test.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. Copyright 2014 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 abac
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "reflect"
  18. "testing"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apiserver/pkg/authentication/user"
  21. "k8s.io/apiserver/pkg/authorization/authorizer"
  22. "k8s.io/kubernetes/pkg/apis/abac"
  23. "k8s.io/kubernetes/pkg/apis/abac/v0"
  24. "k8s.io/kubernetes/pkg/apis/abac/v1beta1"
  25. )
  26. func TestEmptyFile(t *testing.T) {
  27. _, err := newWithContents(t, "")
  28. if err != nil {
  29. t.Errorf("unable to read policy file: %v", err)
  30. }
  31. }
  32. func TestOneLineFileNoNewLine(t *testing.T) {
  33. _, err := newWithContents(t, `{"user":"scheduler", "readonly": true, "resource": "pods", "namespace":"ns1"}`)
  34. if err != nil {
  35. t.Errorf("unable to read policy file: %v", err)
  36. }
  37. }
  38. func TestTwoLineFile(t *testing.T) {
  39. _, err := newWithContents(t, `{"user":"scheduler", "readonly": true, "resource": "pods"}
  40. {"user":"scheduler", "readonly": true, "resource": "services"}
  41. `)
  42. if err != nil {
  43. t.Errorf("unable to read policy file: %v", err)
  44. }
  45. }
  46. // Test the file that we will point users at as an example.
  47. func TestExampleFile(t *testing.T) {
  48. _, err := NewFromFile("./example_policy_file.jsonl")
  49. if err != nil {
  50. t.Errorf("unable to read policy file: %v", err)
  51. }
  52. }
  53. func TestAuthorizeV0(t *testing.T) {
  54. a, err := newWithContents(t, `{ "readonly": true, "resource": "events" }
  55. {"user":"scheduler", "readonly": true, "resource": "pods" }
  56. {"user":"scheduler", "resource": "bindings" }
  57. {"user":"kubelet", "readonly": true, "resource": "bindings" }
  58. {"user":"kubelet", "resource": "events" }
  59. {"user":"alice", "namespace": "projectCaribou"}
  60. {"user":"bob", "readonly": true, "namespace": "projectCaribou"}
  61. `)
  62. if err != nil {
  63. t.Fatalf("unable to read policy file: %v", err)
  64. }
  65. authenticatedGroup := []string{user.AllAuthenticated}
  66. uScheduler := user.DefaultInfo{Name: "scheduler", UID: "uid1", Groups: authenticatedGroup}
  67. uAlice := user.DefaultInfo{Name: "alice", UID: "uid3", Groups: authenticatedGroup}
  68. uChuck := user.DefaultInfo{Name: "chuck", UID: "uid5", Groups: authenticatedGroup}
  69. testCases := []struct {
  70. User user.DefaultInfo
  71. Verb string
  72. Resource string
  73. NS string
  74. APIGroup string
  75. Path string
  76. ExpectDecision authorizer.Decision
  77. }{
  78. // Scheduler can read pods
  79. {User: uScheduler, Verb: "list", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  80. {User: uScheduler, Verb: "list", Resource: "pods", NS: "", ExpectDecision: authorizer.DecisionAllow},
  81. // Scheduler cannot write pods
  82. {User: uScheduler, Verb: "create", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  83. {User: uScheduler, Verb: "create", Resource: "pods", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  84. // Scheduler can write bindings
  85. {User: uScheduler, Verb: "get", Resource: "bindings", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  86. {User: uScheduler, Verb: "get", Resource: "bindings", NS: "", ExpectDecision: authorizer.DecisionAllow},
  87. // Alice can read and write anything in the right namespace.
  88. {User: uAlice, Verb: "get", Resource: "pods", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  89. {User: uAlice, Verb: "get", Resource: "widgets", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  90. {User: uAlice, Verb: "get", Resource: "", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  91. {User: uAlice, Verb: "update", Resource: "pods", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  92. {User: uAlice, Verb: "update", Resource: "widgets", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  93. {User: uAlice, Verb: "update", Resource: "", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  94. {User: uAlice, Verb: "update", Resource: "foo", NS: "projectCaribou", APIGroup: "bar", ExpectDecision: authorizer.DecisionAllow},
  95. // .. but not the wrong namespace.
  96. {User: uAlice, Verb: "get", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  97. {User: uAlice, Verb: "get", Resource: "widgets", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  98. {User: uAlice, Verb: "get", Resource: "", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  99. // Chuck can read events, since anyone can.
  100. {User: uChuck, Verb: "get", Resource: "events", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  101. {User: uChuck, Verb: "get", Resource: "events", NS: "", ExpectDecision: authorizer.DecisionAllow},
  102. // Chuck can't do other things.
  103. {User: uChuck, Verb: "update", Resource: "events", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  104. {User: uChuck, Verb: "get", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  105. {User: uChuck, Verb: "get", Resource: "floop", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  106. // Chunk can't access things with no kind or namespace
  107. {User: uChuck, Verb: "get", Path: "/", Resource: "", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  108. }
  109. for i, tc := range testCases {
  110. attr := authorizer.AttributesRecord{
  111. User: &tc.User,
  112. Verb: tc.Verb,
  113. Resource: tc.Resource,
  114. Namespace: tc.NS,
  115. APIGroup: tc.APIGroup,
  116. Path: tc.Path,
  117. ResourceRequest: len(tc.NS) > 0 || len(tc.Resource) > 0,
  118. }
  119. decision, _, _ := a.Authorize(attr)
  120. if tc.ExpectDecision != decision {
  121. t.Logf("tc: %v -> attr %v", tc, attr)
  122. t.Errorf("%d: Expected allowed=%v but actually allowed=%v\n\t%v",
  123. i, tc.ExpectDecision, decision, tc)
  124. }
  125. }
  126. }
  127. func getResourceRules(infos []authorizer.ResourceRuleInfo) []authorizer.DefaultResourceRuleInfo {
  128. rules := make([]authorizer.DefaultResourceRuleInfo, len(infos))
  129. for i, info := range infos {
  130. rules[i] = authorizer.DefaultResourceRuleInfo{
  131. Verbs: info.GetVerbs(),
  132. APIGroups: info.GetAPIGroups(),
  133. Resources: info.GetResources(),
  134. ResourceNames: info.GetResourceNames(),
  135. }
  136. }
  137. return rules
  138. }
  139. func getNonResourceRules(infos []authorizer.NonResourceRuleInfo) []authorizer.DefaultNonResourceRuleInfo {
  140. rules := make([]authorizer.DefaultNonResourceRuleInfo, len(infos))
  141. for i, info := range infos {
  142. rules[i] = authorizer.DefaultNonResourceRuleInfo{
  143. Verbs: info.GetVerbs(),
  144. NonResourceURLs: info.GetNonResourceURLs(),
  145. }
  146. }
  147. return rules
  148. }
  149. func TestRulesFor(t *testing.T) {
  150. a, err := newWithContents(t, `
  151. { "readonly": true, "resource": "events" }
  152. {"user":"scheduler", "readonly": true, "resource": "pods" }
  153. {"user":"scheduler", "resource": "bindings" }
  154. {"user":"kubelet", "readonly": true, "resource": "pods" }
  155. {"user":"kubelet", "resource": "events" }
  156. {"user":"alice", "namespace": "projectCaribou"}
  157. {"user":"bob", "readonly": true, "namespace": "projectCaribou"}
  158. {"user":"bob", "readonly": true, "nonResourcePath": "*"}
  159. {"group":"a", "resource": "bindings" }
  160. {"group":"b", "readonly": true, "nonResourcePath": "*"}
  161. `)
  162. if err != nil {
  163. t.Fatalf("unable to read policy file: %v", err)
  164. }
  165. authenticatedGroup := []string{user.AllAuthenticated}
  166. uScheduler := user.DefaultInfo{Name: "scheduler", UID: "uid1", Groups: authenticatedGroup}
  167. uKubelet := user.DefaultInfo{Name: "kubelet", UID: "uid2", Groups: []string{"a", "b"}}
  168. uAlice := user.DefaultInfo{Name: "alice", UID: "uid3", Groups: authenticatedGroup}
  169. uBob := user.DefaultInfo{Name: "bob", UID: "uid4", Groups: authenticatedGroup}
  170. uChuck := user.DefaultInfo{Name: "chuck", UID: "uid5", Groups: []string{"a", "b"}}
  171. testCases := []struct {
  172. User user.DefaultInfo
  173. Namespace string
  174. ExpectResourceRules []authorizer.DefaultResourceRuleInfo
  175. ExpectNonResourceRules []authorizer.DefaultNonResourceRuleInfo
  176. }{
  177. {
  178. User: uScheduler,
  179. Namespace: "ns1",
  180. ExpectResourceRules: []authorizer.DefaultResourceRuleInfo{
  181. {
  182. Verbs: []string{"get", "list", "watch"},
  183. APIGroups: []string{"*"},
  184. Resources: []string{"events"},
  185. },
  186. {
  187. Verbs: []string{"get", "list", "watch"},
  188. APIGroups: []string{"*"},
  189. Resources: []string{"pods"},
  190. },
  191. {
  192. Verbs: []string{"*"},
  193. APIGroups: []string{"*"},
  194. Resources: []string{"bindings"},
  195. },
  196. },
  197. ExpectNonResourceRules: []authorizer.DefaultNonResourceRuleInfo{},
  198. },
  199. {
  200. User: uKubelet,
  201. Namespace: "ns1",
  202. ExpectResourceRules: []authorizer.DefaultResourceRuleInfo{
  203. {
  204. Verbs: []string{"get", "list", "watch"},
  205. APIGroups: []string{"*"},
  206. Resources: []string{"pods"},
  207. },
  208. {
  209. Verbs: []string{"*"},
  210. APIGroups: []string{"*"},
  211. Resources: []string{"events"},
  212. },
  213. {
  214. Verbs: []string{"*"},
  215. APIGroups: []string{"*"},
  216. Resources: []string{"bindings"},
  217. },
  218. {
  219. Verbs: []string{"get", "list", "watch"},
  220. APIGroups: []string{"*"},
  221. Resources: []string{"*"},
  222. },
  223. },
  224. ExpectNonResourceRules: []authorizer.DefaultNonResourceRuleInfo{
  225. {
  226. Verbs: []string{"get", "list", "watch"},
  227. NonResourceURLs: []string{"*"},
  228. },
  229. },
  230. },
  231. {
  232. User: uAlice,
  233. Namespace: "projectCaribou",
  234. ExpectResourceRules: []authorizer.DefaultResourceRuleInfo{
  235. {
  236. Verbs: []string{"get", "list", "watch"},
  237. APIGroups: []string{"*"},
  238. Resources: []string{"events"},
  239. },
  240. {
  241. Verbs: []string{"*"},
  242. APIGroups: []string{"*"},
  243. Resources: []string{"*"},
  244. },
  245. },
  246. ExpectNonResourceRules: []authorizer.DefaultNonResourceRuleInfo{},
  247. },
  248. {
  249. User: uBob,
  250. Namespace: "projectCaribou",
  251. ExpectResourceRules: []authorizer.DefaultResourceRuleInfo{
  252. {
  253. Verbs: []string{"get", "list", "watch"},
  254. APIGroups: []string{"*"},
  255. Resources: []string{"events"},
  256. },
  257. {
  258. Verbs: []string{"get", "list", "watch"},
  259. APIGroups: []string{"*"},
  260. Resources: []string{"*"},
  261. },
  262. {
  263. Verbs: []string{"get", "list", "watch"},
  264. APIGroups: []string{"*"},
  265. Resources: []string{"*"},
  266. },
  267. },
  268. ExpectNonResourceRules: []authorizer.DefaultNonResourceRuleInfo{
  269. {
  270. Verbs: []string{"get", "list", "watch"},
  271. NonResourceURLs: []string{"*"},
  272. },
  273. },
  274. },
  275. {
  276. User: uChuck,
  277. Namespace: "ns1",
  278. ExpectResourceRules: []authorizer.DefaultResourceRuleInfo{
  279. {
  280. Verbs: []string{"*"},
  281. APIGroups: []string{"*"},
  282. Resources: []string{"bindings"},
  283. },
  284. {
  285. Verbs: []string{"get", "list", "watch"},
  286. APIGroups: []string{"*"},
  287. Resources: []string{"*"},
  288. },
  289. },
  290. ExpectNonResourceRules: []authorizer.DefaultNonResourceRuleInfo{
  291. {
  292. Verbs: []string{"get", "list", "watch"},
  293. NonResourceURLs: []string{"*"},
  294. },
  295. },
  296. },
  297. }
  298. for i, tc := range testCases {
  299. attr := authorizer.AttributesRecord{
  300. User: &tc.User,
  301. Namespace: tc.Namespace,
  302. }
  303. resourceRules, nonResourceRules, _, _ := a.RulesFor(attr.GetUser(), attr.GetNamespace())
  304. actualResourceRules := getResourceRules(resourceRules)
  305. if !reflect.DeepEqual(tc.ExpectResourceRules, actualResourceRules) {
  306. t.Logf("tc: %v -> attr %v", tc, attr)
  307. t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n",
  308. i, tc.ExpectResourceRules, actualResourceRules)
  309. }
  310. actualNonResourceRules := getNonResourceRules(nonResourceRules)
  311. if !reflect.DeepEqual(tc.ExpectNonResourceRules, actualNonResourceRules) {
  312. t.Logf("tc: %v -> attr %v", tc, attr)
  313. t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n",
  314. i, tc.ExpectNonResourceRules, actualNonResourceRules)
  315. }
  316. }
  317. }
  318. func TestAuthorizeV1beta1(t *testing.T) {
  319. a, err := newWithContents(t,
  320. `
  321. # Comment line, after a blank line
  322. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"*", "readonly": true, "nonResourcePath": "/api"}}
  323. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"*", "nonResourcePath": "/custom"}}
  324. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"*", "nonResourcePath": "/root/*"}}
  325. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"noresource", "nonResourcePath": "*"}}
  326. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"*", "readonly": true, "resource": "events", "namespace": "*"}}
  327. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"scheduler", "readonly": true, "resource": "pods", "namespace": "*"}}
  328. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"scheduler", "resource": "bindings", "namespace": "*"}}
  329. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"kubelet", "readonly": true, "resource": "bindings", "namespace": "*"}}
  330. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"kubelet", "resource": "events", "namespace": "*"}}
  331. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"alice", "resource": "*", "namespace": "projectCaribou"}}
  332. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"bob", "readonly": true, "resource": "*", "namespace": "projectCaribou"}}
  333. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"debbie", "resource": "pods", "namespace": "projectCaribou"}}
  334. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"apigroupuser", "resource": "*", "namespace": "projectAnyGroup", "apiGroup": "*"}}
  335. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"apigroupuser", "resource": "*", "namespace": "projectEmptyGroup", "apiGroup": "" }}
  336. {"apiVersion":"abac.authorization.kubernetes.io/v1beta1","kind":"Policy","spec":{"user":"apigroupuser", "resource": "*", "namespace": "projectXGroup", "apiGroup": "x"}}`)
  337. if err != nil {
  338. t.Fatalf("unable to read policy file: %v", err)
  339. }
  340. authenticatedGroup := []string{user.AllAuthenticated}
  341. uScheduler := user.DefaultInfo{Name: "scheduler", UID: "uid1", Groups: authenticatedGroup}
  342. uAlice := user.DefaultInfo{Name: "alice", UID: "uid3", Groups: authenticatedGroup}
  343. uChuck := user.DefaultInfo{Name: "chuck", UID: "uid5", Groups: authenticatedGroup}
  344. uDebbie := user.DefaultInfo{Name: "debbie", UID: "uid6", Groups: authenticatedGroup}
  345. uNoResource := user.DefaultInfo{Name: "noresource", UID: "uid7", Groups: authenticatedGroup}
  346. uAPIGroup := user.DefaultInfo{Name: "apigroupuser", UID: "uid8", Groups: authenticatedGroup}
  347. testCases := []struct {
  348. User user.DefaultInfo
  349. Verb string
  350. Resource string
  351. APIGroup string
  352. NS string
  353. Path string
  354. ExpectDecision authorizer.Decision
  355. }{
  356. // Scheduler can read pods
  357. {User: uScheduler, Verb: "list", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  358. {User: uScheduler, Verb: "list", Resource: "pods", NS: "", ExpectDecision: authorizer.DecisionAllow},
  359. // Scheduler cannot write pods
  360. {User: uScheduler, Verb: "create", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  361. {User: uScheduler, Verb: "create", Resource: "pods", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  362. // Scheduler can write bindings
  363. {User: uScheduler, Verb: "get", Resource: "bindings", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  364. {User: uScheduler, Verb: "get", Resource: "bindings", NS: "", ExpectDecision: authorizer.DecisionAllow},
  365. // Alice can read and write anything in the right namespace.
  366. {User: uAlice, Verb: "get", Resource: "pods", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  367. {User: uAlice, Verb: "get", Resource: "widgets", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  368. {User: uAlice, Verb: "get", Resource: "", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  369. {User: uAlice, Verb: "update", Resource: "pods", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  370. {User: uAlice, Verb: "update", Resource: "widgets", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  371. {User: uAlice, Verb: "update", Resource: "", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  372. // .. but not the wrong namespace.
  373. {User: uAlice, Verb: "get", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  374. {User: uAlice, Verb: "get", Resource: "widgets", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  375. {User: uAlice, Verb: "get", Resource: "", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  376. // Debbie can write to pods in the right namespace
  377. {User: uDebbie, Verb: "update", Resource: "pods", NS: "projectCaribou", ExpectDecision: authorizer.DecisionAllow},
  378. // Chuck can read events, since anyone can.
  379. {User: uChuck, Verb: "get", Resource: "events", NS: "ns1", ExpectDecision: authorizer.DecisionAllow},
  380. {User: uChuck, Verb: "get", Resource: "events", NS: "", ExpectDecision: authorizer.DecisionAllow},
  381. // Chuck can't do other things.
  382. {User: uChuck, Verb: "update", Resource: "events", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  383. {User: uChuck, Verb: "get", Resource: "pods", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  384. {User: uChuck, Verb: "get", Resource: "floop", NS: "ns1", ExpectDecision: authorizer.DecisionNoOpinion},
  385. // Chuck can't access things with no resource or namespace
  386. {User: uChuck, Verb: "get", Path: "/", Resource: "", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  387. // but can access /api
  388. {User: uChuck, Verb: "get", Path: "/api", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  389. // though he cannot write to it
  390. {User: uChuck, Verb: "create", Path: "/api", Resource: "", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  391. // while he can write to /custom
  392. {User: uChuck, Verb: "update", Path: "/custom", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  393. // he cannot get "/root"
  394. {User: uChuck, Verb: "get", Path: "/root", Resource: "", NS: "", ExpectDecision: authorizer.DecisionNoOpinion},
  395. // but can get any subpath
  396. {User: uChuck, Verb: "get", Path: "/root/", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  397. {User: uChuck, Verb: "get", Path: "/root/test/1/2/3", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  398. // the user "noresource" can get any non-resource request
  399. {User: uNoResource, Verb: "get", Path: "", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  400. {User: uNoResource, Verb: "get", Path: "/", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  401. {User: uNoResource, Verb: "get", Path: "/foo/bar/baz", Resource: "", NS: "", ExpectDecision: authorizer.DecisionAllow},
  402. // but cannot get any request where IsResourceRequest() == true
  403. {User: uNoResource, Verb: "get", Path: "/", Resource: "", NS: "bar", ExpectDecision: authorizer.DecisionNoOpinion},
  404. {User: uNoResource, Verb: "get", Path: "/foo/bar/baz", Resource: "foo", NS: "bar", ExpectDecision: authorizer.DecisionNoOpinion},
  405. // Test APIGroup matching
  406. {User: uAPIGroup, Verb: "get", APIGroup: "x", Resource: "foo", NS: "projectAnyGroup", ExpectDecision: authorizer.DecisionAllow},
  407. {User: uAPIGroup, Verb: "get", APIGroup: "x", Resource: "foo", NS: "projectEmptyGroup", ExpectDecision: authorizer.DecisionNoOpinion},
  408. {User: uAPIGroup, Verb: "get", APIGroup: "x", Resource: "foo", NS: "projectXGroup", ExpectDecision: authorizer.DecisionAllow},
  409. }
  410. for i, tc := range testCases {
  411. attr := authorizer.AttributesRecord{
  412. User: &tc.User,
  413. Verb: tc.Verb,
  414. Resource: tc.Resource,
  415. APIGroup: tc.APIGroup,
  416. Namespace: tc.NS,
  417. ResourceRequest: len(tc.NS) > 0 || len(tc.Resource) > 0,
  418. Path: tc.Path,
  419. }
  420. // t.Logf("tc %2v: %v -> attr %v", i, tc, attr)
  421. decision, _, _ := a.Authorize(attr)
  422. if tc.ExpectDecision != decision {
  423. t.Errorf("%d: Expected allowed=%v but actually allowed=%v, for case %+v & %+v",
  424. i, tc.ExpectDecision, decision, tc, attr)
  425. }
  426. }
  427. }
  428. func TestSubjectMatches(t *testing.T) {
  429. testCases := map[string]struct {
  430. User user.DefaultInfo
  431. Policy runtime.Object
  432. ExpectMatch bool
  433. }{
  434. "v0 empty policy does not match unauthed user": {
  435. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  436. Policy: &v0.Policy{
  437. User: "",
  438. Group: "",
  439. },
  440. ExpectMatch: false,
  441. },
  442. "v0 * user policy does not match unauthed user": {
  443. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  444. Policy: &v0.Policy{
  445. User: "*",
  446. Group: "",
  447. },
  448. ExpectMatch: false,
  449. },
  450. "v0 * group policy does not match unauthed user": {
  451. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  452. Policy: &v0.Policy{
  453. User: "",
  454. Group: "*",
  455. },
  456. ExpectMatch: false,
  457. },
  458. "v0 empty policy matches authed user": {
  459. User: user.DefaultInfo{Name: "Foo", Groups: []string{user.AllAuthenticated}},
  460. Policy: &v0.Policy{
  461. User: "",
  462. Group: "",
  463. },
  464. ExpectMatch: true,
  465. },
  466. "v0 empty policy matches authed user with groups": {
  467. User: user.DefaultInfo{Name: "Foo", Groups: []string{"a", "b", user.AllAuthenticated}},
  468. Policy: &v0.Policy{
  469. User: "",
  470. Group: "",
  471. },
  472. ExpectMatch: true,
  473. },
  474. "v0 user policy does not match unauthed user": {
  475. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  476. Policy: &v0.Policy{
  477. User: "Foo",
  478. Group: "",
  479. },
  480. ExpectMatch: false,
  481. },
  482. "v0 user policy does not match different user": {
  483. User: user.DefaultInfo{Name: "Bar", Groups: []string{user.AllAuthenticated}},
  484. Policy: &v0.Policy{
  485. User: "Foo",
  486. Group: "",
  487. },
  488. ExpectMatch: false,
  489. },
  490. "v0 user policy is case-sensitive": {
  491. User: user.DefaultInfo{Name: "foo", Groups: []string{user.AllAuthenticated}},
  492. Policy: &v0.Policy{
  493. User: "Foo",
  494. Group: "",
  495. },
  496. ExpectMatch: false,
  497. },
  498. "v0 user policy does not match substring": {
  499. User: user.DefaultInfo{Name: "FooBar", Groups: []string{user.AllAuthenticated}},
  500. Policy: &v0.Policy{
  501. User: "Foo",
  502. Group: "",
  503. },
  504. ExpectMatch: false,
  505. },
  506. "v0 user policy matches username": {
  507. User: user.DefaultInfo{Name: "Foo", Groups: []string{user.AllAuthenticated}},
  508. Policy: &v0.Policy{
  509. User: "Foo",
  510. Group: "",
  511. },
  512. ExpectMatch: true,
  513. },
  514. "v0 group policy does not match unauthed user": {
  515. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  516. Policy: &v0.Policy{
  517. User: "",
  518. Group: "Foo",
  519. },
  520. ExpectMatch: false,
  521. },
  522. "v0 group policy does not match user in different group": {
  523. User: user.DefaultInfo{Name: "FooBar", Groups: []string{"B", user.AllAuthenticated}},
  524. Policy: &v0.Policy{
  525. User: "",
  526. Group: "A",
  527. },
  528. ExpectMatch: false,
  529. },
  530. "v0 group policy is case-sensitive": {
  531. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  532. Policy: &v0.Policy{
  533. User: "",
  534. Group: "b",
  535. },
  536. ExpectMatch: false,
  537. },
  538. "v0 group policy does not match substring": {
  539. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "BBB", "C", user.AllAuthenticated}},
  540. Policy: &v0.Policy{
  541. User: "",
  542. Group: "B",
  543. },
  544. ExpectMatch: false,
  545. },
  546. "v0 group policy matches user in group": {
  547. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  548. Policy: &v0.Policy{
  549. User: "",
  550. Group: "B",
  551. },
  552. ExpectMatch: true,
  553. },
  554. "v0 user and group policy requires user match": {
  555. User: user.DefaultInfo{Name: "Bar", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  556. Policy: &v0.Policy{
  557. User: "Foo",
  558. Group: "B",
  559. },
  560. ExpectMatch: false,
  561. },
  562. "v0 user and group policy requires group match": {
  563. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  564. Policy: &v0.Policy{
  565. User: "Foo",
  566. Group: "D",
  567. },
  568. ExpectMatch: false,
  569. },
  570. "v0 user and group policy matches": {
  571. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  572. Policy: &v0.Policy{
  573. User: "Foo",
  574. Group: "B",
  575. },
  576. ExpectMatch: true,
  577. },
  578. "v1 empty policy does not match unauthed user": {
  579. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  580. Policy: &v1beta1.Policy{
  581. Spec: v1beta1.PolicySpec{
  582. User: "",
  583. Group: "",
  584. },
  585. },
  586. ExpectMatch: false,
  587. },
  588. "v1 * user policy does not match unauthed user": {
  589. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  590. Policy: &v1beta1.Policy{
  591. Spec: v1beta1.PolicySpec{
  592. User: "*",
  593. Group: "",
  594. },
  595. },
  596. ExpectMatch: false,
  597. },
  598. "v1 * group policy does not match unauthed user": {
  599. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  600. Policy: &v1beta1.Policy{
  601. Spec: v1beta1.PolicySpec{
  602. User: "",
  603. Group: "*",
  604. },
  605. },
  606. ExpectMatch: false,
  607. },
  608. "v1 empty policy does not match authed user": {
  609. User: user.DefaultInfo{Name: "Foo", Groups: []string{user.AllAuthenticated}},
  610. Policy: &v1beta1.Policy{
  611. Spec: v1beta1.PolicySpec{
  612. User: "",
  613. Group: "",
  614. },
  615. },
  616. ExpectMatch: false,
  617. },
  618. "v1 empty policy does not match authed user with groups": {
  619. User: user.DefaultInfo{Name: "Foo", Groups: []string{"a", "b", user.AllAuthenticated}},
  620. Policy: &v1beta1.Policy{
  621. Spec: v1beta1.PolicySpec{
  622. User: "",
  623. Group: "",
  624. },
  625. },
  626. ExpectMatch: false,
  627. },
  628. "v1 user policy does not match unauthed user": {
  629. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  630. Policy: &v1beta1.Policy{
  631. Spec: v1beta1.PolicySpec{
  632. User: "Foo",
  633. Group: "",
  634. },
  635. },
  636. ExpectMatch: false,
  637. },
  638. "v1 user policy does not match different user": {
  639. User: user.DefaultInfo{Name: "Bar", Groups: []string{user.AllAuthenticated}},
  640. Policy: &v1beta1.Policy{
  641. Spec: v1beta1.PolicySpec{
  642. User: "Foo",
  643. Group: "",
  644. },
  645. },
  646. ExpectMatch: false,
  647. },
  648. "v1 user policy is case-sensitive": {
  649. User: user.DefaultInfo{Name: "foo", Groups: []string{user.AllAuthenticated}},
  650. Policy: &v1beta1.Policy{
  651. Spec: v1beta1.PolicySpec{
  652. User: "Foo",
  653. Group: "",
  654. },
  655. },
  656. ExpectMatch: false,
  657. },
  658. "v1 user policy does not match substring": {
  659. User: user.DefaultInfo{Name: "FooBar", Groups: []string{user.AllAuthenticated}},
  660. Policy: &v1beta1.Policy{
  661. Spec: v1beta1.PolicySpec{
  662. User: "Foo",
  663. Group: "",
  664. },
  665. },
  666. ExpectMatch: false,
  667. },
  668. "v1 user policy matches username": {
  669. User: user.DefaultInfo{Name: "Foo", Groups: []string{user.AllAuthenticated}},
  670. Policy: &v1beta1.Policy{
  671. Spec: v1beta1.PolicySpec{
  672. User: "Foo",
  673. Group: "",
  674. },
  675. },
  676. ExpectMatch: true,
  677. },
  678. "v1 group policy does not match unauthed user": {
  679. User: user.DefaultInfo{Name: "system:anonymous", Groups: []string{"system:unauthenticated"}},
  680. Policy: &v1beta1.Policy{
  681. Spec: v1beta1.PolicySpec{
  682. User: "",
  683. Group: "Foo",
  684. },
  685. },
  686. ExpectMatch: false,
  687. },
  688. "v1 group policy does not match user in different group": {
  689. User: user.DefaultInfo{Name: "FooBar", Groups: []string{"B", user.AllAuthenticated}},
  690. Policy: &v1beta1.Policy{
  691. Spec: v1beta1.PolicySpec{
  692. User: "",
  693. Group: "A",
  694. },
  695. },
  696. ExpectMatch: false,
  697. },
  698. "v1 group policy is case-sensitive": {
  699. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  700. Policy: &v1beta1.Policy{
  701. Spec: v1beta1.PolicySpec{
  702. User: "",
  703. Group: "b",
  704. },
  705. },
  706. ExpectMatch: false,
  707. },
  708. "v1 group policy does not match substring": {
  709. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "BBB", "C", user.AllAuthenticated}},
  710. Policy: &v1beta1.Policy{
  711. Spec: v1beta1.PolicySpec{
  712. User: "",
  713. Group: "B",
  714. },
  715. },
  716. ExpectMatch: false,
  717. },
  718. "v1 group policy matches user in group": {
  719. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  720. Policy: &v1beta1.Policy{
  721. Spec: v1beta1.PolicySpec{
  722. User: "",
  723. Group: "B",
  724. },
  725. },
  726. ExpectMatch: true,
  727. },
  728. "v1 user and group policy requires user match": {
  729. User: user.DefaultInfo{Name: "Bar", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  730. Policy: &v1beta1.Policy{
  731. Spec: v1beta1.PolicySpec{
  732. User: "Foo",
  733. Group: "B",
  734. },
  735. },
  736. ExpectMatch: false,
  737. },
  738. "v1 user and group policy requires group match": {
  739. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  740. Policy: &v1beta1.Policy{
  741. Spec: v1beta1.PolicySpec{
  742. User: "Foo",
  743. Group: "D",
  744. },
  745. },
  746. ExpectMatch: false,
  747. },
  748. "v1 user and group policy matches": {
  749. User: user.DefaultInfo{Name: "Foo", Groups: []string{"A", "B", "C", user.AllAuthenticated}},
  750. Policy: &v1beta1.Policy{
  751. Spec: v1beta1.PolicySpec{
  752. User: "Foo",
  753. Group: "B",
  754. },
  755. },
  756. ExpectMatch: true,
  757. },
  758. }
  759. for k, tc := range testCases {
  760. policy := &abac.Policy{}
  761. if err := abac.Scheme.Convert(tc.Policy, policy, nil); err != nil {
  762. t.Errorf("%s: error converting: %v", k, err)
  763. continue
  764. }
  765. attr := authorizer.AttributesRecord{
  766. User: &tc.User,
  767. }
  768. actualMatch := subjectMatches(*policy, attr.GetUser())
  769. if tc.ExpectMatch != actualMatch {
  770. t.Errorf("%v: Expected actorMatches=%v but actually got=%v",
  771. k, tc.ExpectMatch, actualMatch)
  772. }
  773. }
  774. }
  775. func newWithContents(t *testing.T, contents string) (PolicyList, error) {
  776. f, err := ioutil.TempFile("", "abac_test")
  777. if err != nil {
  778. t.Fatalf("unexpected error creating policyfile: %v", err)
  779. }
  780. f.Close()
  781. defer os.Remove(f.Name())
  782. if err := ioutil.WriteFile(f.Name(), []byte(contents), 0700); err != nil {
  783. t.Fatalf("unexpected error writing policyfile: %v", err)
  784. }
  785. pl, err := NewFromFile(f.Name())
  786. return pl, err
  787. }
  788. func TestPolicy(t *testing.T) {
  789. tests := []struct {
  790. policy runtime.Object
  791. attr authorizer.Attributes
  792. matches bool
  793. name string
  794. }{
  795. // v0 mismatches
  796. {
  797. policy: &v0.Policy{
  798. Readonly: true,
  799. },
  800. attr: authorizer.AttributesRecord{
  801. User: &user.DefaultInfo{
  802. Name: "foo",
  803. Groups: []string{user.AllAuthenticated},
  804. },
  805. Verb: "create",
  806. },
  807. matches: false,
  808. name: "v0 read-only mismatch",
  809. },
  810. {
  811. policy: &v0.Policy{
  812. User: "foo",
  813. },
  814. attr: authorizer.AttributesRecord{
  815. User: &user.DefaultInfo{
  816. Name: "bar",
  817. Groups: []string{user.AllAuthenticated},
  818. },
  819. },
  820. matches: false,
  821. name: "v0 user name mis-match",
  822. },
  823. {
  824. policy: &v0.Policy{
  825. Resource: "foo",
  826. },
  827. attr: authorizer.AttributesRecord{
  828. User: &user.DefaultInfo{
  829. Name: "foo",
  830. Groups: []string{user.AllAuthenticated},
  831. },
  832. Resource: "bar",
  833. ResourceRequest: true,
  834. },
  835. matches: false,
  836. name: "v0 resource mis-match",
  837. },
  838. {
  839. policy: &v0.Policy{
  840. User: "foo",
  841. Resource: "foo",
  842. Namespace: "foo",
  843. },
  844. attr: authorizer.AttributesRecord{
  845. User: &user.DefaultInfo{
  846. Name: "foo",
  847. Groups: []string{user.AllAuthenticated},
  848. },
  849. Resource: "foo",
  850. Namespace: "foo",
  851. ResourceRequest: true,
  852. },
  853. matches: true,
  854. name: "v0 namespace mis-match",
  855. },
  856. // v0 matches
  857. {
  858. policy: &v0.Policy{},
  859. attr: authorizer.AttributesRecord{
  860. User: &user.DefaultInfo{
  861. Name: "foo",
  862. Groups: []string{user.AllAuthenticated},
  863. },
  864. ResourceRequest: true,
  865. },
  866. matches: true,
  867. name: "v0 null resource",
  868. },
  869. {
  870. policy: &v0.Policy{
  871. Readonly: true,
  872. },
  873. attr: authorizer.AttributesRecord{
  874. User: &user.DefaultInfo{
  875. Name: "foo",
  876. Groups: []string{user.AllAuthenticated},
  877. },
  878. Verb: "get",
  879. },
  880. matches: true,
  881. name: "v0 read-only match",
  882. },
  883. {
  884. policy: &v0.Policy{
  885. User: "foo",
  886. },
  887. attr: authorizer.AttributesRecord{
  888. User: &user.DefaultInfo{
  889. Name: "foo",
  890. Groups: []string{user.AllAuthenticated},
  891. },
  892. },
  893. matches: true,
  894. name: "v0 user name match",
  895. },
  896. {
  897. policy: &v0.Policy{
  898. Resource: "foo",
  899. },
  900. attr: authorizer.AttributesRecord{
  901. User: &user.DefaultInfo{
  902. Name: "foo",
  903. Groups: []string{user.AllAuthenticated},
  904. },
  905. Resource: "foo",
  906. ResourceRequest: true,
  907. },
  908. matches: true,
  909. name: "v0 resource match",
  910. },
  911. // v1 mismatches
  912. {
  913. policy: &v1beta1.Policy{},
  914. attr: authorizer.AttributesRecord{
  915. User: &user.DefaultInfo{
  916. Name: "foo",
  917. Groups: []string{user.AllAuthenticated},
  918. },
  919. ResourceRequest: true,
  920. },
  921. matches: false,
  922. name: "v1 null",
  923. },
  924. {
  925. policy: &v1beta1.Policy{
  926. Spec: v1beta1.PolicySpec{
  927. User: "foo",
  928. },
  929. },
  930. attr: authorizer.AttributesRecord{
  931. User: &user.DefaultInfo{
  932. Name: "bar",
  933. Groups: []string{user.AllAuthenticated},
  934. },
  935. ResourceRequest: true,
  936. },
  937. matches: false,
  938. name: "v1 user name mis-match",
  939. },
  940. {
  941. policy: &v1beta1.Policy{
  942. Spec: v1beta1.PolicySpec{
  943. User: "*",
  944. Readonly: true,
  945. },
  946. },
  947. attr: authorizer.AttributesRecord{
  948. User: &user.DefaultInfo{
  949. Name: "foo",
  950. Groups: []string{user.AllAuthenticated},
  951. },
  952. ResourceRequest: true,
  953. },
  954. matches: false,
  955. name: "v1 read-only mismatch",
  956. },
  957. {
  958. policy: &v1beta1.Policy{
  959. Spec: v1beta1.PolicySpec{
  960. User: "*",
  961. Resource: "foo",
  962. },
  963. },
  964. attr: authorizer.AttributesRecord{
  965. User: &user.DefaultInfo{
  966. Name: "foo",
  967. Groups: []string{user.AllAuthenticated},
  968. },
  969. Resource: "bar",
  970. ResourceRequest: true,
  971. },
  972. matches: false,
  973. name: "v1 resource mis-match",
  974. },
  975. {
  976. policy: &v1beta1.Policy{
  977. Spec: v1beta1.PolicySpec{
  978. User: "foo",
  979. Namespace: "barr",
  980. Resource: "baz",
  981. },
  982. },
  983. attr: authorizer.AttributesRecord{
  984. User: &user.DefaultInfo{
  985. Name: "foo",
  986. Groups: []string{user.AllAuthenticated},
  987. },
  988. Namespace: "bar",
  989. Resource: "baz",
  990. ResourceRequest: true,
  991. },
  992. matches: false,
  993. name: "v1 namespace mis-match",
  994. },
  995. {
  996. policy: &v1beta1.Policy{
  997. Spec: v1beta1.PolicySpec{
  998. User: "*",
  999. NonResourcePath: "/api",
  1000. },
  1001. },
  1002. attr: authorizer.AttributesRecord{
  1003. User: &user.DefaultInfo{
  1004. Name: "foo",
  1005. Groups: []string{user.AllAuthenticated},
  1006. },
  1007. Path: "/api2",
  1008. ResourceRequest: false,
  1009. },
  1010. matches: false,
  1011. name: "v1 non-resource mis-match",
  1012. },
  1013. {
  1014. policy: &v1beta1.Policy{
  1015. Spec: v1beta1.PolicySpec{
  1016. User: "*",
  1017. NonResourcePath: "/api/*",
  1018. },
  1019. },
  1020. attr: authorizer.AttributesRecord{
  1021. User: &user.DefaultInfo{
  1022. Name: "foo",
  1023. Groups: []string{user.AllAuthenticated},
  1024. },
  1025. Path: "/api2/foo",
  1026. ResourceRequest: false,
  1027. },
  1028. matches: false,
  1029. name: "v1 non-resource wildcard subpath mis-match",
  1030. },
  1031. // v1 matches
  1032. {
  1033. policy: &v1beta1.Policy{
  1034. Spec: v1beta1.PolicySpec{
  1035. User: "foo",
  1036. },
  1037. },
  1038. attr: authorizer.AttributesRecord{
  1039. User: &user.DefaultInfo{
  1040. Name: "foo",
  1041. Groups: []string{user.AllAuthenticated},
  1042. },
  1043. ResourceRequest: true,
  1044. },
  1045. matches: true,
  1046. name: "v1 user match",
  1047. },
  1048. {
  1049. policy: &v1beta1.Policy{
  1050. Spec: v1beta1.PolicySpec{
  1051. User: "*",
  1052. },
  1053. },
  1054. attr: authorizer.AttributesRecord{
  1055. User: &user.DefaultInfo{
  1056. Name: "foo",
  1057. Groups: []string{user.AllAuthenticated},
  1058. },
  1059. ResourceRequest: true,
  1060. },
  1061. matches: true,
  1062. name: "v1 user wildcard match",
  1063. },
  1064. {
  1065. policy: &v1beta1.Policy{
  1066. Spec: v1beta1.PolicySpec{
  1067. Group: "bar",
  1068. },
  1069. },
  1070. attr: authorizer.AttributesRecord{
  1071. User: &user.DefaultInfo{
  1072. Name: "foo",
  1073. Groups: []string{"bar", user.AllAuthenticated},
  1074. },
  1075. ResourceRequest: true,
  1076. },
  1077. matches: true,
  1078. name: "v1 group match",
  1079. },
  1080. {
  1081. policy: &v1beta1.Policy{
  1082. Spec: v1beta1.PolicySpec{
  1083. Group: "*",
  1084. },
  1085. },
  1086. attr: authorizer.AttributesRecord{
  1087. User: &user.DefaultInfo{
  1088. Name: "foo",
  1089. Groups: []string{"bar", user.AllAuthenticated},
  1090. },
  1091. ResourceRequest: true,
  1092. },
  1093. matches: true,
  1094. name: "v1 group wildcard match",
  1095. },
  1096. {
  1097. policy: &v1beta1.Policy{
  1098. Spec: v1beta1.PolicySpec{
  1099. User: "*",
  1100. Readonly: true,
  1101. },
  1102. },
  1103. attr: authorizer.AttributesRecord{
  1104. User: &user.DefaultInfo{
  1105. Name: "foo",
  1106. Groups: []string{user.AllAuthenticated},
  1107. },
  1108. Verb: "get",
  1109. ResourceRequest: true,
  1110. },
  1111. matches: true,
  1112. name: "v1 read-only match",
  1113. },
  1114. {
  1115. policy: &v1beta1.Policy{
  1116. Spec: v1beta1.PolicySpec{
  1117. User: "*",
  1118. Resource: "foo",
  1119. },
  1120. },
  1121. attr: authorizer.AttributesRecord{
  1122. User: &user.DefaultInfo{
  1123. Name: "foo",
  1124. Groups: []string{user.AllAuthenticated},
  1125. },
  1126. Resource: "foo",
  1127. ResourceRequest: true,
  1128. },
  1129. matches: true,
  1130. name: "v1 resource match",
  1131. },
  1132. {
  1133. policy: &v1beta1.Policy{
  1134. Spec: v1beta1.PolicySpec{
  1135. User: "foo",
  1136. Namespace: "bar",
  1137. Resource: "baz",
  1138. },
  1139. },
  1140. attr: authorizer.AttributesRecord{
  1141. User: &user.DefaultInfo{
  1142. Name: "foo",
  1143. Groups: []string{user.AllAuthenticated},
  1144. },
  1145. Namespace: "bar",
  1146. Resource: "baz",
  1147. ResourceRequest: true,
  1148. },
  1149. matches: true,
  1150. name: "v1 namespace match",
  1151. },
  1152. {
  1153. policy: &v1beta1.Policy{
  1154. Spec: v1beta1.PolicySpec{
  1155. User: "*",
  1156. NonResourcePath: "/api",
  1157. },
  1158. },
  1159. attr: authorizer.AttributesRecord{
  1160. User: &user.DefaultInfo{
  1161. Name: "foo",
  1162. Groups: []string{user.AllAuthenticated},
  1163. },
  1164. Path: "/api",
  1165. ResourceRequest: false,
  1166. },
  1167. matches: true,
  1168. name: "v1 non-resource match",
  1169. },
  1170. {
  1171. policy: &v1beta1.Policy{
  1172. Spec: v1beta1.PolicySpec{
  1173. User: "*",
  1174. NonResourcePath: "*",
  1175. },
  1176. },
  1177. attr: authorizer.AttributesRecord{
  1178. User: &user.DefaultInfo{
  1179. Name: "foo",
  1180. Groups: []string{user.AllAuthenticated},
  1181. },
  1182. Path: "/api",
  1183. ResourceRequest: false,
  1184. },
  1185. matches: true,
  1186. name: "v1 non-resource wildcard match",
  1187. },
  1188. {
  1189. policy: &v1beta1.Policy{
  1190. Spec: v1beta1.PolicySpec{
  1191. User: "*",
  1192. NonResourcePath: "/api/*",
  1193. },
  1194. },
  1195. attr: authorizer.AttributesRecord{
  1196. User: &user.DefaultInfo{
  1197. Name: "foo",
  1198. Groups: []string{user.AllAuthenticated},
  1199. },
  1200. Path: "/api/foo",
  1201. ResourceRequest: false,
  1202. },
  1203. matches: true,
  1204. name: "v1 non-resource wildcard subpath match",
  1205. },
  1206. }
  1207. for _, test := range tests {
  1208. policy := &abac.Policy{}
  1209. if err := abac.Scheme.Convert(test.policy, policy, nil); err != nil {
  1210. t.Errorf("%s: error converting: %v", test.name, err)
  1211. continue
  1212. }
  1213. matches := matches(*policy, test.attr)
  1214. if test.matches != matches {
  1215. t.Errorf("%s: expected: %t, saw: %t", test.name, test.matches, matches)
  1216. continue
  1217. }
  1218. }
  1219. }