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(cli): Add functionality to operate on Secrets #504

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

anudeeps352
Copy link
Contributor

@anudeeps352 anudeeps352 commented Oct 20, 2024

User description

Description

Add functionality to operate on Secrets

Fixes #302

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


Description

  • Added a new SecretCommand class to manage secrets via CLI, including subcommands for creating, deleting, listing, fetching, updating, and rolling back secrets.
  • Implemented CreateSecret, DeleteSecret, GetSecret, ListSecret, FetchSecretRevisions, RollbackSecret, and UpdateSecret classes, each handling specific secret operations.
  • Registered SecretCommand in the CLI command list to enable secret management functionality.

Changes walkthrough 📝

Relevant files
Enhancement
9 files
secret.command.ts
Add SecretCommand class with subcommands for CLI                 

apps/cli/src/commands/secret.command.ts

  • Introduced SecretCommand class extending BaseCommand.
  • Added subcommands for managing secrets.
  • Implemented getName, getDescription, and getSubCommands methods.
  • +30/-0   
    create.secret.ts
    Implement CreateSecret command for secret creation             

    apps/cli/src/commands/secret/create.secret.ts

  • Created CreateSecret class for creating secrets.
  • Defined command arguments and options.
  • Implemented action method to handle secret creation.
  • +129/-0 
    delete.secret.ts
    Implement DeleteSecret command for secret deletion             

    apps/cli/src/commands/secret/delete.secret.ts

  • Added DeleteSecret class for deleting secrets.
  • Defined command arguments.
  • Implemented action method to handle secret deletion.
  • +44/-0   
    get.secret.ts
    Implement GetSecret command for retrieving secrets             

    apps/cli/src/commands/secret/get.secret.ts

  • Introduced GetSecret class for retrieving secrets.
  • Defined command arguments and options.
  • Implemented action method to fetch secrets.
  • +75/-0   
    list.secret.ts
    Implement ListSecret command for listing secrets                 

    apps/cli/src/commands/secret/list.secret.ts

  • Created ListSecret class for listing secrets.
  • Defined command arguments.
  • Implemented action method to list secrets.
  • +56/-0   
    revisions.secret.ts
    Implement FetchSecretRevisions command for secret revisions

    apps/cli/src/commands/secret/revisions.secret.ts

  • Added FetchSecretRevisions class for fetching secret revisions.
  • Defined command arguments.
  • Implemented action method to retrieve revisions.
  • +62/-0   
    rollback.secret.ts
    Implement RollbackSecret command for secret rollback         

    apps/cli/src/commands/secret/rollback.secret.ts

  • Introduced RollbackSecret class for rolling back secrets.
  • Defined command arguments and options.
  • Implemented action method to perform rollback.
  • +79/-0   
    update.secret.ts
    Implement UpdateSecret command for secret updates               

    apps/cli/src/commands/secret/update.secret.ts

  • Created UpdateSecret class for updating secrets.
  • Defined command arguments and options.
  • Implemented action method to update secrets.
  • +77/-0   
    index.ts
    Register SecretCommand in CLI command list                             

    apps/cli/src/index.ts

    • Added SecretCommand to the list of CLI commands.
    +2/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    302 - Fully compliant

    Fully compliant requirements:

    • List all secrets under a project
    • Fetch all revisions of a secret
    • Create a secret
    • Update a secret
    • Rollback a secret
    • Delete a secret
    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 Security concerns

    Sensitive information exposure:
    In the ListSecret class (apps/cli/src/commands/secret/list.secret.ts), there's a console.log(data) statement that might expose sensitive secret information. This should be removed or replaced with a more secure logging method that doesn't reveal secret values.

    ⚡ Recommended focus areas for review

    Error Handling
    The parseInput method doesn't handle all possible error cases, such as invalid rotateAfter values.

    Potential Security Issue
    The console.log(data) statement may expose sensitive information. Consider removing or replacing with a more secure logging method.

    Input Validation
    The action method passes options directly to the controller without validation, which could lead to unexpected behavior or security issues.

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Security
    Remove unnecessary console.log statement to prevent potential information leakage

    Remove the console.log statement as it's not necessary and may expose sensitive
    information in production.

    apps/cli/src/commands/secret/list.secret.ts [42-45]

     if (success) {
    -  console.log(data)
       const secrets = data
       if (secrets.length > 0) {
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Removing the console.log statement is crucial for preventing potential information leakage, especially in a production environment where sensitive data might be exposed inadvertently.

    9
    Possible issue
    Validate the rotateAfter option to ensure only accepted values are used

    Add input validation for the rotateAfter option to ensure it only accepts valid
    values.

    apps/cli/src/commands/secret/create.secret.ts [122-128]

    +const validRotateAfterValues = ['24', '168', '720', '8760', 'never'];
    +if (rotateAfter && !validRotateAfterValues.includes(rotateAfter)) {
    +  throw new Error('Invalid rotateAfter value. Must be 24, 168, 720, 8760, or never.');
    +}
     return {
       name,
       note,
       rotateAfter,
       entries: parsedEntries
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding input validation for the rotateAfter option is important to prevent invalid values from being processed, which could lead to unexpected behavior or errors.

    8
    Validate the rotate-after option in the update command to ensure only accepted values are used

    Add input validation for the rotate-after option to ensure it only accepts valid
    values, similar to the create command.

    apps/cli/src/commands/secret/update.secret.ts [55-64]

     async action({ args, options }: CommandActionData): Promise<void> {
       const [secretSlug] = args
    +  const validRotateAfterValues = ['24', '168', '720', '8760', 'never'];
    +  if (options.rotateAfter && !validRotateAfterValues.includes(options.rotateAfter)) {
    +    Logger.error('Invalid rotate-after value. Must be 24, 168, 720, 8760, or never.');
    +    return;
    +  }
     
       const { data, error, success } =
         await ControllerInstance.getInstance().secretController.updateSecret(
           {
             secretSlug,
             ...options
           },
           this.headers
         )
    Suggestion importance[1-10]: 8

    Why: Similar to the create command, input validation for the rotate-after option in the update command is necessary to ensure data integrity and prevent invalid values from causing issues.

    8
    Enhancement
    Improve readability and consistency of logged revision information

    Use template literals for better readability and consistency when logging revision
    information.

    apps/cli/src/commands/secret/revisions.secret.ts [47-53]

    -Logger.info(`Id ${revision.id}`)
    -Logger.info(`value ${revision.value}`)
    -Logger.info(`version ${revision.version}`)
    -Logger.info(`secretID ${revision.secretId}`)
    -Logger.info(`Created On ${revision.createdOn}`)
    -Logger.info(`Created By Id ${revision.createdById}`)
    -Logger.info(`environmentId ${revision.environmentId}`)
    +Logger.info(`Id: ${revision.id}`)
    +Logger.info(`Value: ${revision.value}`)
    +Logger.info(`Version: ${revision.version}`)
    +Logger.info(`Secret ID: ${revision.secretId}`)
    +Logger.info(`Created On: ${revision.createdOn}`)
    +Logger.info(`Created By Id: ${revision.createdById}`)
    +Logger.info(`Environment ID: ${revision.environmentId}`)
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: While using template literals with added labels improves readability and consistency, the impact is moderate as it does not affect functionality or address any critical issues.

    5

    💡 Need additional feedback ? start a PR chat

    @rajdip-b rajdip-b self-requested a review as a code owner October 24, 2024 11:52
    Copy link

    codecov bot commented Oct 24, 2024

    Codecov Report

    All modified and coverable lines are covered by tests ✅

    Project coverage is 87.64%. Comparing base (ce50743) to head (87e9d3a).
    Report is 194 commits behind head on develop.

    Additional details and impacted files
    @@             Coverage Diff             @@
    ##           develop     #504      +/-   ##
    ===========================================
    - Coverage    91.71%   87.64%   -4.08%     
    ===========================================
      Files          111      105       -6     
      Lines         2510     2743     +233     
      Branches       469      415      -54     
    ===========================================
    + Hits          2302     2404     +102     
    - Misses         208      339     +131     
    Flag Coverage Δ
    api-e2e-tests 87.64% <ø> (-4.08%) ⬇️

    Flags with carried forward coverage won't be shown. Click here to find out more.

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @rajdip-b rajdip-b force-pushed the Operate-On-Secret-CLI branch from 87e9d3a to 6b6b6f5 Compare October 24, 2024 12:03
    @rajdip-b rajdip-b merged commit 1b4bf2f into keyshade-xyz:develop Oct 24, 2024
    4 checks passed
    rajdip-b pushed a commit that referenced this pull request Oct 24, 2024
    ## [2.6.0](v2.5.0...v2.6.0) (2024-10-24)
    
    ### 🚀 Features
    
    * **api:**  Add icon and remove description field from workspace ([#435](#435)) ([a99c0db](a99c0db))
    * **api-client:** Added workspace-membership and related tests ([#452](#452)) ([6a1c091](6a1c091))
    * **api-client:** Create controller for User module ([#484](#484)) ([f9d8e83](f9d8e83))
    * **api:** Add prod env schema in env file ([#436](#436)) ([21c3004](21c3004))
    * **api:** Add resend otp implementation ([#445](#445)) ([4dc6aa1](4dc6aa1))
    * **api:** Fetch total count of environments, [secure]s and variables in project ([#434](#434)) ([0c9e50a](0c9e50a))
    * **api:** Replace `projectId` with `name` and `slug` in workspace-role response.  ([#432](#432)) ([af06071](af06071))
    * **cli:** Add functionality to operate on Secrets ([#504](#504)) ([1b4bf2f](1b4bf2f))
    * **cli:** Add project command ([#451](#451)) ([70448e1](70448e1))
    * **cli:** Add workspace operations ([#441](#441)) ([ed38d22](ed38d22))
    * **cli:** implement commands to get, list, update, and delete, workspace roles ([#469](#469)) ([957ea8d](957ea8d))
    * **cli:** Implemented pagination support ([#453](#453)) ([feb1806](feb1806))
    * **cli:** Secret scan ([#438](#438)) ([85cb8ab](85cb8ab))
    * **cli:** Update environment command outputs ([f4af874](f4af874))
    * **platform:** Clearing email field after waitlisting the user email ([#481](#481)) ([256d659](256d659))
    * Remove project IDs from workspace role export data and update tests ([#448](#448)) ([8fdb328](8fdb328))
    * **web:** Configured extra check for waitlisted users already in the list and created toast message for them ([#492](#492)) ([2ddd0ef](2ddd0ef))
    * **web:** show the toast only when email add successfully ([#490](#490)) ([783c411](783c411))
    
    ### 🐛 Bug Fixes
    
    * **api,api-client:** Add environmentSlug in multiple places across the variable module ([#468](#468)) ([d970aff](d970aff))
    * **api:** Replace the id with slug in the global-search service ([#455](#455)) ([74804b1](74804b1))
    * **platform:** Fixed duplicate Google Logo UI fix  ([#450](#450)) ([fb0d6f7](fb0d6f7))
    * resolve footer website name cut-off or overlap issue ([#444](#444)) ([fe03ba2](fe03ba2))
    * **web:** Horizontal Scrolling issue on the website ([#440](#440)) ([655177b](655177b))
    
    ### 📚 Documentation
    
    * Add documentation for environment in CLI ([#462](#462)) ([dad7394](dad7394))
    * Add documentation for project in CLI ([#466](#466)) ([341fb32](341fb32))
    * Add documentation for scan in CLI ([#461](#461)) ([72281e6](72281e6))
    * Add documentation for workspace command ([#464](#464)) ([4aad8a2](4aad8a2))
    * Add instructions for resetting the local Prisma database ([#495](#495)) ([#501](#501)) ([b07ea17](b07ea17))
    * Added docker support documentation ([#465](#465)) ([bc04be4](bc04be4))
    * Added documentation for running the platform ([#473](#473)) ([8b8386b](8b8386b))
    * Added missing mappings to pages ([5de9fd8](5de9fd8))
    * Fix Documentation Hyperlink and update expired Discord invite link ([#496](#496)) ([5a10e39](5a10e39))
    * Updated CLI docs ([#460](#460)) ([c7e0f13](c7e0f13))
    
    ### 🔧 Miscellaneous Chores
    
    * Add more logging to Sentry init ([#470](#470)) ([de4925d](de4925d))
    * **api:** Optimise API docker image size ([#360](#360)) ([ea40dc1](ea40dc1))
    * **api:** Updated lockfile ([a968e78](a968e78))
    * **CI:** Add [secure] scan validation ([f441262](f441262))
    * **cli:** Update controller invocation in environment commands ([#477](#477)) ([596bd1a](596bd1a))
    * Minor changes to variables ([fe01ca6](fe01ca6))
    * **[secure]-scan:** Failing lint issues ([#507](#507)) ([48f45df](48f45df))
    * **[secure]-scan:** Formatted files ([5884833](5884833))
    * Update .env.example ([70ad4f7](70ad4f7))
    * Updated scripts ([9eb76a7](9eb76a7))
    * **web:** email validation ([#487](#487)) ([e8e737a](e8e737a))
    @rajdip-b
    Copy link
    Member

    🎉 This PR is included in version 2.6.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    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.

    CLI: Add functionality to operate on Secrets
    2 participants