<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ozgur Ozvaris</title>
    <description>The latest articles on DEV Community by Ozgur Ozvaris (@ozgurozvaris).</description>
    <link>https://dev.to/ozgurozvaris</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1113122%2Fd1217f08-465d-4d27-a6c6-7c867db85760.jpg</url>
      <title>DEV Community: Ozgur Ozvaris</title>
      <link>https://dev.to/ozgurozvaris</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ozgurozvaris"/>
    <language>en</language>
    <item>
      <title>x.509 Certificate Authentication TLS/SSL connection to Docker MongoDB 7.0.1 - 2/2</title>
      <dc:creator>Ozgur Ozvaris</dc:creator>
      <pubDate>Thu, 07 Sep 2023 11:34:27 +0000</pubDate>
      <link>https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-701-22-4jgi</link>
      <guid>https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-701-22-4jgi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the first article &lt;a href="https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-1-2hik"&gt;here&lt;/a&gt; we discussed about how we can generate a x.509 certificate for secure connection to MongoDB. In this article we will implement these certificate files into docker MongoDB container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring MongoDB docker container instance with the x.509 certificate.
&lt;/h2&gt;

&lt;p&gt;mongod.conf&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;net:
   port: 27017
   bindIp: 0.0.0.0
   tls:
      mode: requireTLS
      certificateKeyFile: /etc/ssl/server.pem
      CAFile: /etc/ssl/ca.crt
security:
   authorization: enabled
   clusterAuthMode: x509
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MongoDB Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official MongoDB Docker image as base
FROM mongo:latest

# Port for MongoDB to run on
EXPOSE 27017

# Update the image and install the Vim package
RUN apt-get update &amp;amp;&amp;amp; \
    apt-get install -y vim &amp;amp;&amp;amp; \
    rm -rf /var/lib/apt/lists/*

# Copy certificates to the container
COPY ./crt/server.pem /etc/ssl
COPY ./crt/client.pem /etc/ssl
COPY ./crt/ca.crt /etc/ssl

# Copy the configuration file to the container
COPY mongod.conf /etc/mongod.conf

RUN echo "********************************************************"

# Start MongoDB with custom configuration
CMD ["mongod", "--config", "/etc/mongod.conf"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;docker-compose.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
services:
  mongo:
    build: ./mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - ./mongodata:/data/db
      - ./mongo/cert:/cert
    ports:
      - 27017:27017  

volumes:
  mongodata:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;directory structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── mongo
│   ├── crt
│   ├── Dockerfile
│   └── mongod.conf
├── ubuntu
│   ├── crt
│   │   ├── ca.key
│   │   ├── ca.srl
│   │   ├── client.crt
│   │   ├── client.csr
│   │   ├── client.key
│   │   ├── server.crt
│   │   ├── server.csr
│   │   └── server.key
│   └── Dockerfile
├── .gitignore
└── docker-compose.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the certificate files from the /ubuntu/crt folder to the /mongo/crt folder.&lt;/p&gt;

&lt;p&gt;The new directory structure after copying the certificate files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── mongo
│   ├── crt
│   │   ├── ca.crt
│   │   ├── client.pem
│   │   └── server.pem
│   ├── Dockerfile
│   └── mongod.conf
├── ubuntu
│   ├── crt
│   │   ├── ca.key
│   │   ├── ca.srl
│   │   ├── client.crt
│   │   ├── client.csr
│   │   ├── client.key
│   │   ├── server.crt
│   │   ├── server.csr
│   │   └── server.key
│   └── Dockerfile
├── .gitignore
└── docker-compose.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run/up docker compose&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose up
docker ps --all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CONTAINER ID   IMAGE           PORTS           NAMES
aa8e5b6a87af   root-mongo      ...             root-mongo-1
182e54aeeca1   ubuntu-custom                   ubuntu1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;exec root-mongo-1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it root-mongo-1 bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root@aa8:/# 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing a MongoDB instance secured with the x.509 certificate through Mongosh
&lt;/h2&gt;

&lt;p&gt;Run mongosh with certificates parameters to connect mongoDB&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongosh --host localhost --tls \
  --tlsCertificateKeyFile /etc/ssl/client.pem \
  --tlsCAFile /etc/ssl/ca.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Using MongoDB:          7.0.1
Using Mongosh:          1.10.6

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we established a successful x.509 certificate tls connection to MongoDB using the previously generated mongodb-cert.key and mongodb.pem x.509 certificate files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ozvaris/mongoDBx509.git" rel="noopener noreferrer"&gt;https://github.com/ozvaris/mongoDBx509.git&lt;/a&gt; &lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>tls</category>
      <category>docker</category>
    </item>
    <item>
      <title>x.509 Certificate Authentication TLS/SSL connection to Docker MongoDB 7.0.1 - 1/2</title>
      <dc:creator>Ozgur Ozvaris</dc:creator>
      <pubDate>Thu, 07 Sep 2023 09:08:16 +0000</pubDate>
      <link>https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-1-2hik</link>
      <guid>https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-1-2hik</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In today's digital world, keeping data safe is crucial. One of the ways databases like MongoDB ensure this safety is by using secure connections. The x.509 certificate is a tool that helps make these connections safe. When used with TLS/SSL, it makes sure both parties in the communication are genuine. This article will explain how MongoDB uses x.509 certificates with TLS/SSL to keep its connections secure.&lt;/p&gt;

&lt;p&gt;In this article series, we will delve into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding the fundamentals of TLS/SSL.&lt;/li&gt;
&lt;li&gt;Generating x.509 certificates using OpenSSL in docker container.&lt;/li&gt;
&lt;li&gt;Configuring MongoDB docker container instance with the x.509 certificate.&lt;/li&gt;
&lt;li&gt;Accessing a MongoDB instance secured with the x.509 certificate through Mongosh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding the fundamentals of TLS/SSL
&lt;/h2&gt;

&lt;p&gt;The main goal of TLS/SSL is to provide privacy and data integrity between two communicating computer applications.&lt;/p&gt;

&lt;p&gt;SSL is an older technology that contains some security flaws. &lt;em&gt;Transport Layer Security (TLS) is the upgraded version of SSL&lt;/em&gt; that fixes existing SSL vulnerabilities. TLS authenticates more efficiently and continues to support encrypted communication channels.&lt;/p&gt;

&lt;p&gt;When a connection is made, a "handshake" occurs. During this handshake, both parties agree on a version of the protocol, choose cryptographic algorithms, and authenticate each other (&lt;em&gt;typically through certificates like x.509&lt;/em&gt;). This ensures the session is both confidential (due to encryption) and reliable (due to integrity checks).&lt;/p&gt;

&lt;p&gt;Digital certificates, often based on the &lt;em&gt;x.509 standard&lt;/em&gt;, play a crucial role in TLS/SSL. They are used to authenticate the identity of a website or server. Certificates are issued by trusted organizations called &lt;em&gt;Certificate Authorities (CAs)&lt;/em&gt;. For the purposes of our demonstration, we will employ &lt;em&gt;OpenSSL&lt;/em&gt; to generate such a certificate&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating x.509 certificates using OpenSSL in docker container.
&lt;/h2&gt;

&lt;p&gt;When establishing a secure x.509 connection for MongoDB, there are specific steps and considerations to take into account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker Containerized Environment: We'll be leveraging Docker, a powerful tool that lets us create isolated, containerized environments. This ensures that our OpenSSL operations are consistent and easily reproducible across different platforms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenSSL and Self-Signed Certificates: OpenSSL is a robust toolkit for the TLS and SSL protocols. For our purposes, we'll use OpenSSL to produce a &lt;em&gt;'self-signed'&lt;/em&gt; certificate. While self-signed certificates offer encryption, they don't have the backing of a &lt;em&gt;Certificate Authority (CA)&lt;/em&gt;, which means they might not be suitable for production environments where trust verification is crucial.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Key and Certificate Files: The certificate generation process will yield two main files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A key file (*.key): This private key should be kept secret and is used for decryption.&lt;/li&gt;
&lt;li&gt;Certificate Signing Request (CSR) file (*.csr): This file is a formal request to a Certificate Authority (CA) for a certificate. It contains information about the entity and the public key. While not directly used for the connection, the CSR is vital for generating the actual certificate, especially if you plan to get your certificate signed by a CA in the future.&lt;/li&gt;
&lt;li&gt;A certificate file (*.crt): This public certificate contains the public key and can be shared. It’s what servers and clients will use to establish a mutual trust.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Docker Containerized Environment
&lt;/h1&gt;

&lt;p&gt;Folder Structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── crt
└── Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dockerfile &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;openssl added inside the custom ubuntu image
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Using the official Ubuntu 20.04 base Docker image
FROM ubuntu:20.04

# Maintainer information
LABEL maintainer="your name &amp;lt;email@example.com&amp;gt;"

# Ensure non-interactive setup (useful when installing packages)
ENV DEBIAN_FRONTEND=noninteractive

# Update packages and install required packages
RUN apt-get update &amp;amp;&amp;amp; \
    apt-get install -y \
    wget \
    curl \
    build-essential \
    openssl \
    vim &amp;amp;&amp;amp; \  
    apt-get clean

# Set the working directory
WORKDIR /app

# Command to run when the Docker container starts
CMD ["/bin/bash"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;docker image build and run docker container&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;project "crt" folder mounted as volume
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t ubuntu-custom .
docker run -it -v "$(pwd)/crt:/app/crt" --name ubuntu1 ubuntu-custom 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;console output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root@182:/app# cd crt
root@182:/app/crt#
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certificate Authority (CA)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -days 3650 -subj '/CN=MyCA/OU=myOrgUnit/O=myOrg/L=myLocality/ST=myState/C=US' -out ca.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generating the server x.509 Certificate files. Run commands below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -newkey rsa:2048 -days 3650 -nodes -subj '/CN=localhost/OU=myOrgUnit/O=myOrg/L=myLocality/ST=myState/C=US' -out server.csr -keyout server.key
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650 -extfile &amp;lt;(echo -e "keyUsage = digitalSignature, keyEncipherment\nextendedKeyUsage = serverAuth")
cat server.key server.crt &amp;gt; server.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generating the client x.509 Certificate files. Run commands below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -newkey rsa:2048 -days 3650 -nodes -subj '/CN=x509user/OU=myOrgUnit/O=myOrg/L=myLocality/ST=myState/C=US' -out client.csr -keyout client.key
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 3650 -extfile &amp;lt;(echo -e "keyUsage = digitalSignature, keyEncipherment\nextendedKeyUsage = clientAuth")
cat client.key client.crt &amp;gt; client.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Revised folder structure after x.509 certificate production&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── crt
│   ├── ca.key
│   ├── ca.srl
│   ├── client.crt
│   ├── client.csr
│   ├── client.key
│   ├── server.crt
│   ├── server.csr
│   └── server.key
└── Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we generated the mongodb-cert.key and mongodb.pem x.509 certificate files. We will be implementing these files into MongoDB in the next article &lt;a href="https://dev.to/ozgurozvaris/x509-certificate-authentication-tlsssl-connection-to-mongodb-701-22-4jgi"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ozvaris/mongoDBx509.git" rel="noopener noreferrer"&gt;https://github.com/ozvaris/mongoDBx509.git&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>tls</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
