From bf237808ac109b30461a453c59ff4e9cc9b297f4 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Wed, 8 Feb 2023 14:26:24 +0100 Subject: [PATCH] Refactor check earlier registration Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: Ic9f6038faa123325a0c8a8be7a7f5cc5f7a586a0 --- capifcore/internal/invokermanagement/invokermanagement.go | 12 ++++++------ capifcore/internal/invokermanagementapi/typevalidation.go | 7 +++++-- .../internal/invokermanagementapi/typevalidation_test.go | 6 +++--- capifcore/internal/providermanagement/providermanagement.go | 12 ++++++------ capifcore/internal/providermanagementapi/typevalidation.go | 7 +++++-- .../internal/providermanagementapi/typevalidation_test.go | 6 +++--- capifcore/internal/publishservice/publishservice.go | 12 ++++++------ capifcore/internal/publishserviceapi/typevalidation.go | 7 +++++-- capifcore/internal/publishserviceapi/typevalidation_test.go | 6 +++--- 9 files changed, 42 insertions(+), 33 deletions(-) diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index 1fbe2f0..43bdc02 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -106,8 +106,8 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error { return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker")) } - if im.isInvokerOnboarded(newInvoker) { - return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, "invoker already onboarded")) + if err := im.isInvokerOnboarded(newInvoker); err != nil { + return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err)) } if err := im.validateInvoker(newInvoker, ctx); err != nil { @@ -129,13 +129,13 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error { return nil } -func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) bool { +func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) error { for _, invoker := range im.onboardedInvokers { - if invoker.IsOnboarded(newInvoker) { - return true + if err := invoker.ValidateAlreadyOnboarded(newInvoker); err != nil { + return err } } - return false + return nil } func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) { diff --git a/capifcore/internal/invokermanagementapi/typevalidation.go b/capifcore/internal/invokermanagementapi/typevalidation.go index 8bfb784..10db338 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation.go +++ b/capifcore/internal/invokermanagementapi/typevalidation.go @@ -42,6 +42,9 @@ func (ied *APIInvokerEnrolmentDetails) Validate() error { return nil } -func (ied *APIInvokerEnrolmentDetails) IsOnboarded(otherInvoker APIInvokerEnrolmentDetails) bool { - return ied.OnboardingInformation.ApiInvokerPublicKey == otherInvoker.OnboardingInformation.ApiInvokerPublicKey +func (ied *APIInvokerEnrolmentDetails) ValidateAlreadyOnboarded(otherInvoker APIInvokerEnrolmentDetails) error { + if ied.OnboardingInformation.ApiInvokerPublicKey == otherInvoker.OnboardingInformation.ApiInvokerPublicKey { + return errors.New("invoker with identical public key already onboarded") + } + return nil } diff --git a/capifcore/internal/invokermanagementapi/typevalidation_test.go b/capifcore/internal/invokermanagementapi/typevalidation_test.go index 499b0a1..b6b491d 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation_test.go +++ b/capifcore/internal/invokermanagementapi/typevalidation_test.go @@ -54,7 +54,7 @@ func TestValidateInvoker(t *testing.T) { assert.Nil(t, err) } -func TestIsOnboarded(t *testing.T) { +func TestValidateAlreadyOnboarded(t *testing.T) { publicKey := "publicKey" invokerUnderTest := APIInvokerEnrolmentDetails{ OnboardingInformation: OnboardingInformation{ @@ -67,8 +67,8 @@ func TestIsOnboarded(t *testing.T) { ApiInvokerPublicKey: "otherPublicKey", }, } - assert.False(t, invokerUnderTest.IsOnboarded(otherInvoker)) + assert.Nil(t, invokerUnderTest.ValidateAlreadyOnboarded(otherInvoker)) otherInvoker.OnboardingInformation.ApiInvokerPublicKey = publicKey - assert.True(t, invokerUnderTest.IsOnboarded(otherInvoker)) + assert.NotNil(t, invokerUnderTest.ValidateAlreadyOnboarded(otherInvoker)) } diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go index 5e9211e..d5e7a63 100644 --- a/capifcore/internal/providermanagement/providermanagement.go +++ b/capifcore/internal/providermanagement/providermanagement.go @@ -76,8 +76,8 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for provider")) } - if pm.isProviderRegistered(newProvider) { - return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, "provider already registered")) + if err := pm.isProviderRegistered(newProvider); err != nil { + return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err)) } if err := newProvider.Validate(); err != nil { @@ -95,13 +95,13 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { return nil } -func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) bool { +func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) error { for _, prov := range pm.registeredProviders { - if prov.IsRegistered(newProvider) { - return true + if err := prov.ValidateAlreadyRegistered(newProvider); err != nil { + return err } } - return false + return nil } func (pm *ProviderManager) prepareNewProvider(newProvider *provapi.APIProviderEnrolmentDetails) { diff --git a/capifcore/internal/providermanagementapi/typevalidation.go b/capifcore/internal/providermanagementapi/typevalidation.go index 3c514a8..bb32991 100644 --- a/capifcore/internal/providermanagementapi/typevalidation.go +++ b/capifcore/internal/providermanagementapi/typevalidation.go @@ -68,6 +68,9 @@ func (pd APIProviderEnrolmentDetails) validateFunctions() error { return nil } -func (pd APIProviderEnrolmentDetails) IsRegistered(otherProvider APIProviderEnrolmentDetails) bool { - return pd.RegSec == otherProvider.RegSec +func (pd APIProviderEnrolmentDetails) ValidateAlreadyRegistered(otherProvider APIProviderEnrolmentDetails) error { + if pd.RegSec == otherProvider.RegSec { + return errors.New("provider with identical regSec already registered") + } + return nil } diff --git a/capifcore/internal/providermanagementapi/typevalidation_test.go b/capifcore/internal/providermanagementapi/typevalidation_test.go index b79a13b..a5a4e3c 100644 --- a/capifcore/internal/providermanagementapi/typevalidation_test.go +++ b/capifcore/internal/providermanagementapi/typevalidation_test.go @@ -106,7 +106,7 @@ func TestValidateAPIProviderEnrolmentDetails(t *testing.T) { assert.Nil(t, providerDetailsUnderTest.Validate()) } -func TestIsRegistered(t *testing.T) { +func TestValidateAlreadyRegistered(t *testing.T) { regSec := "regSec" providerUnderTest := APIProviderEnrolmentDetails{ RegSec: regSec, @@ -115,10 +115,10 @@ func TestIsRegistered(t *testing.T) { otherProvider := APIProviderEnrolmentDetails{ RegSec: "otherRegSec", } - assert.False(t, providerUnderTest.IsRegistered(otherProvider)) + assert.Nil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider)) otherProvider.RegSec = regSec - assert.True(t, providerUnderTest.IsRegistered(otherProvider)) + assert.NotNil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider)) } func getProvider() APIProviderEnrolmentDetails { diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index bf79899..7960f12 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -118,8 +118,8 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId)) } - if ps.isServicePublished(newServiceAPIDescription) { - return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, "service already published")) + if err := ps.isServicePublished(newServiceAPIDescription); err != nil { + return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, err)) } if err := newServiceAPIDescription.Validate(); err != nil { @@ -161,15 +161,15 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e return nil } -func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) bool { +func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) error { for _, services := range ps.publishedServices { for _, service := range services { - if service.IsPublished(newService) { - return true + if err := service.ValidateAlreadyPublished(newService); err != nil { + return err } } } - return false + return nil } func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) { diff --git a/capifcore/internal/publishserviceapi/typevalidation.go b/capifcore/internal/publishserviceapi/typevalidation.go index ad19324..287a07d 100644 --- a/capifcore/internal/publishserviceapi/typevalidation.go +++ b/capifcore/internal/publishserviceapi/typevalidation.go @@ -32,6 +32,9 @@ func (sd ServiceAPIDescription) Validate() error { return nil } -func (sd ServiceAPIDescription) IsPublished(otherService ServiceAPIDescription) bool { - return sd.ApiName == otherService.ApiName +func (sd ServiceAPIDescription) ValidateAlreadyPublished(otherService ServiceAPIDescription) error { + if sd.ApiName == otherService.ApiName { + return errors.New("service with identical apiName is already published") + } + return nil } diff --git a/capifcore/internal/publishserviceapi/typevalidation_test.go b/capifcore/internal/publishserviceapi/typevalidation_test.go index 39e3619..d7b59d5 100644 --- a/capifcore/internal/publishserviceapi/typevalidation_test.go +++ b/capifcore/internal/publishserviceapi/typevalidation_test.go @@ -39,7 +39,7 @@ func TestValidate(t *testing.T) { } -func TestIsServicePublished(t *testing.T) { +func TestValidateAlreadyPublished(t *testing.T) { apiName := "apiName" serviceUnderTest := ServiceAPIDescription{ ApiName: apiName, @@ -48,8 +48,8 @@ func TestIsServicePublished(t *testing.T) { otherService := ServiceAPIDescription{ ApiName: "otherApiName", } - assert.False(t, serviceUnderTest.IsPublished(otherService)) + assert.Nil(t, serviceUnderTest.ValidateAlreadyPublished(otherService)) otherService.ApiName = apiName - assert.True(t, serviceUnderTest.IsPublished(otherService)) + assert.NotNil(t, serviceUnderTest.ValidateAlreadyPublished(otherService)) } -- 2.16.6