From da5256ae0a0b62f276469319340cc8b57fe75bc1 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Thu, 16 Nov 2023 06:39:03 +0000 Subject: [PATCH 1/7] Feature: NRF consumer support oauth2 --- internal/context/ausf_context_init.go | 4 ++++ internal/context/context.go | 2 ++ internal/sbi/consumer/nf_accesstoken.go | 25 ++++++++++++++++++++++ internal/sbi/consumer/nf_discovery.go | 8 +++++-- internal/sbi/consumer/nf_management.go | 22 +++++++++++++++---- internal/sbi/producer/functions.go | 3 ++- internal/sbi/producer/ue_authentication.go | 3 ++- pkg/factory/config.go | 1 + 8 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 internal/sbi/consumer/nf_accesstoken.go diff --git a/internal/context/ausf_context_init.go b/internal/context/ausf_context_init.go index 9bbc099..56ede8b 100644 --- a/internal/context/ausf_context_init.go +++ b/internal/context/ausf_context_init.go @@ -22,6 +22,10 @@ func InitAusfContext(context *AUSFContext) { context.NfId = uuid.New().String() context.GroupID = configuration.GroupId context.NrfUri = configuration.NrfUri + if configuration.NrfCerPem != "" { + context.NrfCerPem = configuration.NrfCerPem + } + context.UriScheme = models.UriScheme(configuration.Sbi.Scheme) // default uri scheme context.RegisterIPv4 = factory.AusfSbiDefaultIPv4 // default localhost context.SBIPort = factory.AusfSbiDefaultPort // default port diff --git a/internal/context/context.go b/internal/context/context.go index 07a21be..8c123ed 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -19,11 +19,13 @@ type AUSFContext struct { Url string UriScheme models.UriScheme NrfUri string + NrfCerPem string NfService map[models.ServiceName]models.NfService PlmnList []models.PlmnId UdmUeauUrl string snRegex *regexp.Regexp EapAkaSupiImsiPrefix bool + OAuth2Required bool } type AusfUeContext struct { diff --git a/internal/sbi/consumer/nf_accesstoken.go b/internal/sbi/consumer/nf_accesstoken.go new file mode 100644 index 0000000..4aafe88 --- /dev/null +++ b/internal/sbi/consumer/nf_accesstoken.go @@ -0,0 +1,25 @@ +package consumer + +import ( + "context" + + ausf_context "github.com/free5gc/ausf/internal/context" + "github.com/free5gc/ausf/internal/logger" + "github.com/free5gc/openapi" + "github.com/free5gc/openapi/models" + "github.com/free5gc/openapi/oauth" +) + +func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { + if ausf_context.GetSelf().OAuth2Required { + logger.ConsumerLog.Debugln("GetToekenCtx") + ausfSelf := ausf_context.GetSelf() + tok, pd, err := oauth.SendAccTokenReq(ausfSelf.NfId, models.NfType_AUSF, scope, targetNF, ausfSelf.NrfUri) + if err != nil { + return nil, pd, err + } + return context.WithValue(context.Background(), + openapi.ContextOAuth2, tok), pd, nil + } + return context.TODO(), nil, nil +} diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index 59035ec..568e588 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -1,7 +1,6 @@ package consumer import ( - "context" "fmt" "net/http" @@ -13,11 +12,16 @@ import ( func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfType, param Nnrf_NFDiscovery.SearchNFInstancesParamOpts, ) (*models.SearchResult, error) { + ctx, _, err := GetTokenCtx("nnrf-disc", "NRF") + if err != nil { + return nil, err + } + configuration := Nnrf_NFDiscovery.NewConfiguration() configuration.SetBasePath(nrfUri) client := Nnrf_NFDiscovery.NewAPIClient(configuration) - result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(context.TODO(), + result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(ctx, targetNfType, requestNfType, ¶m) if rspErr != nil { return nil, fmt.Errorf("NFInstancesStoreApi Response error: %+w", rspErr) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 4c4cc8e..86de562 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -34,7 +34,8 @@ func BuildNFInstance(ausfContext *ausf_context.AUSFContext) (profile models.NfPr } // func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (resouceNrfUri string, -// retrieveNfInstanceID string, err error) { +// +// retrieveNfInstanceID string, err error) { func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (string, string, error) { configuration := Nnrf_NFManagement.NewConfiguration() configuration.SetBasePath(nrfUri) @@ -42,8 +43,8 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil var res *http.Response for { - if _, resTmp, err := client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, - profile); err != nil || resTmp == nil { + nf, resTmp, err := client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile) + if err != nil || resTmp == nil { logger.ConsumerLog.Errorf("AUSF register to NRF Error[%v]", err) time.Sleep(2 * time.Second) continue @@ -64,6 +65,14 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil resourceUri := res.Header.Get("Location") resourceNrfUri := resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")] retrieveNfInstanceID := resourceUri[strings.LastIndex(resourceUri, "/")+1:] + + oauth2 := nf.CustomInfo["oauth2"].(bool) + ausf_context.GetSelf().OAuth2Required = oauth2 + logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2) + if oauth2 && ausf_context.GetSelf().NrfCerPem == "" { + logger.CfgLog.Error("OAuth2 enable but no nrfCerPem provided in config.") + } + return resourceNrfUri, retrieveNfInstanceID, nil } else { fmt.Println(fmt.Errorf("handler returned wrong status code %d", status)) @@ -76,13 +85,18 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil func SendDeregisterNFInstance() (*models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") + ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF") + if err != nil { + return pd, err + } + ausfSelf := ausf_context.GetSelf() // Set client and set url configuration := Nnrf_NFManagement.NewConfiguration() configuration.SetBasePath(ausfSelf.NrfUri) client := Nnrf_NFManagement.NewAPIClient(configuration) - res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(context.Background(), ausfSelf.NfId) + res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, ausfSelf.NfId) if err == nil { return nil, err } else if res != nil { diff --git a/internal/sbi/producer/functions.go b/internal/sbi/producer/functions.go index 393fd82..8175efe 100644 --- a/internal/sbi/producer/functions.go +++ b/internal/sbi/producer/functions.go @@ -135,7 +135,8 @@ func EapEncodeAttribute(attributeType string, data string) (string, error) { } // func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) (K_encr string, K_aut string, K_re string, -// MSK string, EMSK string) { +// +// MSK string, EMSK string) { func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) ([]byte, []byte, []byte, []byte, []byte) { keyAp := ikPrime + ckPrime diff --git a/internal/sbi/producer/ue_authentication.go b/internal/sbi/producer/ue_authentication.go index fae4e69..4db7a6e 100644 --- a/internal/sbi/producer/ue_authentication.go +++ b/internal/sbi/producer/ue_authentication.go @@ -82,7 +82,8 @@ func HandleUeAuthPostRequest(request *httpwrapper.Request) *httpwrapper.Response } // func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) ( -// response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) { +// +// response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) { func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) (*models.UeAuthenticationCtx, string, *models.ProblemDetails, ) { diff --git a/pkg/factory/config.go b/pkg/factory/config.go index a24ef32..eaa9895 100644 --- a/pkg/factory/config.go +++ b/pkg/factory/config.go @@ -57,6 +57,7 @@ type Configuration struct { Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"` ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"` NrfUri string `yaml:"nrfUri,omitempty" valid:"url,required"` + NrfCerPem string `yaml:"nrfCerPem,omitempty" valid:"type(string),minstringlength(1),optional"` PlmnSupportList []models.PlmnId `yaml:"plmnSupportList,omitempty" valid:"required"` GroupId string `yaml:"groupId,omitempty" valid:"type(string),minstringlength(1)"` EapAkaSupiImsiPrefix bool `yaml:"eapAkaSupiImsiPrefix,omitempty" valid:"type(bool),optional"` From ff97bf96bfc4b5732c39d8d37c23f1c5d4b571e0 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Fri, 17 Nov 2023 04:53:26 +0000 Subject: [PATCH 2/7] update go.mod --- go.mod | 2 +- go.sum | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 386d587..4de5c01 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/antihax/optional v1.0.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/bronze1man/radius v0.0.0-20190516032554-afd8baec892d - 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/gopacket v1.1.19 diff --git a/go.sum b/go.sum index ecef5d3..4976549 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,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= @@ -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= @@ -525,6 +524,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= From 37638faa08b09cf591dfccd094a979cddfbc176e Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Mon, 11 Dec 2023 04:44:57 +0000 Subject: [PATCH 3/7] Fix: add minor change --- internal/context/ausf_context_init.go | 4 ++-- internal/context/context.go | 2 +- internal/sbi/consumer/nf_management.go | 10 ++++++---- pkg/factory/config.go | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/context/ausf_context_init.go b/internal/context/ausf_context_init.go index 56ede8b..7bf239d 100644 --- a/internal/context/ausf_context_init.go +++ b/internal/context/ausf_context_init.go @@ -22,8 +22,8 @@ func InitAusfContext(context *AUSFContext) { context.NfId = uuid.New().String() context.GroupID = configuration.GroupId context.NrfUri = configuration.NrfUri - if configuration.NrfCerPem != "" { - context.NrfCerPem = configuration.NrfCerPem + if configuration.NrfCertPem != "" { + context.NrfCertPem = configuration.NrfCertPem } context.UriScheme = models.UriScheme(configuration.Sbi.Scheme) // default uri scheme diff --git a/internal/context/context.go b/internal/context/context.go index 8c123ed..8d9b36c 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -19,7 +19,7 @@ type AUSFContext struct { Url string UriScheme models.UriScheme NrfUri string - NrfCerPem string + NrfCertPem string NfService map[models.ServiceName]models.NfService PlmnList []models.PlmnId UdmUeauUrl string diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 86de562..94298e6 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -65,12 +65,14 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil resourceUri := res.Header.Get("Location") resourceNrfUri := resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")] retrieveNfInstanceID := resourceUri[strings.LastIndex(resourceUri, "/")+1:] - - oauth2 := nf.CustomInfo["oauth2"].(bool) + oauth2 := false + if nf.CustomInfo != nil { + oauth2 = nf.CustomInfo["oauth2"].(bool) + } ausf_context.GetSelf().OAuth2Required = oauth2 logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2) - if oauth2 && ausf_context.GetSelf().NrfCerPem == "" { - logger.CfgLog.Error("OAuth2 enable but no nrfCerPem provided in config.") + if oauth2 && ausf_context.GetSelf().NrfCertPem == "" { + logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.") } return resourceNrfUri, retrieveNfInstanceID, nil diff --git a/pkg/factory/config.go b/pkg/factory/config.go index eaa9895..14fef17 100644 --- a/pkg/factory/config.go +++ b/pkg/factory/config.go @@ -57,7 +57,7 @@ type Configuration struct { Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"` ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"` NrfUri string `yaml:"nrfUri,omitempty" valid:"url,required"` - NrfCerPem string `yaml:"nrfCerPem,omitempty" valid:"type(string),minstringlength(1),optional"` + NrfCertPem string `yaml:"nrfCertPem,omitempty" valid:"type(string),minstringlength(1),optional"` PlmnSupportList []models.PlmnId `yaml:"plmnSupportList,omitempty" valid:"required"` GroupId string `yaml:"groupId,omitempty" valid:"type(string),minstringlength(1)"` EapAkaSupiImsiPrefix bool `yaml:"eapAkaSupiImsiPrefix,omitempty" valid:"type(bool),optional"` From fedce4745206f8366a0d7c5abc80c625d3114124 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Wed, 13 Dec 2023 03:24:49 +0000 Subject: [PATCH 4/7] Fix: prevent assertion and modify config setting --- internal/sbi/consumer/nf_management.go | 8 ++++++-- pkg/factory/config.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 94298e6..b2d7333 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -65,12 +65,16 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil resourceUri := res.Header.Get("Location") resourceNrfUri := resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")] retrieveNfInstanceID := resourceUri[strings.LastIndex(resourceUri, "/")+1:] + oauth2 := false if nf.CustomInfo != nil { - oauth2 = nf.CustomInfo["oauth2"].(bool) + v, ok := nf.CustomInfo["oauth2"].(bool) + if ok { + oauth2 = v + logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2) + } } ausf_context.GetSelf().OAuth2Required = oauth2 - logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2) if oauth2 && ausf_context.GetSelf().NrfCertPem == "" { logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.") } diff --git a/pkg/factory/config.go b/pkg/factory/config.go index 14fef17..2545100 100644 --- a/pkg/factory/config.go +++ b/pkg/factory/config.go @@ -57,7 +57,7 @@ type Configuration struct { Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"` ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"` NrfUri string `yaml:"nrfUri,omitempty" valid:"url,required"` - NrfCertPem string `yaml:"nrfCertPem,omitempty" valid:"type(string),minstringlength(1),optional"` + NrfCertPem string `yaml:"nrfCertPem,omitempty" valid:"optional"` PlmnSupportList []models.PlmnId `yaml:"plmnSupportList,omitempty" valid:"required"` GroupId string `yaml:"groupId,omitempty" valid:"type(string),minstringlength(1)"` EapAkaSupiImsiPrefix bool `yaml:"eapAkaSupiImsiPrefix,omitempty" valid:"type(bool),optional"` From 8013bf117648663ebbf2013af0f46d150eea94ed Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Mon, 18 Dec 2023 05:22:43 +0000 Subject: [PATCH 5/7] Fix: move GetTokenCtx() and fix logic --- internal/context/ausf_context_init.go | 5 +---- internal/context/context.go | 12 ++++++++++++ internal/sbi/consumer/nf_accesstoken.go | 25 ------------------------- internal/sbi/consumer/nf_discovery.go | 3 ++- internal/sbi/consumer/nf_management.go | 2 +- 5 files changed, 16 insertions(+), 31 deletions(-) delete mode 100644 internal/sbi/consumer/nf_accesstoken.go diff --git a/internal/context/ausf_context_init.go b/internal/context/ausf_context_init.go index 7bf239d..575a04f 100644 --- a/internal/context/ausf_context_init.go +++ b/internal/context/ausf_context_init.go @@ -22,10 +22,7 @@ func InitAusfContext(context *AUSFContext) { context.NfId = uuid.New().String() context.GroupID = configuration.GroupId context.NrfUri = configuration.NrfUri - if configuration.NrfCertPem != "" { - context.NrfCertPem = configuration.NrfCertPem - } - + context.NrfCertPem = configuration.NrfCertPem context.UriScheme = models.UriScheme(configuration.Sbi.Scheme) // default uri scheme context.RegisterIPv4 = factory.AusfSbiDefaultIPv4 // default localhost context.SBIPort = factory.AusfSbiDefaultPort // default port diff --git a/internal/context/context.go b/internal/context/context.go index 8d9b36c..53c3b03 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -1,11 +1,13 @@ package context import ( + "context" "regexp" "sync" "github.com/free5gc/ausf/internal/logger" "github.com/free5gc/openapi/models" + "github.com/free5gc/openapi/oauth" ) type AUSFContext struct { @@ -157,3 +159,13 @@ func GetSelf() *AUSFContext { func (a *AUSFContext) GetSelfID() string { return a.NfId } + +func (c *AUSFContext) GetTokenCtx(scope, targetNF string) ( + context.Context, *models.ProblemDetails, error, +) { + if !c.OAuth2Required { + return context.TODO(), nil, nil + } + return oauth.GetTokenCtx(models.NfType_AUSF, + c.NfId, c.NrfUri, scope, targetNF) +} diff --git a/internal/sbi/consumer/nf_accesstoken.go b/internal/sbi/consumer/nf_accesstoken.go deleted file mode 100644 index 4aafe88..0000000 --- a/internal/sbi/consumer/nf_accesstoken.go +++ /dev/null @@ -1,25 +0,0 @@ -package consumer - -import ( - "context" - - ausf_context "github.com/free5gc/ausf/internal/context" - "github.com/free5gc/ausf/internal/logger" - "github.com/free5gc/openapi" - "github.com/free5gc/openapi/models" - "github.com/free5gc/openapi/oauth" -) - -func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { - if ausf_context.GetSelf().OAuth2Required { - logger.ConsumerLog.Debugln("GetToekenCtx") - ausfSelf := ausf_context.GetSelf() - tok, pd, err := oauth.SendAccTokenReq(ausfSelf.NfId, models.NfType_AUSF, scope, targetNF, ausfSelf.NrfUri) - if err != nil { - return nil, pd, err - } - return context.WithValue(context.Background(), - openapi.ContextOAuth2, tok), pd, nil - } - return context.TODO(), nil, nil -} diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index 568e588..6278fde 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + ausf_context "github.com/free5gc/ausf/internal/context" "github.com/free5gc/ausf/internal/logger" "github.com/free5gc/openapi/Nnrf_NFDiscovery" "github.com/free5gc/openapi/models" @@ -12,7 +13,7 @@ import ( func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfType, param Nnrf_NFDiscovery.SearchNFInstancesParamOpts, ) (*models.SearchResult, error) { - ctx, _, err := GetTokenCtx("nnrf-disc", "NRF") + ctx, _, err := ausf_context.GetSelf().GetTokenCtx("nnrf-disc", "NRF") if err != nil { return nil, err } diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index b2d7333..ab6175a 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -91,7 +91,7 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil func SendDeregisterNFInstance() (*models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") - ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF") + ctx, pd, err := ausf_context.GetSelf().GetTokenCtx("nnrf-nfm", "NRF") if err != nil { return pd, err } From 28dfba46e4ce20ab53991570b6f7130946a65036 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Mon, 18 Dec 2023 11:12:37 +0000 Subject: [PATCH 6/7] Fix: linter error --- internal/sbi/consumer/nf_management.go | 5 ++--- internal/sbi/producer/functions.go | 3 +-- internal/sbi/producer/ue_authentication.go | 5 ++--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index ab6175a..3a156c4 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -33,9 +33,8 @@ func BuildNFInstance(ausfContext *ausf_context.AUSFContext) (profile models.NfPr return } -// func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (resouceNrfUri string, -// -// retrieveNfInstanceID string, err error) { +// func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile, +// ) (resouceNrfUri string,retrieveNfInstanceID string, err error) { func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (string, string, error) { configuration := Nnrf_NFManagement.NewConfiguration() configuration.SetBasePath(nrfUri) diff --git a/internal/sbi/producer/functions.go b/internal/sbi/producer/functions.go index 8175efe..2999659 100644 --- a/internal/sbi/producer/functions.go +++ b/internal/sbi/producer/functions.go @@ -135,8 +135,7 @@ func EapEncodeAttribute(attributeType string, data string) (string, error) { } // func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) (K_encr string, K_aut string, K_re string, -// -// MSK string, EMSK string) { +// MSK string, EMSK string) { func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) ([]byte, []byte, []byte, []byte, []byte) { keyAp := ikPrime + ckPrime diff --git a/internal/sbi/producer/ue_authentication.go b/internal/sbi/producer/ue_authentication.go index 4db7a6e..0fbf190 100644 --- a/internal/sbi/producer/ue_authentication.go +++ b/internal/sbi/producer/ue_authentication.go @@ -81,9 +81,8 @@ func HandleUeAuthPostRequest(request *httpwrapper.Request) *httpwrapper.Response return httpwrapper.NewResponse(http.StatusForbidden, nil, problemDetails) } -// func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) ( -// -// response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) { +// func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo, +// ) (response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) { func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) (*models.UeAuthenticationCtx, string, *models.ProblemDetails, ) { From 4f7101aa068f9b70d972eff87497cd4264be3f66 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Tue, 19 Dec 2023 05:06:05 +0000 Subject: [PATCH 7/7] Fix: Update openapi ans util version --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 4de5c01..7dffdc1 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/antihax/optional v1.0.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/bronze1man/radius v0.0.0-20190516032554-afd8baec892d - github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e - github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 + github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 + github.com/free5gc/util v1.0.5-0.20231205080047-308f623d6808 github.com/gin-gonic/gin v1.9.1 github.com/google/gopacket v1.1.19 github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum index 4976549..0947f12 100644 --- a/go.sum +++ b/go.sum @@ -61,10 +61,10 @@ 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.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/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 h1:8P/wOkTAQMgZJe9pUUNSTE5PWeAdlMrsU9kLsI+VAVE= +github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA= +github.com/free5gc/util v1.0.5-0.20231205080047-308f623d6808 h1:8/IoWEgcO2DLlLCqbsxwduD7CzXdKe/BFJU2tcAqnxo= +github.com/free5gc/util v1.0.5-0.20231205080047-308f623d6808/go.mod h1:d+79g84a3YHhzvjJ2IhurrBOavOA8xWIQ/GCywPXqQk= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=