From 460842b86075834306b01aac3b8a6a3e8934244a Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Fri, 16 Jul 2021 17:08:36 -0400 Subject: [PATCH] fix: improve error message when onboarding requests are missing data (#21869) --- CHANGELOG.md | 1 + tenant/error.go | 5 ----- tenant/service_onboarding.go | 26 ++++++++++++++++++++++++-- testing/onboarding.go | 25 +++---------------------- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a821cfa950..85a4e311f98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Because of the version bump to `go`, the macOS build for this release requires a 1. [21866](https://github.com/influxdata/influxdb/pull/21866): Remove incorrect optimization for group-by. 1. [21867](https://github.com/influxdata/influxdb/pull/21867): Return an error instead of panicking when InfluxQL statement rewrites fail. 1. [21868](https://github.com/influxdata/influxdb/pull/21868): Migrate restored KV snapshots to latest schema before using them. +1. [21869](https://github.com/influxdata/influxdb/pull/21869): Specify which fields are missing when rejecting an incomplete onboarding request. ## v2.0.7 [2021-06-04] ---------------------- diff --git a/tenant/error.go b/tenant/error.go index 6f1885165f9..06dc6477f79 100644 --- a/tenant/error.go +++ b/tenant/error.go @@ -34,11 +34,6 @@ var ( Msg: "onboarding has already been completed", } - ErrOnboardInvalid = &influxdb.Error{ - Code: influxdb.EEmptyValue, - Msg: "onboard failed, missing value", - } - ErrNotFound = &influxdb.Error{ Code: influxdb.ENotFound, Msg: "not found", diff --git a/tenant/service_onboarding.go b/tenant/service_onboarding.go index ad9b01659f0..52cf3c00c12 100644 --- a/tenant/service_onboarding.go +++ b/tenant/service_onboarding.go @@ -3,6 +3,8 @@ package tenant import ( "context" "fmt" + "strings" + "github.com/influxdata/influxdb/v2" icontext "github.com/influxdata/influxdb/v2/context" "github.com/influxdata/influxdb/v2/kv" @@ -89,8 +91,28 @@ func (s *OnboardService) OnboardUser(ctx context.Context, req *influxdb.Onboardi // onboardUser allows us to onboard new users. func (s *OnboardService) onboardUser(ctx context.Context, req *influxdb.OnboardingRequest, permFn func(orgID, userID influxdb.ID) []influxdb.Permission) (*influxdb.OnboardingResults, error) { - if req == nil || req.User == "" || req.Org == "" || req.Bucket == "" { - return nil, ErrOnboardInvalid + if req == nil { + return nil, &influxdb.Error{ + Code: influxdb.EEmptyValue, + Msg: "onboarding failed: no request body provided", + } + } + + var missingFields []string + if req.User == "" { + missingFields = append(missingFields, "username") + } + if req.Org == "" { + missingFields = append(missingFields, "org") + } + if req.Bucket == "" { + missingFields = append(missingFields, "bucket") + } + if len(missingFields) > 0 { + return nil, &influxdb.Error{ + Code: influxdb.EUnprocessableEntity, + Msg: fmt.Sprintf("onboarding failed: missing required fields [%s]", strings.Join(missingFields, ",")), + } } result := &influxdb.OnboardingResults{} diff --git a/testing/onboarding.go b/testing/onboarding.go index b48b02e11b4..72bcccf9c80 100644 --- a/testing/onboarding.go +++ b/testing/onboarding.go @@ -82,7 +82,7 @@ func OnboardInitialUser( }, }, wants: wants{ - errCode: platform.EEmptyValue, + errCode: platform.EUnprocessableEntity, }, }, { @@ -101,7 +101,7 @@ func OnboardInitialUser( }, }, wants: wants{ - errCode: platform.EEmptyValue, + errCode: platform.EUnprocessableEntity, }, }, { @@ -120,26 +120,7 @@ func OnboardInitialUser( }, }, wants: wants{ - errCode: platform.EEmptyValue, - }, - }, - { - name: "missing password should fail", - fields: OnboardingFields{ - IDGenerator: &loopIDGenerator{ - s: []string{oneID, twoID, threeID, fourID}, - }, - TokenGenerator: mock.NewTokenGenerator(oneToken, nil), - IsOnboarding: true, - }, - args: args{ - request: &platform.OnboardingRequest{ - User: "admin", - Org: "org1", - }, - }, - wants: wants{ - errCode: platform.EEmptyValue, + errCode: platform.EUnprocessableEntity, }, }, {