Skip to content

feat: add environment file check pass whitelist#913

Merged
hwbrzzl merged 3 commits intogoravel:masterfrom
go-projects:env
Feb 25, 2025
Merged

feat: add environment file check pass whitelist#913
hwbrzzl merged 3 commits intogoravel:masterfrom
go-projects:env

Conversation

@kuafuRace
Copy link
Contributor

@kuafuRace kuafuRace commented Feb 24, 2025

📑 Description

  • add environment file check pass whitelist, if the command on the whitelist does not check whether the environment file exists

Summary by CodeRabbit

  • New Features

    • Improved secure configuration validation now provides more accurate feedback during initialization.
    • Enhanced command handling enables additional environment management operations through a new whitelist approach.
  • Refactor

    • Streamlined internal processing for configuration commands to ensure more consistent behavior.
    • Introduced a new function for checking command membership in a whitelist, enhancing modularity.
    • Updated environment-related variables for improved configuration options.

✅ Checks

  • Added test cases for my code

@kuafuRace kuafuRace requested a review from a team as a code owner February 24, 2025 15:19
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2025

Walkthrough

The changes update how application key initialization and command-line arguments are processed. In config/application.go, the condition to verify APP_KEY now depends on whether the DontVerifyEnvFileExists flag is false. In foundation/application.go, the specific commands ("key:generate" and "env:decrypt") are now checked against a new whitelist instead of using a boolean flag. Additionally, support/constant.go has been refactored: the boolean flag has been removed, and a new whitelist variable has been added alongside a reorganization of variable declarations.

Changes

Files Change Summary
config/application.go Updated the NewApplication function to change the conditional check for APP_KEY initialization from IsKeyGenerateCommand to DontVerifyEnvFileExists.
foundation/application.go Modified the setEnv function to remove the IsKeyGenerateCommand logic and instead check command-line arguments against the new DontVerifyEnvFileWhitelist. Added slices.Contains for argument checking.
support/constant.go Reorganized variable declarations: removed IsKeyGenerateCommand, added DontVerifyEnvFileExists (bool) and DontVerifyEnvFileWhitelist ([]string) with initial values.

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24.0)
Failed executing command with error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24.0)

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@kuafuRace
Copy link
Contributor Author

Ready Review

@codecov
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.83%. Comparing base (96a1644) to head (09019e0).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #913      +/-   ##
==========================================
- Coverage   68.86%   68.83%   -0.03%     
==========================================
  Files         154      154              
  Lines       10196    10190       -6     
==========================================
- Hits         7021     7014       -7     
- Misses       2852     2854       +2     
+ Partials      323      322       -1     

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

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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 96a1644 and 0fb45cc.

📒 Files selected for processing (3)
  • config/application.go (1 hunks)
  • foundation/application.go (1 hunks)
  • support/constant.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
foundation/application.go

[warning] 281-282: foundation/application.go#L281-L282
Added lines #L281 - L282 were not covered by tests


[warning] 284-284: foundation/application.go#L284
Added line #L284 was not covered by tests

🔇 Additional comments (2)
support/constant.go (1)

11-22: LGTM! The variable reorganization improves code readability.

The changes improve code organization by:

  • Grouping related environment variables together.
  • Introducing a whitelist mechanism for environment verification bypass.
config/application.go (1)

38-52: LGTM! The condition change maintains the same logic while using the new whitelist mechanism.

The error messages provide clear guidance on how to initialize the APP_KEY.

Comment on lines 281 to 285
support.EnvVerifyWhitelist = append(support.EnvVerifyWhitelist, arg)
}
if arg == "env:decrypt" {
support.EnvVerifyWhitelist = append(support.EnvVerifyWhitelist, arg)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Add test coverage for the new whitelist entries.

The static analysis indicates that the new lines adding commands to EnvVerifyWhitelist are not covered by tests. Please add test cases to verify:

  1. The "key:generate" command is correctly added to the whitelist.
  2. The "env:decrypt" command is correctly added to the whitelist.

🏁 Script executed:

#!/bin/bash
# Description: Check for existing test files that might be suitable for adding the new test cases.

echo "Searching for test files related to command handling..."
fd -e go -x grep -l "key:generate\|env:decrypt" {}

Length of output: 377


Action Required: Enhance Test Coverage for Whitelist Updates

Static analysis indicates that test coverage for the whitelist updates in foundation/application.go (lines 281-285) is incomplete. While the test file ./foundation/console/env_decrypt_command_test.go appears to cover the "env:decrypt" command, there is no dedicated test ensuring that the "key:generate" command is correctly added to EnvVerifyWhitelist.

Please address the following:

  • Add a test case for "key:generate": Ensure that when the "key:generate" command is processed, it is correctly appended to EnvVerifyWhitelist.
  • Review existing env:decrypt tests: Confirm that the tests in ./foundation/console/env_decrypt_command_test.go explicitly verify that the whitelist update for "env:decrypt" is functioning as expected.
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 281-282: foundation/application.go#L281-L282
Added lines #L281 - L282 were not covered by tests


[warning] 284-284: foundation/application.go#L284
Added line #L284 was not covered by tests

Comment on lines +280 to +283
if arg == "key:generate" {
support.IsKeyGenerateCommand = true
support.EnvVerifyWhitelist = append(support.EnvVerifyWhitelist, arg)
}
if arg == "env:decrypt" {
Copy link
Contributor

@hwbrzzl hwbrzzl Feb 25, 2025

Choose a reason for hiding this comment

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

We can put key:generate and env:decrypt into a slice, then check if the slice contains arg here, if yes, set support.DontVerifyEnvFileExists=true. If there is another command, we can just add it to the slice.

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: 1

🧹 Nitpick comments (1)
foundation/application.go (1)

351-358: Consider using a map for better performance.

While the current implementation is clean and straightforward, using a map instead of a slice would provide O(1) lookup time, which could be beneficial if the whitelist grows larger.

Here's a suggested optimization:

-func InDontVerifyEnvFileWhitelist(item string) bool {
-	for _, eachItem := range support.DontVerifyEnvFileWhitelist {
-		if eachItem == item {
-			return true
-		}
-	}
-	return false
-}
+func InDontVerifyEnvFileWhitelist(item string) bool {
+	// Initialize map once during package initialization
+	whitelistMap := make(map[string]struct{}, len(support.DontVerifyEnvFileWhitelist))
+	for _, cmd := range support.DontVerifyEnvFileWhitelist {
+		whitelistMap[cmd] = struct{}{}
+	}
+	_, exists := whitelistMap[item]
+	return exists
+}

Also, please add test cases to verify:

  1. The function returns true for whitelisted commands
  2. The function returns false for non-whitelisted commands
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 354-355: foundation/application.go#L354-L355
Added lines #L354 - L355 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0fb45cc and 2bec421.

📒 Files selected for processing (3)
  • config/application.go (1 hunks)
  • foundation/application.go (2 hunks)
  • support/constant.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • config/application.go
  • support/constant.go
🧰 Additional context used
🪛 GitHub Check: codecov/patch
foundation/application.go

[warning] 281-281: foundation/application.go#L281
Added line #L281 was not covered by tests


[warning] 354-355: foundation/application.go#L354-L355
Added lines #L354 - L355 were not covered by tests

Comment on lines 280 to 282
if InDontVerifyEnvFileWhitelist(arg) {
support.DontVerifyEnvFileExists = true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

LGTM! But needs test coverage.

The implementation cleanly switches to a whitelist-based approach for commands that bypass environment file verification.

Please add test cases to verify:

  1. Commands in the whitelist correctly set DontVerifyEnvFileExists to true
  2. Commands not in the whitelist don't affect DontVerifyEnvFileExists

🏁 Script executed:

#!/bin/bash
# Description: Check for existing test files that might be suitable for adding the new test cases

echo "Searching for test files related to environment setup..."
fd -e go -x grep -l "setEnv\|DontVerifyEnvFileExists" {}

Length of output: 257


LGTM – Implementation Approved, but Test Coverage is Incomplete

The changes to use the whitelist-based approach in the setEnv function are implemented cleanly and meet the PR objectives. However, our investigation shows that no dedicated test files exist to cover this new behavior. Please add test cases to verify that:

  • Commands present in the whitelist correctly set support.DontVerifyEnvFileExists to true.
  • Commands not in the whitelist leave support.DontVerifyEnvFileExists unchanged.

Additional checks in a test file (e.g., a new application_test.go) should ensure robust regression coverage moving forward.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 281-281: foundation/application.go#L281
Added line #L281 was not covered by tests

Comment on lines +18 to +20
)

var (
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
)
var (

Comment on lines 280 to 282
if InDontVerifyEnvFileWhitelist(arg) {
support.DontVerifyEnvFileExists = true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if InDontVerifyEnvFileWhitelist(arg) {
support.DontVerifyEnvFileExists = true
}
support.DontVerifyEnvFileExists = strings.Contains(support.DontVerifyEnvFileWhitelist, arg)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

support.DontVerifyEnvFileExists = slices.Contains(support.DontVerifyEnvFileWhitelist, arg)

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: 0

🧹 Nitpick comments (2)
support/constant.go (2)

18-19: Naming suggestion for improved clarity

The variable name DontVerifyEnvFileExists uses a negative construction which can be harder to understand in conditional statements. Consider renaming to a positive construction like SkipEnvFileVerification for improved readability.

- DontVerifyEnvFileExists    = false
+ SkipEnvFileVerification    = false

19-19: Add a comment explaining the whitelist purpose

Consider adding a comment above the whitelist to explain why these specific commands are exempted from environment file verification. This will help future maintainers understand the rationale behind this feature.

+ // Commands that don't require environment file verification
  DontVerifyEnvFileWhitelist = []string{"key:generate", "env:decrypt"}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2bec421 and 09019e0.

📒 Files selected for processing (2)
  • foundation/application.go (2 hunks)
  • support/constant.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • foundation/application.go
⏰ Context from checks skipped due to timeout of 300000ms (1)
  • GitHub Check: test / windows (1.23)

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

LGTM

@hwbrzzl hwbrzzl merged commit 341950e into goravel:master Feb 25, 2025
11 of 14 checks passed
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.

2 participants