diff --git a/api/k8s/application/k8sApplicationRestHandler.go b/api/k8s/application/k8sApplicationRestHandler.go index 2fb2d1c718..277d702c20 100644 --- a/api/k8s/application/k8sApplicationRestHandler.go +++ b/api/k8s/application/k8sApplicationRestHandler.go @@ -177,9 +177,7 @@ func (handler *K8sApplicationRestHandlerImpl) GetResource(w http.ResponseWriter, request.AppIdentifier = appIdentifier request.ClusterId = request.AppIdentifier.ClusterId if request.DeploymentType == bean2.HelmInstalledType { - valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest) - if err != nil || !valid { - handler.logger.Errorw("error in validating resource request", "err", err) + if err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest); err != nil { common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -388,9 +386,7 @@ func (handler *K8sApplicationRestHandlerImpl) UpdateResource(w http.ResponseWrit request.AppIdentifier = appIdentifier request.ClusterId = appIdentifier.ClusterId if request.DeploymentType == bean2.HelmAppType { - valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest) - if err != nil || !valid { - handler.logger.Errorw("error in validating resource request", "err", err) + if err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest); err != nil { common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -490,9 +486,7 @@ func (handler *K8sApplicationRestHandlerImpl) DeleteResource(w http.ResponseWrit request.AppIdentifier = appIdentifier request.ClusterId = appIdentifier.ClusterId if request.DeploymentType == bean2.HelmInstalledType { - valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest) - if err != nil || !valid { - handler.logger.Errorw("error in validating resource request", "err", err) + if err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest); err != nil { common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -586,9 +580,7 @@ func (handler *K8sApplicationRestHandlerImpl) ListEvents(w http.ResponseWriter, request.AppIdentifier = appIdentifier request.ClusterId = appIdentifier.ClusterId if request.DeploymentType == bean2.HelmInstalledType { - valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest) - if err != nil || !valid { - handler.logger.Errorw("error in validating resource request", "err", err) + if err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest); err != nil { common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -771,18 +763,8 @@ func generatePodLogsFilename(filename string) string { func (handler *K8sApplicationRestHandlerImpl) requestValidationAndRBAC(w http.ResponseWriter, r *http.Request, token string, request *k8s.ResourceRequestBean) { if request.AppIdentifier != nil { if request.DeploymentType == bean2.HelmInstalledType { - valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest) - if err != nil || !valid { - handler.logger.Errorw("error in validating resource request", "err", err, "request.AppIdentifier", request.AppIdentifier, "request.K8sRequest", request.K8sRequest) - apiError := util2.ApiError{ - InternalMessage: "failed to validate the resource with error " + err.Error(), - UserMessage: "Failed to validate resource", - } - if !valid { - apiError.InternalMessage = "failed to validate the resource" - apiError.UserMessage = "requested Pod or Container doesn't exist" - } - common.WriteJsonResp(w, &apiError, nil, http.StatusBadRequest) + if err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest); err != nil { + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } } else if request.DeploymentType == bean2.ArgoInstalledType { diff --git a/pkg/k8s/application/k8sApplicationService.go b/pkg/k8s/application/k8sApplicationService.go index c94a3af3a8..da59604cd1 100644 --- a/pkg/k8s/application/k8sApplicationService.go +++ b/pkg/k8s/application/k8sApplicationService.go @@ -70,7 +70,7 @@ type K8sApplicationService interface { ValidateTerminalRequestQuery(r *http.Request) (*terminal.TerminalSessionRequest, *k8s.ResourceRequestBean, error) DecodeDevtronAppId(applicationId string) (*bean3.DevtronAppIdentifier, error) GetPodLogs(ctx context.Context, request *k8s.ResourceRequestBean) (io.ReadCloser, error) - ValidateResourceRequest(ctx context.Context, appIdentifier *bean.AppIdentifier, request *k8s2.K8sRequestBean) (bool, error) + ValidateResourceRequest(ctx context.Context, appIdentifier *bean.AppIdentifier, request *k8s2.K8sRequestBean) error ValidateClusterResourceRequest(ctx context.Context, clusterResourceRequest *k8s.ResourceRequestBean, rbacCallback func(clusterName string, resourceIdentifier k8s2.ResourceIdentifier) bool) (bool, error) ValidateClusterResourceBean(ctx context.Context, clusterId int, manifest unstructured.Unstructured, gvk schema.GroupVersionKind, rbacCallback func(clusterName string, resourceIdentifier k8s2.ResourceIdentifier) bool) bool @@ -442,7 +442,24 @@ func (impl *K8sApplicationServiceImpl) ValidateClusterResourceBean(ctx context.C return impl.validateResourceManifest(clusterBean.ClusterName, manifest, gvk, rbacCallback) } -func (impl *K8sApplicationServiceImpl) ValidateResourceRequest(ctx context.Context, appIdentifier *bean.AppIdentifier, request *k8s2.K8sRequestBean) (bool, error) { +func (impl *K8sApplicationServiceImpl) ValidateResourceRequest(ctx context.Context, appIdentifier *bean.AppIdentifier, request *k8s2.K8sRequestBean) error { + if valid, err := impl.validateResourceRequest(ctx, appIdentifier, request); err != nil || !valid { + if !valid { + impl.logger.Errorw("validation error in resource request", "request.AppIdentifier", appIdentifier, "request.K8sRequest", request) + err = &util.ApiError{ + HttpStatusCode: http.StatusBadRequest, + InternalMessage: "validation failed for the requested resource", + UserMessage: fmt.Sprintf("resource %s: \"%s\" doesn't exist", request.ResourceIdentifier.GroupVersionKind.Kind, request.ResourceIdentifier.Name), + } + } else if err != nil { + impl.logger.Errorw("error in validating resource request", "err", err, "request.AppIdentifier", appIdentifier, "request.K8sRequest", request) + } + return err + } + return nil +} + +func (impl *K8sApplicationServiceImpl) validateResourceRequest(ctx context.Context, appIdentifier *bean.AppIdentifier, request *k8s2.K8sRequestBean) (bool, error) { app, err := impl.helmAppService.GetApplicationDetail(ctx, appIdentifier) if err != nil { impl.logger.Errorw("error in getting app detail", "err", err, "appDetails", appIdentifier)