Skip to content

Commit 3bbdfd7

Browse files
authored
ROX-10303: Replace errox wrappers (#1389)
* Remove NewErrNoCredentials * Remove NewErrInvariantViolation * Remove NewErrNotAuthorized * Remove NewErrInvalidArgs * Fix regex for roxctl wrapcheck linter * Updated error messages - review comment discussion_r859310460
1 parent 8e91e10 commit 3bbdfd7

File tree

36 files changed

+61
-81
lines changed

36 files changed

+61
-81
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ linters-settings:
8080
wrapcheck:
8181
ignoreSigRegexps:
8282
- utils\.Should
83-
- errox\.New.*
83+
- errox\..+\.CausedBy(f)?
8484
- retry\.MakeRetryable
8585
- policy\.NewErr.*
8686

central/apitoken/service/service_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *serviceImpl) GenerateToken(ctx context.Context, req *v1.GenerateTokenRe
101101
return nil, err
102102
}
103103
if err := verifyNoPrivilegeEscalation(id.Roles(), roles); err != nil {
104-
return nil, errox.NewErrNotAuthorized(err.Error())
104+
return nil, errox.NotAuthorized.CausedBy(err.Error())
105105
}
106106

107107
token, metadata, err := s.backend.IssueRoleToken(ctx, req.GetName(), utils.RoleNames(roles))

central/authprovider/service/service_impl.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (s *serviceImpl) AuthFuncOverride(ctx context.Context, fullMethodName strin
7070
// GetAuthProvider retrieves the authProvider based on the id passed
7171
func (s *serviceImpl) GetAuthProvider(_ context.Context, request *v1.GetAuthProviderRequest) (*storage.AuthProvider, error) {
7272
if request.GetId() == "" {
73-
return nil, errox.NewErrInvalidArgs("auth provider id is required")
73+
return nil, errox.InvalidArgs.CausedBy("auth provider id is empty")
7474
}
7575
authProvider := s.registry.GetProvider(request.GetId())
7676
if authProvider == nil {
@@ -158,37 +158,37 @@ func (s *serviceImpl) GetAuthProviders(_ context.Context, request *v1.GetAuthPro
158158
func (s *serviceImpl) PostAuthProvider(ctx context.Context, request *v1.PostAuthProviderRequest) (*storage.AuthProvider, error) {
159159
providerReq := request.GetProvider()
160160
if providerReq.GetName() == "" {
161-
return nil, errox.NewErrInvalidArgs("no auth provider name specified")
161+
return nil, errox.InvalidArgs.CausedBy("no auth provider name specified")
162162
}
163163
if providerReq.GetId() != "" {
164-
return nil, errox.NewErrInvalidArgs("auth provider id must be empty")
164+
return nil, errox.InvalidArgs.CausedBy("auth provider id is not empty")
165165
}
166166
if providerReq.GetLoginUrl() != "" {
167-
return nil, errox.NewErrInvalidArgs("auth provider loginUrl field must be empty")
167+
return nil, errox.InvalidArgs.CausedBy("auth provider loginUrl field is not empty")
168168
}
169169

170170
provider, err := s.registry.CreateProvider(ctx, authproviders.WithStorageView(providerReq), authproviders.WithValidateCallback(datastore.Singleton()))
171171
if err != nil {
172-
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
172+
return nil, errors.Wrap(errox.InvalidArgs.CausedBy(err.Error()), "creating auth provider instance")
173173
}
174174
return provider.StorageView(), nil
175175
}
176176

177177
func (s *serviceImpl) PutAuthProvider(ctx context.Context, request *storage.AuthProvider) (*storage.AuthProvider, error) {
178178
if request.GetId() == "" {
179-
return nil, errox.NewErrInvalidArgs("auth provider id must not be empty")
179+
return nil, errox.InvalidArgs.CausedBy("auth provider id is empty")
180180
}
181181

182182
provider := s.registry.GetProvider(request.GetId())
183183
if provider == nil {
184-
return nil, errox.NewErrInvalidArgs(fmt.Sprintf("auth provider with id %q does not exist", request.GetId()))
184+
return nil, errox.InvalidArgs.CausedBy(fmt.Sprintf("auth provider with id %q does not exist", request.GetId()))
185185
}
186186

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

190190
if err := s.registry.ValidateProvider(ctx, authproviders.WithStorageView(request)); err != nil {
191-
return nil, errox.NewErrInvalidArgs(fmt.Sprintf("auth provider validation check failed: %v", err))
191+
return nil, errox.InvalidArgs.CausedBy(fmt.Sprintf("auth provider validation check failed: %v", err))
192192
}
193193

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

199199
provider, err := s.registry.CreateProvider(ctx, authproviders.WithStorageView(request), authproviders.WithValidateCallback(datastore.Singleton()))
200200
if err != nil {
201-
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "creating auth provider instance")
201+
return nil, errors.Wrap(errox.InvalidArgs.CausedBy(err.Error()), "creating auth provider instance")
202202
}
203203
return provider.StorageView(), nil
204204
}
205205

206206
func (s *serviceImpl) UpdateAuthProvider(ctx context.Context, request *v1.UpdateAuthProviderRequest) (*storage.AuthProvider, error) {
207207
if request.GetId() == "" {
208-
return nil, errox.NewErrInvalidArgs("auth provider id must not be empty")
208+
return nil, errox.InvalidArgs.CausedBy("auth provider id is empty")
209209
}
210210

211211
var options []authproviders.ProviderOption
@@ -217,15 +217,15 @@ func (s *serviceImpl) UpdateAuthProvider(ctx context.Context, request *v1.Update
217217
}
218218
provider, err := s.registry.UpdateProvider(ctx, request.GetId(), options...)
219219
if err != nil {
220-
return nil, errors.Wrap(errox.NewErrInvalidArgs(err.Error()), "updating auth provider")
220+
return nil, errors.Wrap(errox.InvalidArgs.CausedBy(err.Error()), "updating auth provider")
221221
}
222222
return provider.StorageView(), nil
223223
}
224224

225225
// DeleteAuthProvider deletes an auth provider from the system
226226
func (s *serviceImpl) DeleteAuthProvider(ctx context.Context, request *v1.ResourceByID) (*v1.Empty, error) {
227227
if request.GetId() == "" {
228-
return nil, errox.NewErrInvalidArgs("auth provider id is required")
228+
return nil, errox.InvalidArgs.CausedBy("auth provider id is empty")
229229
}
230230

231231
// Get auth provider.

central/cluster/datastore/datastore_impl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ func (ds *datastoreImpl) LookupOrCreateClusterFromConfig(ctx context.Context, cl
929929

930930
func normalizeCluster(cluster *storage.Cluster) error {
931931
if cluster == nil {
932-
return errox.NewErrInvariantViolation("cannot normalize nil cluster object")
932+
return errox.InvariantViolation.CausedBy("cannot normalize nil cluster object")
933933
}
934934

935935
cluster.CentralApiEndpoint = strings.TrimPrefix(cluster.GetCentralApiEndpoint(), "https://")
@@ -947,7 +947,7 @@ func validateInput(cluster *storage.Cluster) error {
947947
// `cluster.* bool` flags remain untouched.
948948
func addDefaults(cluster *storage.Cluster) error {
949949
if cluster == nil {
950-
return errox.NewErrInvariantViolation("cannot enrich nil cluster object")
950+
return errox.InvariantViolation.CausedBy("cannot enrich nil cluster object")
951951
}
952952
// For backwards compatibility reasons, if Collection Method is not set then honor defaults for runtime support
953953
if cluster.GetCollectionMethod() == storage.CollectionMethod_UNSET_COLLECTION {

central/compliance/standards/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ func IsSupported(standardID string) bool {
3434

3535
// UnSupportedStandardsErr builds error message for unsupported compliance standards and returns the error
3636
func UnSupportedStandardsErr(unsupported ...string) error {
37-
return errox.NewErrInvalidArgs(fmt.Sprintf("unsupported standard(s): %+v. Supported standards are %+v", unsupported, GetSupportedStandards()))
37+
return errox.InvalidArgs.CausedBy(fmt.Sprintf("unsupported standard(s): %+v. Supported standards are %+v", unsupported, GetSupportedStandards()))
3838
}

central/detection/service/service_impl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (s *serviceImpl) DetectBuildTime(ctx context.Context, req *apiV1.BuildDetec
126126
}
127127
}
128128
if image.GetName() == nil {
129-
return nil, errox.NewErrInvalidArgs("image or image_name must be specified")
129+
return nil, errox.InvalidArgs.CausedBy("image or image_name must be specified")
130130
}
131131
// This is a workaround for those who post the full image, but don't fill in fullname
132132
if name := image.GetName(); name != nil && name.GetFullName() == "" {
@@ -288,7 +288,7 @@ func getObjectsFromList(list *coreV1.List) ([]k8sRuntime.Object, []string, error
288288
// DetectDeployTimeFromYAML runs detection on a deployment.
289289
func (s *serviceImpl) DetectDeployTimeFromYAML(ctx context.Context, req *apiV1.DeployYAMLDetectionRequest) (*apiV1.DeployDetectionResponse, error) {
290290
if req.GetYaml() == "" {
291-
return nil, errox.NewErrInvalidArgs("yaml field must be specified in detection request")
291+
return nil, errox.InvalidArgs.CausedBy("yaml field must be specified in detection request")
292292
}
293293

294294
resources, ignoredObjectRefs, err := getObjectsFromYAML(req.GetYaml())
@@ -347,7 +347,7 @@ func (s *serviceImpl) populateDeploymentWithClusterInfo(ctx context.Context, clu
347347
// DetectDeployTime runs detection on a deployment.
348348
func (s *serviceImpl) DetectDeployTime(ctx context.Context, req *apiV1.DeployDetectionRequest) (*apiV1.DeployDetectionResponse, error) {
349349
if req.GetDeployment() == nil {
350-
return nil, errox.NewErrInvalidArgs("deployment must be passed to deploy time detection")
350+
return nil, errox.InvalidArgs.CausedBy("deployment must be passed to deploy time detection")
351351
}
352352
if err := s.populateDeploymentWithClusterInfo(ctx, req.GetClusterId(), req.GetDeployment()); err != nil {
353353
return nil, err

central/networkpolicies/service/service_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ func compileValidateYaml(simulationYaml string) ([]*storage.NetworkPolicy, error
10751075
// Check that all resulting policies have namespaces.
10761076
for _, policy := range policies {
10771077
if policy.GetNamespace() == "" {
1078-
return nil, errox.NewErrInvalidArgs("yamls tested against must apply to a namespace")
1078+
return nil, errox.InvalidArgs.CausedBy("yamls tested against must apply to a namespace")
10791079
}
10801080
}
10811081

central/policy/service/service_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ func (s *serviceImpl) PolicyFromSearch(ctx context.Context, request *v1.PolicyFr
853853
func (s *serviceImpl) parsePolicy(ctx context.Context, searchString string) (*storage.Policy, []search.FieldLabel, bool, error) {
854854
// Handle empty input query case.
855855
if len(searchString) == 0 {
856-
return nil, nil, false, errox.NewErrInvalidArgs("can not generate a policy from an empty query")
856+
return nil, nil, false, errox.InvalidArgs.CausedBy("can not generate a policy from an empty query")
857857
}
858858
// Have a filled query, parse it.
859859
fieldMap, err := getFieldMapFromQueryString(searchString)

central/role/service/service_impl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (s *serviceImpl) CreateRole(ctx context.Context, roleRequest *v1.CreateRole
113113

114114
// Check role request correctness.
115115
if role.GetName() != "" && role.GetName() != roleRequest.GetName() {
116-
return nil, errox.NewErrInvalidArgs("different role names in path and body")
116+
return nil, errox.InvalidArgs.CausedBy("different role names in path and body")
117117
}
118118
role.Name = roleRequest.GetName()
119119

@@ -208,7 +208,7 @@ func (s *serviceImpl) ListPermissionSets(ctx context.Context, _ *v1.Empty) (*v1.
208208

209209
func (s *serviceImpl) PostPermissionSet(ctx context.Context, permissionSet *storage.PermissionSet) (*storage.PermissionSet, error) {
210210
if permissionSet.GetId() != "" {
211-
return nil, errox.NewErrInvalidArgs("setting id field is not allowed")
211+
return nil, errox.InvalidArgs.CausedBy("setting id field is not allowed")
212212
}
213213
permissionSet.Id = rolePkg.GeneratePermissionSetID()
214214

@@ -274,7 +274,7 @@ func (s *serviceImpl) ListSimpleAccessScopes(ctx context.Context, _ *v1.Empty) (
274274

275275
func (s *serviceImpl) PostSimpleAccessScope(ctx context.Context, scope *storage.SimpleAccessScope) (*storage.SimpleAccessScope, error) {
276276
if scope.GetId() != "" {
277-
return nil, errox.NewErrInvalidArgs("setting id field is not allowed")
277+
return nil, errox.InvalidArgs.CausedBy("setting id field is not allowed")
278278
}
279279
scope.Id = rolePkg.GenerateAccessScopeID()
280280

central/sensor/service/service_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (s *serviceImpl) Communicate(server central.SensorService_CommunicateServer
7070

7171
svc := identity.Service()
7272
if svc == nil || svc.GetType() != storage.ServiceType_SENSOR_SERVICE {
73-
return errox.NewErrNotAuthorized("only sensor may access this API")
73+
return errox.NotAuthorized.CausedBy("only sensor may access this API")
7474
}
7575

7676
sensorHello, sensorSupportsHello, err := receiveSensorHello(server)

0 commit comments

Comments
 (0)