在构建和推送 docker 图像之前在 NodeJS 应用程序中加密数据库密码
Encrypt DB password in NodeJS application before building and pushing docker image
基础设施设置:
- Docker MacOS 上的桌面环境 运行,
- Minikube 集群也在这个 MacOS 上 运行。
- Jenkins pod 运行 在 Minikube 集群上
我有这个 NodeJS 应用程序,它与 MySQL 数据库(一个 docker 容器)对话,以从名为 hello_world 的数据库中检索文本字符串。在 NodeJS 图像中构建应用程序之前,我需要加密数据库密码。该图像随后将被推送到 DockerHub 存储库。
然后它将通过 dev 命名空间中的 Jenkins 管道下载并作为 Minikube 集群中的 pod 安装。在“dev”命名空间中已经设置了一个 MySQL 数据库 pod,管道中的后续阶段将在其中测试从数据库中检索文本字符串。
➜ nodejs-app git:(main) ✗ cat app.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
// parse application/json
app.use(bodyParser.json());
//create database connection
const conn = mysql.createConnection({
host: 'dev-mysqldb',
user: 'mysqluser',
password: ‘xxxxxxxx’,
database: 'hello_world'
});
//connect to database
conn.connect((err) =>{
if(err) throw err;
console.log('Mysql Connected...');
});
//show all products
app.get('/',(req, res) => {
let sql = "SELECT * FROM messages";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
// res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
res.send(results);
});
});
//Server listening
app.listen(80,() =>{
console.log('Server started on port 80...');
});
我这里有 2 个问题。
- 如何在上述 NodeJS 应用程序中加密数据库密码?
- 如何让Kubernete pod解密原来在docker环境下设置的数据库密码?
谢谢。
此处最简单的方法是使用 dotenv。
用法:
在项目的根目录中创建一个 .env
文件:
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
尽早在您的应用程序中导入和配置 dotenv:
require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it working
.. 还是使用 ES6?
import 'dotenv/config' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import express from 'express'
确保查看 https://github.com/motdotla/dotenv#readme 了解更多信息。
基础设施设置:
- Docker MacOS 上的桌面环境 运行,
- Minikube 集群也在这个 MacOS 上 运行。
- Jenkins pod 运行 在 Minikube 集群上
我有这个 NodeJS 应用程序,它与 MySQL 数据库(一个 docker 容器)对话,以从名为 hello_world 的数据库中检索文本字符串。在 NodeJS 图像中构建应用程序之前,我需要加密数据库密码。该图像随后将被推送到 DockerHub 存储库。
然后它将通过 dev 命名空间中的 Jenkins 管道下载并作为 Minikube 集群中的 pod 安装。在“dev”命名空间中已经设置了一个 MySQL 数据库 pod,管道中的后续阶段将在其中测试从数据库中检索文本字符串。
➜ nodejs-app git:(main) ✗ cat app.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
// parse application/json
app.use(bodyParser.json());
//create database connection
const conn = mysql.createConnection({
host: 'dev-mysqldb',
user: 'mysqluser',
password: ‘xxxxxxxx’,
database: 'hello_world'
});
//connect to database
conn.connect((err) =>{
if(err) throw err;
console.log('Mysql Connected...');
});
//show all products
app.get('/',(req, res) => {
let sql = "SELECT * FROM messages";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
// res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
res.send(results);
});
});
//Server listening
app.listen(80,() =>{
console.log('Server started on port 80...');
});
我这里有 2 个问题。
- 如何在上述 NodeJS 应用程序中加密数据库密码?
- 如何让Kubernete pod解密原来在docker环境下设置的数据库密码?
谢谢。
此处最简单的方法是使用 dotenv。
用法:
在项目的根目录中创建一个 .env
文件:
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
尽早在您的应用程序中导入和配置 dotenv:
require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it working
.. 还是使用 ES6?
import 'dotenv/config' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import express from 'express'
确保查看 https://github.com/motdotla/dotenv#readme 了解更多信息。