Skip to content

add public-viewer support to SpaceLister's List #452

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

Merged
merged 14 commits into from
Aug 26, 2024
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
39 changes: 31 additions & 8 deletions pkg/proxy/handlers/spacelister_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
"net/http"
"time"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/registration-service/pkg/context"
"github.com/codeready-toolchain/registration-service/pkg/proxy/metrics"
"github.com/labstack/echo/v4"
errs "github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/registration-service/pkg/context"
"github.com/codeready-toolchain/registration-service/pkg/proxy/metrics"
"github.com/codeready-toolchain/registration-service/pkg/signup"
)

func HandleSpaceListRequest(spaceLister *SpaceLister) echo.HandlerFunc {
return func(ctx echo.Context) error {
// list all user workspaces
requestReceivedTime := ctx.Get(context.RequestReceivedTime).(time.Time)
ctx.Set(context.PublicViewerEnabled, false) // disable public-viewer on list endpoint
workspaces, err := ListUserWorkspaces(ctx, spaceLister)
if err != nil {
spaceLister.ProxyMetrics.RegServWorkspaceHistogramVec.WithLabelValues(fmt.Sprintf("%d", http.StatusInternalServerError), metrics.MetricsLabelVerbList).Observe(time.Since(requestReceivedTime).Seconds()) // using list as the default value for verb to minimize label combinations for prometheus to process
Expand All @@ -41,17 +45,33 @@
if signup == nil {
return []toolchainv1alpha1.Workspace{}, nil
}
murName := signup.CompliantUsername

// get MUR Names
murNames := getMURNamesForList(ctx, signup)

// get all spacebindings with given mur since no workspace was provided
spaceBindings, err := listSpaceBindingsForUser(spaceLister, murName)
spaceBindings, err := listSpaceBindingsForUsers(spaceLister, murNames)
if err != nil {
ctx.Logger().Error(errs.Wrap(err, "error listing space bindings"))
return nil, err
}

return workspacesFromSpaceBindings(ctx, spaceLister, signup.Name, spaceBindings), nil
}

// getMURNamesForList returns a list of MasterUserRecord names to use for listing Workspaces.
// If PublicViewer is enabled, the list will also contain the PublicViewer username.
func getMURNamesForList(ctx echo.Context, signup *signup.Signup) []string {
names := []string{}
if signup != nil && signup.CompliantUsername != "" {
names = append(names, signup.CompliantUsername)
}
if context.IsPublicViewerEnabled(ctx) {
names = append(names, toolchainv1alpha1.KubesawAuthenticatedUsername)
}
return names
}

func listWorkspaceResponse(ctx echo.Context, workspaces []toolchainv1alpha1.Workspace) error {
workspaceList := &toolchainv1alpha1.WorkspaceList{
TypeMeta: metav1.TypeMeta{
Expand All @@ -66,9 +86,12 @@
return json.NewEncoder(ctx.Response().Writer).Encode(workspaceList)
}

func listSpaceBindingsForUser(spaceLister *SpaceLister, murName string) ([]toolchainv1alpha1.SpaceBinding, error) {
mrr, _ := labels.SelectorFromSet(labels.Set{toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey: murName}).Requirements()
return spaceLister.GetInformerServiceFunc().ListSpaceBindings(mrr[0])
func listSpaceBindingsForUsers(spaceLister *SpaceLister, murNames []string) ([]toolchainv1alpha1.SpaceBinding, error) {
murSelector, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey, selection.In, murNames)
if err != nil {
return nil, err

Check warning on line 92 in pkg/proxy/handlers/spacelister_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/proxy/handlers/spacelister_list.go#L92

Added line #L92 was not covered by tests
}
return spaceLister.GetInformerServiceFunc().ListSpaceBindings(*murSelector)
}

func workspacesFromSpaceBindings(ctx echo.Context, spaceLister *SpaceLister, signupName string, spaceBindings []toolchainv1alpha1.SpaceBinding) []toolchainv1alpha1.Workspace {
Expand Down
Loading
Loading