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

chore: Reduce lint exceptions in ctl #37643

Merged
merged 8 commits into from
Nov 22, 2024
Merged

chore: Reduce lint exceptions in ctl #37643

merged 8 commits into from
Nov 22, 2024

Conversation

sharat87
Copy link
Member

@sharat87 sharat87 commented Nov 22, 2024

Fix linting exceptions in ctl.

Automation

/test sanity

🔍 Cypress test results

Warning

Tests have not run on the HEAD 1f2242a yet


Fri, 22 Nov 2024 10:06:23 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced backup and restore processes with improved user prompts and error handling.
    • Added support for optional command-line flags during database imports.
  • Bug Fixes

    • Improved error handling for various operations, including database exports and imports.
    • Enhanced logging for backup errors to provide more context.
  • Documentation

    • Updated user prompts and error messages for clarity during backup and restore operations.
  • Tests

    • Expanded test coverage for backup functionalities and utility functions to ensure robust error handling and output validation.
  • Chores

    • Updated dependencies to enhance TypeScript development experience.

Copy link
Contributor

coderabbitai bot commented Nov 22, 2024

Walkthrough

The pull request introduces several modifications across multiple files in the appsmith-rts project. Key changes include the addition of a TypeScript type definition dependency, updates to ESLint configurations, enhancements to error handling and user interactions in backup and restore processes, and improvements in command execution functions. Additionally, several utility functions have been converted to asynchronous operations to improve error handling and control flow. Overall, the changes aim to enhance the robustness and clarity of the codebase.

Changes

File Path Change Summary
app/client/packages/rts/package.json Added dependency: "@types/node": "*" in devDependencies.
app/client/packages/rts/src/ctl/.eslintrc.json Updated extends from ["../../../../.eslintrc.base.json"] to ["../../.eslintrc.json"]. Removed multiple ESLint rules.
app/client/packages/rts/src/ctl/backup.test.ts Enhanced tests with new assertions, improved error handling, and added console logging. Expanded tests for backup paths and commands, and refined user prompts for encryption passwords.
app/client/packages/rts/src/ctl/backup.ts Improved error handling, added logging for user interactions, changed generateBackupRootPath to an asynchronous function, and enhanced cleanup procedures.
app/client/packages/rts/src/ctl/check_replica_set.ts Added error handling for the changeStream event listener and updated promise resolution logic.
app/client/packages/rts/src/ctl/export_db.ts Added directory creation for backup path before executing mongodump.
app/client/packages/rts/src/ctl/import_db.ts Enhanced error handling and user confirmation logic for database import. Updated run function signature to include forceOption: boolean.
app/client/packages/rts/src/ctl/index.ts Captured forceOption flag for import command from command-line arguments.
app/client/packages/rts/src/ctl/logger.ts Added error handling for log directory access and creation in backup_error and backup_info functions.
app/client/packages/rts/src/ctl/mailer.ts Enhanced email notifications by appending domainName and instanceName to the email body when sending backup error notifications.
app/client/packages/rts/src/ctl/mongo_shell_utils.ts Implemented error handling in exec function and added conditional formatting for MongoDB queries in execMongoEval.
app/client/packages/rts/src/ctl/restore.ts Improved user interaction and feedback during backup restoration, refined input handling, and enhanced encryption key prompts.
app/client/packages/rts/src/ctl/utils.test.ts Added test cases for output and error handling in execCommandReturningOutput and execCommandSilent.
app/client/packages/rts/src/ctl/utils.ts Converted several functions to asynchronous operations, enhanced error handling, and improved control flow.
app/client/packages/rts/src/ctl/version.ts Enhanced error handling and control flow in the exec function.

Possibly related PRs

Suggested labels

Task, ok-to-test

Suggested reviewers

  • AmanAgarwal041
  • ApekshaBhosale
  • pratapaprasanna
  • mohanarpit

🎉 In the realm of code where changes unfold,
New types and tests, a story retold.
With logs and prompts, the user’s delight,
Robust and clear, our functions take flight!
Each line refined, a developer’s dream,
Together we code, a collaborative team! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sharat87 sharat87 changed the title chore/ctl-linting chore: Reduce lint exceptions in ctl Nov 22, 2024
@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Nov 22, 2024
@sharat87 sharat87 marked this pull request as ready for review November 22, 2024 10:26
@sharat87 sharat87 merged commit 0685d33 into release Nov 22, 2024
21 of 22 checks passed
@sharat87 sharat87 deleted the chore/ctl-linting branch November 22, 2024 10:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Outside diff range and nitpick comments (35)
app/client/packages/rts/src/ctl/utils.ts (5)

Line range hint 86-100: Simplify event handling with 'once'

Use p.once() instead of p.on() and remove isPromiseDone.

Apply this diff:

- p.on("exit", (code) => {
-   if (isPromiseDone) {
-     return;
-   }
-   isPromiseDone = true;
    if (code === 0) {
      resolve();
    } else {
      reject();
    }
  });

- p.on("error", (err) => {
-   if (isPromiseDone) {
-     return;
-   }
-   isPromiseDone = true;
    console.error("Error running command", err);
    reject();
  });
+ p.once("exit", (code) => {
+   if (code === 0) {
+     resolve();
+   } else {
+     reject();
+   }
+ });

+ p.once("error", (err) => {
+   console.error("Error running command", err);
+   reject();
+ });

Line range hint 218-245: Simplify event handling with 'once'

Similarly, for execCommandSilent, use p.once() to handle events.

Apply this diff:

- p.on("close", (code) => {
-   if (isPromiseDone) {
-     return;
-   }
-   isPromiseDone = true;
    if (code === 0) {
      resolve();
    } else {
      reject();
    }
  });

- p.on("error", (err) => {
-   if (isPromiseDone) {
-     return;
-   }
-   isPromiseDone = true;
    reject(err);
  });
+ p.once("close", (code) => {
+   if (code === 0) {
+     resolve();
+   } else {
+     reject();
+   }
+ });

+ p.once("error", (err) => {
+   reject(err);
+ });

Line range hint 146-159: Use async/await consistently

Refactor to use async/await for better readability.

Apply this diff:

 const backupFiles = [];

- await fsPromises
-   .readdir(Constants.BACKUP_PATH)
-   .then((filenames) => {
-     for (const filename of filenames) {
-       if (filename.match(/^appsmith-backup-.*\.tar\.gz(\.enc)?$/)) {
-         backupFiles.push(filename);
-       }
-     }
-   })
-   .catch((err) => {
-     console.log(err);
-   });
+ try {
+   const filenames = await fsPromises.readdir(Constants.BACKUP_PATH);
+   for (const filename of filenames) {
+     if (filename.match(/^appsmith-backup-.*\.tar\.gz(\.enc)?$/)) {
+       backupFiles.push(filename);
+     }
+   }
+ } catch (err) {
+   console.log(err);
+ }

254-256: Use 'ConnectionString' for URI parsing

Simplify getDatabaseNameFromMongoURI by using ConnectionString.

Apply this diff:

 export function getDatabaseNameFromMongoURI(uri) {
-  const uriParts = uri.split("/");
-  return uriParts[uriParts.length - 1].split("?")[0];
+  const cs = new ConnectionString(uri);
+  return cs.pathname.replace('/', '');
 }

69-71: Simplify condition for 'dbEnvUrl'

Consider removing the check for dbEnvUrl !== "undefined" if unnecessary.

app/client/packages/rts/src/ctl/version.ts (1)

Line range hint 12-18: Consider standardizing error message format.

While the error handling is good, the error message formats are slightly inconsistent ("Error:" prefix in one but not the other).

-    console.error("Error fetching current Appsmith version", err);
+    console.error("Error: failed to fetch current Appsmith version", err);
     process.exit(1);
app/client/packages/rts/src/ctl/mongo_shell_utils.ts (2)

Line range hint 7-15: Enhance error message specificity

The error handling is good, but the error message could be more descriptive by including the query that failed.

-    console.error("Error evaluating the mongo query", err);
+    console.error("Error evaluating mongo query:", queryExpression, "\nError:", err);

Line range hint 26-30: Validate MongoDB URI before execution

The MongoDB URI is used directly without validation. Add checks to ensure it's a valid connection string.

+  if (!appsmithMongoURI?.startsWith('mongodb')) {
+    throw new Error('Invalid MongoDB connection string');
+  }
   return await utils.execCommand([
app/client/packages/rts/src/ctl/export_db.ts (1)

Line range hint 17-45: Consider adding timeout handling for long-running operations

The database export and application lifecycle management could benefit from timeout handling, especially for large databases.

+async function withTimeout(promise: Promise<any>, timeoutMs: number, operation: string): Promise<any> {
+  const timeoutPromise = new Promise((_, reject) => {
+    setTimeout(() => reject(new Error(`${operation} timed out after ${timeoutMs}ms`)), timeoutMs);
+  });
+  return Promise.race([promise, timeoutPromise]);
+}

 export async function run() {
   let errorCode = 0;
   await utils.ensureSupervisorIsRunning();
 
   try {
     console.log("stop backend & rts application before export database");
-    await utils.stop(["backend", "rts"]);
-    await exportDatabase();
+    await withTimeout(utils.stop(["backend", "rts"]), 30000, "stopping services");
+    await withTimeout(exportDatabase(), 300000, "database export");
app/client/packages/rts/src/ctl/check_replica_set.ts (3)

29-29: Remove unnecessary empty line

  await client.connect();
-
  return await new Promise<boolean>((resolve) => {

Line range hint 36-47: Improve error message comparison reliability

The current string comparison for error detection is fragile and might break if MongoDB changes their error message format.

Consider using error codes instead:

-            err.toString() ==
-              "MongoServerError: The $changeStream stage is only supported on replica sets"
+            err.codeName === "ChangeStreamNotSupported" || 
+            err.code === 40573  // MongoDB error code for change stream not supported

Line range hint 51-54: Improve timeout handling and cleanup

The current implementation has potential issues:

  1. Hardcoded timeout value
  2. Possible race condition between error and timeout
  3. Cleanup might not execute in error scenarios

Consider this improved implementation:

+      const REPLICA_CHECK_TIMEOUT = 1000; // Move to constants
+      let changeStreamClosed = false;
+      
+      const cleanup = () => {
+        if (!changeStreamClosed) {
+          changeStreamClosed = true;
+          changeStream.close().catch(console.error);
+        }
+      };

       setTimeout(() => {
         resolve(true);
-        changeStream.close();
+        cleanup();
-      }, 1000);
+      }, REPLICA_CHECK_TIMEOUT);

+      // Ensure cleanup on error
+      process.on('unhandledRejection', cleanup);
app/client/packages/rts/src/ctl/utils.test.ts (2)

12-12: Remove unnecessary empty lines before assertions

The empty lines before assertions don't add to readability in this context.

    ]);
-

    expect(result).toBe("hello world");

Also applies to: 22-22, 32-32, 42-42, 52-52


64-64: Remove unnecessary empty line before command execution

The empty line doesn't add to readability here.

    const consoleSpy = jest.spyOn(console, "log");
-

    await utils.execCommandSilent(["node", "--eval", "console.log('test')"]);
app/client/packages/rts/src/ctl/index.ts (2)

Line range hint 16-30: Consolidate duplicate environment variable checks

The checks for APPSMITH_DB_URL and fallback to APPSMITH_MONGODB_URI are duplicated before and after loading the config. This can be refactored into a single function for better maintainability.

+function setupDatabaseUrl() {
+  if (!process.env.APPSMITH_DB_URL) {
+    process.env.APPSMITH_DB_URL = process.env.APPSMITH_MONGODB_URI;
+    delete process.env.APPSMITH_MONGODB_URI;
+  }
+}

-// Check if APPSMITH_DB_URL is set, if not set, fall back to APPSMITH_MONGODB_URI
-if (!process.env.APPSMITH_DB_URL) {
-  process.env.APPSMITH_DB_URL = process.env.APPSMITH_MONGODB_URI;
-  delete process.env.APPSMITH_MONGODB_URI;
-}

// Loading latest application configuration
config({ path: APPLICATION_CONFIG_PATH });

-// AGAIN: Check if APPSMITH_DB_URL is set, if not set, fall back to APPSMITH_MONGODB_URI
-if (!process.env.APPSMITH_DB_URL) {
-  process.env.APPSMITH_DB_URL = process.env.APPSMITH_MONGODB_URI;
-  delete process.env.APPSMITH_MONGODB_URI;
-}
+setupDatabaseUrl();

Force flag needs documentation in help text and command options

Based on the help text search results, the force flag is not documented in the command's help text, which could lead to confusion for users. Please add documentation for the force flag in:

  • Command help text in utils.ts
  • Command options definition where the flag is registered
🔗 Analysis chain

Line range hint 41-48: LGTM! Consider documenting the force flag

The implementation of the force flag and error handling looks good. However, it would be helpful to verify if this flag is documented in the help text.


Let me check the command definition to see if the force flag is documented in the command options.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if force flag is documented in help text
rg -A 5 "import-db|import_db" ./app/client/packages/rts/src/ctl/utils.ts

Length of output: 408


Script:

#!/bin/bash
# Check if force flag is documented in command line options
ast-grep --pattern 'const import_db = new Command($$$)' -l ts

Length of output: 63


Script:

#!/bin/bash
# Let's try a broader search for the import_db command definition
rg -A 10 "import_db = new" ./app/client/packages/rts/src/ctl/

Length of output: 63

app/client/packages/rts/src/ctl/import_db.ts (3)

Line range hint 33-47: Consider upgrading to mongosh and improve error handling

The code uses the deprecated mongo shell. Additionally, the error handling could be more specific.

Apply these improvements:

  try {
    shellCmdResult = await utils.execCommandReturningOutput([
-     "mongo",
+     "mongosh",
      process.env.APPSMITH_DB_URL,
      "--quiet",
      "--eval",
      "db.getCollectionNames().length",
    ]);
  } catch (error) {
-   console.error("Failed to execute mongo command:", error);
+   console.error("Failed to get collection count:", error);
+   console.error("Please ensure MongoDB is running and accessible");
    throw error;
  }
- const collectionsLen = parseInt(shellCmdResult.trimEnd());
+ const collectionsLen = parseInt(shellCmdResult.trimEnd(), 10);

48-54: Add type annotation for forceOption parameter

The function parameter lacks type annotation which could lead to type-related issues.

Apply this change:

- export async function run(forceOption) {
+ export async function run(forceOption: boolean) {

Line range hint 55-74: Simplify warning message using template literals

The warning message could be more concise using template literals.

Consider this improvement:

-      console.log();
-      console.log(
-        "**************************** WARNING ****************************",
-      );
-      console.log(
-        `Your target database is not empty, it has data in ${collectionsLen} collections.`,
-      );
+      console.log(`
+**************************** WARNING ****************************
+Your target database is not empty, it has data in ${collectionsLen} collections.
+`);
🧰 Tools
🪛 Biome (1.9.4)

[error] 65-65: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

app/client/packages/rts/src/ctl/mailer.ts (4)

Line range hint 62-72: Security: Use HTTPS for admin settings URL

The URL construction for admin settings uses HTTP, which could expose sensitive information. Additionally, the URL construction might need to handle trailing slashes in the domain name.

Apply this fix:

      if (domainName) {
        text =
          text +
          "Link to Appsmith admin settings: " +
-         "http://" +
+         "https://" +
-         domainName +
+         domainName.replace(/\/$/, '') +
          "/settings/general" +
          "\n";
      }

Line range hint 89-91: Enhance error handling for email failures

The catch block silently logs the error without providing context about the email sending failure. Consider adding more detailed logging and potentially implementing a retry mechanism.

  } catch (err) {
+   const errorContext = `Failed to send backup error notification email. Error: ${err.message}`;
+   console.error(errorContext);
    await logger.backup_error(err.stack);
+   throw new Error(errorContext);
  }

Line range hint 17-35: Simplify environment variable validation

The code contains a redundant check for mailEnabled and uses nested if-else conditions that could be simplified.

Consider this cleaner approach:

-   if (
-     !mailEnabled ||
-     !mailFrom ||
-     !mailHost ||
-     !mailPort ||
-     !mailUser ||
-     !mailPass
-   ) {
-     throw new Error(
-       "Failed to send error mail. Email provider is not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/email to configure it.",
-     );
-   } else if (!mailTo) {
-     throw new Error(
-       "Failed to send error mail. Admin email(s) not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/disable-user-signup#administrator-emails to configure it.",
-     );
-   } else if (!mailEnabled) {
+   if (!mailEnabled) {
      throw new Error(
        "Mail not sent! APPSMITH_MAIL_ENABLED env val is disabled, please refer to https://docs.appsmith.com/setup/instance-configuration/email to enable it.",
      );
+   }
+   
+   if (!mailFrom || !mailHost || !mailPort || !mailUser || !mailPass) {
+     throw new Error(
+       "Failed to send error mail. Email provider is not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/email to configure it.",
+     );
+   }
+   
+   if (!mailTo) {
+     throw new Error(
+       "Failed to send error mail. Admin email(s) not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/disable-user-signup#administrator-emails to configure it.",
+     );
    }

Line range hint 74-82: Improve email transport security configuration

The nodemailer transport configuration uses a type assertion and doesn't explicitly configure TLS/SSL options.

Consider adding proper typing and secure transport configuration:

      const transporter = nodemailer.createTransport({
        host: mailHost,
        port: mailPort,
+       secure: Number(mailPort) === 465,
        auth: {
          user: mailUser,
          pass: mailPass,
        },
-     } as any);
+     });
app/client/packages/rts/src/ctl/backup.ts (4)

46-55: Fix typo in error message.

The error message contains a typo: "enctyption" should be "encryption".

-          "Backup process aborted because a valid enctyption password could not be obtained from the user",
+          "Backup process aborted because a valid encryption password could not be obtained from the user",

Line range hint 151-181: Consider extracting retry attempts to a constant.

The retry logic is well-implemented, but the magic number 3 should be extracted to a named constant for better maintainability.

+const MAX_PASSWORD_RETRY_ATTEMPTS = 3;
+
 export function getEncryptionPasswordFromUser() {
-  for (const attempt of [1, 2, 3]) {
+  for (const attempt of Array.from({length: MAX_PASSWORD_RETRY_ATTEMPTS}, (_, i) => i + 1)) {

269-275: Add logging for removed backup files.

Consider adding logging when removing old backup files to maintain an audit trail.

   while (backupFiles.length > backupArchivesLimit) {
     const fileName = backupFiles.shift();
+    await logger.backup_info(`Removing old backup file: ${fileName}`);
     await fsPromises.rm(Constants.BACKUP_PATH + "/" + fileName);
   }

Line range hint 307-317: Consider using array of patterns for better maintainability.

The sensitive data filtering could be more maintainable using an array of patterns.

+const SENSITIVE_ENV_PATTERNS = [
+  'APPSMITH_ENCRYPTION',
+  'APPSMITH_MONGODB',
+  'APPSMITH_DB_URL='
+];
+
 export function removeSensitiveEnvData(content) {
   const output_lines = [];
   content.split(/\r?\n/).forEach((line) => {
-    if (
-      !line.startsWith("APPSMITH_ENCRYPTION") &&
-      !line.startsWith("APPSMITH_MONGODB") &&
-      !line.startsWith("APPSMITH_DB_URL=")
-    ) {
+    if (!SENSITIVE_ENV_PATTERNS.some(pattern => line.startsWith(pattern))) {
       output_lines.push(line);
     }
   });
app/client/packages/rts/src/ctl/backup.test.ts (4)

53-53: Consider adding edge cases for path generation

While the basic path generation test is good, consider adding tests for:

  • Path with special characters
  • Path with spaces
  • Maximum path length scenarios

140-145: Remove redundant console.log statements

Multiple test cases contain identical console.log statements. Consider using a Jest custom matcher instead.

-console.log(res);
+expect(res).toMatchSnapshot();

Also applies to: 153-158, 166-171, 179-184, 192-197, 204-209


258-258: Remove debug logging

Remove unnecessary console.log statement from the test.


269-269: Add error cases for MongoDB URI parsing

While the happy path cases are well covered, consider adding tests for:

  • Invalid URI format
  • Missing database name
  • Special characters in database name

Also applies to: 278-278, 287-287, 295-295

app/client/packages/rts/src/ctl/restore.ts (4)

Line range hint 12-54: LGTM! Consider enhancing error feedback

The improvements to user interaction and input validation are good. The visual separators and clear messaging make the interface more user-friendly.

Consider adding specific feedback when the input is invalid, e.g., "Input must be a number between 0 and {backupFiles.length - 1}".

-      "Invalid input, please try the command again with a valid option",
+      `Invalid input. Please enter a number between 0 and ${backupFiles.length - 1}`,

Line range hint 62-92: LGTM! Consider enhancing error handling

Good improvements to the retry messaging and loop readability.

Consider logging specific error details in catch block for debugging:

-      console.log("Invalid password. Please try again:");
+      console.log(`Decryption failed (attempt ${attempt}/3): ${error.message}`);

Line range hint 148-226: LGTM! Consider extracting prompt messages

Good implementation of interactive encryption key handling.

Consider extracting the prompt messages to constants for better maintainability:

const ENCRYPTION_PROMPT = 'If you are restoring to the same Appsmith deployment...';
const PASSWORD_PROMPT = 'Enter the APPSMITH_ENCRYPTION_PASSWORD: ';
const SALT_PROMPT = 'Enter the APPSMITH_ENCRYPTION_SALT: ';

Line range hint 319-380: Consider enhancing cleanup error handling

While the cleanup process is good, errors during cleanup might be silently ignored.

Consider adding specific error handling for cleanup operations:

   } finally {
     if (cleanupArchive) {
-      await fsPromises.rm(backupFilePath, { force: true });
+      try {
+        await fsPromises.rm(backupFilePath, { force: true });
+      } catch (cleanupError) {
+        console.warn('Failed to cleanup temporary files:', cleanupError.message);
+      }
     }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between afa2324 and 1f2242a.

⛔ Files ignored due to path filters (1)
  • app/client/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (15)
  • app/client/packages/rts/package.json (1 hunks)
  • app/client/packages/rts/src/ctl/.eslintrc.json (1 hunks)
  • app/client/packages/rts/src/ctl/backup.test.ts (11 hunks)
  • app/client/packages/rts/src/ctl/backup.ts (14 hunks)
  • app/client/packages/rts/src/ctl/check_replica_set.ts (2 hunks)
  • app/client/packages/rts/src/ctl/export_db.ts (1 hunks)
  • app/client/packages/rts/src/ctl/import_db.ts (3 hunks)
  • app/client/packages/rts/src/ctl/index.ts (1 hunks)
  • app/client/packages/rts/src/ctl/logger.ts (0 hunks)
  • app/client/packages/rts/src/ctl/mailer.ts (2 hunks)
  • app/client/packages/rts/src/ctl/mongo_shell_utils.ts (2 hunks)
  • app/client/packages/rts/src/ctl/restore.ts (18 hunks)
  • app/client/packages/rts/src/ctl/utils.test.ts (6 hunks)
  • app/client/packages/rts/src/ctl/utils.ts (13 hunks)
  • app/client/packages/rts/src/ctl/version.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • app/client/packages/rts/src/ctl/logger.ts
✅ Files skipped from review due to trivial changes (1)
  • app/client/packages/rts/src/ctl/.eslintrc.json
🔇 Additional comments (15)
app/client/packages/rts/src/ctl/utils.ts (1)

Line range hint 73-100: Ensure callers handle the updated async functions

The functions execCommand, execCommandReturningOutput, and execCommandSilent have been converted to async. Make sure all callers are updated to handle the returned promises appropriately.

app/client/packages/rts/src/ctl/version.ts (1)

5-11: LGTM! Robust error handling for version fetch.

The try-catch block properly handles potential errors from the async operation with clear error messaging.

app/client/packages/rts/src/ctl/mongo_shell_utils.ts (1)

Line range hint 26-30: Verify command injection prevention in utils.execCommand

Let's ensure the execCommand function properly escapes shell arguments.

✅ Verification successful

Command injection is properly prevented through array-based spawn usage

The implementation uses Node's childProcess.spawn with array-based command and arguments, which inherently prevents command injection:

  • Command is passed as cmd[0] for the executable
  • Arguments are passed separately as cmd.slice(1)
  • No string concatenation or shell interpretation is involved

This is the recommended secure approach for executing system commands in Node.js.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for execCommand implementation to verify shell argument handling
ast-grep --pattern 'function execCommand($_) {
  $$$
}'

Length of output: 57


Script:

#!/bin/bash
# Search for execCommand implementation with broader pattern and ripgrep
rg "execCommand" -A 10

# Also search for any spawn or exec related functions
rg "spawn|exec" -A 10

Length of output: 104173


Script:

#!/bin/bash
# Search for the implementation of execCommand in utils.ts
rg -A 10 "export async function execCommand" app/client/packages/rts/src/ctl/utils.ts

Length of output: 1026

app/client/packages/rts/src/ctl/export_db.ts (1)

8-9: Add specific error handling for directory creation

While the recursive directory creation is good, consider adding specific error handling for filesystem operations.

-  await fsPromises.mkdir(Constants.BACKUP_PATH, { recursive: true });
+  try {
+    await fsPromises.mkdir(Constants.BACKUP_PATH, { recursive: true });
+  } catch (err) {
+    if (err.code !== 'EEXIST') {
+      throw new Error(`Failed to create backup directory: ${err.message}`);
+    }
+  }

Let's verify the backup path configuration:

app/client/packages/rts/src/ctl/check_replica_set.ts (1)

Line range hint 6-11: Verify MongoDB connection string handling

Ensure that the preprocessMongoDBURI function properly sanitizes the connection string.

✅ Verification successful

MongoDB connection string handling is secure and well-implemented

The preprocessMongoDBURI function properly handles connection strings using the official mongodb-connection-string-url package and implements security best practices:

  • Safely parses and validates the URI using ConnectionString
  • Sets appropriate timeouts for localhost connections
  • Handles direct connections and replica sets correctly
  • Doesn't expose sensitive connection details
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for preprocessMongoDBURI implementation
ast-grep --pattern 'function preprocessMongoDBURI($_) {
  $$$
}'

Length of output: 66


Script:

#!/bin/bash
# Search for preprocessMongoDBURI implementation with different patterns
rg -U "preprocessMongoDBURI.*=.*{" -A 10

# Also search for export or function declaration
rg "export.*function.*preprocessMongoDBURI"

# Search for any file containing preprocessMongoDBURI
rg "preprocessMongoDBURI"

Length of output: 635


Script:

#!/bin/bash
# Get the implementation of preprocessMongoDBURI from utils.ts
cat app/client/packages/rts/src/ctl/utils.ts

Length of output: 6512

app/client/packages/rts/src/ctl/utils.test.ts (2)

Line range hint 5-54: Well-structured test coverage for command output handling

The test suite thoroughly covers various output scenarios including stdout, stderr, and their combinations. The assertions are clear and specific.


Line range hint 58-74: Solid test implementation with proper spy cleanup and error handling

The test suite effectively verifies command silencing and error scenarios. Good use of jest.spyOn with proper cleanup, and appropriate async/await error handling pattern.

app/client/packages/rts/src/ctl/backup.ts (2)

25-25: LGTM! Good addition of disk space verification.

The space check before backup prevents potential failures during the backup process.


59-71: Verify encryption parameters across environments.

The encryption implementation uses strong parameters (AES-256-CBC with PBKDF2). However, let's verify these parameters are consistently used across the codebase.

✅ Verification successful

Let me check the encryption implementation details to ensure consistency.


Encryption parameters are consistently implemented across backup and restore operations

The encryption parameters are consistently used in both backup and restore operations:

  • AES-256-CBC encryption
  • PBKDF2 key derivation
  • 100,000 iterations for key strengthening

The implementation is secure and uniform across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other encryption implementations to ensure consistency
rg -g '!*.{lock,json,md}' 'enc.*aes-256-cbc|pbkdf2'

Length of output: 1105


Script:

#!/bin/bash
# Check the actual encryption implementation in backup.ts and restore.ts
cat app/client/packages/rts/src/ctl/backup.ts app/client/packages/rts/src/ctl/restore.ts

Length of output: 21152

app/client/packages/rts/src/ctl/backup.test.ts (3)

23-23: LGTM: Space requirement assertions are well-defined

The assertions correctly verify disk space requirements and error conditions.

Also applies to: 29-29, 35-35


108-108: LGTM: Version check is properly implemented

The version check correctly handles the snapshot version format.


216-216: LGTM: Password validation tests are comprehensive

Tests properly handle both matching and non-matching password scenarios.

Also applies to: 225-230

app/client/packages/rts/src/ctl/restore.ts (3)

Line range hint 117-134: LGTM! Clear database restoration feedback

Good addition of database name logging and manifest error handling.


Line range hint 285-305: LGTM! Good database name handling

The implementation properly handles both command-line arguments and manifest file for database name resolution.


Line range hint 252-276: Verify Docker image suggestion security implications

While the version compatibility check is good, suggesting specific Docker image versions needs verification.

Let's verify if the suggested Docker image version is from a trusted source:

Consider implementing a version compatibility matrix or range check instead of exact version matching.

✅ Verification successful

The previous attempt didn't work as expected because restoreVersion was a variable in the code. Let's analyze how the version is handled in the codebase to understand the security implications better.


I want to understand how the version is validated and where it comes from before making a final decision about the security implications.


Docker image suggestion is safe and properly validated

The version compatibility check is well implemented:

  • The version is read from a manifest.json file that is part of the backup
  • The Docker image reference uses the official registry (index.docker.io) and official image (appsmith/appsmith-ce)
  • The version comes directly from the backup's manifest without manipulation
  • Users are given a clear warning and choice to continue or cancel

The implementation follows security best practices by:

  1. Using the official Docker registry
  2. Not allowing arbitrary image names/tags
  3. Providing version transparency
  4. Giving users control over the process
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the Docker image exists in the official registry
curl -s "https://hub.docker.com/v2/repositories/appsmith/appsmith-ce/tags/" | jq -r '.results[].name' | grep -q "${restoreVersion}"

Length of output: 114


Script:

#!/bin/bash
# Let's analyze how the Docker image version is constructed and validated
rg -A 5 "restoreVersion" --type ts

# Check if there are any version validation functions
ast-grep --pattern 'function $FUNC($$$) {
  $$$
  version $$$
  $$$
}'

# Look for Docker image related constants or configurations
rg -i "docker" --type ts -A 3

Length of output: 41764


Script:

#!/bin/bash
# Let's check how the version is read from the manifest
ast-grep --pattern 'const $VAR = manifest_json["appsmithVersion"]'

# Check for version validation patterns
ast-grep --pattern 'function $FUNC($$$) {
  $$$
  if ($VERSION === $$$) {
    $$$
  }
}'

# Look for manifest handling
rg -A 3 "manifest_json" --type ts

Length of output: 160377

@@ -2,12 +2,14 @@ import * as utils from "./utils";

export async function exec() {
let version = null;

try {
version = await utils.getCurrentAppsmithVersion();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Error handling for getCurrentAppsmithVersion needs improvement

The implementation in utils.ts shows that getCurrentAppsmithVersion can fail in two ways:

  • File read errors from fsPromises.readFile
  • JSON parse errors

The current error handling in version.ts only logs and exits, but other callers (backup.ts, restore.ts) don't handle errors at all. Add try-catch blocks in all locations that call this function.

  • app/client/packages/rts/src/ctl/backup.ts:3
  • app/client/packages/rts/src/ctl/restore.ts:3
🔗 Analysis chain

Verify error scenarios from getCurrentAppsmithVersion.

Let's ensure our error handling covers all possible failure modes from the utility function.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find implementation of getCurrentAppsmithVersion to verify error cases
ast-grep --pattern 'getCurrentAppsmithVersion = async function() {
  $$$
}'

# Also check for any existing error handling patterns
rg -A 3 'getCurrentAppsmithVersion.*catch'

Length of output: 122


Script:

#!/bin/bash
# Try alternative pattern to find getCurrentAppsmithVersion implementation
ast-grep --pattern 'getCurrentAppsmithVersion = $$$'

# Search for the function with ripgrep
rg -A 10 'getCurrentAppsmithVersion'

# Look for the utils file specifically
fd utils.ts --exec cat {}

Length of output: 142066


Script:

#!/bin/bash
# Try finding utils file in the same directory as version.ts
fd utils.ts -p app/client/packages/rts/src/ctl/

# Search for getCurrentAppsmithVersion with more context
rg -B 2 -A 5 "getCurrentAppsmithVersion"

# Look for error handling patterns around version
rg -B 2 -A 5 "version.*catch" app/client/packages/rts/src/ctl/

Length of output: 3074

Comment on lines +20 to +24

if (command_args.includes("--pretty")) {
queryExpression += ".pretty()";
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add input validation for query expression

The query expression is used directly without validation. Consider adding checks for malicious input.

 async function execMongoEval(queryExpression, appsmithMongoURI) {
+  if (!queryExpression || typeof queryExpression !== 'string') {
+    throw new Error('Query expression must be a non-empty string');
+  }
   queryExpression = queryExpression.trim();

Committable suggestion skipped: line range outside the PR's diff.

@@ -36,6 +36,7 @@
"devDependencies": {
"@types/express": "^4.17.14",
"@types/jest": "^29.2.3",
"@types/node": "*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace wildcard version with a specific version for @types/node

Using "*" for @types/node could lead to unexpected type definition changes and potential conflicts. Consider using a specific version that matches your Node.js runtime version.

-    "@types/node": "*",
+    "@types/node": "^20.0.0"

Committable suggestion skipped: line range outside the PR's diff.


💡 Codebase verification

Update @types/node to match Node.js v20.11.1

The project uses Node.js v20.11.1, but @types/node is set to "*". Using a wildcard version can lead to type definition mismatches. Update to a compatible version:

"@types/node": "^20.11.1"
🔗 Analysis chain

Verify Node.js version compatibility

Ensure the @types/node version matches your Node.js runtime version to prevent type definition mismatches.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check Node.js version in package.json and related files

# Check for Node.js version specifications in package.json or .nvmrc
fd -t f "package.json|.nvmrc" -x cat {} \; | grep -i "node"

# Check for Node.js engine constraints
cat package.json | jq '.engines.node'

Length of output: 1305


Script:

#!/bin/bash
# Let's check the root package.json and any .nvmrc files for Node.js version
cat app/client/package.json | jq '.engines.node'

# Also check the specific package's dependencies
cat app/client/packages/rts/package.json | jq '.dependencies["@types/node"], .devDependencies["@types/node"]'

Length of output: 180

Comment on lines +331 to +334

await fsPromises.rm(Constants.BACKUP_PATH + "/" + fileName);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consolidate duplicate backup removal logic.

This logic is duplicated with postBackupCleanup. Consider consolidating into a single utility function.

+async function removeBackupFile(fileName: string) {
+  await logger.backup_info(`Removing backup file: ${fileName}`);
+  await fsPromises.rm(Constants.BACKUP_PATH + "/" + fileName);
+}
+
 export async function removeOldBackups(backupFiles, backupArchivesLimit) {
   while (backupFiles.length > backupArchivesLimit) {
-    const fileName = backupFiles.shift();
-    await fsPromises.rm(Constants.BACKUP_PATH + "/" + fileName);
+    await removeBackupFile(backupFiles.shift());
   }
   return backupFiles;
 }

Committable suggestion skipped: line range outside the PR's diff.

expect(res).toBe("temp/dir/appsmithctl-backup-");
});

test("Test backup contents path generation", () => {
const root = "/rootDir";
const timestamp = "0000-00-0T00-00-00.00Z";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for special characters in paths

The command generation tests should include scenarios with paths containing spaces and special characters to ensure proper escaping.

Also applies to: 72-72, 94-94

github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Nov 27, 2024
Fix linting exceptions in `ctl`.


## Automation

/test sanity

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!WARNING]
> Tests have not run on the HEAD
1f2242a yet
> <hr>Fri, 22 Nov 2024 10:06:23 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Enhanced backup and restore processes with improved user prompts and
error handling.
- Added support for optional command-line flags during database imports.

- **Bug Fixes**
- Improved error handling for various operations, including database
exports and imports.
	- Enhanced logging for backup errors to provide more context.

- **Documentation**
- Updated user prompts and error messages for clarity during backup and
restore operations.

- **Tests**
- Expanded test coverage for backup functionalities and utility
functions to ensure robust error handling and output validation.

- **Chores**
	- Updated dependencies to enhance TypeScript development experience.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants