| 1 |
/* |
| 2 |
Copyright 2019 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 |
*/ |
| 16 |
|
| 17 |
package flowcontrol |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"math/rand" |
| 22 |
"testing" |
| 23 |
|
| 24 |
flowcontrol "k8s.io/api/flowcontrol/v1" |
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
"k8s.io/apimachinery/pkg/util/sets" |
| 27 |
"k8s.io/apiserver/pkg/authentication/user" |
| 28 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 29 |
fcfmt "k8s.io/apiserver/pkg/util/flowcontrol/format" |
| 30 |
) |
| 31 |
|
| 32 |
func TestMatching(t *testing.T) { |
| 33 |
checkFTR(t, mandFTRExempt) |
| 34 |
checkFTR(t, mandFTRCatchAll) |
| 35 |
rngOuter := rand.New(rand.NewSource(42)) |
| 36 |
goodPLNames := sets.NewString("pl1", "pl2", "pl3", "pl4", "pl5") |
| 37 |
badPLNames := sets.NewString("ql1", "ql2", "ql3", "ql4", "ql5") |
| 38 |
for i := 0; i < 300; i++ { |
| 39 |
rng := rand.New(rand.NewSource(int64(rngOuter.Uint64()))) |
| 40 |
t.Run(fmt.Sprintf("trial%d:", i), func(t *testing.T) { |
| 41 |
ftr := genFS(t, rng, fmt.Sprintf("fs%d", i), rng.Float32() < 0.2, goodPLNames, badPLNames) |
| 42 |
checkFTR(t, ftr) |
| 43 |
}) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func checkFTR(t *testing.T, ftr *fsTestingRecord) { |
| 48 |
for expectMatch, digests1 := range ftr.digests { |
| 49 |
if testDebugLogs { |
| 50 |
t.Logf("%s.digests[%v] = %#+v", ftr.fs.Name, expectMatch, digests1) |
| 51 |
} |
| 52 |
for _, digests2 := range digests1 { |
| 53 |
for _, digest := range digests2 { |
| 54 |
actualMatch := matchesFlowSchema(digest, ftr.fs) |
| 55 |
if expectMatch != actualMatch { |
| 56 |
t.Errorf("Fail for %s vs %#+v: expectedMatch=%v, actualMatch=%v", fcfmt.Fmt(ftr.fs), digest, expectMatch, actualMatch) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestPolicyRules(t *testing.T) { |
| 64 |
rngOuter := rand.New(rand.NewSource(42)) |
| 65 |
for i := 0; i < 300; i++ { |
| 66 |
rng := rand.New(rand.NewSource(int64(rngOuter.Uint64()))) |
| 67 |
t.Run(fmt.Sprintf("trial%d:", i), func(t *testing.T) { |
| 68 |
r := rng.Float32() |
| 69 |
n := rng.Float32() |
| 70 |
policyRule, matchingRDigests, matchingNDigests, skippingRDigests, skippingNDigests := genPolicyRuleWithSubjects(t, rng, fmt.Sprintf("t%d", i), rng.Float32() < 0.2, r < 0.10, n < 0.10, r < 0.05, n < 0.05) |
| 71 |
if testDebugLogs { |
| 72 |
t.Logf("policyRule=%s, mrd=%#+v, mnd=%#+v, srd=%#+v, snd=%#+v", fcfmt.Fmt(policyRule), matchingRDigests, matchingNDigests, skippingRDigests, skippingNDigests) |
| 73 |
} |
| 74 |
for _, digest := range append(matchingRDigests, matchingNDigests...) { |
| 75 |
if !matchesPolicyRule(digest, &policyRule) { |
| 76 |
t.Errorf("Fail: expected %s to match %#+v but it did not", fcfmt.Fmt(policyRule), digest) |
| 77 |
} |
| 78 |
} |
| 79 |
for _, digest := range append(skippingRDigests, skippingNDigests...) { |
| 80 |
if matchesPolicyRule(digest, &policyRule) { |
| 81 |
t.Errorf("Fail: expected %s to not match %#+v but it did", fcfmt.Fmt(policyRule), digest) |
| 82 |
} |
| 83 |
} |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestLiterals(t *testing.T) { |
| 89 |
ui := &user.DefaultInfo{Name: "goodu", UID: "1", |
| 90 |
Groups: []string{"goodg1", "goodg2"}} |
| 91 |
reqRN := RequestDigest{ |
| 92 |
RequestInfo: &request.RequestInfo{ |
| 93 |
IsResourceRequest: true, |
| 94 |
Path: "/apis/goodapig/v1/namespaces/goodns/goodrscs", |
| 95 |
Verb: "goodverb", |
| 96 |
APIPrefix: "apis", |
| 97 |
APIGroup: "goodapig", |
| 98 |
APIVersion: "v1", |
| 99 |
Namespace: "goodns", |
| 100 |
Resource: "goodrscs", |
| 101 |
Name: "eman", |
| 102 |
Parts: []string{"goodrscs", "eman"}, |
| 103 |
}, |
| 104 |
User: ui, |
| 105 |
} |
| 106 |
reqRU := RequestDigest{ |
| 107 |
RequestInfo: &request.RequestInfo{ |
| 108 |
IsResourceRequest: true, |
| 109 |
Path: "/apis/goodapig/v1/goodrscs", |
| 110 |
Verb: "goodverb", |
| 111 |
APIPrefix: "apis", |
| 112 |
APIGroup: "goodapig", |
| 113 |
APIVersion: "v1", |
| 114 |
Namespace: "", |
| 115 |
Resource: "goodrscs", |
| 116 |
Name: "eman", |
| 117 |
Parts: []string{"goodrscs", "eman"}, |
| 118 |
}, |
| 119 |
User: ui, |
| 120 |
} |
| 121 |
reqN := RequestDigest{ |
| 122 |
RequestInfo: &request.RequestInfo{ |
| 123 |
IsResourceRequest: false, |
| 124 |
Path: "/openapi/v2", |
| 125 |
Verb: "goodverb", |
| 126 |
}, |
| 127 |
User: ui, |
| 128 |
} |
| 129 |
checkRules(t, true, reqRN, []flowcontrol.PolicyRulesWithSubjects{{ |
| 130 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 131 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 132 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 133 |
Verbs: []string{"goodverb"}, |
| 134 |
APIGroups: []string{"goodapig"}, |
| 135 |
Resources: []string{"goodrscs"}, |
| 136 |
Namespaces: []string{"goodns"}}}}, { |
| 137 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindGroup, |
| 138 |
Group: &flowcontrol.GroupSubject{Name: "goodg1"}}}, |
| 139 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 140 |
Verbs: []string{"goodverb"}, |
| 141 |
APIGroups: []string{"goodapig"}, |
| 142 |
Resources: []string{"goodrscs"}, |
| 143 |
Namespaces: []string{"goodns"}}}}, { |
| 144 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 145 |
User: &flowcontrol.UserSubject{Name: "*"}}}, |
| 146 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 147 |
Verbs: []string{"goodverb"}, |
| 148 |
APIGroups: []string{"goodapig"}, |
| 149 |
Resources: []string{"goodrscs"}, |
| 150 |
Namespaces: []string{"goodns"}}}}, { |
| 151 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindGroup, |
| 152 |
Group: &flowcontrol.GroupSubject{Name: "*"}}}, |
| 153 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 154 |
Verbs: []string{"goodverb"}, |
| 155 |
APIGroups: []string{"goodapig"}, |
| 156 |
Resources: []string{"goodrscs"}, |
| 157 |
Namespaces: []string{"goodns"}}}}, { |
| 158 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 159 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 160 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 161 |
Verbs: []string{"*"}, |
| 162 |
APIGroups: []string{"goodapig"}, |
| 163 |
Resources: []string{"goodrscs"}, |
| 164 |
Namespaces: []string{"goodns"}}}}, { |
| 165 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 166 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 167 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 168 |
Verbs: []string{"goodverb"}, |
| 169 |
APIGroups: []string{"*"}, |
| 170 |
Resources: []string{"goodrscs"}, |
| 171 |
Namespaces: []string{"goodns"}}}}, { |
| 172 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 173 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 174 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 175 |
Verbs: []string{"goodverb"}, |
| 176 |
APIGroups: []string{"goodapig"}, |
| 177 |
Resources: []string{"*"}, |
| 178 |
Namespaces: []string{"goodns"}}}}, { |
| 179 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 180 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 181 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 182 |
Verbs: []string{"goodverb"}, |
| 183 |
APIGroups: []string{"goodapig"}, |
| 184 |
Resources: []string{"goodrscs"}, |
| 185 |
Namespaces: []string{"*"}}}}, |
| 186 |
}) |
| 187 |
checkRules(t, false, reqRN, []flowcontrol.PolicyRulesWithSubjects{{ |
| 188 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 189 |
User: &flowcontrol.UserSubject{Name: "badu"}}}, |
| 190 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 191 |
Verbs: []string{"goodverb"}, |
| 192 |
APIGroups: []string{"goodapig"}, |
| 193 |
Resources: []string{"goodrscs"}, |
| 194 |
Namespaces: []string{"goodns"}}}}, { |
| 195 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindGroup, |
| 196 |
Group: &flowcontrol.GroupSubject{Name: "badg"}}}, |
| 197 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 198 |
Verbs: []string{"goodverb"}, |
| 199 |
APIGroups: []string{"goodapig"}, |
| 200 |
Resources: []string{"goodrscs"}, |
| 201 |
Namespaces: []string{"goodns"}}}}, { |
| 202 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 203 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 204 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 205 |
Verbs: []string{"badverb"}, |
| 206 |
APIGroups: []string{"goodapig"}, |
| 207 |
Resources: []string{"goodrscs"}, |
| 208 |
Namespaces: []string{"goodns"}}}}, { |
| 209 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 210 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 211 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 212 |
Verbs: []string{"goodverb"}, |
| 213 |
APIGroups: []string{"badapig"}, |
| 214 |
Resources: []string{"goodrscs"}, |
| 215 |
Namespaces: []string{"goodns"}}}}, { |
| 216 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 217 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 218 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 219 |
Verbs: []string{"goodverb"}, |
| 220 |
APIGroups: []string{"goodapig"}, |
| 221 |
Resources: []string{"badrscs"}, |
| 222 |
Namespaces: []string{"goodns"}}}}, { |
| 223 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 224 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 225 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 226 |
Verbs: []string{"goodverb"}, |
| 227 |
APIGroups: []string{"goodapig"}, |
| 228 |
Resources: []string{"goodrscs"}, |
| 229 |
Namespaces: []string{"badns"}}}}, |
| 230 |
}) |
| 231 |
checkRules(t, true, reqRU, []flowcontrol.PolicyRulesWithSubjects{{ |
| 232 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 233 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 234 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 235 |
Verbs: []string{"goodverb"}, |
| 236 |
APIGroups: []string{"goodapig"}, |
| 237 |
Resources: []string{"goodrscs"}, |
| 238 |
ClusterScope: true}}}, { |
| 239 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 240 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 241 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 242 |
Verbs: []string{"*"}, |
| 243 |
APIGroups: []string{"goodapig"}, |
| 244 |
Resources: []string{"goodrscs"}, |
| 245 |
ClusterScope: true}}}, { |
| 246 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 247 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 248 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 249 |
Verbs: []string{"goodverb"}, |
| 250 |
APIGroups: []string{"*"}, |
| 251 |
Resources: []string{"goodrscs"}, |
| 252 |
ClusterScope: true}}}, { |
| 253 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 254 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 255 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 256 |
Verbs: []string{"goodverb"}, |
| 257 |
APIGroups: []string{"goodapig"}, |
| 258 |
Resources: []string{"*"}, |
| 259 |
ClusterScope: true}}}}) |
| 260 |
checkRules(t, false, reqRU, []flowcontrol.PolicyRulesWithSubjects{{ |
| 261 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 262 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 263 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 264 |
Verbs: []string{"badverb"}, |
| 265 |
APIGroups: []string{"goodapig"}, |
| 266 |
Resources: []string{"goodrscs"}, |
| 267 |
ClusterScope: true}}}, { |
| 268 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 269 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 270 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 271 |
Verbs: []string{"goodverb"}, |
| 272 |
APIGroups: []string{"badapig"}, |
| 273 |
Resources: []string{"goodrscs"}, |
| 274 |
ClusterScope: true}}}, { |
| 275 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 276 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 277 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 278 |
Verbs: []string{"goodverb"}, |
| 279 |
APIGroups: []string{"goodapig"}, |
| 280 |
Resources: []string{"badrscs"}, |
| 281 |
ClusterScope: true}}}, { |
| 282 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 283 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 284 |
ResourceRules: []flowcontrol.ResourcePolicyRule{{ |
| 285 |
Verbs: []string{"goodverb"}, |
| 286 |
APIGroups: []string{"goodapig"}, |
| 287 |
Resources: []string{"goodrscs"}, |
| 288 |
ClusterScope: false}}}, |
| 289 |
}) |
| 290 |
checkRules(t, true, reqN, []flowcontrol.PolicyRulesWithSubjects{{ |
| 291 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 292 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 293 |
NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 294 |
Verbs: []string{"goodverb"}, |
| 295 |
NonResourceURLs: []string{"/openapi/v2"}}}}, { |
| 296 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 297 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 298 |
NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 299 |
Verbs: []string{"*"}, |
| 300 |
NonResourceURLs: []string{"/openapi/v2"}}}}, { |
| 301 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 302 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 303 |
NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 304 |
Verbs: []string{"goodverb"}, |
| 305 |
NonResourceURLs: []string{"*"}}}}, |
| 306 |
}) |
| 307 |
checkRules(t, false, reqN, []flowcontrol.PolicyRulesWithSubjects{{ |
| 308 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 309 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 310 |
NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 311 |
Verbs: []string{"badverb"}, |
| 312 |
NonResourceURLs: []string{"/openapi/v2"}}}}, { |
| 313 |
Subjects: []flowcontrol.Subject{{Kind: flowcontrol.SubjectKindUser, |
| 314 |
User: &flowcontrol.UserSubject{Name: "goodu"}}}, |
| 315 |
NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 316 |
Verbs: []string{"goodverb"}, |
| 317 |
NonResourceURLs: []string{"/closedapi/v2"}}}}, |
| 318 |
}) |
| 319 |
} |
| 320 |
|
| 321 |
func checkRules(t *testing.T, expectMatch bool, digest RequestDigest, rules []flowcontrol.PolicyRulesWithSubjects) { |
| 322 |
for idx, rule := range rules { |
| 323 |
fs := &flowcontrol.FlowSchema{ |
| 324 |
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("rule%d", idx)}, |
| 325 |
Spec: flowcontrol.FlowSchemaSpec{ |
| 326 |
Rules: []flowcontrol.PolicyRulesWithSubjects{rule}}} |
| 327 |
actualMatch := matchesFlowSchema(digest, fs) |
| 328 |
if expectMatch != actualMatch { |
| 329 |
t.Errorf("expectMatch=%v, actualMatch=%v, digest=%#+v, fs=%s", expectMatch, actualMatch, digest, fcfmt.Fmt(fs)) |
| 330 |
} |
| 331 |
} |
| 332 |
} |