Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: made the RECAPTCHA_SITE_KEY mandatory if RECAPTCHA_SITE_KEY is … #1632

Merged
merged 23 commits into from
Jan 18, 2024
Merged
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
faadb22
fix: made the RECAPTCHA_SITE_KEY mandatory if RECAPTCHA_SITE_KEY is …
Doraemon012 Jan 4, 2024
90a2741
fix: The script now verifies and displays if the recaptcha site key i…
Doraemon012 Jan 4, 2024
7f8629e
fix: The script now verifies and displays if the recaptcha site key i…
Doraemon012 Jan 4, 2024
ad78be7
fix: Update setup.ts
Doraemon012 Jan 6, 2024
d076e40
fix: Updated setup to handel comments and deleted parameters in .env
Doraemon012 Jan 6, 2024
e3ac11d
fix: Made MongoDB and Redis mandatory
Doraemon012 Jan 10, 2024
919cbe1
fix: Fixed the redis url check functionality
Doraemon012 Jan 10, 2024
56d7624
feat: Added a prompt for node environment
Doraemon012 Jan 10, 2024
bd33c89
feat: Added a warning for LAST_RESORT_SUPERADMIN_EMAIL
Doraemon012 Jan 10, 2024
6dccc28
fix: Removed the Redis password being shown on the screen
Doraemon012 Jan 11, 2024
c5f0074
Delete additional video
Doraemon012 Jan 11, 2024
72659ba
feat: Added confirmation prompts for Mongo DB and Redis
Doraemon012 Jan 13, 2024
a0d003d
feat: Added comments for TsDoc
Doraemon012 Jan 13, 2024
96d896f
Merge branch 'develop' into talawa-api-v4
Doraemon012 Jan 13, 2024
29f59db
fix: added remaining comments
Doraemon012 Jan 13, 2024
15011ce
fix: Fixed merge confilcts and fixed a bug
Doraemon012 Jan 13, 2024
94f0fe2
fix: Fixed prompts
Doraemon012 Jan 15, 2024
9d47698
fix: Removed incorrect prompt for MongoDB
Doraemon012 Jan 15, 2024
badb669
feat: Automated Redis setup
Doraemon012 Jan 15, 2024
8c00168
fix: Fixed SMTP and Mail configuration
Doraemon012 Jan 15, 2024
8b53477
fix: Fixed Redis URL being displayed on screen
Doraemon012 Jan 16, 2024
1bb063f
Merge remote-tracking branch 'upstream/develop' into talawa-api-v4
Doraemon012 Jan 18, 2024
be5e175
fix: set the default for import sample data to no and fixed some tsdo…
Doraemon012 Jan 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 96 additions & 109 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@
const envSample = dotenv.parse(fs.readFileSync(".env.sample"));
const misplaced = Object.keys(envSample).filter((key) => !(key in env));
if (misplaced.length > 0) {
console.log("Please copy the contents of .env.sample to .env file");
abort();
// copy the missing fields from .env.sample to .env
for (const key of misplaced) {
fs.appendFileSync(".env", `${key}=${envSample[key]}\n`);
}
}
}
// Update the value of an environment variable in .env file
function updateEnvVariable(config: { [key: string]: string | number }): void {
const existingContent: string = fs.readFileSync(".env", "utf8");

let updatedContent: string = existingContent;
for (const key in config) {
const regex = new RegExp(`^${key}=.*`, "gm");
updatedContent = updatedContent.replace(regex, `${key}=${config[key]}`);
}

fs.writeFileSync(".env", updatedContent, "utf8");
}

// Generate and update the access and refresh token secrets in .env
Expand All @@ -29,26 +43,20 @@
if (accessTokenSecret === null) {
accessTokenSecret = cryptolib.randomBytes(32).toString("hex");
config.ACCESS_TOKEN_SECRET = accessTokenSecret;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
}

if (refreshTokenSecret === null) {
refreshTokenSecret = cryptolib.randomBytes(32).toString("hex");
config.REFRESH_TOKEN_SECRET = refreshTokenSecret;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
}
}

// Check connection to Redis with the specified URL.
async function checkRedisConnection(url: string): Promise<boolean> {
let response = false;
const client = redis.createClient(url);
const client = redis.createClient({ url });

console.log("\nChecking Redis connection....");

Expand All @@ -58,8 +66,9 @@
response = true;
} catch (error) {
console.log(`\nConnection to Redis failed. Please try again.\n`);
} finally {
client.quit();
}
client.quit();
return response;
}

Expand Down Expand Up @@ -100,18 +109,34 @@
try {
let isConnected = false,
url = "";
let host!: string;
let port!: number;
let password!: string;

while (!isConnected) {
const { host, port, password } = await askForRedisUrl();
const result = await askForRedisUrl();
host = result.host;
port = result.port;
password = result.password;

url = `redis://${password ? password + "@" : ""}${host}:${port}`;
isConnected = await checkRedisConnection(url);
}

// Set the Redis parameters in process.env
process.env.REDIS_URL = url;
process.env.REDIS_HOST = host;
process.env.REDIS_PORT = port.toString();
process.env.REDIS_PASSWORD = password;

// Update the .env file
const config = dotenv.parse(fs.readFileSync(".env"));
config.REDIS_URL = url;
palisadoes marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
config.REDIS_HOST = host;
config.REDIS_PORT = port;
config.REDIS_PASSWORD = password;
updateEnvVariable(config);
console.log("\nRedis configuration updated successfully!");
} catch (err) {
console.error(err);
abort();
Expand All @@ -138,14 +163,9 @@
async function superAdmin(): Promise<void> {
try {
const email = await askForSuperAdminEmail();

const config = dotenv.parse(fs.readFileSync(".env"));

config.LAST_RESORT_SUPERADMIN_EMAIL = email;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
} catch (err) {
console.log(err);
abort();
Expand Down Expand Up @@ -204,28 +224,26 @@
config.MONGO_DB_URL = DB_URL;
// Modifying the environment variable to be able to access the url in importData function.
process.env.MONGO_DB_URL = DB_URL;
Doraemon012 marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
} catch (err) {
console.error(err);
abort();
}
}

// Function to ask if the user wants to keep the entered values
async function askToKeepValues(): Promise<boolean> {
const { keepValues } = await inquirer.prompt({
type: "confirm",
name: "keepValues",
message: `Would you like to keep the entered key? `,
default: true,
});
return keepValues;
}

//Get recaptcha details
async function recaptcha(): Promise<void> {
console.log(
"\nPlease visit this URL to set up reCAPTCHA:\n\nhttps://www.google.com/recaptcha/admin/create"
);
console.log(
'\nSelect reCAPTCHA v2 and the "I`m not a robot" checkbox option'
);
console.log(
'\nAdd "localhost" in domains and accept the terms, then press submit'
);

const { recaptchaSecretKey } = await inquirer.prompt([
{
type: "input",
Expand All @@ -239,28 +257,29 @@
},
},
]);
const config = dotenv.parse(fs.readFileSync(".env"));
config.RECAPTCHA_SECRET_KEY = recaptchaSecretKey;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);

const shouldKeepDetails = await askToKeepValues();

if (shouldKeepDetails) {
const config = dotenv.parse(fs.readFileSync(".env"));
config.RECAPTCHA_SECRET_KEY = recaptchaSecretKey;
updateEnvVariable(config);
} else {
await recaptcha();
}
}

async function recaptchaSiteKey(): Promise<void> {
console.log(
"\nPlease visit this URL to set up reCAPTCHA:\n\nhttps://www.google.com/recaptcha/admin/create"
);
console.log(
'\nSelect reCAPTCHA v2 and the "I`m not a robot" checkbox option'
);
console.log(
palisadoes marked this conversation as resolved.
Show resolved Hide resolved
'\nAdd "localhost" in domains and accept the terms, then press submit'
);
if (process.env.RECAPTCHA_SITE_KEY) {
console.log(
`\nreCAPTCHA site key already exists with the value ${process.env.RECAPTCHA_SITE_KEY}`
);
}
palisadoes marked this conversation as resolved.
Show resolved Hide resolved

const { recaptchaSiteKey } = await inquirer.prompt([
const { recaptchaSiteKeyInp } = await inquirer.prompt([
{
type: "input",
name: "recaptchaSiteKey",
name: "recaptchaSiteKeyInp",
message: "Enter your reCAPTCHA site key:",
validate: async (input: string): Promise<boolean | string> => {
if (validateRecaptcha(input)) {
Expand All @@ -270,11 +289,15 @@
},
},
]);
const config = dotenv.parse(fs.readFileSync(".env"));
config.RECAPTCHA_SITE_KEY = recaptchaSiteKey;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);

const shouldKeepDetails = await askToKeepValues();

if (shouldKeepDetails) {
const config = dotenv.parse(fs.readFileSync(".env"));
config.RECAPTCHA_SITE_KEY = recaptchaSiteKeyInp;
updateEnvVariable(config);
} else {
await recaptchaSiteKey();
}
}

Expand Down Expand Up @@ -326,10 +349,7 @@

config.MAIL_USERNAME = email;
config.MAIL_PASSWORD = password;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
}

//Checks if the data exists and ask for deletion
Expand Down Expand Up @@ -393,15 +413,8 @@
abort();
}
console.log(`Output: ${stdout}`);
console.log(
"\nCongratulations! Talawa API has been successfully setup! 🥂🎉"
);
}
);
} else {
console.log(
"\nCongratulations! Talawa API has been successfully setup! 🥂🎉"
);
}
}

Expand All @@ -425,7 +438,7 @@
type: "confirm",
name: "shouldGenerateAccessToken",
message: "Would you like to generate a new access token secret?",
default: true,
default: process.env.ACCESS_TOKEN_SECRET ? false : true,
});

if (shouldGenerateAccessToken) {
Expand All @@ -441,7 +454,7 @@
type: "confirm",
name: "shouldGenerateRefreshToken",
message: "Would you like to generate a new refresh token secret?",
default: true,
default: process.env.REFRESH_TOKEN_SECRET ? false : true,
});

if (shouldGenerateRefreshToken) {
Expand All @@ -458,37 +471,23 @@
});
if (!isDockerInstallation) {
// Redis configuration
if (process.env.REDIS_URL) {
console.log(
`\nRedis URL already exists with the value:\n${process.env.REDIS_URL}`
);
}
const { shouldSetRedis } = await inquirer.prompt({
type: "confirm",
name: "shouldSetRedis",
message: "Would you like to set up a Redis URL?",
default: true,
});
if (shouldSetRedis) {
await redisConfiguration();
if (process.env.REDIS_HOST && process.env.REDIS_PORT) {
const url = `redis://${
process.env.REDIS_PASSWORD ? process.env.REDIS_PASSWORD + "@" : ""
}${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`;
console.log(`\nRedis URL already exists with the value:\n${url}`);
Fixed Show fixed Hide fixed
}

await redisConfiguration();

// MongoDB configuration
if (process.env.MONGO_DB_URL) {
console.log(
`\nMongoDB URL already exists with the value:\n${process.env.MONGO_DB_URL}`
);
}
const { shouldSetMongoDb } = await inquirer.prompt({
type: "confirm",
name: "shouldSetMongoDb",
message: "Would you like to set up a MongoDB URL?",
default: true,
});

if (shouldSetMongoDb) {
await mongoDB();
}
await mongoDB();
}
if (process.env.RECAPTCHA_SECRET_KEY) {
console.log(
Expand All @@ -499,21 +498,11 @@
type: "confirm",
name: "shouldSetRecaptcha",
message: "Would you like to set up a reCAPTCHA secret key?",
default: true,
default: process.env.RECAPTCHA_SECRET_KEY ? false : true,
});

if (shouldSetRecaptcha) {
await recaptcha();
}

const { shouldSetRecaptchaSiteKey } = await inquirer.prompt({
type: "confirm",
name: "shouldSetRecaptchaSiteKey",
message: "Would you like to set up a reCAPTCHA site key?",
default: true,
});

if (shouldSetRecaptchaSiteKey) {
await recaptchaSiteKey();
}

Expand All @@ -527,6 +516,7 @@
type: "confirm",
name: "shouldSetMail",
message: "Would you like to setup the mail username and password?",
default: process.env.MAIL_USERNAME ? false : true,
},
]);
if (shouldSetMail) {
Expand All @@ -544,7 +534,7 @@
type: "confirm",
name: "shouldSetSuperUserEmail",
message: "Would you like to setup a Super Admin email of last resort?",
default: true,
default: process.env.LAST_RESORT_SUPERADMIN_EMAIL ? false : true,
},
]);
if (shouldSetSuperUserEmail) {
Expand All @@ -563,10 +553,7 @@
}
const config = dotenv.parse(fs.readFileSync(".env"));
config.LAST_RESORT_SUPERADMIN_EMAIL = config.MAIL_USERNAME;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
updateEnvVariable(config);
}

if (!isDockerInstallation) {
Expand Down
Loading