Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport fixes for #1049 and #1046 to 0.19.x branch #1052

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions pkg/library/status/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ func CheckPodsState(workspaceID string, namespace string, labelSelector k8sclien
if msg, err := CheckPodEvents(&pod, workspaceID, ignoredEvents, clusterAPI); err != nil || msg != "" {
return msg, err
}
if pod.Status.Phase == corev1.PodPending {
if msg := checkPodConditions(&pod); msg != "" {
return msg, nil
}
}
}
return "", nil
}
Expand Down Expand Up @@ -170,18 +165,6 @@ func checkIfUnrecoverableEventIgnored(reason string, ignoredEvents []string) (ig
return false
}

func checkPodConditions(pod *corev1.Pod) (msg string) {
if pod.Status.Conditions != nil {
for _, condition := range pod.Status.Conditions {
// Pod unschedulable condition
if condition.Type == corev1.PodScheduled && condition.Status == corev1.ConditionFalse && condition.Reason == corev1.PodReasonUnschedulable {
return fmt.Sprintf("Pod is unschedulable: %s", condition.Message)
}
}
}
return ""
}

// Returns the number of times an event has occurred.
// This function exists for the following reasons:
//
Expand Down
76 changes: 47 additions & 29 deletions webhook/workspace/handler/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"sigs.k8s.io/yaml"
)

var (
// Verbs used in SAR checks when users attempt to use a Kubernetes/OpenShift component. A longer list results in
// more SAR checks per request. We cannot use '*' as a verb as the SAR will check for literal '*' permissions
// rather than "all verbs"
userVerbs = []string{"get", "create", "update", "delete"}
)

func (h *WebhookHandler) validateKubernetesObjectPermissionsOnCreate(ctx context.Context, req admission.Request, wksp *dwv2.DevWorkspaceTemplateSpec) error {
kubeComponents := getKubeComponentsFromWorkspace(wksp)
for componentName, component := range kubeComponents {
Expand Down Expand Up @@ -97,37 +104,14 @@ func (h *WebhookHandler) validatePermissionsOnObject(ctx context.Context, req ad
// Convert e.g. Pod -> pods, Deployment -> deployments
resourceType := fmt.Sprintf("%ss", strings.ToLower(typeMeta.Kind))

sar := &authv1.LocalSubjectAccessReview{
ObjectMeta: metav1.ObjectMeta{
Namespace: req.Namespace,
},
Spec: authv1.SubjectAccessReviewSpec{
ResourceAttributes: &authv1.ResourceAttributes{
Namespace: req.Namespace,
Verb: "*",
Group: typeMeta.GroupVersionKind().Group,
Version: typeMeta.GroupVersionKind().Version,
Resource: resourceType,
},
User: req.UserInfo.Username,
Groups: req.UserInfo.Groups,
UID: req.UserInfo.UID,
},
}

err := h.Client.Create(ctx, sar)
if err != nil {
return fmt.Errorf("failed to create subjectaccessreview for request: %w", err)
}

username := req.UserInfo.Username
if username == "" {
username = req.UserInfo.UID
}
if !sar.Status.Allowed {
return fmt.Errorf("user %s does not have permissions to work with objects of kind %s defined in component %s", username, typeMeta.GroupVersionKind().String(), componentName)
// Check that user has permissions to use the resource
for _, verb := range userVerbs {
if err := h.checkSAR(ctx, req, typeMeta, resourceType, verb, componentName); err != nil {
return err
}
}

// Check that DWO has '*' permissions for the relevant resource
ssar := &authv1.LocalSubjectAccessReview{
ObjectMeta: metav1.ObjectMeta{
Namespace: req.Namespace,
Expand Down Expand Up @@ -250,3 +234,37 @@ func getKubeLikeComponent_v1alpha1(component *dwv1.Component) (*dwv1.K8sLikeComp
}
return nil, fmt.Errorf("component does not specify kubernetes or openshift fields")
}

func (h *WebhookHandler) checkSAR(ctx context.Context, req admission.Request, typeMeta *metav1.TypeMeta, resourceType, verb, componentName string) error {
sar := &authv1.LocalSubjectAccessReview{
ObjectMeta: metav1.ObjectMeta{
Namespace: req.Namespace,
},
Spec: authv1.SubjectAccessReviewSpec{
ResourceAttributes: &authv1.ResourceAttributes{
Namespace: req.Namespace,
Verb: verb,
Group: typeMeta.GroupVersionKind().Group,
Version: typeMeta.GroupVersionKind().Version,
Resource: resourceType,
},
User: req.UserInfo.Username,
Groups: req.UserInfo.Groups,
UID: req.UserInfo.UID,
},
}

err := h.Client.Create(ctx, sar)
if err != nil {
return fmt.Errorf("failed to create subjectaccessreview for request: %w", err)
}

username := req.UserInfo.Username
if username == "" {
username = req.UserInfo.UID
}
if !sar.Status.Allowed {
return fmt.Errorf("user %s does not have permissions to '%s' objects of kind %s defined in component %s", username, verb, typeMeta.GroupVersionKind().String(), componentName)
}
return nil
}