|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 api |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/julienschmidt/httprouter" |
| 25 | + kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1" |
| 26 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 29 | + |
| 30 | + "github.com/kubeflow/notebooks/workspaces/backend/internal/auth" |
| 31 | + "github.com/kubeflow/notebooks/workspaces/backend/internal/helper" |
| 32 | + models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/workspacekinds/assets" |
| 33 | + repository "github.com/kubeflow/notebooks/workspaces/backend/internal/repositories/workspacekinds" |
| 34 | +) |
| 35 | + |
| 36 | +// getWorkspaceKindAssetHandler is a helper function that handles common logic for retrieving |
| 37 | +// and serving workspace kind assets (icon or logo). It validates path parameters, performs |
| 38 | +// authentication, retrieves the asset, and serves it. |
| 39 | +func (a *App) getWorkspaceKindAssetHandler( |
| 40 | + w http.ResponseWriter, |
| 41 | + r *http.Request, |
| 42 | + ps httprouter.Params, |
| 43 | + getAsset func(icon, logo models.WorkspaceKindAsset) models.WorkspaceKindAsset, |
| 44 | +) { |
| 45 | + name := ps.ByName(ResourceNamePathParam) |
| 46 | + |
| 47 | + // validate path parameters |
| 48 | + var valErrs field.ErrorList |
| 49 | + valErrs = append(valErrs, helper.ValidateFieldIsDNS1123Subdomain(field.NewPath(ResourceNamePathParam), name)...) |
| 50 | + if len(valErrs) > 0 { |
| 51 | + a.failedValidationResponse(w, r, errMsgPathParamsInvalid, valErrs, nil) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + // =========================== AUTH =========================== |
| 56 | + authPolicies := []*auth.ResourcePolicy{ |
| 57 | + auth.NewResourcePolicy( |
| 58 | + auth.ResourceVerbGet, |
| 59 | + &kubefloworgv1beta1.WorkspaceKind{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{Name: name}, |
| 61 | + }, |
| 62 | + ), |
| 63 | + } |
| 64 | + if success := a.requireAuth(w, r, authPolicies); !success { |
| 65 | + return |
| 66 | + } |
| 67 | + // ============================================================ |
| 68 | + |
| 69 | + // Get both assets using the helper function |
| 70 | + icon, logo, err := a.repositories.WorkspaceKind.GetWorkspaceKindAssets(r.Context(), name) |
| 71 | + if err != nil { |
| 72 | + if errors.Is(err, repository.ErrWorkspaceKindNotFound) { |
| 73 | + a.notFoundResponse(w, r) |
| 74 | + return |
| 75 | + } |
| 76 | + a.serverErrorResponse(w, r, err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Get the appropriate asset (icon or logo) using the provided function |
| 81 | + asset := getAsset(icon, logo) |
| 82 | + |
| 83 | + // Serve the asset |
| 84 | + a.serveWorkspaceKindAsset(w, r, asset) |
| 85 | +} |
| 86 | + |
| 87 | +// GetWorkspaceKindIconHandler serves the icon image for a WorkspaceKind. |
| 88 | +// |
| 89 | +// @Summary Get workspace kind icon |
| 90 | +// @Description Returns the icon image for a specific workspace kind. If the icon is stored in a ConfigMap, it serves the image content. If the icon is a remote URL, returns 404 (browser should fetch directly). |
| 91 | +// @Tags workspacekinds |
| 92 | +// @ID getWorkspaceKindIcon |
| 93 | +// @Accept json |
| 94 | +// @Produce image/svg+xml |
| 95 | +// @Param name path string true "Name of the workspace kind" |
| 96 | +// @Success 200 {string} string "SVG image content" |
| 97 | +// @Failure 404 {object} ErrorEnvelope "Not Found. Icon uses remote URL or resource does not exist." |
| 98 | +// @Failure 500 {object} ErrorEnvelope "Internal server error." |
| 99 | +// @Router /workspacekinds/{name}/assets/icon.svg [get] |
| 100 | +func (a *App) GetWorkspaceKindIconHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 101 | + a.getWorkspaceKindAssetHandler(w, r, ps, func(icon, _ models.WorkspaceKindAsset) models.WorkspaceKindAsset { |
| 102 | + return icon |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +// GetWorkspaceKindLogoHandler serves the logo image for a WorkspaceKind. |
| 107 | +// |
| 108 | +// @Summary Get workspace kind logo |
| 109 | +// @Description Returns the logo image for a specific workspace kind. If the logo is stored in a ConfigMap, it serves the image content. If the logo is a remote URL, returns 404 (browser should fetch directly). |
| 110 | +// @Tags workspacekinds |
| 111 | +// @ID getWorkspaceKindLogo |
| 112 | +// @Accept json |
| 113 | +// @Produce image/svg+xml |
| 114 | +// @Param name path string true "Name of the workspace kind" |
| 115 | +// @Success 200 {string} string "SVG image content" |
| 116 | +// @Failure 404 {object} ErrorEnvelope "Not Found. Logo uses remote URL or resource does not exist." |
| 117 | +// @Failure 500 {object} ErrorEnvelope "Internal server error." |
| 118 | +// @Router /workspacekinds/{name}/assets/logo.svg [get] |
| 119 | +func (a *App) GetWorkspaceKindLogoHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 120 | + a.getWorkspaceKindAssetHandler(w, r, ps, func(_, logo models.WorkspaceKindAsset) models.WorkspaceKindAsset { |
| 121 | + return logo |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +// serveWorkspaceKindAsset serves an icon or logo asset from a WorkspaceKind. |
| 126 | +// If the asset uses a remote URL, it returns 404 (browser should fetch directly). |
| 127 | +// If the asset uses a ConfigMap, it retrieves and serves the content with proper headers. |
| 128 | +func (a *App) serveWorkspaceKindAsset(w http.ResponseWriter, r *http.Request, asset models.WorkspaceKindAsset) { |
| 129 | + // If URL is set, return 404 - browser should fetch directly from source |
| 130 | + if asset.URL != nil && *asset.URL != "" { |
| 131 | + a.notFoundResponse(w, r) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + // If ConfigMap is not set, return 404 |
| 136 | + if asset.ConfigMap == nil { |
| 137 | + a.notFoundResponse(w, r) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + imageContent, err := a.repositories.WorkspaceKind.GetConfigMapContent(r.Context(), asset) |
| 142 | + if err != nil { |
| 143 | + // TODO: determine how to handle the CONFIGMAP_MISSING error |
| 144 | + if apierrors.IsNotFound(err) { |
| 145 | + a.notFoundResponse(w, r) |
| 146 | + return |
| 147 | + } |
| 148 | + a.serverErrorResponse(w, r, fmt.Errorf("error retrieving ConfigMap content: %w", err)) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + // Write the SVG response |
| 153 | + a.imageResponse(w, r, []byte(imageContent)) |
| 154 | +} |
0 commit comments