X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Finvokermanagement%2Finvokermanagement.go;h=f0a4155b42ac629a43a5f40224a172f27fb76695;hb=4974b9d1c7256e90cb206b327b0c81f7364beeab;hp=43bdc02591f279552b5c12dccdf403697ef8033c;hpb=bf237808ac109b30461a453c59ff4e9cc9b297f4;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index 43bdc02..f0a4155 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -27,13 +27,13 @@ import ( "sync" "oransc.org/nonrtric/capifcore/internal/eventsapi" + "oransc.org/nonrtric/capifcore/internal/keycloak" "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 @@ -53,16 +53,18 @@ type InvokerManager struct { onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails publishRegister publishservice.PublishRegister nextId int64 + keycloak keycloak.AccessManagement eventChannel chan<- eventsapi.EventNotification lock sync.Mutex } // Creates a manager that implements both the InvokerRegister and the invokermanagementapi.ServerInterface interfaces. -func NewInvokerManager(publishRegister publishservice.PublishRegister, eventChannel chan<- eventsapi.EventNotification) *InvokerManager { +func NewInvokerManager(publishRegister publishservice.PublishRegister, km keycloak.AccessManagement, eventChannel chan<- eventsapi.EventNotification) *InvokerManager { return &InvokerManager{ onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails), publishRegister: publishRegister, nextId: 1000, + keycloak: km, eventChannel: eventChannel, } } @@ -100,17 +102,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, ctx); err != nil { return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err)) } @@ -120,7 +123,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 @@ -147,9 +151,24 @@ func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnr newInvoker.PrepareNewInvoker() + im.addClientInKeycloak(newInvoker) + im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker } +func (im *InvokerManager) addClientInKeycloak(newInvoker *invokerapi.APIInvokerEnrolmentDetails) error { + if err := im.keycloak.AddClient(*newInvoker.ApiInvokerId, "invokerrealm"); err != nil { + return err + } + + if body, err := im.keycloak.GetClientRepresentation(*newInvoker.ApiInvokerId, "invokerrealm"); err != nil { + return err + } else { + newInvoker.OnboardingInformation.OnboardingSecret = body.Secret + } + return nil +} + // Deletes an individual API Invoker. func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error { if _, ok := im.onboardedInvokers[onboardingId]; ok { @@ -167,29 +186,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, ctx); 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