Guide on transferring the subject name from a certificate to set up an x509 user in MongoDB through the command line by employing the --eval flag


I encountered an issue with passing the subject name from a shell script to my mongo instance in order to create an x509 user. The SUBJECT environment variable seems to have stripped spaces and lost the CN= portion upon authentication.

Here is the shell script I used to extract the subject from the certificate:

SUBJECT=$(openssl x509 -in server-cert.pem -inform PEM -subject -nameopt RFC2253 | grep subject | sed 's/subject=//')

And here is the command line used to execute JavaScript code with the saved SUBJECT environment variable:

/usr/bin/mongo --ssl --sslPEMKeyFile server-combined.pem admin --eval "var SUBJECT = '$SUBJECT'" createUser.js

Below is the content of my createUser.js file:

    addUser(SUBJECT);
    function addUser(SUBJECT) {
     print(SUBJECT);
     db.getSiblingDB("\$external").runCommand({ createUser:SUBJECT,roles: [{role: "readWrite", db: "test"},{ role: "userAdminAnyDatabase", db: "admin" }],  writeConcern: { w: "majority" , wtimeout: 5000 }});
     db.getSiblingDB("\$external").auth({mechanism: "MONGODB-X509", user: SUBJECT})}

Expected value:

CN=\ 10.1.1.1,OU=A,O=PP,ST=PA,C=US

Actual value:

CN= 10.1.1.1,OU=A,O=PP,ST=PA,C=US

Error message from MongoDB logs:

   Error: 18 Username " CN= 10.1.1.1,OU=A,O=PP,ST=PA,C=US" does not match the provided client certificate user "CN=\ 10.1.1.1,OU=A,O=PP,ST=PA,C=US"

Build environment details:

Operating System: Linux Ubuntu

MongoDB version: v2.6

    docker pull mongo:2.6
    docker build . 

Dockerfile contents:

    FROM mongo:2.6 
    ADD createUser.js /opt/
    ADD scriptuser.sh /opt/

Answer №1

I have created a new file named env.js where I store the SN information. This allows me to set up the correct SN and load my createUsers.js file when connecting to mongo.

/usr/bin/mongo --ssl --sslPEMKeyFile server.pem admin env.js createUser.js

Within the createUser.js file, I made some modifications:

function addUser() {
SUBJECTJS=SUBJECTJS.toString().replace("CN=","CN=\\");
print(SUBJECTJS);
printjson(db.getSiblingDB('\$external').runCommand({ createUser: SUBJECTJS,roles: [{role: "readWrite", db: "sda"},{ role: "userAdminAnyDatabase", db: "admin" }],  writeConcern: { w: "majority" , wtimeout: 5000 }}));
printjson(db.getSiblingDB("\$external").auth({ mechanism: "MONGODB-X509", user: SUBJECTJS }));
} 
addUser();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a data property error while trying to render ejs using axios

I am encountering an error message "TypeError: Cannot read property 'data' of undefined" when trying to display results.ejs using data from an API. Can someone help me identify what's incorrect with the rendering code? Thank you. index.js: ...

Initiate the process of displaying data on a datetime chart using Highcharts

I am currently developing a yearly chart, but I've encountered a small issue. The chart begins in January, however there is no data available until May. The client specifically wants the chart to only display when there is data available, and unfortu ...

Exploring the Mystery of why Three.js Fails to Apply Textures to Objects

I'm attempting to add a texture to an object with the following code: <html> <head> <title>My first Three.js app</title> <style> body { margin: 0; } canvas { width: 100%; h ...

Trouble accessing Login.html page

Working on a web server project for my job has been both challenging and rewarding. One recurring issue I have encountered is with the login page not redirecting me to the main page and failing to accept the username or password. The codebase was originall ...

Establish and define the global variable "Protractor"

In order to pass a string value from the function editPage.CreateEntity();, you can use that value in multiple test cases by assigning it to the variable entityName. You are able to retrieve the value by setting up the test case as follows: fit('Te ...

Issue with adding a video to a playlist using Youtube API

While I have successfully implemented GET requests using the Youtube API v3, I am encountering difficulties when trying to make a POST request to add a video to a playlist. Despite trying various approaches such as including the key in the query string, pl ...

Updating the view manually is necessary when using AngularJS $routeProvider and resolve feature

I am facing issues with $routeProvider in updating the view automatically. Currently, I am working on converting a chapter 22 example from Adam Freeman's Pro Angular book, originally based on Deployd, to Express-Mongo. Strangely, the automatic refres ...

Guide on updating location and reloading page in AngularJS

I have a special function: $scope.insert = function(){ var info = { 'username' : $scope.username, 'password' : $scope.password, 'full_name' : $scope.full_name } $http({ method: &ap ...

"Can you provide some guidance on transferring the selected row value to a button that is located outside the grid, including a parameter in v-data-table

<v-toolbar flat> <v-toolbar-title>Details</v-toolbar-title> <div style="width:100%"> <v-col class="text-right"> <v-btn id="btnCopy" @click="Redirect()" clas ...

Issue: Unable to establish connection on 127.0.0.1:8000 while testing Express Js using Postman

I am completely new to this industry and I'm encountering an error when attempting to check the database connection using API requests in Postman... I'm seeking assistance to resolve this issue... I just want to test the MongoDB database by send ...

The forward button is malfunctioning after using history.pushState

I have managed to resolve issues with the back button, however, I am still unable to fix the forward button. Despite the URL changing, the page does not reload. Here is the code snippet that I am currently using: $('.anchor .wrapper').css({ &a ...

Creating a masterpiece on the final piece of paper

As someone who is new to programming with JavaScript and canvas, I have a function called drawLines(canvasIndex, startPosition) that is used to draw lines on a canvas. The function takes in two arguments: canvasIndex, which represents the canvas being draw ...

Challenges with updating one-to-many relations in MongoDB

I've been working on setting up a blog using MongoDB. Let's take a closer look at how MongoDB suggests we store blog posts and their comments (): Posts should be stored in a collection, while comments should be embedded as objects within a pos ...

Speeding up the loading time of my background images

body { background: url(http://leona-anderson.com/wp-content/uploads/2014/10/finalbackgroundMain.png) fixed; background-size:100% auto; } I have unique background images on each of my sites, but they are large in size and take some time to load due to bein ...

How can I fill in 3 textboxes with the selected Autocomplete value in MVC 4?

I am trying to implement autocomplete functionality in my form, where a text box is already attached to autocomplete. However, I am unsure how to trigger the ActionResult (which returns JSON) when a value is selected, extract the JSON result, and populate ...

Implementing AJAX to dynamically insert content into div elements on the page

Currently facing a small issue with my AJAX implementation for creating comments on posts. The functionality is working well, but the problem arises when executing it in the index.html.erb view. The create.js.erb file locates the initial div labeled "comme ...

Executing dynamic filters in MongoDB updateMany

Is there a way to leverage UpdateMany with dynamic filters in the MongoDB Realm Node.js SDK? I'm working with an array of objects and I need to update/insert each object into the database using UpdateMany with dynamic filters. Below is a sample of th ...

Loading images dynamically without changing the src attribute

My website displays 40 baby names on each page, with user-uploaded photos associated with each name. Currently, I am using iframes to show the images when a user clicks on the "show photos" button. However, this method prevents the images from being indexe ...

Is it possible to merge string and span elements to create a unified element?

In my current project using React, I am dealing with an array of strings and JSX Elements. For instance, the array has items like: [<span class="name">JCrew0</span>, "edited this document"]. I am attempting to display thes ...

Executing a function right away when it run is a trait of a React functional component

Having a fully functional React component with useState hooks, and an array containing five text input fields whose values are stored in the state within an array can be quite challenging. The main issue I am facing is that I need to update the inputfields ...