Skip to content

Commit

Permalink
Store authentication keys in a separate database (#63)
Browse files Browse the repository at this point in the history
Signed-off-by: gatici <[email protected]>
  • Loading branch information
gatici authored Feb 23, 2024
1 parent af50413 commit 71d8b3e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ cscope.*
*.log
*.pcap
vendor*

# coverage
.coverage
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
./ ./...

5 changes: 5 additions & 0 deletions factory/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions producer/data_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 17 additions & 3 deletions producer/db_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 71d8b3e

Please sign in to comment.