{"id":947,"date":"2018-12-27T23:55:05","date_gmt":"2018-12-27T23:55:05","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=947"},"modified":"2018-12-28T19:47:25","modified_gmt":"2018-12-28T19:47:25","slug":"securing-a-containerized-instance-of-mongodb","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/12\/27\/securing-a-containerized-instance-of-mongodb\/","title":{"rendered":"Securing a Containerized Instance of MongoDB"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2017\/03\/09103614\/mongodb-300x81.png\" alt=\"Securing\nMongoDB\" \/>MongoDB, the popular<br \/>\nopen source NoSQL database, has been in the news a lot recently\u2014and<br \/>\nnot for reasons that are good for MongoDB admins. Early this year,<br \/>\n<a href=\"https:\/\/arstechnica.com\/information-technology\/2017\/01\/more-than-10000-online-databases-taken-hostage-by-ransomware-attackers\/\">reports began<br \/>\nappearing<\/a><br \/>\nof MongoDB databases being \u201ctaken hostage\u201d by attackers who delete all<br \/>\nof the data stored inside the databases, then demand ransoms to restore<br \/>\nit. Security is always important, no matter which type of database<br \/>\nyou\u2019re using. But the recent spate of MongoDB attacks makes it<br \/>\nespecially crucial to secure any MongoDB databases that you may use as<br \/>\npart of your container stack. This article explains what you need to<br \/>\nknow to keep MongoDB secure when it is running as a container. We\u2019ll go<br \/>\nover how to close the vulnerability behind the recent ransomware attacks<br \/>\nusing a MongoDB container while the container is running\u2014as well as<br \/>\nhow to modify a MongoDB Dockerfile to change the default behavior<br \/>\npermanently.<\/p>\n<h3>Why the Ransomware Happened: MongoDB\u2019s Default Security Configuration<\/h3>\n<p><a href=\"https:\/\/attendee.gotowebinar.com\/rt\/2905734731496917249\"><img decoding=\"async\" src=\"http:\/\/cdn.rancher.com\/wp-content\/uploads\/2017\/02\/16180354\/Training.png\" alt=\"\" \/><\/a><br \/>\nRegister now for free online training on deploying containers with<br \/>\nRancher The ransomware attacks against MongoDB weren\u2019t<br \/>\nenabled by a flaw inherent in MongoDB itself, per se, but rather by some<br \/>\nweaknesses that result from default configuration parameters in an<br \/>\nout-of-the-box installation of MongoDB. By default, MongoDB databases,<br \/>\nunlike most other popular database platforms, don\u2019t require login<br \/>\ncredentials. That means anyone can log into the database and start<br \/>\nmodifying or removing data. Securing a MongoDB Container In order to<br \/>\nmitigate that vulnerability and run a secure containerized instance of<br \/>\nMongoDB, follow the steps below. Start a MongoDB instance First of<br \/>\nall, start a MongoDB instance in Docker using the most up-to-date image<br \/>\navailable. Since Docker uses the most recent image by default, a simple<br \/>\ncommand like this will start a MongoDB instance based on an up-to-date<br \/>\nimage:<\/p>\n<p>docker run &#8211;name mongo-database -d mongo<\/p>\n<p>Create a secure MongoDB account Before disabling password-less<br \/>\nauthentication to MongoDB, we need to create an account that we can use<br \/>\nto log in after we change the default settings. To do this, first log<br \/>\ninto the MongoDB container with:<\/p>\n<p>docker exec -it mongo-database bash<\/p>\n<p>Then, from inside the container, log into the MongoDB admin interface:<\/p>\n<p>Now, enter this stanza of code and press Enter:<\/p>\n<p>use admin<br \/>\ndb.createUser(<br \/>\n{<br \/>\nuser: &#8220;db_user&#8221;,<br \/>\npwd: &#8220;your_super_secure_password&#8221;,<br \/>\nroles: [ { role: &#8220;userAdminAnyDatabase&#8221;, db: &#8220;admin&#8221; } ]<br \/>\n}<br \/>\n)<\/p>\n<p>This creates a MongoDB user with username db_user and password<br \/>\nyour_super_secure_password (Feel free to change this, of course, to<br \/>\nsomething more secure!) The user has admin privileges. Changing<br \/>\ndefault behavior in Dockerfile If you want to make the MongoDB process<br \/>\nstart with authentication required by default all of the time, you can<br \/>\ndo so by editing the Dockerfile used to build the container. To do this<br \/>\nlocally, we\u2019ll first pull the MongoDB Dockerfile from GitHub with:<\/p>\n<p>git clone https:\/\/github.com\/dockerfile\/mongodb<\/p>\n<p>Now, cd into the mongodb directory that Git just created and open the<br \/>\nDockerfile inside using your favorite text editor. Look for the<br \/>\nfollowing section of the Dockerfile:<\/p>\n<p># Define default command.<br \/>\nCMD [&#8220;mongod&#8221;]<\/p>\n<p>Change this to:<\/p>\n<p># Define default command.<br \/>\nCMD [&#8220;mongod &#8211;auth&#8221;]<\/p>\n<p>This way, when mongodb is called when the container starts, it will run<br \/>\nwith the &#8211;auth flag by default.<\/p>\n<h3>Conclusion<\/h3>\n<p>If you follow the steps above, you\u2019ll be able to run MongoDB as a Docker<br \/>\ncontainer without becoming one of the tens of thousands of admins whose<br \/>\nMongoDB databases were wiped out and held for ransom by attackers. And<br \/>\nthere really is not much to it, other than being aware of the<br \/>\nvulnerabilities inherent in a default MongoDB installation and the steps<br \/>\nfor resolving them. <em>Chris Riley (@HoardingInfo) is a technologist who<br \/>\nhas spent 12 years helping organizations transition from traditional<br \/>\ndevelopment practices to a modern set of culture, processes and tooling.<br \/>\nIn addition to being a research analyst, he is an O\u2019Reilly author,<br \/>\nregular speaker, and subject matter expert in the areas of DevOps<br \/>\nstrategy and culture. Chris believes the biggest challenges faced in the<br \/>\ntech market are not tools, but rather people and planning.<\/em><\/p>\n<p><a href=\"https:\/\/rancher.com\/securing-containerized-instance-mongodb\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB, the popular open source NoSQL database, has been in the news a lot recently\u2014and not for reasons that are good for MongoDB admins. Early this year, reports began appearing of MongoDB databases being \u201ctaken hostage\u201d by attackers who delete all of the data stored inside the databases, then demand ransoms to restore it. Security &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/12\/27\/securing-a-containerized-instance-of-mongodb\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Securing a Containerized Instance of MongoDB&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-947","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/947\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}