Skip to content

Commit

Permalink
Merge pull request #21 from babylonchain/add-auth-for-mongo
Browse files Browse the repository at this point in the history
fix: add mongo auth connection
  • Loading branch information
jrwbabylonlab authored Jul 12, 2024
2 parents 6409ebf + ade6960 commit f2cec60
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions bin/init-mongo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ mongosh --eval "rs.initiate({_id: 'RS', members: [{ _id: 0, host: 'localhost:270
# Wait for replica set to initiate
sleep 5

# Create the root user
mongosh --eval "
db = db.getSiblingDB('admin');
db.createUser({
user: 'root',
pwd: 'example',
roles: [{ role: 'root', db: 'admin' }]
});
"

# Create the necessary indexes
mongosh --eval "
db = db.getSiblingDB('staking-api-service');
Expand Down
2 changes: 1 addition & 1 deletion cmd/staking-expiry-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
metrics.Init(metricsPort)

// create new db client
dbClient, err := db.New(ctx, cfg.Db.DbName, cfg.Db.Address)
dbClient, err := db.New(ctx, cfg.Db)
if err != nil {
log.Fatal().Err(err).Msg("error while creating db client")
}
Expand Down
2 changes: 2 additions & 0 deletions config/config-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ poller:
interval: 5s
log-level: debug
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
btc:
Expand Down
2 changes: 2 additions & 0 deletions config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ poller:
interval: 5s
log-level: debug
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
btc:
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ services:
hostname: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- ./bin/init-mongo.sh:/init-mongo.sh
entrypoint: [ "/init-mongo.sh" ]
Expand Down
14 changes: 12 additions & 2 deletions internal/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import (
)

type DbConfig struct {
DbName string `mapstructure:"db-name"`
Address string `mapstructure:"address"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DbName string `mapstructure:"db-name"`
Address string `mapstructure:"address"`
}

func (cfg *DbConfig) Validate() error {
if cfg.Username == "" {
return fmt.Errorf("missing db username")
}

if cfg.Password == "" {
return fmt.Errorf("missing db password")
}

if cfg.Address == "" {
return fmt.Errorf("missing db address")
}
Expand Down
11 changes: 8 additions & 3 deletions internal/db/dbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/babylonchain/staking-expiry-checker/internal/config"
"github.com/babylonchain/staking-expiry-checker/internal/db/model"
)

Expand All @@ -17,15 +18,19 @@ type Database struct {
client *mongo.Client
}

func New(ctx context.Context, dbName string, dbURI string) (*Database, error) {
clientOps := options.Client().ApplyURI(dbURI)
func New(ctx context.Context, cfg config.DbConfig) (*Database, error) {
credential := options.Credential{
Username: cfg.Username,
Password: cfg.Password,
}
clientOps := options.Client().ApplyURI(cfg.Address).SetAuth(credential)
client, err := mongo.Connect(ctx, clientOps)
if err != nil {
return nil, err
}

return &Database{
dbName: dbName,
dbName: cfg.DbName,
client: client,
}, nil
}
Expand Down
2 changes: 2 additions & 0 deletions tests/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ poller:
interval: 2s
log-level: debug
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
btc:
Expand Down
2 changes: 1 addition & 1 deletion tests/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func setupTestServer(t *testing.T, dep *TestServerDependency) (*queue.QueueManag
dbClient = dep.MockDbClient
} else {
setupTestDB(cfg)
dbClient, err = db.New(ctx, cfg.Db.DbName, cfg.Db.Address)
dbClient, err = db.New(ctx, cfg.Db)
if err != nil {
t.Fatalf("Failed to initialize db client: %v", err)
}
Expand Down

0 comments on commit f2cec60

Please sign in to comment.