diff --git a/remotes/internal/validator.go b/remotes/internal/validator.go deleted file mode 100644 index 891a4a9879..0000000000 --- a/remotes/internal/validator.go +++ /dev/null @@ -1,17 +0,0 @@ -package internal - -import ( - "context" - - ierrors "github.com/influxdata/influxdb/v2/kit/platform/errors" -) - -func NewValidator() *stubValidator { - return &stubValidator{} -} - -type stubValidator struct{} - -func (n stubValidator) ValidateRemoteConnectionHTTPConfig(context.Context, *RemoteConnectionHTTPConfig) error { - return &ierrors.Error{Code: ierrors.ENotImplemented, Msg: "remote validation not implemented"} -} diff --git a/remotes/mock/service.go b/remotes/mock/service.go index 925db33aa8..fdbebc95fa 100644 --- a/remotes/mock/service.go +++ b/remotes/mock/service.go @@ -1,16 +1,16 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/influxdata/influxdb/v2 (interfaces: RemoteConnectionService) +// Source: github.com/influxdata/influxdb/v2/remotes/transport (interfaces: RemoteConnectionService) // Package mock is a generated GoMock package. package mock import ( - "context" - "reflect" + context "context" + reflect "reflect" - "github.com/golang/mock/gomock" - "github.com/influxdata/influxdb/v2" - "github.com/influxdata/influxdb/v2/kit/platform" + gomock "github.com/golang/mock/gomock" + influxdb "github.com/influxdata/influxdb/v2" + platform "github.com/influxdata/influxdb/v2/kit/platform" ) // MockRemoteConnectionService is a mock of RemoteConnectionService interface. @@ -109,45 +109,3 @@ func (mr *MockRemoteConnectionServiceMockRecorder) UpdateRemoteConnection(arg0, mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateRemoteConnection", reflect.TypeOf((*MockRemoteConnectionService)(nil).UpdateRemoteConnection), arg0, arg1, arg2) } - -// ValidateNewRemoteConnection mocks base method. -func (m *MockRemoteConnectionService) ValidateNewRemoteConnection(arg0 context.Context, arg1 influxdb.CreateRemoteConnectionRequest) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateNewRemoteConnection", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// ValidateNewRemoteConnection indicates an expected call of ValidateNewRemoteConnection. -func (mr *MockRemoteConnectionServiceMockRecorder) ValidateNewRemoteConnection(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateNewRemoteConnection", reflect.TypeOf((*MockRemoteConnectionService)(nil).ValidateNewRemoteConnection), arg0, arg1) -} - -// ValidateRemoteConnection mocks base method. -func (m *MockRemoteConnectionService) ValidateRemoteConnection(arg0 context.Context, arg1 platform.ID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateRemoteConnection", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// ValidateRemoteConnection indicates an expected call of ValidateRemoteConnection. -func (mr *MockRemoteConnectionServiceMockRecorder) ValidateRemoteConnection(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateRemoteConnection", reflect.TypeOf((*MockRemoteConnectionService)(nil).ValidateRemoteConnection), arg0, arg1) -} - -// ValidateUpdatedRemoteConnection mocks base method. -func (m *MockRemoteConnectionService) ValidateUpdatedRemoteConnection(arg0 context.Context, arg1 platform.ID, arg2 influxdb.UpdateRemoteConnectionRequest) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateUpdatedRemoteConnection", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// ValidateUpdatedRemoteConnection indicates an expected call of ValidateUpdatedRemoteConnection. -func (mr *MockRemoteConnectionServiceMockRecorder) ValidateUpdatedRemoteConnection(arg0, arg1, arg2 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateUpdatedRemoteConnection", reflect.TypeOf((*MockRemoteConnectionService)(nil).ValidateUpdatedRemoteConnection), arg0, arg1, arg2) -} diff --git a/remotes/mock/validator.go b/remotes/mock/validator.go deleted file mode 100644 index af9f76d957..0000000000 --- a/remotes/mock/validator.go +++ /dev/null @@ -1,50 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/influxdata/influxdb/v2/remotes (interfaces: RemoteConnectionValidator) - -// Package mock is a generated GoMock package. -package mock - -import ( - context "context" - reflect "reflect" - - gomock "github.com/golang/mock/gomock" - internal "github.com/influxdata/influxdb/v2/remotes/internal" -) - -// MockRemoteConnectionValidator is a mock of RemoteConnectionValidator interface. -type MockRemoteConnectionValidator struct { - ctrl *gomock.Controller - recorder *MockRemoteConnectionValidatorMockRecorder -} - -// MockRemoteConnectionValidatorMockRecorder is the mock recorder for MockRemoteConnectionValidator. -type MockRemoteConnectionValidatorMockRecorder struct { - mock *MockRemoteConnectionValidator -} - -// NewMockRemoteConnectionValidator creates a new mock instance. -func NewMockRemoteConnectionValidator(ctrl *gomock.Controller) *MockRemoteConnectionValidator { - mock := &MockRemoteConnectionValidator{ctrl: ctrl} - mock.recorder = &MockRemoteConnectionValidatorMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockRemoteConnectionValidator) EXPECT() *MockRemoteConnectionValidatorMockRecorder { - return m.recorder -} - -// ValidateRemoteConnectionHTTPConfig mocks base method. -func (m *MockRemoteConnectionValidator) ValidateRemoteConnectionHTTPConfig(arg0 context.Context, arg1 *internal.RemoteConnectionHTTPConfig) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateRemoteConnectionHTTPConfig", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// ValidateRemoteConnectionHTTPConfig indicates an expected call of ValidateRemoteConnectionHTTPConfig. -func (mr *MockRemoteConnectionValidatorMockRecorder) ValidateRemoteConnectionHTTPConfig(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateRemoteConnectionHTTPConfig", reflect.TypeOf((*MockRemoteConnectionValidator)(nil).ValidateRemoteConnectionHTTPConfig), arg0, arg1) -} diff --git a/remotes/service.go b/remotes/service.go index b24436d98e..b78b5ee78d 100644 --- a/remotes/service.go +++ b/remotes/service.go @@ -9,7 +9,6 @@ import ( "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/kit/platform" ierrors "github.com/influxdata/influxdb/v2/kit/platform/errors" - "github.com/influxdata/influxdb/v2/remotes/internal" "github.com/influxdata/influxdb/v2/snowflake" "github.com/influxdata/influxdb/v2/sqlite" ) @@ -21,22 +20,16 @@ var ( } ) -type RemoteConnectionValidator interface { - ValidateRemoteConnectionHTTPConfig(context.Context, *internal.RemoteConnectionHTTPConfig) error -} - func NewService(store *sqlite.SqlStore) *service { return &service{ store: store, idGenerator: snowflake.NewIDGenerator(), - validator: internal.NewValidator(), } } type service struct { store *sqlite.SqlStore idGenerator platform.IDGenerator - validator RemoteConnectionValidator } func (s service) ListRemoteConnections(ctx context.Context, filter influxdb.RemoteConnectionListFilter) (*influxdb.RemoteConnections, error) { @@ -95,24 +88,6 @@ func (s service) CreateRemoteConnection(ctx context.Context, request influxdb.Cr return &rc, nil } -func (s service) ValidateNewRemoteConnection(ctx context.Context, request influxdb.CreateRemoteConnectionRequest) error { - config := internal.RemoteConnectionHTTPConfig{ - RemoteURL: request.RemoteURL, - RemoteToken: request.RemoteToken, - RemoteOrgID: request.RemoteOrgID, - AllowInsecureTLS: request.AllowInsecureTLS, - } - - if err := s.validator.ValidateRemoteConnectionHTTPConfig(ctx, &config); err != nil { - return &ierrors.Error{ - Code: ierrors.EInvalid, - Msg: "remote parameters fail validation", - Err: err, - } - } - return nil -} - func (s service) GetRemoteConnection(ctx context.Context, id platform.ID) (*influxdb.RemoteConnection, error) { q := sq.Select("id", "org_id", "name", "description", "remote_url", "remote_org_id", "allow_insecure_tls"). From("remotes"). @@ -175,34 +150,6 @@ func (s service) UpdateRemoteConnection(ctx context.Context, id platform.ID, req return &rc, nil } -func (s service) ValidateUpdatedRemoteConnection(ctx context.Context, id platform.ID, request influxdb.UpdateRemoteConnectionRequest) error { - config, err := s.getConnectionHTTPConfig(ctx, id) - if err != nil { - return err - } - if request.AllowInsecureTLS != nil { - config.AllowInsecureTLS = *request.AllowInsecureTLS - } - if request.RemoteOrgID != nil { - config.RemoteOrgID = *request.RemoteOrgID - } - if request.RemoteToken != nil { - config.RemoteToken = *request.RemoteToken - } - if request.RemoteURL != nil { - config.RemoteURL = *request.RemoteURL - } - - if err := s.validator.ValidateRemoteConnectionHTTPConfig(ctx, config); err != nil { - return &ierrors.Error{ - Code: ierrors.EInvalid, - Msg: "validation fails after applying update", - Err: err, - } - } - return nil -} - func (s service) DeleteRemoteConnection(ctx context.Context, id platform.ID) error { s.store.Mu.Lock() defer s.store.Mu.Unlock() @@ -222,38 +169,3 @@ func (s service) DeleteRemoteConnection(ctx context.Context, id platform.ID) err } return nil } - -func (s service) ValidateRemoteConnection(ctx context.Context, id platform.ID) error { - config, err := s.getConnectionHTTPConfig(ctx, id) - if err != nil { - return err - } - if err := s.validator.ValidateRemoteConnectionHTTPConfig(ctx, config); err != nil { - return &ierrors.Error{ - Code: ierrors.EInvalid, - Msg: "remote failed validation", - Err: err, - } - } - return nil -} - -func (s service) getConnectionHTTPConfig(ctx context.Context, id platform.ID) (*internal.RemoteConnectionHTTPConfig, error) { - q := sq.Select("remote_url", "remote_api_token", "remote_org_id", "allow_insecure_tls"). - From("remotes"). - Where(sq.Eq{"id": id}) - - query, args, err := q.ToSql() - if err != nil { - return nil, err - } - - var rc internal.RemoteConnectionHTTPConfig - if err := s.store.DB.GetContext(ctx, &rc, query, args...); err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, errRemoteNotFound - } - return nil, err - } - return &rc, nil -} diff --git a/remotes/service_test.go b/remotes/service_test.go index d80208f3c6..0ff55f5b36 100644 --- a/remotes/service_test.go +++ b/remotes/service_test.go @@ -2,15 +2,11 @@ package remotes import ( "context" - "errors" "testing" - "github.com/golang/mock/gomock" "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/kit/platform" "github.com/influxdata/influxdb/v2/mock" - "github.com/influxdata/influxdb/v2/remotes/internal" - remotesMock "github.com/influxdata/influxdb/v2/remotes/mock" "github.com/influxdata/influxdb/v2/sqlite" "github.com/influxdata/influxdb/v2/sqlite/migrations" "github.com/stretchr/testify/require" @@ -40,12 +36,6 @@ var ( RemoteOrgID: connection.RemoteOrgID, AllowInsecureTLS: connection.AllowInsecureTLS, } - httpConfig = internal.RemoteConnectionHTTPConfig{ - RemoteURL: connection.RemoteURL, - RemoteToken: fakeToken, - RemoteOrgID: connection.RemoteOrgID, - AllowInsecureTLS: connection.AllowInsecureTLS, - } fakeToken2 = "qrstuvwxyz" fals = false updateReq = influxdb.UpdateRemoteConnectionRequest{ @@ -61,25 +51,18 @@ var ( RemoteOrgID: connection.RemoteOrgID, AllowInsecureTLS: *updateReq.AllowInsecureTLS, } - updatedHttpConfig = internal.RemoteConnectionHTTPConfig{ - RemoteURL: updatedConnection.RemoteURL, - RemoteToken: fakeToken2, - RemoteOrgID: updatedConnection.RemoteOrgID, - AllowInsecureTLS: updatedConnection.AllowInsecureTLS, - } ) func TestCreateAndGetConnection(t *testing.T) { t.Parallel() - svc, mockValidator, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) - // Getting or validating an invalid ID should return an error. + // Getting an invalid ID should return an error. got, err := svc.GetRemoteConnection(ctx, initID) require.Equal(t, errRemoteNotFound, err) require.Nil(t, got) - require.Equal(t, errRemoteNotFound, svc.ValidateRemoteConnection(ctx, initID)) // Create a connection, check the results. created, err := svc.CreateRemoteConnection(ctx, createReq) @@ -90,51 +73,12 @@ func TestCreateAndGetConnection(t *testing.T) { got, err = svc.GetRemoteConnection(ctx, initID) require.NoError(t, err) require.Equal(t, connection, *got) - - // Validate the connection; this is mostly a no-op, for this test, but it allows - // us to assert that the auth token was properly persisted. - fakeErr := errors.New("O NO") - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &httpConfig).Return(fakeErr) - require.Contains(t, svc.ValidateRemoteConnection(ctx, initID).Error(), fakeErr.Error()) -} - -func TestValidateConnectionWithoutPersisting(t *testing.T) { - t.Parallel() - - t.Run("error", func(t *testing.T) { - t.Parallel() - - svc, mockValidator, clean := newTestService(t) - defer clean(t) - - fakeErr := errors.New("O NO") - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &httpConfig).Return(fakeErr) - require.Contains(t, svc.ValidateNewRemoteConnection(ctx, createReq).Error(), fakeErr.Error()) - - got, err := svc.GetRemoteConnection(ctx, initID) - require.Equal(t, errRemoteNotFound, err) - require.Nil(t, got) - }) - - t.Run("no error", func(t *testing.T) { - t.Parallel() - - svc, mockValidator, clean := newTestService(t) - defer clean(t) - - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &httpConfig).Return(nil) - require.NoError(t, svc.ValidateNewRemoteConnection(ctx, createReq)) - - got, err := svc.GetRemoteConnection(ctx, initID) - require.Equal(t, errRemoteNotFound, err) - require.Nil(t, got) - }) } func TestUpdateAndGetConnection(t *testing.T) { t.Parallel() - svc, mockValidator, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) // Updating a nonexistent ID fails. @@ -152,15 +96,16 @@ func TestUpdateAndGetConnection(t *testing.T) { require.NoError(t, err) require.Equal(t, updatedConnection, *updated) - // Validate the updated connection to check that the new auth token persisted. - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &updatedHttpConfig).Return(nil) - require.NoError(t, svc.ValidateRemoteConnection(ctx, initID)) + // Read the updated connection and assert it matches the updated response. + got, err := svc.GetRemoteConnection(ctx, initID) + require.NoError(t, err) + require.Equal(t, updated, got) } func TestUpdateNoop(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) // Create a connection. @@ -172,67 +117,17 @@ func TestUpdateNoop(t *testing.T) { updated, err := svc.UpdateRemoteConnection(ctx, initID, influxdb.UpdateRemoteConnectionRequest{}) require.NoError(t, err) require.Equal(t, connection, *updated) -} -func TestValidateUpdatedConnectionWithoutPersisting(t *testing.T) { - t.Parallel() - - setup := func(t *testing.T, svc *service) { - // Validating an update to a nonexistent ID fails. - updated, err := svc.UpdateRemoteConnection(ctx, initID, updateReq) - require.Equal(t, errRemoteNotFound, err) - require.Nil(t, updated) - - // Create a connection. - created, err := svc.CreateRemoteConnection(ctx, createReq) - require.NoError(t, err) - require.Equal(t, connection, *created) - } - fakeErr := errors.New("O NO") - - t.Run("update causes error", func(t *testing.T) { - t.Parallel() - - svc, mockValidator, clean := newTestService(t) - defer clean(t) - setup(t, svc) - - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &updatedHttpConfig).Return(fakeErr) - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &httpConfig).Return(nil) - - require.Contains(t, svc.ValidateUpdatedRemoteConnection(ctx, initID, updateReq).Error(), fakeErr.Error()) - - // Ensure the update wasn't applied. - got, err := svc.GetRemoteConnection(ctx, initID) - require.NoError(t, err) - require.Equal(t, connection, *got) - require.NoError(t, svc.ValidateRemoteConnection(ctx, initID)) - }) - - t.Run("update fixes error", func(t *testing.T) { - t.Parallel() - - svc, mockValidator, clean := newTestService(t) - defer clean(t) - setup(t, svc) - - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &updatedHttpConfig).Return(nil) - mockValidator.EXPECT().ValidateRemoteConnectionHTTPConfig(gomock.Any(), &httpConfig).Return(fakeErr) - - require.NoError(t, svc.ValidateUpdatedRemoteConnection(ctx, initID, updateReq)) - - // Ensure the update wasn't applied. - got, err := svc.GetRemoteConnection(ctx, initID) - require.NoError(t, err) - require.Equal(t, connection, *got) - require.Contains(t, svc.ValidateRemoteConnection(ctx, initID).Error(), fakeErr.Error()) - }) + // Read the updated connection and assert it matches the updated response. + got, err := svc.GetRemoteConnection(ctx, initID) + require.NoError(t, err) + require.Equal(t, updated, got) } func TestDeleteConnection(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) // Deleting a nonexistent ID should return an error. @@ -271,7 +166,7 @@ func TestListConnections(t *testing.T) { t.Run("list all", func(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) allConns := setup(t, svc) @@ -283,7 +178,7 @@ func TestListConnections(t *testing.T) { t.Run("list by name", func(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) allConns := setup(t, svc) @@ -298,7 +193,7 @@ func TestListConnections(t *testing.T) { t.Run("list by URL", func(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) allConns := setup(t, svc) @@ -313,7 +208,7 @@ func TestListConnections(t *testing.T) { t.Run("list by other org ID", func(t *testing.T) { t.Parallel() - svc, _, clean := newTestService(t) + svc, clean := newTestService(t) defer clean(t) setup(t, svc) @@ -323,18 +218,16 @@ func TestListConnections(t *testing.T) { }) } -func newTestService(t *testing.T) (*service, *remotesMock.MockRemoteConnectionValidator, func(t *testing.T)) { +func newTestService(t *testing.T) (*service, func(t *testing.T)) { store, clean := sqlite.NewTestStore(t) logger := zaptest.NewLogger(t) sqliteMigrator := sqlite.NewMigrator(store, logger) require.NoError(t, sqliteMigrator.Up(ctx, migrations.AllUp)) - mockValidator := remotesMock.NewMockRemoteConnectionValidator(gomock.NewController(t)) svc := service{ store: store, idGenerator: mock.NewIncrementingIDGenerator(initID), - validator: mockValidator, } - return &svc, mockValidator, clean + return &svc, clean } diff --git a/remotes/transport/http.go b/remotes/transport/http.go index 2aa179a334..0b3f9a0269 100644 --- a/remotes/transport/http.go +++ b/remotes/transport/http.go @@ -38,26 +38,14 @@ type RemoteConnectionService interface { // CreateRemoteConnection registers a new remote InfluxDB connection. CreateRemoteConnection(context.Context, influxdb.CreateRemoteConnectionRequest) (*influxdb.RemoteConnection, error) - // ValidateNewRemoteConnection validates that the given settings for a remote InfluxDB connection are usable, - // without persisting the connection info. - ValidateNewRemoteConnection(context.Context, influxdb.CreateRemoteConnectionRequest) error - // GetRemoteConnection returns metadata about the remote InfluxDB connection with the given ID. GetRemoteConnection(context.Context, platform.ID) (*influxdb.RemoteConnection, error) // UpdateRemoteConnection updates the settings for the remote InfluxDB connection with the given ID. UpdateRemoteConnection(context.Context, platform.ID, influxdb.UpdateRemoteConnectionRequest) (*influxdb.RemoteConnection, error) - // ValidateUpdatedRemoteConnection validates that a remote InfluxDB connection is still usable after applying the - // given update, without persisting the new info. - ValidateUpdatedRemoteConnection(context.Context, platform.ID, influxdb.UpdateRemoteConnectionRequest) error - // DeleteRemoteConnection deletes all info for the remote InfluxDB connection with the given ID. DeleteRemoteConnection(context.Context, platform.ID) error - - // ValidateRemoteConnection checks that the remote InfluxDB connection with the given ID is still usable - // with its persisted settings. - ValidateRemoteConnection(context.Context, platform.ID) error } type RemoteConnectionHandler struct { @@ -103,7 +91,6 @@ func newRemoteConnectionHandler(log *zap.Logger, svc RemoteConnectionService) *R r.Get("/", h.handleGetRemote) r.Patch("/", h.handlePatchRemote) r.Delete("/", h.handleDeleteRemote) - r.Post("/validate", h.handleValidateRemote) }) }) @@ -161,24 +148,13 @@ func (h *RemoteConnectionHandler) handleGetRemotes(w http.ResponseWriter, r *htt func (h *RemoteConnectionHandler) handlePostRemote(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - q := r.URL.Query() - validate := q.Get("validate") == "true" var req influxdb.CreateRemoteConnectionRequest if err := h.api.DecodeJSON(r.Body, &req); err != nil { h.api.Err(w, r, err) return } - if validate { - if err := h.remotesService.ValidateNewRemoteConnection(ctx, req); err != nil { - h.api.Err(w, r, err) - return - } - h.api.Respond(w, r, http.StatusNoContent, nil) - return - } - remote, err := h.remotesService.CreateRemoteConnection(ctx, req) if err != nil { h.api.Err(w, r, err) @@ -210,24 +186,13 @@ func (h *RemoteConnectionHandler) handlePatchRemote(w http.ResponseWriter, r *ht } ctx := r.Context() - q := r.URL.Query() - validate := q.Get("validate") == "true" var req influxdb.UpdateRemoteConnectionRequest if err := h.api.DecodeJSON(r.Body, &req); err != nil { h.api.Err(w, r, err) return } - if validate { - if err := h.remotesService.ValidateUpdatedRemoteConnection(ctx, *id, req); err != nil { - h.api.Err(w, r, err) - return - } - h.api.Respond(w, r, http.StatusNoContent, nil) - return - } - remote, err := h.remotesService.UpdateRemoteConnection(ctx, *id, req) if err != nil { h.api.Err(w, r, err) @@ -249,17 +214,3 @@ func (h *RemoteConnectionHandler) handleDeleteRemote(w http.ResponseWriter, r *h } h.api.Respond(w, r, http.StatusNoContent, nil) } - -func (h *RemoteConnectionHandler) handleValidateRemote(w http.ResponseWriter, r *http.Request) { - id, err := platform.IDFromString(chi.URLParam(r, "id")) - if err != nil { - h.api.Err(w, r, errBadId) - return - } - - if err := h.remotesService.ValidateRemoteConnection(r.Context(), *id); err != nil { - h.api.Err(w, r, err) - return - } - h.api.Respond(w, r, http.StatusNoContent, nil) -} diff --git a/remotes/transport/http_test.go b/remotes/transport/http_test.go index a30b0278c1..adc4adaa0c 100644 --- a/remotes/transport/http_test.go +++ b/remotes/transport/http_test.go @@ -19,6 +19,8 @@ import ( "go.uber.org/zap/zaptest" ) +//go:generate go run github.com/golang/mock/mockgen -package mock -destination ../mock/service.go github.com/influxdata/influxdb/v2/remotes/transport RemoteConnectionService + var ( orgStr = "1234123412341234" orgID, _ = platform.IDFromString(orgStr) @@ -89,29 +91,6 @@ func TestRemoteConnectionHandler(t *testing.T) { require.Equal(t, testConn, got) }) - t.Run("dry-run create happy path", func(t *testing.T) { - ts, svc := newTestServer(t) - defer ts.Close() - - body := influxdb.CreateRemoteConnectionRequest{ - OrgID: testConn.OrgID, - Name: testConn.Name, - RemoteURL: testConn.RemoteURL, - RemoteToken: "my super secret token", - RemoteOrgID: testConn.RemoteOrgID, - AllowInsecureTLS: testConn.AllowInsecureTLS, - } - - req := newTestRequest(t, "POST", ts.URL, &body) - q := req.URL.Query() - q.Add("validate", "true") - req.URL.RawQuery = q.Encode() - - svc.EXPECT().ValidateNewRemoteConnection(gomock.Any(), body).Return(nil) - - doTestRequest(t, req, http.StatusNoContent, false) - }) - t.Run("get remote happy path", func(t *testing.T) { ts, svc := newTestServer(t) defer ts.Close() @@ -156,34 +135,6 @@ func TestRemoteConnectionHandler(t *testing.T) { require.Equal(t, testConn, got) }) - t.Run("dry-run update happy path", func(t *testing.T) { - ts, svc := newTestServer(t) - defer ts.Close() - - newToken := "a new even more secret token" - body := influxdb.UpdateRemoteConnectionRequest{RemoteToken: &newToken} - - req := newTestRequest(t, "PATCH", ts.URL+"/"+id.String(), &body) - q := req.URL.Query() - q.Add("validate", "true") - req.URL.RawQuery = q.Encode() - - svc.EXPECT().ValidateUpdatedRemoteConnection(gomock.Any(), *id, body).Return(nil) - - doTestRequest(t, req, http.StatusNoContent, false) - }) - - t.Run("validate remote happy path", func(t *testing.T) { - ts, svc := newTestServer(t) - defer ts.Close() - - req := newTestRequest(t, "POST", ts.URL+"/"+id.String()+"/validate", nil) - - svc.EXPECT().ValidateRemoteConnection(gomock.Any(), *id).Return(nil) - - doTestRequest(t, req, http.StatusNoContent, false) - }) - t.Run("invalid remote IDs return 400", func(t *testing.T) { ts, _ := newTestServer(t) defer ts.Close() diff --git a/remotes/transport/middleware_auth.go b/remotes/transport/middleware_auth.go index 1707f50d14..49218c6e52 100644 --- a/remotes/transport/middleware_auth.go +++ b/remotes/transport/middleware_auth.go @@ -47,14 +47,6 @@ func (a authCheckingService) CreateRemoteConnection(ctx context.Context, request return a.underlying.CreateRemoteConnection(ctx, request) } -func (a authCheckingService) ValidateNewRemoteConnection(ctx context.Context, request influxdb.CreateRemoteConnectionRequest) error { - if _, _, err := authorizer.AuthorizeCreate(ctx, influxdb.RemotesResourceType, request.OrgID); err != nil { - return err - } - - return a.underlying.ValidateNewRemoteConnection(ctx, request) -} - func (a authCheckingService) GetRemoteConnection(ctx context.Context, id platform.ID) (*influxdb.RemoteConnection, error) { r, err := a.underlying.GetRemoteConnection(ctx, id) if err != nil { @@ -77,17 +69,6 @@ func (a authCheckingService) UpdateRemoteConnection(ctx context.Context, id plat return a.underlying.UpdateRemoteConnection(ctx, id, request) } -func (a authCheckingService) ValidateUpdatedRemoteConnection(ctx context.Context, id platform.ID, request influxdb.UpdateRemoteConnectionRequest) error { - r, err := a.underlying.GetRemoteConnection(ctx, id) - if err != nil { - return err - } - if _, _, err := authorizer.AuthorizeWrite(ctx, influxdb.RemotesResourceType, id, r.OrgID); err != nil { - return err - } - return a.underlying.ValidateUpdatedRemoteConnection(ctx, id, request) -} - func (a authCheckingService) DeleteRemoteConnection(ctx context.Context, id platform.ID) error { r, err := a.underlying.GetRemoteConnection(ctx, id) if err != nil { @@ -98,14 +79,3 @@ func (a authCheckingService) DeleteRemoteConnection(ctx context.Context, id plat } return a.underlying.DeleteRemoteConnection(ctx, id) } - -func (a authCheckingService) ValidateRemoteConnection(ctx context.Context, id platform.ID) error { - r, err := a.underlying.GetRemoteConnection(ctx, id) - if err != nil { - return err - } - if _, _, err := authorizer.AuthorizeRead(ctx, influxdb.RemotesResourceType, id, r.OrgID); err != nil { - return err - } - return a.underlying.ValidateRemoteConnection(ctx, id) -} diff --git a/remotes/transport/middleware_logging.go b/remotes/transport/middleware_logging.go index 5b047b88e1..c4cfd319b3 100644 --- a/remotes/transport/middleware_logging.go +++ b/remotes/transport/middleware_logging.go @@ -47,18 +47,6 @@ func (l loggingService) CreateRemoteConnection(ctx context.Context, request infl return l.underlying.CreateRemoteConnection(ctx, request) } -func (l loggingService) ValidateNewRemoteConnection(ctx context.Context, request influxdb.CreateRemoteConnectionRequest) (err error) { - defer func(start time.Time) { - dur := zap.Duration("took", time.Since(start)) - if err != nil { - l.logger.Debug("failed to validate remote create", zap.Error(err), dur) - return - } - l.logger.Debug("remote validate create", dur) - }(time.Now()) - return l.underlying.ValidateNewRemoteConnection(ctx, request) -} - func (l loggingService) GetRemoteConnection(ctx context.Context, id platform.ID) (r *influxdb.RemoteConnection, err error) { defer func(start time.Time) { dur := zap.Duration("took", time.Since(start)) @@ -83,18 +71,6 @@ func (l loggingService) UpdateRemoteConnection(ctx context.Context, id platform. return l.underlying.UpdateRemoteConnection(ctx, id, request) } -func (l loggingService) ValidateUpdatedRemoteConnection(ctx context.Context, id platform.ID, request influxdb.UpdateRemoteConnectionRequest) (err error) { - defer func(start time.Time) { - dur := zap.Duration("took", time.Since(start)) - if err != nil { - l.logger.Debug("failed to validate remote update", zap.Error(err), dur) - return - } - l.logger.Debug("remote validate update", dur) - }(time.Now()) - return l.underlying.ValidateUpdatedRemoteConnection(ctx, id, request) -} - func (l loggingService) DeleteRemoteConnection(ctx context.Context, id platform.ID) (err error) { defer func(start time.Time) { dur := zap.Duration("took", time.Since(start)) @@ -106,15 +82,3 @@ func (l loggingService) DeleteRemoteConnection(ctx context.Context, id platform. }(time.Now()) return l.underlying.DeleteRemoteConnection(ctx, id) } - -func (l loggingService) ValidateRemoteConnection(ctx context.Context, id platform.ID) (err error) { - defer func(start time.Time) { - dur := zap.Duration("took", time.Since(start)) - if err != nil { - l.logger.Debug("failed to validate remote", zap.Error(err), dur) - return - } - l.logger.Debug("remote validate", dur) - }(time.Now()) - return l.underlying.ValidateRemoteConnection(ctx, id) -} diff --git a/remotes/transport/middleware_metrics.go b/remotes/transport/middleware_metrics.go index ea776dcba1..300b3ad1e1 100644 --- a/remotes/transport/middleware_metrics.go +++ b/remotes/transport/middleware_metrics.go @@ -37,11 +37,6 @@ func (m metricsService) CreateRemoteConnection(ctx context.Context, request infl return rc, rec(err) } -func (m metricsService) ValidateNewRemoteConnection(ctx context.Context, request influxdb.CreateRemoteConnectionRequest) error { - rec := m.rec.Record("validate_create_remote") - return rec(m.underlying.ValidateNewRemoteConnection(ctx, request)) -} - func (m metricsService) GetRemoteConnection(ctx context.Context, id platform.ID) (*influxdb.RemoteConnection, error) { rec := m.rec.Record("find_remote_by_id") rc, err := m.underlying.GetRemoteConnection(ctx, id) @@ -54,17 +49,7 @@ func (m metricsService) UpdateRemoteConnection(ctx context.Context, id platform. return rc, rec(err) } -func (m metricsService) ValidateUpdatedRemoteConnection(ctx context.Context, id platform.ID, request influxdb.UpdateRemoteConnectionRequest) error { - rec := m.rec.Record("validate_update_remote") - return rec(m.underlying.ValidateUpdatedRemoteConnection(ctx, id, request)) -} - func (m metricsService) DeleteRemoteConnection(ctx context.Context, id platform.ID) error { rec := m.rec.Record("delete_remote") return rec(m.underlying.DeleteRemoteConnection(ctx, id)) } - -func (m metricsService) ValidateRemoteConnection(ctx context.Context, id platform.ID) error { - rec := m.rec.Record("validate_remote") - return rec(m.underlying.ValidateRemoteConnection(ctx, id)) -} diff --git a/scripts/fetch-swagger.sh b/scripts/fetch-swagger.sh index e7934a4025..a423f4a69a 100755 --- a/scripts/fetch-swagger.sh +++ b/scripts/fetch-swagger.sh @@ -10,7 +10,7 @@ declare -r ROOT_DIR=$(dirname ${SCRIPT_DIR}) declare -r STATIC_DIR="$ROOT_DIR/static" # Pins the swagger that will be downloaded to a specific commit -declare -r OPENAPI_SHA=12c8165b70cb379486592ffbf43ac5a3dde04904 +declare -r OPENAPI_SHA=1243aa6c501b26aabb1c32121de1e235152398a6 # Don't do a shallow clone since the commit we want might be several commits # back; but do only clone the main branch.