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

ns: executor supports environment variables #12

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ns/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ func (nsexec *Executor) CryptsetupWithPassphrase(passphrase string, args []strin
// For Talos Linux, cryptsetup comes pre-installed in the host namespace
// (ref: https://github.com/siderolabs/pkgs/blob/release-1.4/reproducibility/pkg.yaml#L10)
// for the [Disk Encryption](https://www.talos.dev/v1.4/talos-guides/configuration/disk-encryption/).
return nsexec.ExecuteWithStdin(types.BinaryCryptsetup, args, passphrase, timeout)
return nsexec.ExecuteWithStdin(nil, types.BinaryCryptsetup, args, passphrase, timeout)
}
19 changes: 12 additions & 7 deletions ns/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewNamespaceExecutor(processName, procDirectory string, namespaces []types.
}

// prepareCommandArgs prepares the nsenter command arguments.
func (nsexec *Executor) prepareCommandArgs(binary string, args []string) []string {
func (nsexec *Executor) prepareCommandArgs(binary string, args, envs []string) []string {
cmdArgs := []string{}
for _, ns := range nsexec.namespaces {
nsPath := filepath.Join(nsexec.nsDirectory, ns.String())
Expand All @@ -59,24 +59,29 @@ func (nsexec *Executor) prepareCommandArgs(binary string, args []string) []strin
cmdArgs = append(cmdArgs, "--net="+nsPath)
}
}
if len(envs) > 0 {
cmdArgs = append(cmdArgs, "env", "-i")
derekbit marked this conversation as resolved.
Show resolved Hide resolved
cmdArgs = append(cmdArgs, envs...)
}

cmdArgs = append(cmdArgs, binary)
return append(cmdArgs, args...)
}

// Execute executes the command in the namespace. If NsDirectory is empty,
// it will execute the command in the current namespace.
func (nsexec *Executor) Execute(binary string, args []string, timeout time.Duration) (string, error) {
return nsexec.executor.Execute(nil, types.NsBinary, nsexec.prepareCommandArgs(binary, args), timeout)
func (nsexec *Executor) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) {
return nsexec.executor.Execute(nil, types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), timeout)
}

// ExecuteWithStdin executes the command in the namespace with stdin.
// If NsDirectory is empty, it will execute the command in the current namespace.
func (nsexec *Executor) ExecuteWithStdin(binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdin(types.NsBinary, nsexec.prepareCommandArgs(binary, args), stdinString, timeout)
func (nsexec *Executor) ExecuteWithStdin(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdin(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout)
}

// ExecuteWithStdinPipe executes the command in the namespace with stdin pipe.
// If NsDirectory is empty, it will execute the command in the current namespace.
func (nsexec *Executor) ExecuteWithStdinPipe(binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdinPipe(types.NsBinary, nsexec.prepareCommandArgs(binary, args), stdinString, timeout)
func (nsexec *Executor) ExecuteWithStdinPipe(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdinPipe(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout)
}
31 changes: 28 additions & 3 deletions ns/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *TestSuite) TestExecute(c *C) {
nsexec.nsDirectory = testCase.nsDirectory
nsexec.executor = &fake.Executor{}

output, err := nsexec.Execute("binary", []string{"arg1", "arg2"}, types.ExecuteDefaultTimeout)
output, err := nsexec.Execute(nil, "binary", []string{"arg1", "arg2"}, types.ExecuteDefaultTimeout)
c.Assert(err, IsNil)
c.Assert(output, Equals, "output")
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *TestSuite) TestExecuteWithTimeout(c *C) {

nsexec.executor = &fake.Executor{}

output, err := nsexec.Execute("binary", []string{"arg1", "arg2"}, testCase.timeout)
output, err := nsexec.Execute(nil, "binary", []string{"arg1", "arg2"}, testCase.timeout)
derekbit marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(err, IsNil)
c.Assert(output, Equals, "output")
}
Expand All @@ -88,8 +88,33 @@ func (s *TestSuite) TestExecuteWithStdinPipe(c *C) {
nsexec.nsDirectory = testCase.nsDirectory
nsexec.executor = &fake.Executor{}

output, err := nsexec.ExecuteWithStdinPipe("binary", []string{"arg1", "arg2"}, "stdin", types.ExecuteDefaultTimeout)
output, err := nsexec.ExecuteWithStdinPipe(nil, "binary", []string{"arg1", "arg2"}, "stdin", types.ExecuteDefaultTimeout)
c.Assert(err, IsNil)
c.Assert(output, Equals, "output")
}
}

func (s *TestSuite) TestExecuteWithEnvs(c *C) {
type testCase struct {
timeout time.Duration
}
testCases := map[string]testCase{
"Execute(...)": {
timeout: types.ExecuteNoTimeout,
},
"Execute(...): with namespace": {
timeout: types.ExecuteNoTimeout,
},
}
for testName := range testCases {
c.Logf("testing namespace.%v", testName)

namespaces := []types.Namespace{}
nsexec, err := NewNamespaceExecutor(types.ProcessNone, types.HostProcDirectory, namespaces)
c.Assert(err, IsNil)

output, err := nsexec.Execute([]string{"K1=V1", "K2=V2"}, "env", nil, types.ExecuteDefaultTimeout)
c.Assert(err, IsNil)
c.Assert(output, Equals, "K1=V1\nK2=V2\n")
}
}