-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add context-scoped managed environment attachments #1266
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
61031b8
feat(env): add context-scoped environment bindings
skevetter 5119873
feat(workspace): inject attached managed environment variables
skevetter c96af03
feat(desktop): manage workspace environment attachments
skevetter e94e101
test(env): cover automatic managed environment injection
skevetter 32c3c4f
fix(env): guard attachment metadata lookup
skevetter 744317f
fix(workspace): reject ambiguous env targets
skevetter 788347d
fix(e2e): configure docker in managed env context
skevetter 27a8657
fix(config): serialize and atomically persist mutations
skevetter 55224b0
fix(env): preserve attachment invariants
skevetter eac3d6e
fix(workspace): preserve repeated explicit env mappings
skevetter c837119
fix(desktop): scope env attachments to context
skevetter a878454
test(desktop): isolate environment attachment renders
skevetter 0f409f3
style: apply Go formatter output
skevetter 2ed87be
fix(config): satisfy atomic writer lint
skevetter 35b0561
docs: clarify managed environment invariants
skevetter cfe87f3
fix(config): restore empty temporary providers
skevetter 2ed87c7
fix(config): preserve symlink targets during atomic save
skevetter 8448bc3
fix(env): compensate failed value deletion
skevetter f68b225
fix(secrets): compensate failed secret deletion
skevetter 67ca479
fix(desktop): scope env deletion to displayed context
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,120 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
|
|
||
| "github.com/devsy-org/devsy/cmd/flags" | ||
| "github.com/devsy-org/devsy/pkg/config" | ||
| "github.com/devsy-org/devsy/pkg/log" | ||
| "github.com/devsy-org/devsy/pkg/secrets" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type AttachCmd struct{ *flags.GlobalFlags } | ||
|
|
||
| func NewAttachCmd(flags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &AttachCmd{GlobalFlags: flags} | ||
| return &cobra.Command{ | ||
| Use: "attach NAME", | ||
| Short: "Bind a non-sensitive environment variable to the active context", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(c *cobra.Command, args []string) error { | ||
| return cmd.Run(c.Context(), args[0]) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (cmd *AttachCmd) Run(_ context.Context, name string) error { | ||
| if err := secrets.ValidateName(name); err != nil { | ||
| return err | ||
| } | ||
| unlock, err := config.LockConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer unlock() | ||
| devsyConfig, err := config.LoadConfig(cmd.Context, "") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| contextName := devsyConfig.DefaultContext | ||
| ctxConfig := devsyConfig.Contexts[contextName] | ||
| if ctxConfig == nil { | ||
| return fmt.Errorf("context %q doesn't exist", contextName) | ||
| } | ||
| if err := verifyAttachableEnv(devsyConfig, contextName, name); err != nil { | ||
| return err | ||
| } | ||
| if slices.Contains(ctxConfig.EnvVars, name) { | ||
| log.Infof("environment variable %q already attached to context %q", name, contextName) | ||
| return nil | ||
| } | ||
| ctxConfig.EnvVars = append(ctxConfig.EnvVars, name) | ||
| if err := config.SaveConfig(devsyConfig); err != nil { | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| return fmt.Errorf("save config: %w", err) | ||
| } | ||
| log.Infof("environment variable %q attached to context %q", name, contextName) | ||
| return nil | ||
| } | ||
|
|
||
| func verifyAttachableEnv(devsyConfig *config.Config, contextName, name string) error { | ||
| store, err := secrets.NewStoreForConfig(devsyConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| meta, err := store.Meta(contextName, name) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot attach environment variable %q: %w", name, err) | ||
| } | ||
| if meta.Sensitive() { | ||
| return fmt.Errorf("%q is a secret; use \"devsy secret\"", name) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type DetachCmd struct{ *flags.GlobalFlags } | ||
|
|
||
| func NewDetachCmd(flags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &DetachCmd{GlobalFlags: flags} | ||
| return &cobra.Command{ | ||
| Use: "detach NAME", | ||
| Short: "Unbind an environment variable from the active context", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(c *cobra.Command, args []string) error { | ||
| return cmd.Run(c.Context(), args[0]) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (cmd *DetachCmd) Run(_ context.Context, name string) error { | ||
| if err := secrets.ValidateName(name); err != nil { | ||
| return err | ||
| } | ||
| unlock, err := config.LockConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer unlock() | ||
| devsyConfig, err := config.LoadConfig(cmd.Context, "") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| contextName := devsyConfig.DefaultContext | ||
| ctxConfig := devsyConfig.Contexts[contextName] | ||
| if ctxConfig == nil { | ||
| return fmt.Errorf("context %q doesn't exist", contextName) | ||
| } | ||
| idx := slices.Index(ctxConfig.EnvVars, name) | ||
| if idx < 0 { | ||
| log.Infof("environment variable %q is not attached to context %q", name, contextName) | ||
| return nil | ||
| } | ||
| ctxConfig.EnvVars = slices.Delete(ctxConfig.EnvVars, idx, idx+1) | ||
| if err := config.SaveConfig(devsyConfig); err != nil { | ||
| return fmt.Errorf("save config: %w", err) | ||
| } | ||
| log.Infof("environment variable %q detached from context %q", name, contextName) | ||
| return nil | ||
| } | ||
This file contains hidden or 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,122 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/cmd/flags" | ||
| "github.com/devsy-org/devsy/pkg/config" | ||
| "github.com/devsy-org/devsy/pkg/secrets" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func setupEnvCommandTest(t *testing.T) *flags.GlobalFlags { | ||
| t.Helper() | ||
| config.ResetPathManager() | ||
| t.Cleanup(config.ResetPathManager) | ||
| t.Setenv(config.EnvHome, t.TempDir()) | ||
| t.Setenv("DEVSY_SECRETS_BACKEND", "file") | ||
| t.Setenv("DEVSY_SECRETS_PASSPHRASE", "test-passphrase") | ||
| require.NoError(t, config.SaveConfig(&config.Config{ | ||
| DefaultContext: config.DefaultContext, | ||
| Contexts: map[string]*config.ContextConfig{config.DefaultContext: {}}, | ||
| })) | ||
| return &flags.GlobalFlags{} | ||
| } | ||
|
|
||
| func TestAttachAndDetachEnvironmentVariable(t *testing.T) { | ||
| globalFlags := setupEnvCommandTest(t) | ||
| set := &SetCmd{GlobalFlags: globalFlags, Value: "debug"} | ||
| require.NoError(t, set.Run(context.Background(), "LOG_LEVEL")) | ||
|
|
||
| attach := &AttachCmd{GlobalFlags: globalFlags} | ||
| require.NoError(t, attach.Run(context.Background(), "LOG_LEVEL")) | ||
| require.NoError(t, attach.Run(context.Background(), "LOG_LEVEL")) | ||
| cfg, err := config.LoadConfig("", "") | ||
| require.NoError(t, err) | ||
| require.Equal(t, []string{"LOG_LEVEL"}, cfg.Current().EnvVars) | ||
|
|
||
| detach := &DetachCmd{GlobalFlags: globalFlags} | ||
| require.NoError(t, detach.Run(context.Background(), "LOG_LEVEL")) | ||
| require.NoError(t, detach.Run(context.Background(), "LOG_LEVEL")) | ||
| cfg, err = config.LoadConfig("", "") | ||
| require.NoError(t, err) | ||
| require.Empty(t, cfg.Current().EnvVars) | ||
| } | ||
|
|
||
| func TestAttachRejectsSecret(t *testing.T) { | ||
| globalFlags := setupEnvCommandTest(t) | ||
| store, err := secrets.NewStoreForConfig(mustLoadConfig(t)) | ||
| require.NoError(t, err) | ||
| require.NoError(t, store.Set(config.DefaultContext, "API_TOKEN", "secret", secrets.KindSecret)) | ||
| err = (&AttachCmd{GlobalFlags: globalFlags}).Run(context.Background(), "API_TOKEN") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "is a secret") | ||
| } | ||
|
|
||
| func TestDeleteUnbindsEnvironmentVariable(t *testing.T) { | ||
| globalFlags := setupEnvCommandTest(t) | ||
| set := &SetCmd{GlobalFlags: globalFlags, Value: "debug"} | ||
| require.NoError(t, set.Run(context.Background(), "LOG_LEVEL")) | ||
| require.NoError( | ||
| t, | ||
| (&AttachCmd{GlobalFlags: globalFlags}).Run(context.Background(), "LOG_LEVEL"), | ||
| ) | ||
| require.NoError( | ||
| t, | ||
| (&DeleteCmd{GlobalFlags: globalFlags}).Run(context.Background(), "LOG_LEVEL"), | ||
| ) | ||
| cfg, err := config.LoadConfig("", "") | ||
| require.NoError(t, err) | ||
| require.Empty(t, cfg.Current().EnvVars) | ||
| } | ||
|
|
||
| func TestSetRejectsConvertingAttachedSecretToEnvironment(t *testing.T) { | ||
| globalFlags := setupEnvCommandTest(t) | ||
| cfg := mustLoadConfig(t) | ||
| store, err := secrets.NewStoreForConfig(cfg) | ||
| require.NoError(t, err) | ||
| require.NoError(t, store.Set(config.DefaultContext, "TOKEN", "secret", secrets.KindSecret)) | ||
| cfg.Current().Secrets = []string{"TOKEN"} | ||
| require.NoError(t, config.SaveConfig(cfg)) | ||
| err = (&SetCmd{GlobalFlags: globalFlags, Value: "plain"}).Run(context.Background(), "TOKEN") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "attached as a secret") | ||
| } | ||
|
|
||
| func TestConcurrentAttachmentsPreserveBothChanges(t *testing.T) { | ||
| globalFlags := setupEnvCommandTest(t) | ||
| set := func(name string) { | ||
| require.NoError( | ||
| t, | ||
| (&SetCmd{GlobalFlags: globalFlags, Value: name}).Run(context.Background(), name), | ||
| ) | ||
| } | ||
| set("FIRST") | ||
| set("SECOND") | ||
|
|
||
| var wg sync.WaitGroup | ||
| for _, name := range []string{"FIRST", "SECOND"} { | ||
| wg.Add(1) | ||
| go func(name string) { | ||
| defer wg.Done() | ||
| require.NoError( | ||
| t, | ||
| (&AttachCmd{GlobalFlags: globalFlags}).Run(context.Background(), name), | ||
| ) | ||
| }(name) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| cfg, err := config.LoadConfig("", "") | ||
| require.NoError(t, err) | ||
| require.ElementsMatch(t, []string{"FIRST", "SECOND"}, cfg.Current().EnvVars) | ||
| } | ||
|
|
||
| func mustLoadConfig(t *testing.T) *config.Config { | ||
| t.Helper() | ||
| cfg, err := config.LoadConfig("", "") | ||
| require.NoError(t, err) | ||
| return cfg | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.