Skip to content

Commit

Permalink
feat: add management scripts #34
Browse files Browse the repository at this point in the history
  • Loading branch information
tuana9a committed Sep 3, 2024
1 parent 3b3fbe2 commit 7449d0b
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 27 deletions.
2 changes: 0 additions & 2 deletions api-gateway/src/cfg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import crypto from "crypto";
import * as dotenv from "dotenv";
import { random } from "./utils";

Expand All @@ -14,7 +13,6 @@ export const cfg = {
RABBITMQ_CONNECTION_STRING: process.env.RABBITMQ_CONNECTION_STRING || "amqp://localhost:5672",
MONGODB_CONNECTION_STRING: process.env.MONGODB_CONNECTION_STRING || "mongodb://localhost:27017",
DATABASE_NAME: process.env.DATABASE_NAME || "dkhptd",
INIT_ROOT_PASSWORD: process.env.INIT_ROOT_PASSWORD || crypto.randomBytes(32).toString("hex"),
JOB_MAX_TRY: 10,
LOG_WORKER_DOING: parseInt(process.env.LOG_WORKER_DOING),
LOG_WORKER_PING: parseInt(process.env.LOG_WORKER_PING),
Expand Down
27 changes: 2 additions & 25 deletions api-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,11 @@ import * as amqplib from "amqplib/callback_api";
import { MongoClient } from "mongodb";
import cors from "cors";

import { cfg, CollectionName, QueueName, Role } from "./cfg";
import { cfg, CollectionName, QueueName } from "./cfg";
import logger from "./loggers/logger";
import { ensureIndex, toJson, toSHA256 } from "./utils";
import { ensureIndex, toJson } from "./utils";
import { mongoConnectionPool, rabbitmqConnectionPool } from "./connections";
import { cachedSettings } from "./services";
import { Account } from "./entities";

async function ensureRootAccount() {
const doc = await mongoConnectionPool
.getClient()
.db(cfg.DATABASE_NAME)
.collection(CollectionName.ACCOUNT)
.findOne({ username: "root" });

if (doc) return;
const password = cfg.INIT_ROOT_PASSWORD;
const account = new Account({ username: "root", password: toSHA256(password), role: Role.ADMIN });

await mongoConnectionPool
.getClient()
.db(cfg.DATABASE_NAME)
.collection(CollectionName.ACCOUNT)
.insertOne(account);

logger.info(`Insert root account with password ${password}`);
}

async function main() {
logger.info(`Config: ${toJson(cfg)}`);
Expand Down Expand Up @@ -62,8 +41,6 @@ async function main() {
ensureIndex(client.db(cfg.DATABASE_NAME), CollectionName.SUBJECT, { subjectName: 1 });
ensureIndex(client.db(cfg.DATABASE_NAME), CollectionName.SUBJECT, { subjectId: 1, subjectName: 1 });

ensureRootAccount();

amqplib.connect(cfg.RABBITMQ_CONNECTION_STRING, (error0, connection) => {
if (error0) {
logger.error(error0);
Expand Down
107 changes: 107 additions & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# @tuana9a
*tmp*
46 changes: 46 additions & 0 deletions scripts/ensureRootAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const crypto = require("crypto");
const { MongoClient } = require("mongodb");

require("dotenv").config();

const Role = {
ADMIN: "ADMIN",
};

const CollectionName = {
ACCOUNT: "account",
CTR: "classToRegister",
PREFERENCE: "preference",
DKHPTD: "dkhptd",
DKHPTDResult: "dkhptdResult",
DKHPTDV1: "dkhptdV1",
DKHPTDV1Result: "dkhptdV1Result",
DKHPTDV2: "dkhptdV2",
DKHPTDV2Result: "dkhptdV2Result",
SETTINGS: "settings",
SUBJECT: "subject",
};

const cfg = {
MONGODB_CONNECTION_STRING: process.env.MONGODB_CONNECTION_STRING || "mongodb://localhost:27017",
DATABASE_NAME: process.env.DATABASE_NAME || "dkhptd",
INIT_ROOT_PASSWORD: process.env.INIT_ROOT_PASSWORD || crypto.randomBytes(32).toString("hex"),
};

const toSHA256 = (input) => crypto.createHash("sha256").update(input).digest("hex");

async function main() {
const client = await new MongoClient(cfg.MONGODB_CONNECTION_STRING).connect();
const password = cfg.INIT_ROOT_PASSWORD;

await client
.db(cfg.DATABASE_NAME)
.collection(CollectionName.ACCOUNT)
.updateOne({ username: "root" }, { $set: { password: toSHA256(password), role: Role.ADMIN } });

console.log(`ensure root account with password ${password}`);

process.exit(0);
}

main();
Loading

0 comments on commit 7449d0b

Please sign in to comment.