Skip to content

fix: avoid deleting root filesystem when KANIKO_DIR not set #160

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

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ $ vim .devcontainer/Dockerfile

Exit the container, and re-run the `docker run` command... after the build completes, `htop` should exist in the container! 🥳

> **Note:** Envbuilder performs destructive filesystem operations! To guard against accidental data
> loss, it will refuse to run if it detects that KANIKO_DIR is not set to a specific value.
> If you need to bypass this behaviour for any reason, you can bypass this safety check by setting
> `FORCE_SAFE=true`.
Copy link
Member

Choose a reason for hiding this comment

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

I'd love for us to start using the ENVBUILDER_ prefix for our envs, can we start now?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll do this in a separate PR. This isn't a new option, it's been there all the time but not doing anything.


### Git Branch Selection

Choose a branch using `GIT_URL` with a _ref/heads_ reference. For instance:
Expand Down
20 changes: 18 additions & 2 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ func Run(ctx context.Context, options Options) error {

// It's possible that the container will already have files in it, and
// we don't want to merge a new container with the old one.
err = util.DeleteFilesystem()
if err != nil {
if err := maybeDeleteFilesystem(options.ForceSafe); err != nil {
return nil, fmt.Errorf("delete filesystem: %w", err)
}

Expand Down Expand Up @@ -1063,3 +1062,20 @@ func findDevcontainerJSON(options Options) (string, string, error) {

return "", "", errors.New("can't find devcontainer.json, is it a correct spec?")
}

// maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop
// folks from unwittingly deleting their entire root directory.
func maybeDeleteFilesystem(force bool) error {
Copy link
Member

Choose a reason for hiding this comment

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

I love this name ❤️

Copy link
Member Author

Choose a reason for hiding this comment

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

It's technically correct!

kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
if !ok || strings.TrimSpace(kanikoDir) != MagicDir {
if force {
_, _ = fmt.Fprintf(os.Stderr, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE %s!\n", kanikoDir)
} else {
_, _ = fmt.Fprintf(os.Stderr, "KANIKO_DIR is not set to %s. Bailing!\n", MagicDir)
_, _ = fmt.Fprintln(os.Stderr, "To bypass this check, set FORCE_SAFE=true.")
return errors.New("safety check failed")
}
}

return util.DeleteFilesystem()
}
36 changes: 36 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ const (
testImageUbuntu = "localhost:5000/envbuilder-test-ubuntu:latest"
)

func TestForceSafe(t *testing.T) {
t.Parallel()

t.Run("Safe", func(t *testing.T) {
t.Parallel()
srv := createGitServer(t, gitServerOptions{
files: map[string]string{
"Dockerfile": "FROM " + testImageAlpine,
},
})
_, err := runEnvbuilder(t, options{env: []string{
"GIT_URL=" + srv.URL,
"KANIKO_DIR=/not/envbuilder",
"DOCKERFILE_PATH=Dockerfile",
}})
require.ErrorContains(t, err, "delete filesystem: safety check failed")
})

// Careful with this one!
t.Run("Unsafe", func(t *testing.T) {
t.Parallel()
srv := createGitServer(t, gitServerOptions{
files: map[string]string{
"Dockerfile": "FROM " + testImageAlpine,
},
})
_, err := runEnvbuilder(t, options{env: []string{
"GIT_URL=" + srv.URL,
"KANIKO_DIR=/not/envbuilder",
"FORCE_SAFE=true",
"DOCKERFILE_PATH=Dockerfile",
}})
require.NoError(t, err)
})
}

func TestFailsGitAuth(t *testing.T) {
t.Parallel()
srv := createGitServer(t, gitServerOptions{
Expand Down
Loading