-
Notifications
You must be signed in to change notification settings - Fork 847
Add allow-env and block-env option for limactl shell #4483
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,8 @@ func newShellCommand() *cobra.Command { | |
| shellCmd.Flags().Bool("reconnect", false, "Reconnect to the SSH session") | ||
| shellCmd.Flags().Bool("preserve-env", false, "Propagate environment variables to the shell") | ||
| shellCmd.Flags().Bool("start", false, "Start the instance if it is not already running") | ||
| shellCmd.Flags().StringSlice("allow-env", []string{}, "Comma-separated list of environment variable patterns to allow when --preserve-env is set (overrides LIMA_SHELLENV_ALLOW)") | ||
| shellCmd.Flags().StringSlice("block-env", []string{}, "Comma-separated list of environment variable patterns to allow when --preserve-env is set (overrides LIMA_SHELLENV_BLOCK)") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the line above, but the description here has a cut&pasto: needs to say |
||
| return shellCmd | ||
| } | ||
|
|
||
|
|
@@ -216,8 +218,16 @@ func shellAction(cmd *cobra.Command, args []string) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| allowListRaw, err := cmd.Flags().GetStringSlice("allow-env") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| blockListRaw, err := cmd.Flags().GetStringSlice("block-env") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if preserveEnv { | ||
| filteredEnv := envutil.FilterEnvironment() | ||
| filteredEnv := envutil.FilterEnvironment(allowListRaw, blockListRaw) | ||
| if len(filteredEnv) > 0 { | ||
| envPrefix = "env " | ||
| for _, envVar := range filteredEnv { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,15 +56,20 @@ func validatePattern(pattern string) error { | |
| } | ||
|
|
||
| // getBlockList returns the list of environment variable patterns to be blocked. | ||
| func getBlockList() []string { | ||
| blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK") | ||
| if blockEnv == "" { | ||
| return defaultBlockList | ||
| func getBlockList(blockListRaw []string) []string { | ||
| var shouldAppend bool | ||
| patterns := blockListRaw | ||
| if len(patterns) == 0 { | ||
| blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK") | ||
|
Comment on lines
+62
to
+63
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are replacing the environment variable with the content of the Same thing applies to the allow list (with the exception that there is no default allow list). Please re-read the issue, and implement the functionality as specified. Or start a discussion in the issue if you think the requested semantics are wrong! |
||
| if blockEnv == "" { | ||
| return defaultBlockList | ||
| } | ||
| shouldAppend = strings.HasPrefix(blockEnv, "+") | ||
| patterns = parseEnvList(strings.TrimPrefix(blockEnv, "+")) | ||
| } else { | ||
| shouldAppend = strings.HasPrefix(patterns[0], "+") | ||
| } | ||
|
|
||
| shouldAppend := strings.HasPrefix(blockEnv, "+") | ||
| patterns := parseEnvList(strings.TrimPrefix(blockEnv, "+")) | ||
|
|
||
| for _, pattern := range patterns { | ||
| if err := validatePattern(pattern); err != nil { | ||
| logrus.Fatalf("Invalid LIMA_SHELLENV_BLOCK pattern: %v", err) | ||
|
|
@@ -78,14 +83,16 @@ func getBlockList() []string { | |
| } | ||
|
|
||
| // getAllowList returns the list of environment variable patterns to be allowed. | ||
| func getAllowList() []string { | ||
| allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW") | ||
| if allowEnv == "" { | ||
| return nil | ||
| func getAllowList(allowListRaw []string) []string { | ||
| patterns := allowListRaw | ||
| if len(patterns) == 0 { | ||
| allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW") | ||
| if allowEnv == "" { | ||
| return nil | ||
| } | ||
| patterns = parseEnvList(allowEnv) | ||
| } | ||
|
|
||
| patterns := parseEnvList(allowEnv) | ||
|
|
||
| for _, pattern := range patterns { | ||
| if err := validatePattern(pattern); err != nil { | ||
| logrus.Fatalf("Invalid LIMA_SHELLENV_ALLOW pattern: %v", err) | ||
|
|
@@ -131,11 +138,11 @@ func matchesAnyPattern(name string, patterns []string) bool { | |
| // FilterEnvironment filters environment variables based on configuration from environment variables. | ||
| // It returns a slice of environment variables that are not blocked by the current configuration. | ||
| // The filtering is controlled by LIMA_SHELLENV_BLOCK and LIMA_SHELLENV_ALLOW environment variables. | ||
| func FilterEnvironment() []string { | ||
| func FilterEnvironment(allowListRaw, blockListRaw []string) []string { | ||
| return filterEnvironmentWithLists( | ||
| os.Environ(), | ||
| getAllowList(), | ||
| getBlockList(), | ||
| getAllowList(allowListRaw), | ||
| getBlockList(blockListRaw), | ||
| ) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4263 says:
These options are convenience methods to make it easier to append custom patterns for just a single command (otherwise you would change the env variables if you want the changes to persist for longer). So it makes no sense to require the user to also specify
--preserve-envas well.