Coder Social home page Coder Social logo

node-mongo-with-auth's Introduction

Node - Mongo with authentication enabled

🇬🇧 ⚠️ ⚠️ ⚠️ This repo is simply intended as a playground to try fixing issues I got with authentication in a dockerized Node.js/mongoDB application. Don't take my word for it.

🇫🇷 ⚠️ ⚠️ ⚠️ Ce repo est un bac à sable visant à identifier et régler des problèmes rencontrés avec l'authentification, dans une application Node.js/mongoDB. Ne prenez pas le contenu de ce repo pour argent comptant !

Sources:

Directement sur l'instance de l'hôte macOS

Tuto medium adapté pour macOS (mongo 4.4 installé avec brew)

1. démarrage mongo

2. connexion

Connection en local (j'avais pas besoin de mettre l'URL de connexion mais bon).``` mongo mongodb://localhost:27017


show dbs admin 0.000GB config 0.000GB local 0.000GB np_media_cache 0.135GB nporder_dev 0.011GB pfpa 0.000GB pfpa_dev 0.000GB


### 3. créer admin

Exécuter `use admin`

Puis :

db.createUser( { user: "superadmin", pwd: "AdminP4ss", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )


Logout

### 4. activer l'authentification

Ajouter à `/usr/local/etc/mongod.conf` :

security: authorization: "enabled"


Identifier le service mongo avec `brew services list`

Puis redémarrer le service : `brew services restart [email protected]`

### 5. Se connecter comme admin

mongo mongodb://localhost:27017 ```Puis dans le shell mongo db.auth("superadmin","AdminP4ss"); (par contre l'auth direct en mettant `mongo mongodb://superadmin:AdminP4ss@localhost` ne fonctionne pas)Et effectivement, `show dbs` non identifié ne montre rien, mais une fois identifié on voit les dbs.

6. créer d'autres utilisateurs

a. test app Node.js

AJOUTER LIEN REPO

Lancer l'app node : MONGO_HOST=localhost node index (username = nodeapp, password = passwd, nom db = nodemongo) Echec (tente de se connecter sur mongodb://nodeapp:passwd@localhost:27017/nodemongo) :

MongoServerError: Authentication failed.
    at Connection.onMessage (/Users/benoit/Code/node-mongo-with-auth/node_modules/mongodb/lib/cmap/connection.js:230:30)
    at MessageStream.<anonymous> (/Users/benoit/Code/node-mongo-with-auth/node_modules/mongodb/lib/cmap/connection.js:61:60)
    at MessageStream.emit (node:events:527:28)
    at processIncomingData (/Users/benoit/Code/node-mongo-with-auth/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
    at MessageStream._write (/Users/benoit/Code/node-mongo-with-auth/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:527:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}

b. création user

db.createUser(
  {
    user: "nodeapp",
    pwd: "passwd",
    roles: [ { role: "readWrite", db: "nodemongo" } ]
  }
)

c. lancement app Node après coup

Toujours la même erreur. Erreurs possibles ? Ai-je créé l'user en étant dans la db admin ?

Relance mongo puis db.auth("superadmin","AdminP4ss"); puis use admin puis :

db.createUser(
  {
    user: "nodeapp",
    pwd: "passwd",
    roles: [ { role: "readWrite", db: "nodemongo" } ]
  }
)

TOUJOURS erreur en lançant mongo mongodb://nodeapp:AdminP4ss@localhost:27017/nodemongo?authSource=admin

MAIS je viens de voir dans l'article Medium qu'il fallait se mettre sur la db nodemongo (eux c'est test) avant de créer l'user !!

Mais même en faisant use nodemongo avant de créer l'user, même pb

Dans une VM Linux

Remplacé 6.0 par 4.4.

sudo apt-get install -y gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Récupération app

Cloné le repo de l'app, elle marche sans l'auth.

1. Ajout admin mongo

Suivre le tuto de DigitalOcean

Run mongo, puis use admin, puis adapte l'original du tuto de ceci :

db.createUser(
  {
    user: "AdminSammy",
    pwd: passwordPrompt(),
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

… à ceci (mix entre tuto medium et celui-ci) :

db.createUser(
  {
    user: "superadmin",
    pwd: "AdminP4ss",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Puis quitte.

2. Activation auth

sudo nano /etc/mongod.conf

et ajoute :

security:
  authorization: enabled

puis :

sudo systemctl restart mongod

Test l'accès :

mongo -u superadmin -p --authenticationDatabase admin

Entre le pwd AdminP4ss. Ça marche.

Dans l'app node...

Ça marche avec le compte admin.

Le .env qui va avec :

MONGO_HOST=localhost
MONGO_USERNAME=superadmin
MONGO_PASSWORD=AdminP4ss
MONGO_DBNAME=admin

Création d'un compte standard

db.createUser(
  {
    user: "nodeapp",
    pwd: "passwd",
    roles: [
      { role: "readWrite", db: "nodemongo" }
    ]
  }
)

Le .env qui va avec :

MONGO_HOST=localhost
MONGO_USERNAME=nodeapp
MONGO_PASSWORD=passwd
MONGO_DBNAME=nodemongo

Et ça marche !

3. essayons de comprendre

Comment se crée le compte utilisateur dans l'image Docker ?

On clone localement le repo de l'image officielle mongo.

Ajout de cette ligne au fichier 4.4/docker-entrypoint.sh, ligne 372.

			echo ">>> CREATING USER: ${mongo[@]} $rootAuthDatabase"

En fait ${mongo[@]} $rootAuthDatabase se traduit, une fois exécuté, en mongo --host 127.0.0.1 --port 27017 --quiet admin.

Donc ça exécute la création de l'user sur la db admin.

Une fois le conteneur lancé avec :

  MONGO_INITDB_DATABASE: nodemongo
  MONGO_INITDB_ROOT_USERNAME: nodeapp
  MONGO_INITDB_ROOT_PASSWORD: passwd
  • On se connecte avec docker exec -it mongo bash,
  • puis mongo --host 127.0.0.1 --port 27017 --quiet admin,
  • puis db.auth("nodeapp", "passwd"),
  • puis on peut lister les utilisateurs : db.system.users.find().

Résultat :

{
  "_id": "admin.nodeapp",
  "userId": UUID("59c1743a-2704-46ea-8789-14967267f924"),
  "user": "nodeapp",
  "db": "admin",
  "credentials": {
    "SCRAM-SHA-1": {
      "iterationCount": 10000,
      "salt": "5GKmqdLk7U8g/hkR0XEGmg==",
      "storedKey": "LScjo2icydGuaBzWE/vdg8rjTsM=",
      "serverKey": "jTvWUXAAef35Psq/WXJNy/VtSMY="
    },
    "SCRAM-SHA-256": {
      "iterationCount": 15000,
      "salt": "C1zDT6l5j0n/yUTqBzoGelpABR+KkEAdOIykaQ==",
      "storedKey": "AXC7NyYhGKgCxzYKpMpBKa0ssH8uuahchnhMjAj5jiU=",
      "serverKey": "NfYKZvHRaNuRtm5UDl21EHFk8QLbBoQfGJ5wYAbdLcQ="
    }
  },
  "roles": [
    {
      "role": "root",
      "db": "admin"
    }
  ]
}

node-mongo-with-auth's People

Contributors

bhubr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.