diff --git a/central/main.go b/central/main.go index 15adcbed479eb..1d936d0c07020 100644 --- a/central/main.go +++ b/central/main.go @@ -164,6 +164,7 @@ import ( "github.com/stackrox/rox/pkg/grpc/authz/user" "github.com/stackrox/rox/pkg/grpc/errors" "github.com/stackrox/rox/pkg/grpc/routes" + "github.com/stackrox/rox/pkg/httputil" "github.com/stackrox/rox/pkg/httputil/proxy" "github.com/stackrox/rox/pkg/logging" pkgMetrics "github.com/stackrox/rox/pkg/metrics" @@ -754,14 +755,9 @@ func customRoutes() (customRoutes []routes.CustomRoute) { } func notImplementedOnManagedServices(fn http.Handler) http.Handler { - if !env.ManagedCentral.BooleanSetting() { - return fn - } - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - errMsg := "api is not supported in a managed central environment." - log.Error(errMsg) - http.Error(w, errMsg, http.StatusNotImplemented) - }) + return utils.IfThenElse[http.Handler]( + env.ManagedCentral.BooleanSetting(), httputil.NotImplementedHandler("api is not supported in a managed central environment."), + fn) } func debugRoutes() []routes.CustomRoute { diff --git a/central/probeupload/service/service_impl.go b/central/probeupload/service/service_impl.go index c669d6381b49a..030f251edcf03 100644 --- a/central/probeupload/service/service_impl.go +++ b/central/probeupload/service/service_impl.go @@ -14,6 +14,7 @@ import ( "github.com/stackrox/rox/central/role/resources" v1 "github.com/stackrox/rox/generated/api/v1" "github.com/stackrox/rox/pkg/auth/permissions" + "github.com/stackrox/rox/pkg/env" "github.com/stackrox/rox/pkg/grpc/authz" "github.com/stackrox/rox/pkg/grpc/authz/idcheck" "github.com/stackrox/rox/pkg/grpc/authz/perrpc" @@ -22,6 +23,7 @@ import ( "github.com/stackrox/rox/pkg/httputil" "github.com/stackrox/rox/pkg/logging" "github.com/stackrox/rox/pkg/probeupload" + "github.com/stackrox/rox/pkg/utils" "google.golang.org/grpc" "google.golang.org/grpc/codes" ) @@ -77,10 +79,12 @@ func (s *service) GetExistingProbes(ctx context.Context, req *v1.GetExistingProb func (s *service) CustomRoutes() []routes.CustomRoute { return []routes.CustomRoute{ { - Route: "/api/extensions/probeupload", - Authorizer: user.With(permissions.Modify(resources.ProbeUpload)), - ServerHandler: http.HandlerFunc(s.handleProbeUpload), - Compression: false, + Route: "/api/extensions/probeupload", + Authorizer: user.With(permissions.Modify(resources.ProbeUpload)), + ServerHandler: utils.IfThenElse[http.Handler]( + env.EnableKernelPackageUpload.BooleanSetting(), http.HandlerFunc(s.handleProbeUpload), + httputil.NotImplementedHandler("api is not supported because kernel package upload is disabled.")), + Compression: false, }, { Route: "/kernel-objects/", diff --git a/image/templates/helm/stackrox-central/templates/01-central-13-deployment.yaml b/image/templates/helm/stackrox-central/templates/01-central-13-deployment.yaml index ff5e82d3c7e2a..c66da004aea62 100644 --- a/image/templates/helm/stackrox-central/templates/01-central-13-deployment.yaml +++ b/image/templates/helm/stackrox-central/templates/01-central-13-deployment.yaml @@ -109,6 +109,8 @@ spec: value: "true" - name: ROX_ENABLE_CENTRAL_DIAGNOSTICS value: "false" + - name: ROX_ENABLE_KERNEL_PACKAGE_UPLOAD + value: "false" {{- end }} {{- if ._rox.central.db.enabled }} - name: ROX_POSTGRES_DATASTORE diff --git a/pkg/env/kernel_package_upload.go b/pkg/env/kernel_package_upload.go new file mode 100644 index 0000000000000..001c5eccc9988 --- /dev/null +++ b/pkg/env/kernel_package_upload.go @@ -0,0 +1,6 @@ +package env + +var ( + // EnableKernelPackageUpload is set to true to signal that kernel support package uploads should be supported. + EnableKernelPackageUpload = RegisterBooleanSetting("ROX_ENABLE_KERNEL_PACKAGE_UPLOAD", true) +) diff --git a/pkg/httputil/handler.go b/pkg/httputil/handler.go index 876af2a30142d..a725cb1e5f176 100644 --- a/pkg/httputil/handler.go +++ b/pkg/httputil/handler.go @@ -1,6 +1,8 @@ package httputil -import "net/http" +import ( + "net/http" +) // WrapHandlerFunc wraps a function returning an error into an HTTP handler func that returns a 200 OK with empty // contents upon success, and sends an error formatted according to `WriteError` to the client otherwise. @@ -13,3 +15,11 @@ func WrapHandlerFunc(handlerFn func(req *http.Request) error) http.HandlerFunc { } }) } + +// NotImplementedHandler returns an HTTP Handler func that returns 501 Not Implemented with a custom error message. +func NotImplementedHandler(errMsg string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + log.Error(errMsg) + http.Error(w, errMsg, http.StatusNotImplemented) + }) +} diff --git a/pkg/utils/if_then_else.go b/pkg/utils/if_then_else.go new file mode 100644 index 0000000000000..e5e1ba12e8b61 --- /dev/null +++ b/pkg/utils/if_then_else.go @@ -0,0 +1,9 @@ +package utils + +// IfThenElse is a ternary operator function that will return `a` if `cond` is true, otherwise it will return `b` +func IfThenElse[T any](cond bool, a, b T) T { + if cond { + return a + } + return b +}