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