Merge "CI: Add silent prescan SonarCloud job"
[nonrtric/plt/sme.git] / capifcore / internal / invokermanagement / invokermanagement.go
index ee7030a..0dea817 100644 (file)
@@ -2,7 +2,8 @@
 //   ========================LICENSE_START=================================
 //   O-RAN-SC
 //   %%
-//   Copyright (C) 2022: Nordix Foundation
+//   Copyright (C) 2022-2023: Nordix Foundation
+//   Copyright (C) 2024: OpenInfra Foundation Europe
 //   %%
 //   Licensed under the Apache License, Version 2.0 (the "License");
 //   you may not use this file except in compliance with the License.
@@ -31,10 +32,9 @@ import (
 
        "oransc.org/nonrtric/capifcore/internal/common29122"
        invokerapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
-
        "oransc.org/nonrtric/capifcore/internal/publishservice"
 
-       "github.com/labstack/echo/v4"
+       echo "github.com/labstack/echo/v4"
 )
 
 //go:generate mockery --name InvokerRegister
@@ -103,17 +103,18 @@ func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APILis
 
 // Creates a new individual API Invoker profile.
 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
-       var newInvoker invokerapi.APIInvokerEnrolmentDetails
        errMsg := "Unable to onboard invoker due to %s"
-       if err := ctx.Bind(&newInvoker); err != nil {
-               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
+
+       newInvoker, err := getInvokerFromRequest(ctx)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       if err := im.isInvokerOnboarded(newInvoker); err != nil {
+       if err = im.isInvokerOnboarded(newInvoker); err != nil {
                return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
        }
 
-       if err := im.validateInvoker(newInvoker, ctx); err != nil {
+       if err = im.validateInvoker(newInvoker); err != nil {
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
@@ -123,7 +124,8 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
 
        uri := ctx.Request().Host + ctx.Request().URL.String()
        ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
-       err := ctx.JSON(http.StatusCreated, newInvoker)
+
+       err = ctx.JSON(http.StatusCreated, newInvoker)
        if err != nil {
                // Something really bad happened, tell Echo that our handler failed
                return err
@@ -142,15 +144,25 @@ func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnr
 }
 
 func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) {
-       var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
-       newInvoker.ApiList = &apiList
+       var apiListRequestedServices invokerapi.APIList = nil
+       if newInvoker.ApiList != nil {
+               apiListRequestedServices = *newInvoker.ApiList
+       }
+       var allowedPublishedServices invokerapi.APIList = im.publishRegister.GetAllowedPublishedServices(apiListRequestedServices)
+       newInvoker.ApiList = &allowedPublishedServices
 
        im.lock.Lock()
        defer im.lock.Unlock()
 
        newInvoker.PrepareNewInvoker()
 
-       im.addClientInKeycloak(newInvoker)
+       if im.keycloak != nil {
+               // The type assertion fails when unit testing from ServiceManager where we use Capif as a library, and we are not using Keycloak.
+               if _, ok := im.keycloak.(*keycloak.KeycloakManager); !ok {
+                       // im.keycloak is not nil and its dynamic value is not nil.
+                       im.addClientInKeycloak(newInvoker)
+        }
+       }
 
        im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker
 }
@@ -185,29 +197,40 @@ func (im *InvokerManager) deleteInvoker(onboardingId string) {
        delete(im.onboardedInvokers, onboardingId)
 }
 
+func getInvokerFromRequest(ctx echo.Context) (invokerapi.APIInvokerEnrolmentDetails, error) {
+       var invoker invokerapi.APIInvokerEnrolmentDetails
+       if err := ctx.Bind(&invoker); err != nil {
+               return invokerapi.APIInvokerEnrolmentDetails{}, fmt.Errorf("invalid format for invoker")
+       }
+       return invoker, nil
+}
+
 // Updates an individual API invoker details.
 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
-       var invoker invokerapi.APIInvokerEnrolmentDetails
        errMsg := "Unable to update invoker due to %s"
-       if err := ctx.Bind(&invoker); err != nil {
-               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
+
+       newInvoker, err := getInvokerFromRequest(ctx)
+       if err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       if onboardingId != *invoker.ApiInvokerId {
-               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "ApiInvokerId not matching"))
+       // Additional validation for PUT
+       if (newInvoker.ApiInvokerId == nil) || (*newInvoker.ApiInvokerId != onboardingId) {
+               errMismatch := "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter"
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, errMismatch))
        }
 
-       if err := im.validateInvoker(invoker, ctx); err != nil {
+       if err := im.validateInvoker(newInvoker); err != nil {
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
        if _, ok := im.onboardedInvokers[onboardingId]; ok {
-               im.updateInvoker(invoker)
+               im.updateInvoker(newInvoker)
        } else {
                return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
        }
 
-       err := ctx.JSON(http.StatusOK, invoker)
+       err = ctx.JSON(http.StatusOK, newInvoker)
        if err != nil {
                // Something really bad happened, tell Echo that our handler failed
                return err
@@ -226,7 +249,7 @@ func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardi
        return ctx.NoContent(http.StatusNotImplemented)
 }
 
-func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) error {
+func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails) error {
        if err := invoker.Validate(); err != nil {
                return err
        }