-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cfg): env secret/string support (#592)
<!-- !!!! README !!!! Please fill this out. Please follow conventional commit naming conventions: https://www.conventionalcommits.org/en/v1.0.0/#summary --> Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional information on contributing to this repository! <!-- A short description of what your PR does and what it solves. --> ## What this PR does / why we need it <!-- <<Stencil::Block(jiraPrefix)>> --> ## Jira ID [XX-XX] <!-- <</Stencil::Block>> --> <!-- Notes that may be helpful for anyone reviewing this PR --> ## Notes for your reviewers <!-- <<Stencil::Block(custom)>> --> <!-- <</Stencil::Block>> --> --------- Co-authored-by: Mark Lee <[email protected]>
- Loading branch information
1 parent
e706de0
commit 07b206d
Showing
2 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
|
||
// Description: Support for env loading env vars as strings or as SecretData | ||
|
||
package cfg | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// EnvSecret looks up a secret from the environment. | ||
func EnvSecret(name string) (SecretData, error) { | ||
val, err := EnvString(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
return SecretData(val), nil | ||
} | ||
|
||
// EnvString looks up a string from the environment. | ||
func EnvString(name string) (string, error) { | ||
var ( | ||
ok bool | ||
val string | ||
) | ||
val, ok = os.LookupEnv(name) | ||
if !ok { | ||
return "", errors.Errorf("%q environment variable not set", name) | ||
} | ||
return val, nil | ||
} |
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,49 @@ | ||
package cfg | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestEnvString(t *testing.T) { | ||
key := uuid.NewString() | ||
value := uuid.NewString() | ||
notsetKey := uuid.NewString() | ||
err := os.Setenv(key, value) | ||
assert.NilError(t, err, "failed to set environment variable") | ||
|
||
t.Run("if key not set; error is returned", func(t *testing.T) { | ||
_, err := EnvString(notsetKey) | ||
assert.ErrorContains(t, err, notsetKey) | ||
assert.ErrorContains(t, err, "environment variable not set") | ||
}) | ||
|
||
t.Run("returns set value", func(t *testing.T) { | ||
v, err := EnvString(key) | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, v, value) | ||
}) | ||
} | ||
|
||
func TestEnvSecret(t *testing.T) { | ||
key := uuid.NewString() | ||
value := uuid.NewString() | ||
notsetKey := uuid.NewString() | ||
err := os.Setenv(key, value) | ||
assert.NilError(t, err, "failed to set environment variable") | ||
|
||
t.Run("if key not set; error is returned", func(t *testing.T) { | ||
_, err := EnvSecret(notsetKey) | ||
assert.ErrorContains(t, err, notsetKey) | ||
assert.ErrorContains(t, err, "environment variable not set") | ||
}) | ||
|
||
v, err := EnvSecret(key) | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, string(v), value) | ||
} |