Merge "NONRTRIC-946: Validate ApiProvDomId for PUT"
[nonrtric/plt/sme.git] / capifcore / internal / invokermanagement / invokermanagement.go
index 1fbe2f0..ee7030a 100644 (file)
@@ -27,6 +27,7 @@ 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"
@@ -53,16 +54,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,
        }
 }
@@ -106,8 +109,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 +132,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) {
@@ -147,9 +150,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 {