Skip to content

Commit

Permalink
Fix possible Subscription ID collision (#23)
Browse files Browse the repository at this point in the history
* Expand range for subsription IDs in NF management

- also used a cryptographically secure RNG, which is
  probably not that important here

* Add missing error handling in nf_management.go
  • Loading branch information
GKnirps authored Sep 5, 2023
1 parent 6c1e2f3 commit ec6e777
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 9 additions & 6 deletions internal/context/management_data.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package context

import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"time"

"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -44,10 +44,13 @@ func NnrfNFManagementDataModel(nf *models.NfProfile, nfprofile models.NfProfile)
return nil
}

func SetsubscriptionId() string {
rand.Seed(time.Now().UnixNano())
x := rand.Intn(100)
return strconv.Itoa(x)
func SetsubscriptionId() (string, error) {
buffer := make([]byte, 16)
_, err := rand.Read(buffer)
if err != nil {
return "", err
}
return hex.EncodeToString(buffer), nil
}

func nnrfNFManagementCondition(nf *models.NfProfile, nfprofile models.NfProfile) {
Expand Down
20 changes: 18 additions & 2 deletions internal/sbi/producer/nf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,32 @@ func HandleCreateSubscriptionRequest(request *httpwrapper.Request) *httpwrapper.
}

func CreateSubscriptionProcedure(subscription models.NrfSubscriptionData) (bson.M, *models.ProblemDetails) {
subscription.SubscriptionId = nrf_context.SetsubscriptionId()
subscriptionID, err := nrf_context.SetsubscriptionId()
if err != nil {
logger.NfmLog.Errorf("Unable to create subscription ID in CreateSubscriptionProcedure: %+v", err)
return nil, &models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "CREATE_SUBSCRIPTION_ERROR",
}
}
subscription.SubscriptionId = subscriptionID

tmp, err := json.Marshal(subscription)
if err != nil {
logger.NfmLog.Errorln("Marshal error in CreateSubscriptionProcedure: ", err)
return nil, &models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "CREATE_SUBSCRIPTION_ERROR",
}
}
putData := bson.M{}
err = json.Unmarshal(tmp, &putData)
if err != nil {
logger.NfmLog.Errorln("Unmarshal error in CreateSubscriptionProcedure: ", err)
return nil, &models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "CREATE_SUBSCRIPTION_ERROR",
}
}

// TODO: need to store Condition !
Expand All @@ -198,7 +214,7 @@ func CreateSubscriptionProcedure(subscription models.NrfSubscriptionData) (bson.
logger.NfmLog.Errorf("CreateSubscriptionProcedure err: %+v", err)
}
problemDetails := &models.ProblemDetails{
Status: http.StatusBadRequest,
Status: http.StatusInternalServerError,
Cause: "CREATE_SUBSCRIPTION_ERROR",
}
return nil, problemDetails
Expand Down

0 comments on commit ec6e777

Please sign in to comment.