<?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: Gowtham S</title>
    <description>The latest articles on DEV Community by Gowtham S (@gowtham06).</description>
    <link>https://dev.to/gowtham06</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%2F2285771%2F74540765-88d4-4906-8d09-fae435dbd379.png</url>
      <title>DEV Community: Gowtham S</title>
      <link>https://dev.to/gowtham06</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gowtham06"/>
    <language>en</language>
    <item>
      <title>How to Integrate HashiCorp Vault with Node.js: Securely Manage Sensitive Data</title>
      <dc:creator>Gowtham S</dc:creator>
      <pubDate>Sun, 27 Oct 2024 10:16:35 +0000</pubDate>
      <link>https://dev.to/gowtham06/how-to-integrate-hashicorp-vault-with-nodejs-securely-manage-sensitive-data-55ek</link>
      <guid>https://dev.to/gowtham06/how-to-integrate-hashicorp-vault-with-nodejs-securely-manage-sensitive-data-55ek</guid>
      <description>&lt;p&gt;When working with sensitive data like passwords, API keys, or personal user information, it's essential to store them securely. Hardcoding secrets in your source code or keeping them in plain text files is a risky approach. This is where HashiCorp Vault comes into play. Vault is an open-source tool for managing secrets, such as credentials, API keys, and sensitive configurations.&lt;br&gt;
In this tutorial, I will walk you through the process of:&lt;br&gt;
Installing HashiCorp Vault on Windows&lt;br&gt;
Integrating it with a Node.js application&lt;br&gt;
Performing basic CRUD operations (Create, Read, Update, Delete) with email-password pairs stored in Vault.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;br&gt;
Before we dive in, make sure you have the following:&lt;br&gt;
Node.js installed on your system.&lt;br&gt;
A basic understanding of JavaScript and Node.js.&lt;br&gt;
HashiCorp Vault installed.&lt;/p&gt;




&lt;p&gt;Step 1: Installing HashiCorp Vault on Windows&lt;br&gt;
Let's start by setting up Vault on your local machine.&lt;br&gt;
Download Vault&lt;br&gt;
Go to the official Vault downloads page and download the Windows binary.&lt;br&gt;
Unzip the downloaded file and move the vault.exe file to a directory (e.g., C:\vault).&lt;/p&gt;

&lt;p&gt;Add Vault to PATH&lt;br&gt;
Open the Start Menu and search for Environment Variables.&lt;br&gt;
In System Variables, find Path and click Edit.&lt;br&gt;
Add the directory where vault.exe is located (C:\vault) to the list.&lt;br&gt;
Open a new Command Prompt and verify the installation by running:&lt;/p&gt;

&lt;p&gt;vault --version&lt;br&gt;
Start Vault in Development Mode&lt;br&gt;
Start the Vault server in development mode using this command:&lt;br&gt;
vault server -dev&lt;br&gt;
This command will launch Vault locally, and you should see a Root Token displayed in the terminal. Save it for later; you'll need it to authenticate.&lt;/p&gt;




&lt;p&gt;Step 2: Setting Up a Node.js Project&lt;br&gt;
Now that Vault is running, we will set up a Node.js project to interact with Vault.&lt;br&gt;
Initialize a New Node.js Project&lt;br&gt;
Create a new directory for your project and navigate into it:&lt;/p&gt;

&lt;p&gt;mkdir vault-node-app cd vault-node-app&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize a new Node.js project:
npm init -y&lt;/li&gt;
&lt;li&gt;Install the required dependencies:
npm install node-vault dotenv
node-vault: This is the official Node.js client for Vault.
dotenv: To manage environment variables.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 3: Integrating Vault with Node.js&lt;br&gt;
Configure Environment Variables&lt;br&gt;
Create a .env file in your project root directory to store Vault configurations:&lt;br&gt;
&lt;code&gt;VAULT_ADDR=http://127.0.0.1:8200&lt;br&gt;
VAULT_TOKEN=&amp;lt;your-root-token&amp;gt;&lt;/code&gt;&lt;br&gt;
Replace  with the root token displayed when you started Vault in development mode.&lt;br&gt;
Create the Node.js Script&lt;br&gt;
Create a new index.js file in your project directory and paste the following code:&lt;br&gt;
&lt;code&gt;require('dotenv').config();&lt;br&gt;
const vault = require('node-vault')({&lt;br&gt;
  apiVersion: 'v1', &lt;br&gt;
  endpoint: process.env.VAULT_ADDR, &lt;br&gt;
  token: process.env.VAULT_TOKEN&lt;br&gt;
});&lt;br&gt;
const SECRET_PATH = 'secret/data/users'; // Path to store user secrets&lt;br&gt;
// Save email and password&lt;br&gt;
async function saveCredentials(email, password) {&lt;br&gt;
  try {&lt;br&gt;
    const result = await vault.write(SECRET_PATH, {&lt;br&gt;
      data: {&lt;br&gt;
        [email]: { password }&lt;br&gt;
      }&lt;br&gt;
    });&lt;br&gt;
    console.log(&lt;/code&gt;Credentials saved for ${email}:&lt;code&gt;, result);&lt;br&gt;
  } catch (error) {&lt;br&gt;
    console.error('Error saving credentials:', error);&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
// Update credentials by email&lt;br&gt;
async function updateCredentials(email, newPassword) {&lt;br&gt;
  try {&lt;br&gt;
    const result = await vault.write(SECRET_PATH, {&lt;br&gt;
      data: {&lt;br&gt;
        [email]: { password: newPassword }&lt;br&gt;
      }&lt;br&gt;
    });&lt;br&gt;
    console.log(&lt;/code&gt;Credentials updated for ${email}:&lt;code&gt;, result);&lt;br&gt;
  } catch (error) {&lt;br&gt;
    console.error('Error updating credentials:', error);&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
// Get credentials by email&lt;br&gt;
async function getCredentials(email) {&lt;br&gt;
  try {&lt;br&gt;
    const result = await vault.read(SECRET_PATH);&lt;br&gt;
    const userData = result.data.data[email];&lt;br&gt;
    if (userData) {&lt;br&gt;
      console.log(&lt;/code&gt;Retrieved credentials for ${email}:&lt;code&gt;, userData);&lt;br&gt;
    } else {&lt;br&gt;
      console.log(&lt;/code&gt;No credentials found for ${email}&lt;code&gt;);&lt;br&gt;
    }&lt;br&gt;
  } catch (error) {&lt;br&gt;
    console.error('Error retrieving credentials:', error);&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
// Delete credentials by email&lt;br&gt;
async function deleteCredentials(email) {&lt;br&gt;
  try {&lt;br&gt;
    const result = await vault.delete(SECRET_PATH);&lt;br&gt;
    console.log(&lt;/code&gt;Credentials deleted for ${email}&lt;code&gt;);&lt;br&gt;
  } catch (error) {&lt;br&gt;
    console.error('Error deleting credentials:', error);&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
// Example Usage&lt;br&gt;
(async () =&amp;gt; {&lt;br&gt;
  await saveCredentials('test@example.com', 'password123');&lt;br&gt;
  await getCredentials('test@example.com');&lt;br&gt;
  await updateCredentials('test@example.com', 'newpassword456');&lt;br&gt;
  await getCredentials('test@example.com');&lt;br&gt;
  await deleteCredentials('test@example.com');&lt;br&gt;
})();&lt;/code&gt;&lt;br&gt;
Explanation of Code&lt;br&gt;
Save credentials: Stores an email and password in Vault.&lt;br&gt;
Update credentials: Updates the password for the provided email.&lt;br&gt;
Retrieve credentials: Fetches stored credentials using the email.&lt;br&gt;
Delete credentials: Deletes the credentials for the given email.&lt;/p&gt;

&lt;p&gt;We are using the secret/data/users path in Vault to store and manage user data.&lt;/p&gt;




&lt;p&gt;Step 4: Running the Application&lt;br&gt;
To run the application, use the following command:&lt;br&gt;
node index.js&lt;br&gt;
The script will:&lt;br&gt;
Save the email and password to Vault.&lt;br&gt;
Retrieve the credentials using the email.&lt;br&gt;
Update the password.&lt;br&gt;
Retrieve the updated credentials.&lt;br&gt;
Delete the stored credentials.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By following this tutorial, you now have a fully functional Node.js application that integrates with HashiCorp Vault. This setup provides you with a secure and scalable way to manage sensitive information like email-password pairs in your application.&lt;br&gt;
HashiCorp Vault is a powerful tool for storing secrets, and its integration with Node.js is relatively straightforward using the node-vault library. You can expand this setup to store other secrets like API keys, tokens, and more.&lt;br&gt;
If you're building any application that requires sensitive data management, it's highly recommended to adopt a secure storage mechanism like Vault to reduce security risks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
