-
Notifications
You must be signed in to change notification settings - Fork 269
[envsec] Initial envsec integration #1474
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
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,6 @@ | ||
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package featureflag | ||
|
||
var Envsec = disable("ENVSEC") |
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
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,26 @@ | ||
package devconfig | ||
|
||
import ( | ||
"go.jetpack.io/devbox/internal/boxcli/featureflag" | ||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/internal/integrations/envsec" | ||
) | ||
|
||
func (c *Config) ComputedEnv(projectDir string) (map[string]string, error) { | ||
env := map[string]string{} | ||
var err error | ||
if featureflag.Envsec.Enabled() { | ||
if c.FromEnv == "envsec" { | ||
env, err = envsec.Env(projectDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else if c.FromEnv != "" { | ||
return nil, usererr.New("unknown from_env value: %s", c.FromEnv) | ||
} | ||
} | ||
for k, v := range c.Env { | ||
env[k] = v | ||
} | ||
return env, 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
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,63 @@ | ||
package envsec | ||
|
||
import ( | ||
"encoding/json" | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/internal/cmdutil" | ||
) | ||
|
||
func Env(projectDir string) (map[string]string, error) { | ||
|
||
if err := ensureEnvsecInstalled(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := ensureEnvsecInitialized(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return envsecList(projectDir) | ||
} | ||
|
||
func ensureEnvsecInstalled() error { | ||
if !cmdutil.Exists("envsec") { | ||
return usererr.New("envsec is not installed or not in path") | ||
} | ||
return nil | ||
} | ||
|
||
func ensureEnvsecInitialized() error { | ||
cmd := exec.Command("envsec", "init") | ||
// TODO handle user not logged in | ||
// envsec init is currently broken in that it exits with 0 even if the user is not logged in | ||
return cmd.Run() | ||
} | ||
|
||
func envsecList(projectDir string) (map[string]string, error) { | ||
cmd := exec.Command( | ||
"envsec", "ls", "--show", | ||
"--format", "json", | ||
"--environment", "dev") | ||
cmd.Dir = projectDir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
var values []struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} | ||
|
||
if err := json.Unmarshal(out, &values); err != nil { | ||
return nil, errors.Wrap(err, "failed to parse envsec output") | ||
} | ||
|
||
m := map[string]string{} | ||
for _, v := range values { | ||
m[v.Name] = v.Value | ||
} | ||
return m, nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we should do this. I worry that the secrets printed can be discovered through some proc file somewhere.
Can we use the envsec go library directly instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under impression we wanted to do CLI integration in order to avoid bloating devbox. This has the benefit of allowing other 3rd parties to integrate as well if we provide a standard interface.
If stderr and stdout are captured in a buffer, is there really any risk the outputs ends up anywhere else?
We can also go the RPC route, but that's more work and feels a bit overkill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking deeper it might be ok based on this line. So I guess we are good