Skip to content

Commit

Permalink
Add HasSecrets func (#7)
Browse files Browse the repository at this point in the history
* Update readme

* Add HasSecrets func

* Add tests
  • Loading branch information
hebime committed May 14, 2020
1 parent 1f30848 commit 67160ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Serum

> NOTE: Serum is still pre v1.0, the API is still evolving and breaking changes can occur
Serum is a library that facilitates injecting environment variables and secrets into your application at run time.
It can load the key/value pairs from a `.env` file and it can use a `SecretProvider` to decrypt the secrets that
are included in the `.env` file.
Expand Down
6 changes: 6 additions & 0 deletions serum.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ func (in *Injector) Load(path string) error {
in.envVars = envVars
return nil
}

// HasSecrets returns true if the injector contains encrypted
// secrets, false otherwise.
func (in *Injector) HasSecrets() bool {
return len(in.envVars.Secrets) > 0
}
40 changes: 40 additions & 0 deletions serum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,43 @@ func TestInjectDecryptError(t *testing.T) {
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).To(ContainSubstring("serum: error decrypting secret"))
}

func TestHasSecrets(t *testing.T) {
g := NewGomegaWithT(t)

tt := []struct {
name string
env *envparser.EnvVars
expected bool
}{
{
name: "no secrets",
env: &envparser.EnvVars{
Plain: map[string]string{
"PLAIN": "123456",
},
},
expected: false,
},
{
name: "with secrets",
env: &envparser.EnvVars{
Plain: map[string]string{
"PLAIN": "123456",
},
Secrets: map[string]string{
"SECRET": "superSecret111",
},
},
expected: true,
},
}

for _, tc := range tt {
ij := &Injector{
envVars: tc.env,
}

g.Expect(ij.HasSecrets()).To(Equal(tc.expected))
}
}

0 comments on commit 67160ce

Please sign in to comment.