Skip to content

Commit

Permalink
Merge pull request #204 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 b138621 + 549ecbc commit 618f311
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 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: 2 additions & 0 deletions config/config-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "mainnet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://mongodb:27017"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down
2 changes: 2 additions & 0 deletions config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "signet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://localhost:27017/?directConnection=true"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
10 changes: 10 additions & 0 deletions internal/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const (
)

type DbConfig struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DbName string `mapstructure:"db-name"`
Address string `mapstructure:"address"`
MaxPaginationLimit int64 `mapstructure:"max-pagination-limit"`
Expand All @@ -19,6 +21,14 @@ type DbConfig struct {
}

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
6 changes: 5 additions & 1 deletion internal/db/dbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type DbResultMap[T any] struct {
}

func New(ctx context.Context, cfg config.DbConfig) (*Database, error) {
clientOps := options.Client().ApplyURI(cfg.Address)
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
Expand Down
6 changes: 5 additions & 1 deletion internal/db/model/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ var collections = map[string][]index{
}

func Setup(ctx context.Context, cfg *config.Config) error {
clientOps := options.Client().ApplyURI(cfg.Db.Address)
credential := options.Credential{
Username: cfg.Db.Username,
Password: cfg.Db.Password,
}
clientOps := options.Client().ApplyURI(cfg.Db.Address).SetAuth(credential)
client, err := mongo.Connect(ctx, clientOps)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions tests/config/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "signet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down

0 comments on commit 618f311

Please sign in to comment.