|
| 1 | +package hiveownership |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils" |
| 10 | + |
| 11 | + "k8s.io/api/admission/v1beta1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | +) |
| 15 | + |
| 16 | +type hiveOwnershipTestSuites struct { |
| 17 | + testName string |
| 18 | + testID string |
| 19 | + username string |
| 20 | + userGroups []string |
| 21 | + oldObject *runtime.RawExtension |
| 22 | + operation v1beta1.Operation |
| 23 | + labels map[string]string |
| 24 | + shouldBeAllowed bool |
| 25 | +} |
| 26 | + |
| 27 | +const testObjectRaw string = `{ |
| 28 | + "metadata": { |
| 29 | + "name": "%s", |
| 30 | + "uid": "%s", |
| 31 | + "creationTimestamp": "2020-05-10T07:51:00Z", |
| 32 | + "labels": %s |
| 33 | + }, |
| 34 | + "users": null |
| 35 | + }` |
| 36 | + |
| 37 | +// labelsMapToString is a helper to turn a map into a JSON fragment to be |
| 38 | +// inserted into the testNamespaceRaw const. See createRawJSONString. |
| 39 | +func labelsMapToString(labels map[string]string) string { |
| 40 | + ret, _ := json.Marshal(labels) |
| 41 | + return string(ret) |
| 42 | +} |
| 43 | + |
| 44 | +func createRawJSONString(name, uid string, labels map[string]string) string { |
| 45 | + return fmt.Sprintf(testObjectRaw, name, uid, labelsMapToString(labels)) |
| 46 | +} |
| 47 | +func createOldObject(name, uid string, labels map[string]string) *runtime.RawExtension { |
| 48 | + return &runtime.RawExtension{ |
| 49 | + Raw: []byte(createRawJSONString(name, uid, labels)), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func runTests(t *testing.T, tests []hiveOwnershipTestSuites) { |
| 54 | + gvk := metav1.GroupVersionKind{ |
| 55 | + Group: "quota.openshift.io", |
| 56 | + Version: "v1", |
| 57 | + Kind: "ClusterResourceQuota", |
| 58 | + } |
| 59 | + gvr := metav1.GroupVersionResource{ |
| 60 | + Group: "quota.openshift.io", |
| 61 | + Version: "v1", |
| 62 | + Resource: "clusterresourcequotas", |
| 63 | + } |
| 64 | + |
| 65 | + for _, test := range tests { |
| 66 | + obj := createOldObject(test.testName, test.testID, test.labels) |
| 67 | + hook := NewWebhook() |
| 68 | + httprequest, err := testutils.CreateHTTPRequest(hook.GetURI(), |
| 69 | + test.testID, |
| 70 | + gvk, gvr, test.operation, test.username, test.userGroups, obj, test.oldObject) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("Expected no error, got %s", err.Error()) |
| 73 | + } |
| 74 | + |
| 75 | + response, err := testutils.SendHTTPRequest(httprequest, hook) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Expected no error, got %s", err.Error()) |
| 78 | + } |
| 79 | + if response.UID == "" { |
| 80 | + t.Fatalf("No tracking UID associated with the response: %+v", response) |
| 81 | + } |
| 82 | + |
| 83 | + if response.Allowed != test.shouldBeAllowed { |
| 84 | + t.Fatalf("Mismatch: %s (groups=%s) %s %s. Test's expectation is that the user %s", |
| 85 | + test.username, test.userGroups, |
| 86 | + testutils.CanCanNot(response.Allowed), string(test.operation), |
| 87 | + testutils.CanCanNot(test.shouldBeAllowed)) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestThing(t *testing.T) { |
| 93 | + tests := []hiveOwnershipTestSuites{ |
| 94 | + { |
| 95 | + testID: "kube-admin-test", |
| 96 | + username: "kube:admin", |
| 97 | + userGroups: []string{"kube:system", "system:authenticated", "system:authenticated:oauth"}, |
| 98 | + operation: v1beta1.Create, |
| 99 | + shouldBeAllowed: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + testID: "sre-test", |
| 103 | + username: "sre-foo@redhat.com", |
| 104 | + userGroups: []string{adminGroups[0], "system:authenticated", "system:authenticated:oauth"}, |
| 105 | + operation: v1beta1.Update, |
| 106 | + shouldBeAllowed: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + // dedicated-admin users. This should be blocked as making changes as CU on clusterresourcequota which are managed are prohibited. |
| 110 | + testID: "dedicated-admin-test", |
| 111 | + username: "bob@foo.com", |
| 112 | + userGroups: []string{"dedicated-admins", "system:authenticated", "system:authenticated:oauth"}, |
| 113 | + operation: v1beta1.Update, |
| 114 | + labels: map[string]string{"hive.openshift.io/managed": "true"}, |
| 115 | + shouldBeAllowed: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + // no special privileges, only an authenticated user. This should be blocked as making changes on clusterresourcequota which are managed are prohibited. |
| 119 | + testID: "unpriv-update-test", |
| 120 | + username: "unpriv-user", |
| 121 | + userGroups: []string{"system:authenticated", "system:authenticated:oauth"}, |
| 122 | + operation: v1beta1.Update, |
| 123 | + labels: map[string]string{"hive.openshift.io/managed": "true"}, |
| 124 | + shouldBeAllowed: false, |
| 125 | + }, |
| 126 | + } |
| 127 | + runTests(t, tests) |
| 128 | +} |
| 129 | + |
| 130 | +func TestBadRequests(t *testing.T) { |
| 131 | + t.Skip() |
| 132 | +} |
| 133 | + |
| 134 | +func TestName(t *testing.T) { |
| 135 | + if NewWebhook().Name() == "" { |
| 136 | + t.Fatalf("Empty hook name") |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestRules(t *testing.T) { |
| 141 | + if len(NewWebhook().Rules()) == 0 { |
| 142 | + t.Log("No rules for this webhook?") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestGetURI(t *testing.T) { |
| 147 | + if NewWebhook().GetURI()[0] != '/' { |
| 148 | + t.Fatalf("Hook URI does not begin with a /") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestObjectSelector(t *testing.T) { |
| 153 | + obj := &metav1.LabelSelector{ |
| 154 | + MatchLabels: map[string]string{ |
| 155 | + "hive.openshift.io/managed": "true", |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + if !reflect.DeepEqual(NewWebhook().ObjectSelector(), obj) { |
| 160 | + t.Fatalf("hive managed resources label name is not correct.") |
| 161 | + } |
| 162 | +} |
0 commit comments