Refactor infisical-node to config file for birds eye view of envars

This commit is contained in:
Tuan Dang
2023-03-15 00:09:40 +07:00
parent 31111fc63b
commit a6c8638345
33 changed files with 264 additions and 262 deletions
+10 -7
View File
@@ -1,13 +1,17 @@
import infisical from 'infisical-node';
import { PostHog } from 'posthog-node';
import { getLogger } from '../utils/logger';
import {
getNodeEnv,
getTelemetryEnabled,
getPostHogProjectApiKey,
getPostHogHost
} from '../config';
/**
* Logs telemetry enable/disable notice.
*/
const logTelemetryMessage = () => {
const TELEMETRY_ENABLED = infisical.get('TELEMETRY_ENABLED')! !== 'false' && true;
if(!TELEMETRY_ENABLED){
if(!getTelemetryEnabled()){
getLogger("backend-main").info([
"",
"To improve, Infisical collects telemetry data about general usage.",
@@ -23,11 +27,10 @@ const logTelemetryMessage = () => {
*/
const getPostHogClient = () => {
let postHogClient: any;
const TELEMETRY_ENABLED = infisical.get('TELEMETRY_ENABLED')! !== 'false' && true;
if (infisical.get('NODE_ENV') === 'production' && TELEMETRY_ENABLED) {
if (getNodeEnv() === 'production' && getTelemetryEnabled()) {
// case: enable opt-out telemetry in production
postHogClient = new PostHog(infisical.get('POSTHOG_PROJECT_API_KEY')! || 'phc_nSin8j5q2zdhpFDI1ETmFNUIuTG4DwKVyIigrY10XiE', {
host: infisical.get('POSTHOG_HOST')!
postHogClient = new PostHog(getPostHogProjectApiKey(), {
host: getPostHogHost()
});
}
+16 -10
View File
@@ -1,4 +1,3 @@
import infisical from 'infisical-node';
import nodemailer from 'nodemailer';
import {
SMTP_HOST_SENDGRID,
@@ -8,22 +7,29 @@ import {
} from '../variables';
import SMTPConnection from 'nodemailer/lib/smtp-connection';
import * as Sentry from '@sentry/node';
import {
getSmtpHost,
getSmtpUsername,
getSmtpPassword,
getSmtpSecure,
getSmtpPort
} from '../config';
export const initSmtp = () => {
const mailOpts: SMTPConnection.Options = {
host: infisical.get('SMTP_HOST')!,
port: parseInt(infisical.get('SMTP_PORT')!)
host: getSmtpHost(),
port: getSmtpPort()
};
if (infisical.get('SMTP_USERNAME')! && infisical.get('SMTP_PASSWORD')!) {
if (getSmtpUsername() && getSmtpPassword()) {
mailOpts.auth = {
user: infisical.get('SMTP_USERNAME')!,
pass: infisical.get('SMTP_PASSWORD')!
user: getSmtpUsername(),
pass: getSmtpPassword()
};
}
if (infisical.get('SMTP_SECURE')! ? infisical.get('SMTP_SECURE')! === 'true' : false) {
switch (infisical.get('SMTP_HOST')!) {
if (getSmtpSecure() ? getSmtpSecure() : false) {
switch (getSmtpHost()) {
case SMTP_HOST_SENDGRID:
mailOpts.requireTLS = true;
break;
@@ -46,7 +52,7 @@ export const initSmtp = () => {
}
break;
default:
if (infisical.get('SMTP_HOST')!.includes('amazonaws.com')) {
if (getSmtpHost().includes('amazonaws.com')) {
mailOpts.tls = {
ciphers: 'TLSv1.2'
}
@@ -67,7 +73,7 @@ export const initSmtp = () => {
.catch((err) => {
Sentry.setUser(null);
Sentry.captureException(
`SMTP - Failed to connect to ${infisical.get('SMTP_HOST')!}:${infisical.get('SMTP_PORT')!} \n\t${err}`
`SMTP - Failed to connect to ${getSmtpHost()}:${getSmtpPort()} \n\t${err}`
);
});