Skip to content

Commit 414c517

Browse files
authored
[add] migrate allow_insecure from lockfile to config (#1754)
## Summary This PR migrates the `allow_insecure` setting in `devbox.lock` of existing projects to `devbox.json`. ## How was it tested? Set up project with an insecure package `devbox add [email protected] --allow-insecure` using the prod Devbox binary: ``` ❯ git diff diff --git a/devbox.json b/devbox.json index cf698eb..45aac7cc 100644 --- a/devbox.json +++ b/devbox.json @@ -5,6 +5,7 @@ "go": "latest", "runx:golangci/golangci-lint": "latest", "runx:mvdan/gofumpt": "latest", + "python": "2.7.18", }, "env": { "GOENV": "off", diff --git a/devbox.lock b/devbox.lock index 7d4cb7e..b54fd69b 100644 --- a/devbox.lock +++ b/devbox.lock @@ -21,6 +21,14 @@ } } }, + "[email protected]": { + "allow_insecure": true, + "last_modified": "2024-01-14T03:55:27Z", + "plugin_version": "0.0.3", + "resolved": "github:NixOS/nixpkgs/dd5621df6dcb90122b50da5ec31c411a0de3e538#python2", + "source": "devbox-search", + "version": "2.7.18.7" + }, "runx:golangci/golangci-lint@latest": { "resolved": "golangci/[email protected]", "version": "v1.55.2" ``` Then, run `devbox shell`: ``` ❯ devbox shell Info: Allowed insecure python-2.7.18.7 for package [email protected] Info: Modernized the allow_insecure setting for package "[email protected]" by moving it from devbox.lock to devbox.json. Please commit the changes. Ensuring packages are installed. ✓ Computed the Devbox environment. Starting a devbox shell... Info: Your devbox environment may be out of date. Please run eval "$(devbox global shellenv --recompute)" You can activate the virtual environment by running 'source $VENV_DIR/bin/activate' ❯ git diff diff --git a/devbox.json b/devbox.json index cf698eb..60cf203e 100644 --- a/devbox.json +++ b/devbox.json @@ -5,6 +5,10 @@ "go": "latest", "runx:golangci/golangci-lint": "latest", "runx:mvdan/gofumpt": "latest", + "python": { + "version": "2.7.18", + "allow_insecure": ["python-2.7.18.7"], + }, }, "env": { "GOENV": "off", diff --git a/devbox.lock b/devbox.lock index 7d4cb7e..adc9123c 100644 --- a/devbox.lock +++ b/devbox.lock @@ -21,6 +21,13 @@ } } }, + "[email protected]": { + "last_modified": "2024-01-14T03:55:27Z", + "plugin_version": "0.0.3", + "resolved": "github:NixOS/nixpkgs/dd5621df6dcb90122b50da5ec31c411a0de3e538#python2", + "source": "devbox-search", + "version": "2.7.18.7" + }, "runx:golangci/golangci-lint@latest": { "resolved": "golangci/[email protected]", "version": "v1.55.2" (devbox) ```
1 parent 3697cb9 commit 414c517

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

internal/devbox/devbox.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,16 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
113113
}
114114
// if lockfile has any allow insecure, we need to set the env var to ensure
115115
// all nix commands work.
116-
if lock.HasAllowInsecurePackages() {
117-
nix.AllowInsecurePackages()
116+
if err := box.moveAllowInsecureFromLockfile(box.stderr, lock, cfg); err != nil {
117+
ux.Fwarning(
118+
box.stderr,
119+
"Failed to move allow_insecure from devbox.lock to devbox.json. An insecure package may "+
120+
"not work until you invoke `devbox add <pkg> --allow-insecure=<packages>` again: %s\n",
121+
err,
122+
)
123+
// continue on, since we do not want to block user.
118124
}
125+
119126
box.pluginManager.ApplyOptions(
120127
plugin.WithDevbox(box),
121128
plugin.WithLockfile(lock),

internal/devbox/packages.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package devbox
66
import (
77
"context"
88
"fmt"
9+
"io"
910
"io/fs"
1011
"os"
1112
"path/filepath"
@@ -17,8 +18,10 @@ import (
1718
"github.com/pkg/errors"
1819
"github.com/samber/lo"
1920
"go.jetpack.io/devbox/internal/devbox/devopt"
21+
"go.jetpack.io/devbox/internal/devconfig"
2022
"go.jetpack.io/devbox/internal/devpkg"
2123
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
24+
"go.jetpack.io/devbox/internal/lock"
2225
"go.jetpack.io/devbox/internal/nix/nixprofile"
2326
"go.jetpack.io/devbox/internal/shellgen"
2427

@@ -516,3 +519,51 @@ func (d *Devbox) packagesToInstallInProfile(ctx context.Context) ([]*devpkg.Pack
516519
}
517520
return packagesToInstall, nil
518521
}
522+
523+
// moveAllowInsecureFromLockfile will modernize a Devbox project by moving the allow_insecure: boolean
524+
// setting from the devbox.lock file to the corresponding package in devbox.json.
525+
//
526+
// NOTE: ideally, this function would be in devconfig, but it leads to an import cycle with devpkg, so
527+
// leaving in this "top-level" devbox package where we can import devconfig, devpkg and lock.
528+
func (d *Devbox) moveAllowInsecureFromLockfile(writer io.Writer, lockfile *lock.File, cfg *devconfig.Config) error {
529+
if !lockfile.HasAllowInsecurePackages() {
530+
return nil
531+
}
532+
533+
insecurePackages := []string{}
534+
for name, pkg := range lockfile.Packages {
535+
if pkg.AllowInsecure {
536+
insecurePackages = append(insecurePackages, name)
537+
}
538+
pkg.AllowInsecure = false
539+
}
540+
541+
// Set the devbox.json packages to allow_insecure
542+
for _, versionedName := range insecurePackages {
543+
pkg := devpkg.PackageFromStringWithDefaults(versionedName, lockfile)
544+
storeName, err := pkg.StoreName()
545+
if err != nil {
546+
return fmt.Errorf("failed to get package's store name for package %q with error %w", versionedName, err)
547+
}
548+
if err := cfg.Packages.SetAllowInsecure(writer, versionedName, []string{storeName}); err != nil {
549+
return fmt.Errorf("failed to set allow_insecure in devbox.json for package %q with error %w", versionedName, err)
550+
}
551+
}
552+
553+
if err := d.saveCfg(); err != nil {
554+
return err
555+
}
556+
557+
// Now, clear it from the lockfile
558+
if err := lockfile.Save(); err != nil {
559+
return err
560+
}
561+
562+
ux.Finfo(
563+
writer,
564+
"Modernized the allow_insecure setting for package %q by moving it from devbox.lock to devbox.json. Please commit the changes.\n",
565+
strings.Join(insecurePackages, ", "),
566+
)
567+
568+
return nil
569+
}

testscripts/testrunner/examplesrunner.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ func RunDevboxTestscripts(t *testing.T, dir string) {
7575
return nil
7676
}
7777

78-
if strings.Contains(path, "insecure") {
79-
// TODO: next PR will fix this
80-
t.Logf("skipping insecure, config at: %s\n", path)
81-
return nil
82-
}
83-
8478
t.Logf("running testscript for example: %s\n", path)
8579
runSingleDevboxTestscript(t, dir, path)
8680
return nil

0 commit comments

Comments
 (0)