This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from secrethub/feature/auto-splat
Map all secrets from directory to environment variables
- Loading branch information
Showing
7 changed files
with
391 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,142 @@ | ||
package secrethub | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/secrethub/secrethub-go/internals/api" | ||
"github.com/secrethub/secrethub-go/internals/api/uuid" | ||
"github.com/secrethub/secrethub-go/internals/assert" | ||
"github.com/secrethub/secrethub-go/pkg/secrethub" | ||
"github.com/secrethub/secrethub-go/pkg/secrethub/fakeclient" | ||
) | ||
|
||
func TestSecretsDirEnv(t *testing.T) { | ||
const dirPath = "namespace/repo" | ||
rootDirUUID := uuid.New() | ||
subDirUUID := uuid.New() | ||
secretUUID1 := uuid.New() | ||
secretUUID2 := uuid.New() | ||
|
||
cases := map[string]struct { | ||
newClient newClientFunc | ||
expectedValues []string | ||
err error | ||
}{ | ||
"success": { | ||
newClient: func() (secrethub.ClientInterface, error) { | ||
return fakeclient.Client{ | ||
DirService: &fakeclient.DirService{ | ||
GetTreeFunc: func(path string, depth int, ancestors bool) (*api.Tree, error) { | ||
return &api.Tree{ | ||
ParentPath: "namespace", | ||
RootDir: &api.Dir{ | ||
DirID: rootDirUUID, | ||
Name: "repo", | ||
}, | ||
Secrets: map[uuid.UUID]*api.Secret{ | ||
secretUUID1: { | ||
SecretID: secretUUID1, | ||
DirID: rootDirUUID, | ||
Name: "foo", | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
expectedValues: []string{"FOO"}, | ||
}, | ||
"success secret in dir": { | ||
newClient: func() (secrethub.ClientInterface, error) { | ||
return fakeclient.Client{ | ||
DirService: &fakeclient.DirService{ | ||
GetTreeFunc: func(path string, depth int, ancestors bool) (*api.Tree, error) { | ||
return &api.Tree{ | ||
ParentPath: "namespace", | ||
RootDir: &api.Dir{ | ||
DirID: rootDirUUID, | ||
Name: "repo", | ||
}, | ||
Dirs: map[uuid.UUID]*api.Dir{ | ||
subDirUUID: { | ||
DirID: subDirUUID, | ||
ParentID: &rootDirUUID, | ||
Name: "foo", | ||
}, | ||
}, | ||
Secrets: map[uuid.UUID]*api.Secret{ | ||
secretUUID1: { | ||
SecretID: secretUUID1, | ||
DirID: subDirUUID, | ||
Name: "bar", | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
expectedValues: []string{"FOO_BAR"}, | ||
}, | ||
"name collision": { | ||
newClient: func() (secrethub.ClientInterface, error) { | ||
return fakeclient.Client{ | ||
DirService: &fakeclient.DirService{ | ||
GetTreeFunc: func(path string, depth int, ancestors bool) (*api.Tree, error) { | ||
return &api.Tree{ | ||
ParentPath: "namespace", | ||
RootDir: &api.Dir{ | ||
DirID: rootDirUUID, | ||
Name: "repo", | ||
}, | ||
Dirs: map[uuid.UUID]*api.Dir{ | ||
subDirUUID: { | ||
DirID: subDirUUID, | ||
ParentID: &rootDirUUID, | ||
Name: "foo", | ||
}, | ||
}, | ||
Secrets: map[uuid.UUID]*api.Secret{ | ||
secretUUID1: { | ||
SecretID: secretUUID1, | ||
DirID: subDirUUID, | ||
Name: "bar", | ||
}, | ||
secretUUID2: { | ||
SecretID: secretUUID2, | ||
DirID: rootDirUUID, | ||
Name: "foo_bar", | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
err: errNameCollision{ | ||
name: "FOO_BAR", | ||
firstPath: "namespace/repo/foo/bar", | ||
secondPath: "namespace/repo/foo_bar", | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
source := newSecretsDirEnv(tc.newClient, dirPath) | ||
secrets, err := source.env() | ||
if tc.err != nil { | ||
assert.Equal(t, err, tc.err) | ||
} else { | ||
assert.OK(t, err) | ||
assert.Equal(t, len(secrets), len(tc.expectedValues)) | ||
for _, name := range tc.expectedValues { | ||
if _, ok := secrets[name]; !ok { | ||
t.Errorf("expected but not found env var with name: %s", name) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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.