forked from runfinch/finch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Correctly set and pick up environment variables (runfinch#315)
Issue #, if available: Currently the environment variable is set in `/root/.bashrc` so that it is [not easily accessible](https://superuser.com/questions/1254422/linux-sudo-env-does-not-have-environment-variable-while-root-user-has) by regular users even by specifying `sudo -E`. Normally we would access the VM by regular user. In order to pick up the environment variables, we need to store the vars in the /home/<user>.linux/.bashrc and added -E to sudo command as sudo itself won't preserve the environment variables. *Description of changes:* Added -E to sudo command when we call limactl command. Update the profile path so that env vars are written in /home/<user>.linux/.bashrc. Added e2e tests to make sure the content is written correctly into the ~/.finch/.config.json file in host system. *Testing done:* Manual testing and E2E tests - [x] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Signed-off-by: Ang Zhou <[email protected]>
- Loading branch information
Showing
9 changed files
with
111 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package vm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
"github.com/runfinch/common-tests/command" | ||
"github.com/runfinch/common-tests/ffs" | ||
"github.com/runfinch/common-tests/fnet" | ||
"github.com/runfinch/common-tests/option" | ||
) | ||
|
||
// testFinchConfigFile makes sure that DOCKER_CONFIG is properly set to ~/.finch so that related information | ||
// is written to ~/.finch/config.json file. | ||
var testFinchConfigFile = func(o *option.Option) { | ||
ginkgo.Describe("finch config file", func() { | ||
ginkgo.It("should store login credentials", func() { | ||
filename := "htpasswd" | ||
registryImage := "public.ecr.aws/docker/library/registry:2" | ||
registryContainer := "auth-registry" | ||
// The htpasswd is generated by | ||
// `finch run --entrypoint htpasswd public.ecr.aws/docker/library/httpd:2 -Bbn testUser testPassword`. | ||
// We don't want to generate it on the fly because: | ||
// 1. Pulling the httpd image can take a long time, sometimes even more 10 seconds. | ||
// 2. It's unlikely that we will have to update this in the future. | ||
// 3. It's not the thing we want to validate by the functional tests. We only want the output produced by it. | ||
//nolint:gosec // This password is only used for testing purpose. | ||
htpasswd := "testUser:$2y$05$wE0sj3r9O9K9q7R0MXcfPuIerl/06L1IsxXkCuUr3QZ8lHWwicIdS" | ||
htpasswdDir := filepath.Dir(ffs.CreateTempFile(filename, htpasswd)) | ||
ginkgo.DeferCleanup(os.RemoveAll, htpasswdDir) | ||
port := fnet.GetFreePort() | ||
containerID := command.StdoutStr(o, "run", | ||
"-dp", fmt.Sprintf("%d:5000", port), | ||
"--name", registryContainer, | ||
"-v", fmt.Sprintf("%s:/auth", htpasswdDir), | ||
"-e", "REGISTRY_AUTH=htpasswd", | ||
"-e", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", | ||
"-e", fmt.Sprintf("REGISTRY_AUTH_HTPASSWD_PATH=/auth/%s", filename), | ||
registryImage) | ||
ginkgo.DeferCleanup(command.Run, o, "rmi", registryImage) | ||
ginkgo.DeferCleanup(command.Run, o, "rm", "-f", registryContainer) | ||
for command.StdoutStr(o, "inspect", "-f", "{{.State.Running}}", containerID) != "true" { | ||
time.Sleep(1 * time.Second) | ||
} | ||
registry := fmt.Sprintf(`localhost:%d`, port) | ||
command.Run(o, "login", registry, "-u", "testUser", "-p", "testPassword") | ||
|
||
homeDir, err := os.UserHomeDir() | ||
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||
configPath := fmt.Sprintf("%s/.finch/config.json", homeDir) | ||
configContent, err := os.ReadFile(filepath.Clean(configPath)) | ||
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||
|
||
gomega.Expect(string(configContent)).Should(gomega.ContainSubstring(registry)) | ||
command.Run(o, "logout", registry) | ||
|
||
configContent, err = os.ReadFile(filepath.Clean(configPath)) | ||
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||
gomega.Expect(string(configContent)).ShouldNot(gomega.ContainSubstring(registry)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters