Skip to content

Commit 8ba711e

Browse files
Merge pull request #55 from babylonchain/add-auth-for-mongo-2
fix: add mongo auth connection
2 parents ba1be1b + 6be8761 commit 8ba711e

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

bin/init-mongo.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,15 @@ sleep 10
99
# Initiate the replica set
1010
mongosh --eval "rs.initiate({_id: 'RS', members: [{ _id: 0, host: 'mongodb:27017' }]})"
1111

12+
# Create the root user
13+
mongosh --eval "
14+
db = db.getSiblingDB('admin');
15+
db.createUser({
16+
user: 'root',
17+
pwd: 'example',
18+
roles: [{ role: 'root', db: 'admin' }]
19+
});
20+
"
21+
1222
# Keep the container running
1323
tail -f /dev/null

internal/config/db.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ import (
77
)
88

99
type DbConfig struct {
10-
DbName string `mapstructure:"db-name"`
11-
Address string `mapstructure:"address"`
10+
Username string `mapstructure:"username"`
11+
Password string `mapstructure:"password"`
12+
DbName string `mapstructure:"db-name"`
13+
Address string `mapstructure:"address"`
1214
}
1315

1416
func (cfg *DbConfig) Validate() error {
17+
if cfg.Username == "" {
18+
return fmt.Errorf("missing db username")
19+
}
20+
21+
if cfg.Password == "" {
22+
return fmt.Errorf("missing db password")
23+
}
24+
1525
if cfg.Address == "" {
1626
return fmt.Errorf("missing db address")
1727
}
@@ -52,7 +62,9 @@ func (cfg *DbConfig) Validate() error {
5262

5363
func DefaultDBConfig() *DbConfig {
5464
return &DbConfig{
55-
DbName: "staking-api-service",
56-
Address: "mongodb://localhost:27017",
65+
Username: "root",
66+
Password: "example",
67+
DbName: "staking-api-service",
68+
Address: "mongodb://localhost:27017",
5769
}
5870
}

internal/db/dbclient.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"go.mongodb.org/mongo-driver/mongo"
88
"go.mongodb.org/mongo-driver/mongo/options"
99

10+
"github.com/babylonchain/cli-tools/internal/config"
1011
"github.com/babylonchain/cli-tools/internal/db/model"
1112
)
1213

@@ -15,15 +16,19 @@ type Database struct {
1516
Client *mongo.Client
1617
}
1718

18-
func New(ctx context.Context, dbName string, dbURI string) (*Database, error) {
19-
clientOps := options.Client().ApplyURI(dbURI)
19+
func New(ctx context.Context, cfg config.DbConfig) (*Database, error) {
20+
credential := options.Credential{
21+
Username: cfg.Username,
22+
Password: cfg.Password,
23+
}
24+
clientOps := options.Client().ApplyURI(cfg.Address).SetAuth(credential)
2025
client, err := mongo.Connect(ctx, clientOps)
2126
if err != nil {
2227
return nil, err
2328
}
2429

2530
return &Database{
26-
DbName: dbName,
31+
DbName: cfg.DbName,
2732
Client: client,
2833
}, nil
2934
}

internal/services/unbonding_pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewUnbondingPipelineFromConfig(
9797
ret *VersionedParamsRetriever,
9898
) (*UnbondingPipeline, error) {
9999

100-
db, err := db.New(context.TODO(), cfg.Db.DbName, cfg.Db.Address)
100+
db, err := db.New(context.TODO(), cfg.Db)
101101

102102
if err != nil {
103103
return nil, err

itest/containers/containers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ func (m *Manager) RunMongoDbResource() (*dockertest.Resource, error) {
194194
ExposedPorts: []string{
195195
"27017",
196196
},
197+
Env: []string{
198+
"MONGO_INITDB_ROOT_USERNAME=root",
199+
"MONGO_INITDB_ROOT_PASSWORD=example",
200+
},
197201
},
198202
dockerConf,
199203
)

itest/e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func StartManager(
214214
}
215215

216216
if runMongodb {
217-
testDbConnection, err := db.New(context.TODO(), appConfig.Db.DbName, appConfig.Db.Address)
217+
testDbConnection, err := db.New(context.TODO(), appConfig.Db)
218218
require.NoError(t, err)
219219

220220
storeController := services.NewPersistentUnbondingStorage(testDbConnection)
@@ -622,7 +622,7 @@ func TestSendingFreshTransactions(t *testing.T) {
622622
}
623623

624624
func (tm *TestManager) updateSchnorSigInDb(newSig *schnorr.Signature, txHash *chainhash.Hash) {
625-
db, err := db.New(context.TODO(), tm.pipeLineConfig.Db.DbName, tm.pipeLineConfig.Db.Address)
625+
db, err := db.New(context.TODO(), tm.pipeLineConfig.Db)
626626
require.NoError(tm.t, err)
627627
txHashHex := txHash.String()
628628
client := db.Client.Database(db.DbName).Collection(model.UnbondingCollection)

0 commit comments

Comments
 (0)