Skip to content

Commit

Permalink
internal: refactor namespaces to be profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Aug 25, 2024
1 parent 6ce0f32 commit ac6ab3f
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 108 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ GLOBALS:
--help/-h boolean - print help message
```
#### set a namespace
#### create/update a profile
```bash
$ envy set example FOO=1 BAR=2 BAZ=3
```
#### update existing variable in a namespace
#### update existing variable in a profile
```bash
$ envy set example FOO=4
```
#### remove variable from namespace
#### remove variable from profile
```bash
$ envy set example -FOO
Expand Down Expand Up @@ -112,7 +112,7 @@ BAR=2
BAZ-3
```
#### list namespaces
#### list available profiles
```bash
$ envy list
Expand All @@ -122,27 +122,27 @@ nomad/e2e
test
```
#### show namespace
#### show variables in a profile
```bash
$ envy show test
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
```
#### show namespace w/ values
#### show profile variables w/ values
```bash
$ envy show -decrypt test
AWS_ACCESS_KEY_ID=aaabbbccc
AWS_SECRET_ACCESS_KEY=233kjsdf309jfsd
```
#### remove namespace
#### delete profile
```bash
$ envy purge test
purged namespace "test"
purged profile "test"
```
# Contributing
Expand Down
26 changes: 13 additions & 13 deletions internal/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

var (
argRe = regexp.MustCompile(`^(?P<key>\w+)=(?P<secret>.+)$`)
namespaceRe = regexp.MustCompile(`^[-:/\w]+$`)
argRe = regexp.MustCompile(`^(?P<key>\w+)=(?P<secret>.+)$`)
profileRe = regexp.MustCompile(`^[-:/\w]+$`)
)

const (
Expand Down Expand Up @@ -53,16 +53,16 @@ func invoke(args []string, tool *setup.Tool) babycli.Code {
return r.Run()
}

func checkName(namespace string) error {
if !namespaceRe.MatchString(namespace) {
return errors.New("namespace uses non-word characters")
func checkName(profile string) error {
if !profileRe.MatchString(profile) {
return errors.New("name uses non-word characters")
}
return nil
}

type Extractor interface {
PreProcess(args []string) (string, *set.Set[string], *set.HashSet[*conceal.Text, int], error)
Namespace(vars *set.HashSet[*conceal.Text, int]) (*safe.Namespace, error)
Process(args []string) (string, *set.Set[string], *set.HashSet[*conceal.Text, int], error)
Profile(vars *set.HashSet[*conceal.Text, int]) (*safe.Profile, error)
}

type extractor struct {
Expand All @@ -75,14 +75,14 @@ func newExtractor(ring keyring.Ring) Extractor {
}
}

// PreProcess returns
// - the namespace
// Process returns
// - the profile
// - the set of keys to be removed
// - the set of key/values to be added
// - any error
func (e *extractor) PreProcess(args []string) (string, *set.Set[string], *set.HashSet[*conceal.Text, int], error) {
func (e *extractor) Process(args []string) (string, *set.Set[string], *set.HashSet[*conceal.Text, int], error) {
if len(args) < 2 {
return "", nil, nil, errors.New("requires at least 2 arguments (namespace, <key,...>)")
return "", nil, nil, errors.New("requires at least 2 arguments (profile, <key,...>)")
}
ns := args[0]
rm := set.New[string](4)
Expand All @@ -101,12 +101,12 @@ func (e *extractor) PreProcess(args []string) (string, *set.Set[string], *set.Ha
return ns, rm, add, nil
}

func (e *extractor) Namespace(vars *set.HashSet[*conceal.Text, int]) (*safe.Namespace, error) {
func (e *extractor) Profile(vars *set.HashSet[*conceal.Text, int]) (*safe.Profile, error) {
content, err := e.process(vars.Slice())
if err != nil {
return nil, err
}
return &safe.Namespace{
return &safe.Profile{
Name: "",
Content: content,
}, nil
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func newExecCmd(tool *setup.Tool) *babycli.Component {
},
}
}
func env(tool *setup.Tool, ns *safe.Namespace, environment []string) []string {
for key, value := range ns.Content {
func env(tool *setup.Tool, pr *safe.Profile, environment []string) []string {
for key, value := range pr.Content {
secret := tool.Ring.Decrypt(value).Unveil()
environment = append(environment, fmt.Sprintf(
"%s=%s", key, secret,
Expand All @@ -74,13 +74,13 @@ func envContext(insulate bool) []string {
return os.Environ()
}

func newCmd(tool *setup.Tool, ns *safe.Namespace, insulate bool, argVars []string, command string, args []string) *exec.Cmd {
func newCmd(tool *setup.Tool, ns *safe.Profile, insulate bool, argVars []string, command string, args []string) *exec.Cmd {
ctx := context.Background()
cmd := exec.CommandContext(ctx, command, args...)

// Environment variables are injected in the following order:
// 1. OS variables if insulate is false
// 2. envy namespace vars
// 2. envy profile vars
// 3. Variables in input args
cmd.Env = append(env(tool, ns, envContext(insulate)), argVars...)
cmd.Stdout = os.Stdout
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestExecCmd_ok(t *testing.T) {
Box: box,
}

box.GetMock.Expect("myNS").Return(&safe.Namespace{
box.GetMock.Expect("myNS").Return(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"a": {0x1},
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestExecCmd_bad_command(t *testing.T) {
Box: box,
}

box.GetMock.Expect("myNS").Return(&safe.Namespace{
box.GetMock.Expect("myNS").Return(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"a": {0x1},
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func newListCmd(tool *setup.Tool) *babycli.Component {
tool.Writer.Errorf("list command expects no args")
return babycli.Failure
}
namespaces, err := tool.Box.List()
profiles, err := tool.Box.List()
if err != nil {
tool.Writer.Errorf("unable to list namespaces: %v", err)
tool.Writer.Errorf("unable to list profiles: %v", err)
return babycli.Failure
}

for _, ns := range namespaces {
tool.Writer.Printf("%s", ns)
for _, profile := range profiles {
tool.Writer.Printf("%s", profile)
}
return babycli.Success
},
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestListCmd_list_fails(t *testing.T) {

must.One(t, rc)
must.Eq(t, "", a.String())
must.Eq(t, "envy: unable to list namespaces: io error\n", b.String())
must.Eq(t, "envy: unable to list profiles: io error\n", b.String())
}

func TestListCmd_extra_args(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func TestPurgeCmd_bad_profile(t *testing.T) {
Box: box,
}

// namespace must be valid
// profile must be valid
rc := invoke([]string{"purge", "foo=bar"}, tool)

must.One(t, rc)
must.Eq(t, "", a.String())
must.Eq(t, "envy: unable to purge profile: namespace uses non-word characters\n", b.String())
must.Eq(t, "envy: unable to purge profile: name uses non-word characters\n", b.String())
}

func TestPurgeCmd_two_arg(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions internal/commands/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ func newSetCmd(tool *setup.Tool) *babycli.Component {
Function: func(c *babycli.Component) babycli.Code {
args := c.Arguments()
extractor := newExtractor(tool.Ring)
namespace, remove, add, err := extractor.PreProcess(args)
profile, remove, add, err := extractor.Process(args)
if err != nil {
tool.Writer.Errorf("unable to parse args: %v", err)
return babycli.Failure
}

if err = checkName(namespace); err != nil {
tool.Writer.Errorf("could not set namespace: %v", err)
if err = checkName(profile); err != nil {
tool.Writer.Errorf("could not set profile: %v", err)
return babycli.Failure
}

if !remove.Empty() {
if err := tool.Box.Delete(namespace, remove); err != nil {
if err := tool.Box.Delete(profile, remove); err != nil {
tool.Writer.Errorf("coult not remove variables: %v", err)
return babycli.Failure
}
}

n, err := extractor.Namespace(add)
n, err := extractor.Profile(add)
if err != nil {
tool.Writer.Errorf("unable to parse args: %v", err)
return babycli.Failure
}
n.Name = namespace
n.Name = profile

if err = tool.Box.Set(n); err != nil {
tool.Writer.Errorf("unable to set variables: %v", err)
Expand Down
12 changes: 6 additions & 6 deletions internal/commands/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestSetCmd_ok(t *testing.T) {
ring.EncryptMock.When(conceal.New("abc123")).Then(safe.Encrypted{8, 8, 8, 8, 8, 8})
ring.EncryptMock.When(conceal.New("1234")).Then(safe.Encrypted{9, 9, 9, 9})

box.SetMock.Expect(&safe.Namespace{
box.SetMock.Expect(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"foo": {8, 8, 8, 8, 8, 8},
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestSetCmd_io_error(t *testing.T) {

a, b, w := newWriter()

box.SetMock.Expect(&safe.Namespace{
box.SetMock.Expect(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"foo": {8, 8, 8, 8, 8, 8},
Expand Down Expand Up @@ -95,12 +95,12 @@ func TestSetCmd_bad_ns(t *testing.T) {
Box: box,
}

// e.g. forgot to specify namespace
// e.g. forgot to specify profile
rc := invoke([]string{"set", "foo=abc123", "bar=1234"}, tool)

must.One(t, rc)
must.Eq(t, "", a.String())
must.Eq(t, "envy: could not set namespace: namespace uses non-word characters\n", b.String())
must.Eq(t, "envy: could not set profile: name uses non-word characters\n", b.String())
}

func TestSetCmd_no_vars(t *testing.T) {
Expand All @@ -118,10 +118,10 @@ func TestSetCmd_no_vars(t *testing.T) {
Box: box,
}

// e.g. reminder to use purge to remove namespace
// e.g. reminder to use purge to remove profile
rc := invoke([]string{"set", "ns1"}, tool)

must.One(t, rc)
must.Eq(t, "", a.String())
must.Eq(t, "envy: unable to parse args: requires at least 2 arguments (namespace, <key,...>)\n", b.String())
must.Eq(t, "envy: unable to parse args: requires at least 2 arguments (profile, <key,...>)\n", b.String())
}
4 changes: 2 additions & 2 deletions internal/commands/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestShowCmd_Execute(t *testing.T) {
Box: box,
}

box.GetMock.Expect("myNS").Return(&safe.Namespace{
box.GetMock.Expect("myNS").Return(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"foo": {1, 1, 1},
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestShowCmd_Execute_unveil(t *testing.T) {
Box: box,
}

box.GetMock.Expect("myNS").Return(&safe.Namespace{
box.GetMock.Expect("myNS").Return(&safe.Profile{
Name: "myNS",
Content: map[string]safe.Encrypted{
"foo": {1, 1, 1},
Expand Down
Loading

0 comments on commit ac6ab3f

Please sign in to comment.