Skip to content

Commit f662f04

Browse files
committed
Add allow-env and block-env option for limactl shell
Implement allow-env and block-env option for limactl shell command, which is equivalent of setting environment variable "LIMA_SHELLENV_ALLOW" and "LIMA_SHELLENV_BLOCK". Except that "allow-env" and "block-env" are treated as StringSlice rather than pure string in these options. Related issue: #4263 Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
1 parent f1e8803 commit f662f04

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

cmd/limactl/shell.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func newShellCommand() *cobra.Command {
6464
shellCmd.Flags().Bool("reconnect", false, "Reconnect to the SSH session")
6565
shellCmd.Flags().Bool("preserve-env", false, "Propagate environment variables to the shell")
6666
shellCmd.Flags().Bool("start", false, "Start the instance if it is not already running")
67+
shellCmd.Flags().StringSlice("allow-env", []string{}, "Comma-seperated list of environment variable patterns to allow when --preserve-env is set (overrides LIMA_SHELLENV_ALLOW)")
68+
shellCmd.Flags().StringSlice("block-env", []string{}, "Comma-seperated list of environment variable patterns to allow when --preserve-env is set (overrides LIMA_SHELLENV_ALLOW)")
6769
return shellCmd
6870
}
6971

@@ -216,8 +218,16 @@ func shellAction(cmd *cobra.Command, args []string) error {
216218
if err != nil {
217219
return err
218220
}
221+
allowListRaw, err := cmd.Flags().GetStringSlice("allow-env")
222+
if err != nil {
223+
return err
224+
}
225+
blockListRaw, err := cmd.Flags().GetStringSlice("block-env")
226+
if err != nil {
227+
return err
228+
}
219229
if preserveEnv {
220-
filteredEnv := envutil.FilterEnvironment()
230+
filteredEnv := envutil.FilterEnvironment(allowListRaw, blockListRaw)
221231
if len(filteredEnv) > 0 {
222232
envPrefix = "env "
223233
for _, envVar := range filteredEnv {

pkg/envutil/envutil.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ func validatePattern(pattern string) error {
5656
}
5757

5858
// getBlockList returns the list of environment variable patterns to be blocked.
59-
func getBlockList() []string {
60-
blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK")
61-
if blockEnv == "" {
62-
return defaultBlockList
63-
}
59+
func getBlockList(blockListRaw []string) []string {
60+
var shouldAppend bool
61+
patterns := blockListRaw
62+
if len(patterns) == 0 {
63+
blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK")
64+
if blockEnv == "" {
65+
return defaultBlockList
66+
}
67+
shouldAppend = strings.HasPrefix(blockEnv, "+")
68+
patterns = parseEnvList(strings.TrimPrefix(blockEnv, "+"))
6469

65-
shouldAppend := strings.HasPrefix(blockEnv, "+")
66-
patterns := parseEnvList(strings.TrimPrefix(blockEnv, "+"))
70+
} else {
71+
shouldAppend = strings.HasPrefix(patterns[0], "+")
72+
}
6773

6874
for _, pattern := range patterns {
6975
if err := validatePattern(pattern); err != nil {
@@ -78,14 +84,16 @@ func getBlockList() []string {
7884
}
7985

8086
// getAllowList returns the list of environment variable patterns to be allowed.
81-
func getAllowList() []string {
82-
allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW")
83-
if allowEnv == "" {
84-
return nil
87+
func getAllowList(allowListRaw []string) []string {
88+
patterns := allowListRaw
89+
if len(patterns) == 0 {
90+
allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW")
91+
if allowEnv == "" {
92+
return nil
93+
}
94+
patterns = parseEnvList(allowEnv)
8595
}
8696

87-
patterns := parseEnvList(allowEnv)
88-
8997
for _, pattern := range patterns {
9098
if err := validatePattern(pattern); err != nil {
9199
logrus.Fatalf("Invalid LIMA_SHELLENV_ALLOW pattern: %v", err)
@@ -131,11 +139,11 @@ func matchesAnyPattern(name string, patterns []string) bool {
131139
// FilterEnvironment filters environment variables based on configuration from environment variables.
132140
// It returns a slice of environment variables that are not blocked by the current configuration.
133141
// The filtering is controlled by LIMA_SHELLENV_BLOCK and LIMA_SHELLENV_ALLOW environment variables.
134-
func FilterEnvironment() []string {
142+
func FilterEnvironment(allowListRaw, blockListRaw []string) []string {
135143
return filterEnvironmentWithLists(
136144
os.Environ(),
137-
getAllowList(),
138-
getBlockList(),
145+
getAllowList(allowListRaw),
146+
getBlockList(blockListRaw),
139147
)
140148
}
141149

pkg/envutil/envutil_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func TestGetBlockAndAllowLists(t *testing.T) {
8888
t.Setenv("LIMA_SHELLENV_BLOCK", "")
8989
t.Setenv("LIMA_SHELLENV_ALLOW", "")
9090

91-
blockList := getBlockList()
92-
allowList := getAllowList()
91+
blockList := getBlockList([]string{})
92+
allowList := getAllowList([]string{})
9393

9494
assert.Assert(t, isUsingDefaultBlockList())
9595
assert.DeepEqual(t, blockList, defaultBlockList)
@@ -99,7 +99,7 @@ func TestGetBlockAndAllowLists(t *testing.T) {
9999
t.Run("custom blocklist", func(t *testing.T) {
100100
t.Setenv("LIMA_SHELLENV_BLOCK", "PATH,HOME")
101101

102-
blockList := getBlockList()
102+
blockList := getBlockList([]string{})
103103
assert.Assert(t, !isUsingDefaultBlockList())
104104
expected := []string{"PATH", "HOME"}
105105
assert.DeepEqual(t, blockList, expected)
@@ -108,7 +108,7 @@ func TestGetBlockAndAllowLists(t *testing.T) {
108108
t.Run("additive blocklist", func(t *testing.T) {
109109
t.Setenv("LIMA_SHELLENV_BLOCK", "+CUSTOM_VAR")
110110

111-
blockList := getBlockList()
111+
blockList := getBlockList([]string{})
112112
assert.Assert(t, isUsingDefaultBlockList())
113113
expected := slices.Concat(GetDefaultBlockList(), []string{"CUSTOM_VAR"})
114114
assert.DeepEqual(t, blockList, expected)
@@ -117,7 +117,7 @@ func TestGetBlockAndAllowLists(t *testing.T) {
117117
t.Run("allowlist", func(t *testing.T) {
118118
t.Setenv("LIMA_SHELLENV_ALLOW", "FOO,BAR")
119119

120-
allowList := getAllowList()
120+
allowList := getAllowList([]string{})
121121
expected := []string{"FOO", "BAR"}
122122
assert.DeepEqual(t, allowList, expected)
123123
})

0 commit comments

Comments
 (0)