Skip to content
23 changes: 12 additions & 11 deletions central/alert/service/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/stackrox/rox/pkg/auth/permissions"
"github.com/stackrox/rox/pkg/batcher"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/grpc/authz"
"github.com/stackrox/rox/pkg/grpc/authz/perrpc"
"github.com/stackrox/rox/pkg/grpc/authz/user"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (s *serviceImpl) GetAlert(ctx context.Context, request *v1.ResourceByID) (*
return nil, err
}
if !exists {
return nil, errors.Wrapf(errorhelpers.ErrNotFound, "alert with id '%s' does not exist", request.GetId())
return nil, errors.Wrapf(errox.NotFound, "alert with id '%s' does not exist", request.GetId())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to replace such cases with

Suggested change
return nil, errors.Wrapf(errox.NotFound, "alert with id '%s' does not exist", request.GetId())
return nil, errox.NotFound.Newf("alert with id '%s' does not exist", request.GetId())

?
That would remove the useless "not found" from the resulting error message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x656b694d Thank you for the suggestion. We want to make changes in several PRs so that it's easier to review them. This PR is the simple removal of pkg/errorhelpers/errors.go. Next will be removing helper (wrapper) functions for CausedBy. After that, we can start with further polishing. We can decide if we want to keep the message prefix or remove it.

}

return alert, nil
Expand All @@ -128,7 +129,7 @@ func (s *serviceImpl) CountAlerts(ctx context.Context, request *v1.RawQuery) (*v
// Fill in Query.
parsedQuery, err := search.ParseQuery(request.GetQuery(), search.MatchAllIfEmpty())
if err != nil {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, err.Error())
return nil, errors.Wrap(errox.InvalidArgs, err.Error())
}

count, err := s.dataStore.Count(ctx, parsedQuery)
Expand Down Expand Up @@ -179,7 +180,7 @@ func (s *serviceImpl) GetAlertsCounts(ctx context.Context, request *v1.GetAlerts
return response, nil
}

return nil, errors.Wrapf(errorhelpers.ErrInvalidArgs, "unknown group by: %v", request.GetGroupBy())
return nil, errors.Wrapf(errox.InvalidArgs, "unknown group by: %v", request.GetGroupBy())
}

// GetAlertTimeseries returns the timeseries format of the events based on the request parameters
Expand All @@ -202,7 +203,7 @@ func (s *serviceImpl) ResolveAlert(ctx context.Context, req *v1.ResolveAlertRequ
return nil, err
}
if !exists {
return nil, errors.Wrapf(errorhelpers.ErrNotFound, "alert with id '%s' does not exist", req.GetId())
return nil, errors.Wrapf(errox.NotFound, "alert with id '%s' does not exist", req.GetId())
}

if req.GetWhitelist() || req.GetAddToBaseline() {
Expand Down Expand Up @@ -254,7 +255,7 @@ func (s *serviceImpl) ResolveAlerts(ctx context.Context, req *v1.ResolveAlertsRe
query, err := search.ParseQuery(req.GetQuery())
if err != nil {
log.Error(err)
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, err.Error())
return nil, errors.Wrap(errox.InvalidArgs, err.Error())
}
runtimeQuery := search.NewQueryBuilder().AddStrings(search.LifecycleStage, storage.LifecycleStage_RUNTIME.String()).ProtoQuery()
cq := search.ConjunctionQuery(query, runtimeQuery)
Expand Down Expand Up @@ -337,18 +338,18 @@ func (s *serviceImpl) changeAlertState(ctx context.Context, alert *storage.Alert

func (s *serviceImpl) SnoozeAlert(ctx context.Context, req *v1.SnoozeAlertRequest) (*v1.Empty, error) {
if req.GetSnoozeTill() == nil {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, "'snooze_till' cannot be nil")
return nil, errors.Wrap(errox.InvalidArgs, "'snooze_till' cannot be nil")
}
if protoconv.ConvertTimestampToTimeOrNow(req.GetSnoozeTill()).Before(time.Now()) {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, badSnoozeErrorMsg)
return nil, errors.Wrap(errox.InvalidArgs, badSnoozeErrorMsg)
}
alert, exists, err := s.dataStore.GetAlert(ctx, req.GetId())
if err != nil {
log.Error(err)
return nil, err
}
if !exists {
return nil, errors.Wrapf(errorhelpers.ErrNotFound, "alert with id '%s' does not exist", req.GetId())
return nil, errors.Wrapf(errox.NotFound, "alert with id '%s' does not exist", req.GetId())
}
alert.SnoozeTill = req.GetSnoozeTill()
err = s.changeAlertState(ctx, alert, storage.ViolationState_SNOOZED)
Expand All @@ -367,7 +368,7 @@ func (s *serviceImpl) DeleteAlerts(ctx context.Context, request *v1.DeleteAlerts

query, err := search.ParseQuery(request.GetQuery().GetQuery(), search.MatchAllIfEmpty())
if err != nil {
return nil, errors.Wrapf(errorhelpers.ErrInvalidArgs, "error parsing query: %v", err)
return nil, errors.Wrapf(errox.InvalidArgs, "error parsing query: %v", err)
}
paginated.FillPagination(query, request.GetQuery().GetPagination(), math.MaxInt32)

Expand All @@ -379,7 +380,7 @@ func (s *serviceImpl) DeleteAlerts(ctx context.Context, request *v1.DeleteAlerts
}
if matchFieldQuery.MatchFieldQuery.GetField() == search.ViolationState.String() {
if matchFieldQuery.MatchFieldQuery.Value != storage.ViolationState_RESOLVED.String() {
err = errors.Wrapf(errorhelpers.ErrInvalidArgs, "invalid value for violation state: %q. Only resolved alerts can be deleted", matchFieldQuery.MatchFieldQuery.Value)
err = errors.Wrapf(errox.InvalidArgs, "invalid value for violation state: %q. Only resolved alerts can be deleted", matchFieldQuery.MatchFieldQuery.Value)
return
}
specified = true
Expand All @@ -389,7 +390,7 @@ func (s *serviceImpl) DeleteAlerts(ctx context.Context, request *v1.DeleteAlerts
return nil, err
}
if !specified {
return nil, errors.Wrapf(errorhelpers.ErrInvalidArgs, "please specify Violation State:%s in the query to confirm deletion", storage.ViolationState_RESOLVED.String())
return nil, errors.Wrapf(errox.InvalidArgs, "please specify Violation State:%s in the query to confirm deletion", storage.ViolationState_RESOLVED.String())
}

results, err := s.dataStore.Search(ctx, query)
Expand Down
8 changes: 4 additions & 4 deletions central/alert/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
baselineMocks "github.com/stackrox/rox/central/processbaseline/datastore/mocks"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/search"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *getAlertTests) TestGetAlertWhenAlertIsMissing() {

result, err := s.service.GetAlert(fakeContext, s.fakeResourceByIDRequest)

s.EqualError(err, errors.Wrapf(errorhelpers.ErrNotFound, "alert with id '%s' does not exist", alerttest.FakeAlertID).Error())
s.EqualError(err, errors.Wrapf(errox.NotFound, "alert with id '%s' does not exist", alerttest.FakeAlertID).Error())
s.Equal((*storage.Alert)(nil), result)
}

Expand Down Expand Up @@ -852,7 +852,7 @@ func (s *getAlertsCountsTests) TestGetAlertsCountsWhenTheGroupIsUnknown() {
Query: "",
}, GroupBy: unknownGroupBy})

s.EqualError(err, errors.Wrapf(errorhelpers.ErrInvalidArgs, "unknown group by: %v", unknownGroupBy).Error())
s.EqualError(err, errors.Wrapf(errox.InvalidArgs, "unknown group by: %v", unknownGroupBy).Error())
s.Equal((*v1.GetAlertsCountsResponse)(nil), result)
}

Expand Down Expand Up @@ -1057,7 +1057,7 @@ func (s *patchAlertTests) TestSnoozeAlertWithSnoozeTillInThePast() {
snoozeTill, err := types.TimestampProto(time.Now().Add(-1 * time.Hour))
s.NoError(err)
_, err = s.service.SnoozeAlert(context.Background(), &v1.SnoozeAlertRequest{Id: alerttest.FakeAlertID, SnoozeTill: snoozeTill})
s.EqualError(err, errors.Wrap(errorhelpers.ErrInvalidArgs, badSnoozeErrorMsg).Error())
s.EqualError(err, errors.Wrap(errox.InvalidArgs, badSnoozeErrorMsg).Error())
}

func (s *patchAlertTests) TestResolveAlert() {
Expand Down
11 changes: 5 additions & 6 deletions central/apitoken/service/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/auth/permissions"
"github.com/stackrox/rox/pkg/auth/permissions/utils"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/grpc/authn"
"github.com/stackrox/rox/pkg/grpc/authz"
Expand Down Expand Up @@ -43,14 +42,14 @@ type serviceImpl struct {

func (s *serviceImpl) GetAPIToken(ctx context.Context, req *v1.ResourceByID) (*storage.TokenMetadata, error) {
if req.GetId() == "" {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, "empty id passed")
return nil, errors.Wrap(errox.InvalidArgs, "empty id passed")
}
token, err := s.backend.GetTokenOrNil(ctx, req.GetId())
if err != nil {
return nil, errors.Errorf("token retrieval failed: %s", err)
}
if token == nil {
return nil, errors.Wrapf(errorhelpers.ErrInvalidArgs, "token with id '%s' does not exist", req.GetId())
return nil, errors.Wrapf(errox.InvalidArgs, "token with id '%s' does not exist", req.GetId())
}
return token, nil
}
Expand Down Expand Up @@ -78,12 +77,12 @@ func (s *serviceImpl) RevokeToken(ctx context.Context, req *v1.ResourceByID) (*v

func (s *serviceImpl) GenerateToken(ctx context.Context, req *v1.GenerateTokenRequest) (*v1.GenerateTokenResponse, error) {
if req.GetName() == "" {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, "token name cannot be empty")
return nil, errors.Wrap(errox.InvalidArgs, "token name cannot be empty")
}

if req.GetRole() != "" {
if len(req.GetRoles()) > 0 {
return nil, errors.Wrap(errorhelpers.ErrInvalidArgs, "must use either role or roles, but not both")
return nil, errors.Wrap(errox.InvalidArgs, "must use either role or roles, but not both")
}
req.Roles = []string{req.GetRole()}
req.Role = ""
Expand All @@ -94,7 +93,7 @@ func (s *serviceImpl) GenerateToken(ctx context.Context, req *v1.GenerateTokenRe
return nil, errors.Wrap(err, "unable to fetch roles")
}
if len(missingIndices) > 0 {
return nil, errors.Wrapf(errorhelpers.ErrInvalidArgs, "role(s) %s don't exist", strings.Join(sliceutils.StringSelect(req.GetRoles(), missingIndices...), ","))
return nil, errors.Wrapf(errox.InvalidArgs, "role(s) %s don't exist", strings.Join(sliceutils.StringSelect(req.GetRoles(), missingIndices...), ","))
}

id, err := authn.IdentityFromContext(ctx)
Expand Down
30 changes: 15 additions & 15 deletions central/authprovider/service/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stackrox/rox/pkg/auth/authproviders/basic"
"github.com/stackrox/rox/pkg/auth/authproviders/idputil"
"github.com/stackrox/rox/pkg/auth/permissions"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/grpc/authz"
"github.com/stackrox/rox/pkg/grpc/authz/allow"
"github.com/stackrox/rox/pkg/grpc/authz/perrpc"
Expand Down Expand Up @@ -70,11 +70,11 @@ func (s *serviceImpl) AuthFuncOverride(ctx context.Context, fullMethodName strin
// GetAuthProvider retrieves the authProvider based on the id passed
func (s *serviceImpl) GetAuthProvider(_ context.Context, request *v1.GetAuthProviderRequest) (*storage.AuthProvider, error) {
if request.GetId() == "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider id is required")
return nil, errox.NewErrInvalidArgs("auth provider id is required")
}
authProvider := s.registry.GetProvider(request.GetId())
if authProvider == nil {
return nil, errors.Wrapf(errorhelpers.ErrNotFound, "auth provider %q not found", request.GetId())
return nil, errors.Wrapf(errox.NotFound, "auth provider %q not found", request.GetId())
}
return authProvider.StorageView(), nil
}
Expand Down Expand Up @@ -158,37 +158,37 @@ func (s *serviceImpl) GetAuthProviders(_ context.Context, request *v1.GetAuthPro
func (s *serviceImpl) PostAuthProvider(ctx context.Context, request *v1.PostAuthProviderRequest) (*storage.AuthProvider, error) {
providerReq := request.GetProvider()
if providerReq.GetName() == "" {
return nil, errorhelpers.NewErrInvalidArgs("no auth provider name specified")
return nil, errox.NewErrInvalidArgs("no auth provider name specified")
}
if providerReq.GetId() != "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider id must be empty")
return nil, errox.NewErrInvalidArgs("auth provider id must be empty")
}
if providerReq.GetLoginUrl() != "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider loginUrl field must be empty")
return nil, errox.NewErrInvalidArgs("auth provider loginUrl field must be empty")
}

provider, err := s.registry.CreateProvider(ctx, authproviders.WithStorageView(providerReq), authproviders.WithValidateCallback(datastore.Singleton()))
if err != nil {
return nil, errors.Wrap(errorhelpers.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
}
return provider.StorageView(), nil
}

func (s *serviceImpl) PutAuthProvider(ctx context.Context, request *storage.AuthProvider) (*storage.AuthProvider, error) {
if request.GetId() == "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider id must not be empty")
return nil, errox.NewErrInvalidArgs("auth provider id must not be empty")
}

provider := s.registry.GetProvider(request.GetId())
if provider == nil {
return nil, errorhelpers.NewErrInvalidArgs(fmt.Sprintf("auth provider with id %q does not exist", request.GetId()))
return nil, errox.NewErrInvalidArgs(fmt.Sprintf("auth provider with id %q does not exist", request.GetId()))
}

// Attempt to merge configs.
request.Config = provider.MergeConfigInto(request.GetConfig())

if err := s.registry.ValidateProvider(ctx, authproviders.WithStorageView(request)); err != nil {
return nil, errorhelpers.NewErrInvalidArgs(fmt.Sprintf("auth provider validation check failed: %v", err))
return nil, errox.NewErrInvalidArgs(fmt.Sprintf("auth provider validation check failed: %v", err))
}

// This will not log anyone out as the provider was not validated and thus no one has ever logged into it
Expand All @@ -198,14 +198,14 @@ func (s *serviceImpl) PutAuthProvider(ctx context.Context, request *storage.Auth

provider, err := s.registry.CreateProvider(ctx, authproviders.WithStorageView(request), authproviders.WithValidateCallback(datastore.Singleton()))
if err != nil {
return nil, errors.Wrap(errorhelpers.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
}
return provider.StorageView(), nil
}

func (s *serviceImpl) UpdateAuthProvider(ctx context.Context, request *v1.UpdateAuthProviderRequest) (*storage.AuthProvider, error) {
if request.GetId() == "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider id must not be empty")
return nil, errox.NewErrInvalidArgs("auth provider id must not be empty")
}

var options []authproviders.ProviderOption
Expand All @@ -217,21 +217,21 @@ func (s *serviceImpl) UpdateAuthProvider(ctx context.Context, request *v1.Update
}
provider, err := s.registry.UpdateProvider(ctx, request.GetId(), options...)
if err != nil {
return nil, errors.Wrap(errorhelpers.NewErrInvalidArgs(err.Error()), "updating auth provider")
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "updating auth provider")
}
return provider.StorageView(), nil
}

// DeleteAuthProvider deletes an auth provider from the system
func (s *serviceImpl) DeleteAuthProvider(ctx context.Context, request *v1.ResourceByID) (*v1.Empty, error) {
if request.GetId() == "" {
return nil, errorhelpers.NewErrInvalidArgs("auth provider id is required")
return nil, errox.NewErrInvalidArgs("auth provider id is required")
}

// Get auth provider.
authProvider := s.registry.GetProvider(request.GetId())
if authProvider == nil {
return nil, errors.Wrapf(errorhelpers.ErrNotFound, "auth provider %q not found", request.GetId())
return nil, errors.Wrapf(errox.NotFound, "auth provider %q not found", request.GetId())
}
// Delete auth provider.
if err := s.registry.DeleteProvider(ctx, request.GetId(), true); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions central/certgen/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stackrox/rox/central/clusters/zip"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/apiparams"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/httputil"
"github.com/stackrox/rox/pkg/images/defaults"
"github.com/stackrox/rox/pkg/renderer"
Expand All @@ -26,15 +26,15 @@ func (s *serviceImpl) getSensorCerts(r *http.Request) ([]byte, *storage.Cluster,

clusterID := params.ID
if clusterID == "" {
return nil, nil, errors.Wrap(errorhelpers.ErrInvalidArgs, "no cluster ID specified")
return nil, nil, errors.Wrap(errox.InvalidArgs, "no cluster ID specified")
}

cluster, _, err := s.clusters.GetCluster(r.Context(), clusterID)
if err != nil {
return nil, nil, errors.Errorf("failed to retrieve cluster: %v", err)
}
if cluster == nil {
return nil, nil, errors.Wrapf(errorhelpers.ErrNotFound, "cluster with ID %q not found", clusterID)
return nil, nil, errors.Wrapf(errox.NotFound, "cluster with ID %q not found", clusterID)
}

certs, err := zip.GenerateCertsAndAddToZip(nil, cluster, s.serviceIdentities)
Expand Down
5 changes: 3 additions & 2 deletions central/cluster/datastore/datastore_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
clusterValidation "github.com/stackrox/rox/pkg/cluster"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/images/defaults"
"github.com/stackrox/rox/pkg/protoconv"
"github.com/stackrox/rox/pkg/sac"
Expand Down Expand Up @@ -926,7 +927,7 @@ func (ds *datastoreImpl) LookupOrCreateClusterFromConfig(ctx context.Context, cl

func normalizeCluster(cluster *storage.Cluster) error {
if cluster == nil {
return errorhelpers.NewErrInvariantViolation("cannot normalize nil cluster object")
return errox.NewErrInvariantViolation("cannot normalize nil cluster object")
}

cluster.CentralApiEndpoint = strings.TrimPrefix(cluster.GetCentralApiEndpoint(), "https://")
Expand All @@ -944,7 +945,7 @@ func validateInput(cluster *storage.Cluster) error {
// `cluster.* bool` flags remain untouched.
func addDefaults(cluster *storage.Cluster) error {
if cluster == nil {
return errorhelpers.NewErrInvariantViolation("cannot enrich nil cluster object")
return errox.NewErrInvariantViolation("cannot enrich nil cluster object")
}
// For backwards compatibility reasons, if Collection Method is not set then honor defaults for runtime support
if cluster.GetCollectionMethod() == storage.CollectionMethod_UNSET_COLLECTION {
Expand Down
Loading