Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Service Producer support OAuth2.0 #28

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/antihax/optional v1.0.0
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293
github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e
github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.3.0
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293 h1:BSIvKCYu7646sE8J9R1L8v2R435otUik3wOFN33csfs=
github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293/go.mod h1:iw/N0E+FlX44EEx24IBi2EdZW8v+bkj3ETWPGnlK9DI=
github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e h1:mXnoioq+fxpChliDl5Uy+m6+Hm7iWrJPZo9mi6BijHE=
github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA=
github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 h1:tNylIqH/m5Kq+3KuC+jjXGl06Y6EmM8yq61ZUgNrPBY=
github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94/go.mod h1:aMszJZbCkcg5xaGgzya+55jz+OPMsJqPLq5Z3fWDFPE=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
Expand Down Expand Up @@ -308,7 +308,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
Expand Down Expand Up @@ -528,6 +527,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
5 changes: 5 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ type UDMContext struct {
NFDiscoveryClient *Nnrf_NFDiscovery.APIClient
UdmUePool sync.Map // map[supi]*UdmUeContext
NrfUri string
NrfCertPem string
GpsiSupiList models.IdentityData
SharedSubsDataMap map[string]models.SharedData // sharedDataIds as key
SubscriptionOfSharedDataChange sync.Map // subscriptionID as key
SuciProfiles []suci.SuciProfile
EeSubscriptionIDGenerator *idgenerator.IDGenerator
OAuth2Required bool
}

type UdmUeContext struct {
Expand Down Expand Up @@ -121,6 +123,9 @@ func InitUdmContext(context *UDMContext) {
}
}
udmContext.NrfUri = configuration.NrfUri
if configuration.NrfCertPem != "" {
context.NrfCertPem = configuration.NrfCertPem
}
servingNameList := configuration.ServiceNameList

udmContext.SuciProfiles = configuration.SuciProfiles
Expand Down
25 changes: 25 additions & 0 deletions internal/sbi/consumer/nf_accesstoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package consumer

import (
"context"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/openapi/oauth"
udm_context "github.com/free5gc/udm/internal/context"
"github.com/free5gc/udm/internal/logger"
)

func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) {
if udm_context.GetSelf().OAuth2Required {
logger.ConsumerLog.Debugln("GetToekenCtx")
udrSelf := udm_context.GetSelf()
tok, pd, err := oauth.SendAccTokenReq(udrSelf.NfId, models.NfType_UDM, scope, targetNF, udrSelf.NrfUri)
if err != nil {
return nil, pd, err
}
return context.WithValue(context.Background(),
openapi.ContextOAuth2, tok), pd, nil
}
return context.TODO(), nil, nil
}
16 changes: 10 additions & 6 deletions internal/sbi/consumer/nf_discovery.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package consumer

import (
"context"
"fmt"
"net/http"

Expand All @@ -21,27 +20,32 @@ const (

func SendNFIntances(nrfUri string, targetNfType, requestNfType models.NfType,
param Nnrf_NFDiscovery.SearchNFInstancesParamOpts,
) (result models.SearchResult, err error) {
) (*models.SearchResult, error) {
configuration := Nnrf_NFDiscovery.NewConfiguration()
configuration.SetBasePath(nrfUri) // addr
clientNRF := Nnrf_NFDiscovery.NewAPIClient(configuration)

result, res, err1 := clientNRF.NFInstancesStoreApi.SearchNFInstances(context.TODO(), targetNfType,
ctx, _, err := GetTokenCtx("nnrf-disc", "NRF")
if err != nil {
return nil, err
}

result, res, err1 := clientNRF.NFInstancesStoreApi.SearchNFInstances(ctx, targetNfType,
requestNfType, &param)
if err1 != nil {
err = err1
return
return nil, err
}

defer func() {
if rspCloseErr := res.Body.Close(); rspCloseErr != nil {
logger.ConsumerLog.Errorf("SearchNFInstances response body cannot close: %+v", rspCloseErr)
}
}()

if res != nil && res.StatusCode == http.StatusTemporaryRedirect {
err = fmt.Errorf("Temporary Redirect For Non NRF Consumer")
}
return
return &result, err
}

func SendNFIntancesUDR(id string, types int) string {
Expand Down
26 changes: 21 additions & 5 deletions internal/sbi/consumer/nf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil

var res *http.Response
for {
_, res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile)
var nf models.NfProfile
nf, res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile)
if err != nil || res == nil {
// TODO : add log
fmt.Println(fmt.Errorf("UDM register to NRF Error[%v]", err.Error()))
Expand All @@ -69,6 +70,16 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
resourceUri := res.Header.Get("Location")
resouceNrfUri = resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")]
retrieveNfInstanceId = resourceUri[strings.LastIndex(resourceUri, "/")+1:]

oauth2 := false
if nf.CustomInfo != nil {
oauth2 = nf.CustomInfo["oauth2"].(bool)
}
udm_context.GetSelf().OAuth2Required = oauth2
logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2)
if oauth2 && udm_context.GetSelf().NrfCertPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.")
}
break
} else {
fmt.Println("handler returned wrong status code", status)
Expand All @@ -81,6 +92,11 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err error) {
logger.ConsumerLog.Infof("Send Deregister NFInstance")

ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF")
if err != nil {
return pd, err
}

udmSelf := udm_context.GetSelf()
// Set client and set url
configuration := Nnrf_NFManagement.NewConfiguration()
Expand All @@ -89,9 +105,9 @@ func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err erro

var res *http.Response

res, err = client.NFInstanceIDDocumentApi.DeregisterNFInstance(context.Background(), udmSelf.NfId)
res, err = client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, udmSelf.NfId)
if err == nil {
return
return nil, err
} else if res != nil {
defer func() {
if rspCloseErr := res.Body.Close(); rspCloseErr != nil {
Expand All @@ -100,12 +116,12 @@ func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err erro
}()

if res.Status != err.Error() {
return
return nil, err
}
problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails)
problemDetails = &problem
} else {
err = openapi.ReportError("server no response")
}
return
return problemDetails, err
}
5 changes: 5 additions & 0 deletions internal/sbi/eventexposure/api_create_ee_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (

// HTTPCreateEeSubscription - Subscribe
func HTTPCreateEeSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
return
}

var eeSubscriptionReq models.EeSubscription

requestBody, err := c.GetRawData()
Expand Down
5 changes: 5 additions & 0 deletions internal/sbi/eventexposure/api_delete_ee_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (

// DeleteEeSubscription - Unsubscribe
func HTTPDeleteEeSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Params["ueIdentity"] = c.Params.ByName("ueIdentity")
req.Params["subscriptionID"] = c.Params.ByName("subscriptionId")
Expand Down
5 changes: 5 additions & 0 deletions internal/sbi/eventexposure/api_update_ee_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (

// UpdateEeSubscription - Patch
func HTTPUpdateEeSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
return
}

var patchList []models.PatchItem

requestBody, err := c.GetRawData()
Expand Down
5 changes: 5 additions & 0 deletions internal/sbi/eventexposure/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/util"
"github.com/free5gc/udm/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand All @@ -42,6 +43,10 @@ func NewRouter() *gin.Engine {
return router
}

func authorizationCheck(c *gin.Context) error {
return util.AuthorizationCheck(c, "nudm-ee")
}

func AddService(engine *gin.Engine) *gin.RouterGroup {
group := engine.Group(factory.UdmEeResUriPrefix)

Expand Down
4 changes: 4 additions & 0 deletions internal/sbi/httpcallback/data_change_notification_to_nf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
)

func HTTPDataChangeNotificationToNF(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
return
}
var dataChangeNotify models.DataChangeNotify
// step 1: retrieve http request body
requestBody, err := c.GetRawData()
Expand Down
5 changes: 5 additions & 0 deletions internal/sbi/httpcallback/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/util"
logger_util "github.com/free5gc/util/logger"
)

Expand Down Expand Up @@ -39,6 +40,10 @@ func NewRouter() *gin.Engine {
return router
}

func authorizationCheck(c *gin.Context) error {
return util.AuthorizationCheck(c, "nudm-sdm")
}

func AddService(engine *gin.Engine) *gin.RouterGroup {
group := engine.Group("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (

// Update - provision parameters
func HTTPUpdate(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
return
}
var ppDataReq models.PpData

// step 1: retrieve http request body
Expand Down
5 changes: 5 additions & 0 deletions internal/sbi/parameterprovision/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/util"
"github.com/free5gc/udm/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand All @@ -42,6 +43,10 @@ func NewRouter() *gin.Engine {
return router
}

func authorizationCheck(c *gin.Context) error {
return util.AuthorizationCheck(c, "nudm-pp")
}

func AddService(engine *gin.Engine) *gin.RouterGroup {
group := engine.Group(factory.UdmPpResUriPrefix)

Expand Down
20 changes: 16 additions & 4 deletions internal/sbi/producer/generate_auth_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package producer

import (
"context"
cryptoRand "crypto/rand"
"encoding/hex"
"fmt"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/free5gc/openapi/models"
udm_context "github.com/free5gc/udm/internal/context"
"github.com/free5gc/udm/internal/logger"
udm_consumer "github.com/free5gc/udm/internal/sbi/consumer"
"github.com/free5gc/udm/pkg/suci"
"github.com/free5gc/util/httpwrapper"
"github.com/free5gc/util/milenage"
Expand Down Expand Up @@ -127,8 +127,12 @@ func ConfirmAuthDataProcedure(authEvent models.AuthEvent, supi string) (problemD
if err != nil {
return openapi.ProblemDetailsSystemFailure(err.Error())
}
ctx, pd, err := udm_consumer.GetTokenCtx("nudr-dr", "UDR")
if err != nil {
return pd
}
resp, err := client.AuthenticationStatusDocumentApi.CreateAuthenticationStatus(
context.Background(), supi, &createAuthParam)
ctx, supi, &createAuthParam)
if err != nil {
problemDetails = &models.ProblemDetails{
Status: int32(resp.StatusCode),
Expand Down Expand Up @@ -173,7 +177,11 @@ func GenerateAuthDataProcedure(authInfoRequest models.AuthenticationInfoRequest,
if err != nil {
return nil, openapi.ProblemDetailsSystemFailure(err.Error())
}
authSubs, res, err := client.AuthenticationDataDocumentApi.QueryAuthSubsData(context.Background(), supi, nil)
ctx, pd, err := udm_consumer.GetTokenCtx("nudr-dr", "UDR")
if err != nil {
return nil, pd
}
authSubs, res, err := client.AuthenticationDataDocumentApi.QueryAuthSubsData(ctx, supi, nil)
if err != nil {
problemDetails = &models.ProblemDetails{
Status: http.StatusForbidden,
Expand Down Expand Up @@ -461,8 +469,12 @@ func GenerateAuthDataProcedure(authInfoRequest models.AuthenticationInfoRequest,
}

var rsp *http.Response
ctx, pd, err = udm_consumer.GetTokenCtx("nudr-dr", "UDR")
if err != nil {
return nil, pd
}
rsp, err = client.AuthenticationDataDocumentApi.ModifyAuthentication(
context.Background(), supi, patchItemArray)
ctx, supi, patchItemArray)
if err != nil {
problemDetails = &models.ProblemDetails{
Status: http.StatusForbidden,
Expand Down
8 changes: 6 additions & 2 deletions internal/sbi/producer/parameter_provision.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package producer

import (
"context"
"net/http"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/udm/internal/logger"
udm_consumer "github.com/free5gc/udm/internal/sbi/consumer"
"github.com/free5gc/util/httpwrapper"
)

Expand Down Expand Up @@ -34,7 +34,11 @@ func UpdateProcedure(updateRequest models.PpData, gpsi string) (problemDetails *
if err != nil {
return openapi.ProblemDetailsSystemFailure(err.Error())
}
res, err := clientAPI.ProvisionedParameterDataDocumentApi.ModifyPpData(context.Background(), gpsi, nil)
ctx, pd, err := udm_consumer.GetTokenCtx("nudr-dr", "UDR")
if err != nil {
return pd
}
res, err := clientAPI.ProvisionedParameterDataDocumentApi.ModifyPpData(ctx, gpsi, nil)
if err != nil {
problemDetails = &models.ProblemDetails{
Status: int32(res.StatusCode),
Expand Down
Loading