diff --git a/bin/init-mongo.sh b/bin/init-mongo.sh index f25d98e..55a52af 100755 --- a/bin/init-mongo.sh +++ b/bin/init-mongo.sh @@ -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'); diff --git a/config/config-docker.yml b/config/config-docker.yml index 58ebeab..4678dc1 100644 --- a/config/config-docker.yml +++ b/config/config-docker.yml @@ -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 diff --git a/config/config-local.yml b/config/config-local.yml index 8d8310f..8f0a032 100644 --- a/config/config-local.yml +++ b/config/config-local.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 5c216ed..e1f02bb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" ] diff --git a/internal/config/db.go b/internal/config/db.go index 55fe795..e7ee645 100644 --- a/internal/config/db.go +++ b/internal/config/db.go @@ -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"` @@ -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") } diff --git a/internal/db/dbclient.go b/internal/db/dbclient.go index 021e46c..b6b9149 100644 --- a/internal/db/dbclient.go +++ b/internal/db/dbclient.go @@ -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 diff --git a/internal/db/model/setup.go b/internal/db/model/setup.go index 5fafac5..f544a4c 100644 --- a/internal/db/model/setup.go +++ b/internal/db/model/setup.go @@ -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 diff --git a/tests/config/config-test.yml b/tests/config/config-test.yml index ed49ce4..94940ce 100644 --- a/tests/config/config-test.yml +++ b/tests/config/config-test.yml @@ -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