From 67160ce9a3a529a1465e9a4a9265a26943a043c5 Mon Sep 17 00:00:00 2001 From: Robbie Caputo Date: Thu, 14 May 2020 17:17:09 -0400 Subject: [PATCH] Add HasSecrets func (#7) * Update readme * Add HasSecrets func * Add tests --- README.md | 2 ++ serum.go | 6 ++++++ serum_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/README.md b/README.md index c676d6e..045e1d3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/serum.go b/serum.go index 7ebeed7..6ba3b0a 100644 --- a/serum.go +++ b/serum.go @@ -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 +} diff --git a/serum_test.go b/serum_test.go index ae86a1c..cad8c6b 100644 --- a/serum_test.go +++ b/serum_test.go @@ -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)) + } +}