diff --git a/.coverage/coverage-unit.txt b/.coverage/coverage-unit.txt new file mode 100644 index 0000000..da45acb --- /dev/null +++ b/.coverage/coverage-unit.txt @@ -0,0 +1,104 @@ +mode: atomic +github.com/omec-project/udr/service/init.go:68.13,70.2 1 1 +github.com/omec-project/udr/service/init.go:72.44,74.2 1 0 +github.com/omec-project/udr/service/init.go:76.50,81.25 2 0 +github.com/omec-project/udr/service/init.go:81.25,82.66 1 0 +github.com/omec-project/udr/service/init.go:82.66,84.4 1 0 +github.com/omec-project/udr/service/init.go:85.8,87.73 2 0 +github.com/omec-project/udr/service/init.go:87.73,89.4 1 0 +github.com/omec-project/udr/service/init.go:92.2,94.53 2 0 +github.com/omec-project/udr/service/init.go:94.53,96.3 1 0 +github.com/omec-project/udr/service/init.go:98.2,98.12 1 0 +github.com/omec-project/udr/service/init.go:101.31,102.37 1 0 +github.com/omec-project/udr/service/init.go:102.37,105.3 2 0 +github.com/omec-project/udr/service/init.go:107.2,107.41 1 0 +github.com/omec-project/udr/service/init.go:107.41,108.52 1 0 +github.com/omec-project/udr/service/init.go:108.52,109.92 1 0 +github.com/omec-project/udr/service/init.go:109.92,113.5 2 0 +github.com/omec-project/udr/service/init.go:113.10,116.5 2 0 +github.com/omec-project/udr/service/init.go:117.9,120.4 2 0 +github.com/omec-project/udr/service/init.go:121.3,121.68 1 0 +github.com/omec-project/udr/service/init.go:124.2,124.46 1 0 +github.com/omec-project/udr/service/init.go:124.46,125.57 1 0 +github.com/omec-project/udr/service/init.go:125.57,126.97 1 0 +github.com/omec-project/udr/service/init.go:126.97,130.5 2 0 +github.com/omec-project/udr/service/init.go:130.10,132.5 1 0 +github.com/omec-project/udr/service/init.go:133.9,136.4 2 0 +github.com/omec-project/udr/service/init.go:137.3,137.81 1 0 +github.com/omec-project/udr/service/init.go:140.2,140.52 1 0 +github.com/omec-project/udr/service/init.go:140.52,141.63 1 0 +github.com/omec-project/udr/service/init.go:141.63,142.103 1 0 +github.com/omec-project/udr/service/init.go:142.103,146.5 2 0 +github.com/omec-project/udr/service/init.go:146.10,148.5 1 0 +github.com/omec-project/udr/service/init.go:149.9,152.4 2 0 +github.com/omec-project/udr/service/init.go:153.3,153.89 1 0 +github.com/omec-project/udr/service/init.go:157.59,158.39 1 0 +github.com/omec-project/udr/service/init.go:158.39,161.18 3 0 +github.com/omec-project/udr/service/init.go:161.18,162.12 1 0 +github.com/omec-project/udr/service/init.go:165.3,165.40 1 0 +github.com/omec-project/udr/service/init.go:167.2,167.13 1 0 +github.com/omec-project/udr/service/init.go:170.25,195.12 16 0 +github.com/omec-project/udr/service/init.go:195.12,199.3 3 0 +github.com/omec-project/udr/service/init.go:201.2,205.19 4 0 +github.com/omec-project/udr/service/init.go:205.19,208.3 2 0 +github.com/omec-project/udr/service/init.go:210.2,210.16 1 0 +github.com/omec-project/udr/service/init.go:210.16,212.3 1 0 +github.com/omec-project/udr/service/init.go:214.2,215.28 2 0 +github.com/omec-project/udr/service/init.go:215.28,217.3 1 0 +github.com/omec-project/udr/service/init.go:217.8,217.36 1 0 +github.com/omec-project/udr/service/init.go:217.36,219.3 1 0 +github.com/omec-project/udr/service/init.go:221.2,221.16 1 0 +github.com/omec-project/udr/service/init.go:221.16,223.3 1 0 +github.com/omec-project/udr/service/init.go:226.44,234.42 5 0 +github.com/omec-project/udr/service/init.go:234.42,236.3 1 0 +github.com/omec-project/udr/service/init.go:238.2,239.57 2 0 +github.com/omec-project/udr/service/init.go:239.57,241.3 1 0 +github.com/omec-project/udr/service/init.go:241.8,243.3 1 0 +github.com/omec-project/udr/service/init.go:244.2,246.12 3 0 +github.com/omec-project/udr/service/init.go:246.12,248.17 2 0 +github.com/omec-project/udr/service/init.go:248.17,250.4 1 0 +github.com/omec-project/udr/service/init.go:251.3,251.12 1 0 +github.com/omec-project/udr/service/init.go:254.2,255.57 2 0 +github.com/omec-project/udr/service/init.go:255.57,257.3 1 0 +github.com/omec-project/udr/service/init.go:257.8,259.3 1 0 +github.com/omec-project/udr/service/init.go:260.2,260.12 1 0 +github.com/omec-project/udr/service/init.go:260.12,262.17 2 0 +github.com/omec-project/udr/service/init.go:262.17,264.4 1 0 +github.com/omec-project/udr/service/init.go:265.3,265.12 1 0 +github.com/omec-project/udr/service/init.go:268.2,269.12 2 0 +github.com/omec-project/udr/service/init.go:269.12,270.50 1 0 +github.com/omec-project/udr/service/init.go:270.50,273.4 2 0 +github.com/omec-project/udr/service/init.go:274.3,274.12 1 0 +github.com/omec-project/udr/service/init.go:277.2,278.12 2 0 +github.com/omec-project/udr/service/init.go:281.29,285.27 3 0 +github.com/omec-project/udr/service/init.go:285.27,287.3 1 0 +github.com/omec-project/udr/service/init.go:287.8,287.23 1 0 +github.com/omec-project/udr/service/init.go:287.23,289.3 1 0 +github.com/omec-project/udr/service/init.go:289.8,291.3 1 0 +github.com/omec-project/udr/service/init.go:292.2,292.40 1 0 +github.com/omec-project/udr/service/init.go:295.34,296.49 1 0 +github.com/omec-project/udr/service/init.go:296.49,302.17 3 0 +github.com/omec-project/udr/service/init.go:302.17,304.4 1 0 +github.com/omec-project/udr/service/init.go:304.9,306.4 1 0 +github.com/omec-project/udr/service/init.go:310.65,314.35 4 2 +github.com/omec-project/udr/service/init.go:314.35,316.3 1 2 +github.com/omec-project/udr/service/init.go:317.2,319.100 2 2 +github.com/omec-project/udr/service/init.go:322.38,323.27 1 2 +github.com/omec-project/udr/service/init.go:323.27,327.3 3 1 +github.com/omec-project/udr/service/init.go:330.85,337.2 5 0 +github.com/omec-project/udr/service/init.go:340.28,343.27 3 0 +github.com/omec-project/udr/service/init.go:343.27,346.3 2 0 +github.com/omec-project/udr/service/init.go:348.2,357.27 6 0 +github.com/omec-project/udr/service/init.go:357.27,361.65 2 0 +github.com/omec-project/udr/service/init.go:361.65,364.18 2 0 +github.com/omec-project/udr/service/init.go:364.18,366.5 1 0 +github.com/omec-project/udr/service/init.go:368.8,368.23 1 0 +github.com/omec-project/udr/service/init.go:368.23,371.17 3 0 +github.com/omec-project/udr/service/init.go:371.17,373.4 1 0 +github.com/omec-project/udr/service/init.go:376.2,376.35 1 0 +github.com/omec-project/udr/service/init.go:376.35,379.3 1 0 +github.com/omec-project/udr/service/init.go:380.2,382.90 2 0 +github.com/omec-project/udr/service/init.go:385.30,386.44 1 1 +github.com/omec-project/udr/service/init.go:386.44,394.17 7 2 +github.com/omec-project/udr/service/init.go:394.17,397.4 2 2 +github.com/omec-project/udr/service/init.go:397.9,399.4 1 0 diff --git a/Makefile b/Makefile index ddaaff9..78c1f4e 100644 --- a/Makefile +++ b/Makefile @@ -81,3 +81,17 @@ docker-push: for target in $(DOCKER_TARGETS); do \ docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}5gc-$$target:${DOCKER_TAG}; \ done + +.coverage: + rm -rf $(CURDIR)/.coverage + mkdir -p $(CURDIR)/.coverage + +test: .coverage + docker run --rm -v $(CURDIR):/udr -w /udr golang:latest \ + go test \ + -failfast \ + -coverprofile=.coverage/coverage-unit.txt \ + -covermode=atomic \ + -v \ + ./ ./... + diff --git a/bin/udr b/bin/udr new file mode 100755 index 0000000..4ac6270 Binary files /dev/null and b/bin/udr differ diff --git a/factory/config.example.yaml b/factory/config.example.yaml index 4ba7491..fcbead4 100644 --- a/factory/config.example.yaml +++ b/factory/config.example.yaml @@ -12,6 +12,11 @@ configuration: registerIPv4: 127.0.0.4 bindingIPv4: 0.0.0.0 port: 8000 + mongodb: + name: free5gc + url: http://dummy + authKeysDbName: authentication + authUrl: http://dummy plmnSupportList: - plmnId: mcc: "208" diff --git a/factory/config.go b/factory/config.go index bf1b7bc..97513c6 100644 --- a/factory/config.go +++ b/factory/config.go @@ -65,8 +65,10 @@ type Tls struct { } type Mongodb struct { - Name string `yaml:"name"` - Url string `yaml:"url"` + Name string `yaml:"name,omitempty"` + Url string `yaml:"url,omitempty"` + AuthKeysDbName string `yaml:"authKeysDbName"` + AuthUrl string `yaml:"authUrl"` } var ConfigPodTrigger chan bool diff --git a/factory/factory.go b/factory/factory.go index 19505f0..ffa036f 100644 --- a/factory/factory.go +++ b/factory/factory.go @@ -50,6 +50,13 @@ func InitConfigFactory(f string) error { if yamlErr := yaml.Unmarshal(content, &UdrConfig); yamlErr != nil { return yamlErr } + if UdrConfig.Configuration.Mongodb.AuthUrl == "" { + authUrl := UdrConfig.Configuration.Mongodb.Url + UdrConfig.Configuration.Mongodb.AuthUrl = authUrl + } + if UdrConfig.Configuration.Mongodb.AuthKeysDbName == "" { + UdrConfig.Configuration.Mongodb.AuthKeysDbName = "authentication" + } roc := os.Getenv("MANAGED_BY_CONFIG_POD") if roc == "true" { initLog.Infoln("MANAGED_BY_CONFIG_POD is true") diff --git a/producer/data_repository.go b/producer/data_repository.go index 449e65b..2024d2b 100644 --- a/producer/data_repository.go +++ b/producer/data_repository.go @@ -377,7 +377,7 @@ func HandleModifyAuthentication(request *httpwrapper.Request) *httpwrapper.Respo func ModifyAuthenticationProcedure(collName string, ueId string, patchItem []models.PatchItem) *models.ProblemDetails { filter := bson.M{"ueId": ueId} - origValue, errGetOne := CommonDBClient.RestfulAPIGetOne(collName, filter) + origValue, errGetOne := AuthDBClient.RestfulAPIGetOne(collName, filter) if errGetOne != nil { logger.DataRepoLog.Warnln(errGetOne) } @@ -386,10 +386,10 @@ func ModifyAuthenticationProcedure(collName string, ueId string, patchItem []mod if err != nil { logger.DataRepoLog.Error(err) } - failure := CommonDBClient.RestfulAPIJSONPatch(collName, filter, patchJSON) + failure := AuthDBClient.RestfulAPIJSONPatch(collName, filter, patchJSON) if failure == nil { - newValue, errGetOneNew := CommonDBClient.RestfulAPIGetOne(collName, filter) + newValue, errGetOneNew := AuthDBClient.RestfulAPIGetOne(collName, filter) if errGetOneNew != nil { logger.DataRepoLog.Warnln(errGetOneNew) } @@ -421,7 +421,7 @@ func HandleQueryAuthSubsData(request *httpwrapper.Request) *httpwrapper.Response func QueryAuthSubsDataProcedure(collName string, ueId string) (map[string]interface{}, *models.ProblemDetails) { filter := bson.M{"ueId": ueId} - authenticationSubscription, errGetOne := CommonDBClient.RestfulAPIGetOne(collName, filter) + authenticationSubscription, errGetOne := AuthDBClient.RestfulAPIGetOne(collName, filter) if errGetOne != nil { logger.DataRepoLog.Warnln(errGetOne) } diff --git a/producer/db_adapter.go b/producer/db_adapter.go index f22d7d0..f0f83e3 100644 --- a/producer/db_adapter.go +++ b/producer/db_adapter.go @@ -30,12 +30,14 @@ type DBInterface interface { } var CommonDBClient DBInterface +var AuthDBClient DBInterface type MongoDBClient struct { mongoapi.MongoClient } -func getMongoClient(url string, dbname string) error { +// Set CommonDBClient +func setCommonDBClient(url string, dbname string) error { var mClient, errConnect = mongoapi.NewMongoClient(url, dbname) if mClient.Client != nil { CommonDBClient = mClient @@ -44,14 +46,26 @@ func getMongoClient(url string, dbname string) error { return errConnect } -func ConnectMongo(url string, dbname string) { +// Set AuthDBClient +func setAuthDBClient(authurl string, authkeysdbname string) error { + var mClient, errConnect = mongoapi.NewMongoClient(authurl, authkeysdbname) + if mClient.Client != nil { + AuthDBClient = mClient + AuthDBClient.(*mongoapi.MongoClient).Client.Database(authkeysdbname) + } + return errConnect +} + +func ConnectMongo(url string, dbname string, authurl string, authkeysdbname string) { // Connect to MongoDB ticker := time.NewTicker(2 * time.Second) defer func() { ticker.Stop() }() timer := time.After(180 * time.Second) ConnectMongo: for { - if err := getMongoClient(url, dbname); err == nil { + commonDbErr := setCommonDBClient(url, dbname) + authDbErr := setAuthDBClient(authurl, authkeysdbname) + if commonDbErr == nil && authDbErr == nil { break ConnectMongo } select { diff --git a/service/init.go b/service/init.go index d7a7746..316a26e 100644 --- a/service/init.go +++ b/service/init.go @@ -174,7 +174,7 @@ func (udr *UDR) Start() { initLog.Infof("UDR Config Info: Version[%s] Description[%s]", config.Info.Version, config.Info.Description) // Connect to MongoDB - producer.ConnectMongo(mongodb.Url, mongodb.Name) + producer.ConnectMongo(mongodb.Url, mongodb.Name, mongodb.AuthUrl, mongodb.AuthKeysDbName) initLog.Infoln("Server started") router := logger_util.NewGinWithLogrus(logger.GinLog)