mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-23 23:10:50 +00:00
- Replaced the existing logging mechanism with logrus for improved logging capabilities. - Introduced a custom logging configuration with support for different log levels and output formats (text and JSON). - Added structured logging with component context for better traceability. - Implemented tests for logging configuration and structured log entries. - Updated WebRTC module to utilize the new logging system, ensuring consistent logging practices across the application. - Added tests to verify that sensitive information is not logged in ONVIF responses and that HTTP request logs include structured fields.
101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type DB struct {
|
|
Client *mongo.Client
|
|
}
|
|
|
|
var TIMEOUT = 10 * time.Second
|
|
var _init_ctx sync.Once
|
|
var _instance *DB
|
|
|
|
var DatabaseName = os.Getenv("MONGODB_DATABASE_FACTORY")
|
|
|
|
func New() *DB {
|
|
|
|
if DatabaseName == "" {
|
|
DatabaseName = "KerberosFactory"
|
|
}
|
|
|
|
mongodbURI := os.Getenv("MONGODB_URI")
|
|
host := os.Getenv("MONGODB_HOST")
|
|
databaseCredentials := os.Getenv("MONGODB_DATABASE_CREDENTIALS")
|
|
replicaset := os.Getenv("MONGODB_REPLICASET")
|
|
username := os.Getenv("MONGODB_USERNAME")
|
|
password := os.Getenv("MONGODB_PASSWORD")
|
|
authentication := "SCRAM-SHA-256"
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
|
|
defer cancel()
|
|
|
|
_init_ctx.Do(func() {
|
|
_instance = new(DB)
|
|
|
|
// We can also apply the complete URI
|
|
// e.g. "mongodb+srv://<username>:<password>@kerberos-hub.shhng.mongodb.net/?retryWrites=true&w=majority&appName=kerberos-hub"
|
|
if mongodbURI != "" {
|
|
log.WithFields(log.Fields{
|
|
"component": "database",
|
|
"database": DatabaseName,
|
|
"event": "client_configuring",
|
|
"uri_configured": true,
|
|
}).Debug("Configuring MongoDB client")
|
|
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
|
|
opts := options.Client().ApplyURI(mongodbURI).SetServerAPIOptions(serverAPI)
|
|
|
|
// Create a new client and connect to the server
|
|
client, err := mongo.Connect(ctx, opts)
|
|
if err != nil {
|
|
log.WithError(err).WithFields(log.Fields{
|
|
"component": "database",
|
|
"database": DatabaseName,
|
|
"event": "client_configuration_failed",
|
|
}).Fatal("Failed to configure MongoDB client")
|
|
}
|
|
_instance.Client = client
|
|
|
|
} else {
|
|
log.WithFields(log.Fields{
|
|
"component": "database",
|
|
"database": DatabaseName,
|
|
"event": "client_configuring",
|
|
"replicaset_configured": replicaset != "",
|
|
"uri_configured": false,
|
|
}).Debug("Configuring MongoDB client")
|
|
|
|
// New MongoDB driver
|
|
mongodbURI := fmt.Sprintf("mongodb://%s:%s@%s", username, password, host)
|
|
if replicaset != "" {
|
|
mongodbURI = fmt.Sprintf("%s/?replicaSet=%s", mongodbURI, replicaset)
|
|
}
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongodbURI).SetAuth(options.Credential{
|
|
AuthMechanism: authentication,
|
|
AuthSource: databaseCredentials,
|
|
Username: username,
|
|
Password: password,
|
|
}))
|
|
if err != nil {
|
|
log.WithError(err).WithFields(log.Fields{
|
|
"component": "database",
|
|
"database": DatabaseName,
|
|
"event": "client_configuration_failed",
|
|
}).Fatal("Failed to configure MongoDB client")
|
|
}
|
|
_instance.Client = client
|
|
}
|
|
})
|
|
|
|
return _instance
|
|
}
|