func TestEditViewRelationship(t *testing.T) { readVerbs := sets.NewString(bootstrappolicy.Read...) semanticRoles := getSemanticRoles(bootstrappolicy.ClusterRoles()) // modify the edit role rules to make then read-only for comparison against view role rules for i := range semanticRoles.edit.Rules { rule := semanticRoles.edit.Rules[i] remainingVerbs := []string{} for _, verb := range rule.Verbs { if readVerbs.Has(verb) { remainingVerbs = append(remainingVerbs, verb) } } rule.Verbs = remainingVerbs semanticRoles.edit.Rules[i] = rule } // confirm that the view role doesn't already have extra powers for _, rule := range viewEscalatingNamespaceResources { if covers, _ := rbacvalidation.Covers(semanticRoles.view.Rules, []rbac.PolicyRule{rule}); covers { t.Errorf("view has extra powers: %#v", rule) } } semanticRoles.view.Rules = append(semanticRoles.view.Rules, viewEscalatingNamespaceResources...) // at this point, we should have a two way covers relationship if covers, miss := rbacvalidation.Covers(semanticRoles.edit.Rules, semanticRoles.view.Rules); !covers { t.Errorf("edit has lost rules for: %#v", miss) } if covers, miss := rbacvalidation.Covers(semanticRoles.view.Rules, semanticRoles.edit.Rules); !covers { t.Errorf("view is missing rules for: %#v\nIf these are escalating powers, add them to the list. Otherwise, add them to the view role.", miss) } }
// Some roles should always cover others func TestCovers(t *testing.T) { semanticRoles := getSemanticRoles(bootstrappolicy.ClusterRoles()) if covers, miss := rbacvalidation.Covers(semanticRoles.admin.Rules, semanticRoles.edit.Rules); !covers { t.Errorf("failed to cover: %#v", miss) } if covers, miss := rbacvalidation.Covers(semanticRoles.admin.Rules, semanticRoles.view.Rules); !covers { t.Errorf("failed to cover: %#v", miss) } if covers, miss := rbacvalidation.Covers(semanticRoles.edit.Rules, semanticRoles.view.Rules); !covers { t.Errorf("failed to cover: %#v", miss) } }
func TestAdminEditRelationship(t *testing.T) { semanticRoles := getSemanticRoles(bootstrappolicy.ClusterRoles()) // confirm that the edit role doesn't already have extra powers for _, rule := range additionalAdminPowers { if covers, _ := rbacvalidation.Covers(semanticRoles.edit.Rules, []rbac.PolicyRule{rule}); covers { t.Errorf("edit has extra powers: %#v", rule) } } semanticRoles.edit.Rules = append(semanticRoles.edit.Rules, additionalAdminPowers...) // at this point, we should have a two way covers relationship if covers, miss := rbacvalidation.Covers(semanticRoles.admin.Rules, semanticRoles.edit.Rules); !covers { t.Errorf("admin has lost rules for: %#v", miss) } if covers, miss := rbacvalidation.Covers(semanticRoles.edit.Rules, semanticRoles.admin.Rules); !covers { t.Errorf("edit is missing rules for: %#v\nIf these should only be admin powers, add them to the list. Otherwise, add them to the edit role.", miss) } }