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

feat(api): Added total count of env, vars and secrets of a project #311

Closed
wants to merge 3 commits into from

Conversation

yogesh1801
Copy link
Contributor

@yogesh1801 yogesh1801 commented Jul 3, 2024

User description

Description

Give a summary of the change that you have made

Fixes #308

Dependencies

Mention any dependencies/packages used

Future Improvements

Mention any improvements to be done in future related to any file/feature

Mentions

Mention and tag the people

Screenshots of relevant screens

Add screenshots of relevant screens

Developer's checklist

  • My PR follows the style guidelines of this project
  • I have performed a self-check on my work

If changes are made in the code:

  • I have followed the coding guidelines
  • My changes in code generate no new warnings
  • My changes are breaking another fix/feature of the project
  • I have added test cases to show that my feature works
  • I have added relevant screenshots in my PR
  • There are no UI/UX issues

Documentation Update

  • This PR requires an update to the documentation at docs.keyshade.xyz
  • I have made the necessary updates to the documentation, or no documentation changes are required.

PR Type

Enhancement, Other


Description

  • Added new functions to fetch environments, variables, and secrets of a project with authority checks.
  • Integrated these functions into the project service to append counts of environments, variables, and secrets to the project object.
  • Implemented sorting and searching functionality in the new functions.
  • Included decryption check for secrets in the getAllSecretsOfProject function.

Changes walkthrough 📝

Relevant files
Enhancement
get-environmets-of-project.ts
Add function to fetch project environments with authority check

apps/api/src/common/get-environmets-of-project.ts

  • Added a new function getEnvironmentsOfProject to fetch environments of
    a project.
  • Included authority check for reading environments.
  • Implemented sorting and searching functionality.
  • +37/-0   
    get-secrets-of-project.ts
    Add function to fetch project secrets with decryption check

    apps/api/src/common/get-secrets-of-project.ts

  • Added a new function getAllSecretsOfProject to fetch secrets of a
    project.
  • Included authority check for reading secrets.
  • Implemented decryption check and fetching latest secret versions.
  • +150/-0 
    get-variables-of-project.ts
    Add function to fetch project variables with authority check

    apps/api/src/common/get-variables-of-project.ts

  • Added a new function getAllVariablesOfProject to fetch variables of a
    project.
  • Included authority check for reading variables.
  • Implemented fetching latest variable versions.
  • +123/-0 
    project.service.ts
    Integrate environment, variable, and secret counts in project service

    apps/api/src/project/service/project.service.ts

  • Integrated new functions to fetch environments, variables, and secrets
    in getProjectsOfUser.
  • Appended counts of environments, variables, and secrets to the project
    object.
  • +47/-1   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @yogesh1801 yogesh1801 requested a review from rajdip-b as a code owner July 3, 2024 17:18
    Copy link
    Contributor

    Failed to generate code suggestions for PR

    @rajdip-b
    Copy link
    Member

    rajdip-b commented Jul 3, 2024

    @yogesh1801 I don't see you calling this from the service layers. Are you still working on this? If so, you can convert it to draft.

    @yogesh1801 yogesh1801 marked this pull request as draft July 3, 2024 17:24
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 Security concerns

    - Sensitive Information Exposure:
    The decryption of secrets in getAllSecretsOfProject could expose sensitive information if not handled securely. Ensure that the decryption keys are managed securely and that access to these keys is tightly controlled.

    ⚡ Key issues to review

    Possible Performance Issue:
    The implementation in getAllSecretsOfProject and getAllVariablesOfProject involves multiple database queries within a loop which could lead to performance degradation especially with a large number of secrets or variables. Consider optimizing these queries or using batch operations.

    Data Integrity Concern:
    In getAllSecretsOfProject, there is a decryption process for secrets. Ensure that error handling is robust around decryption to prevent data leaks or crashes due to decryption failures.

    Error Handling:
    The functions getEnvironmentsOfProject, getAllVariablesOfProject, and getAllSecretsOfProject should include more comprehensive error handling to manage and log different failure scenarios effectively.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Performance
    Optimize the logic for fetching and organizing secret versions to improve performance and reduce complexity

    Instead of using a nested loop to iterate over secrets and environments, consider
    restructuring the logic to reduce complexity and improve performance. You can use a single
    query to fetch the latest versions of secrets for all environments, then process the
    results to organize them by secret ID.

    apps/api/src/common/get-secrets-of-project.ts [95-146]

    -for (const secret of secrets) {
    -  const envIds = new Map(environmentIds)
    -  let iterations = envIds.size
    -  while (iterations--) {
    -    const latestVersion = await prisma.secretVersion.findFirst({
    -      where: {
    -        secretId: secret.id,
    -        environmentId: {
    -          in: Array.from(envIds.keys())
    -        }
    -      },
    -      orderBy: {
    -        version: 'desc'
    -      }
    -    })
    -    if (!latestVersion) continue
    -    if (secretsWithEnvironmentalValues.has(secret.id)) {
    -      secretsWithEnvironmentalValues.get(secret.id).values.push({
    -        environment: {
    -          id: latestVersion.environmentId,
    -          name: envIds.get(latestVersion.environmentId)
    -        },
    -        value: decryptValue
    -          ? await decrypt(project.privateKey, latestVersion.value)
    -          : latestVersion.value,
    -        version: latestVersion.version
    -      })
    -    } else {
    -      secretsWithEnvironmentalValues.set(secret.id, {
    -        secret,
    -        values: [
    -          {
    -            environment: {
    -              id: latestVersion.environmentId,
    -              name: envIds.get(latestVersion.environmentId)
    -            },
    -            value: decryptValue
    -              ? await decrypt(project.privateKey, latestVersion.value)
    -              : latestVersion.value,
    -            version: latestVersion.version
    -          }
    -        ]
    -      })
    +const latestVersions = await prisma.secretVersion.findMany({
    +  where: {
    +    secretId: {
    +      in: secrets.map(secret => secret.id)
         }
    -    envIds.delete(latestVersion.environmentId)
    +  },
    +  orderBy: {
    +    version: 'desc'
       }
    -}
    +});
    +latestVersions.forEach(version => {
    +  const secretValues = secretsWithEnvironmentalValues.get(version.secretId) || { secret: secrets.find(s => s.id === version.secretId), values: [] };
    +  secretValues.values.push({
    +    environment: {
    +      id: version.environmentId,
    +      name: environmentIds.get(version.environmentId)
    +    },
    +    value: decryptValue ? await decrypt(project.privateKey, version.value) : version.value,
    +    version: version.version
    +  });
    +  secretsWithEnvironmentalValues.set(version.secretId, secretValues);
    +});
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a performance bottleneck and offers a more efficient approach to fetching and organizing secret versions. This change reduces the complexity of the code and can significantly improve performance, especially for large datasets.

    8

    Copy link
    Member

    @rajdip-b rajdip-b left a comment

    Choose a reason for hiding this comment

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

    Okay one thing that we need to change is, we would need to optionally paginate the environments, secrets and variables. So you can send in an optional skipPagination to those 3 methods you created.

    Also, could you provide a screenshot or the response?

    @rajdip-b
    Copy link
    Member

    rajdip-b commented Aug 2, 2024

    @yogesh1801 any progress on this yet?

    @rajdip-b
    Copy link
    Member

    rajdip-b commented Sep 5, 2024

    Closing due to inactivity. Feel free to pick this up if you are up again.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    API: Send Total numbers of Environments, Variables, and Secrets
    2 participants