Skip to content

Commit

Permalink
feat(cfg): env secret/string support (#592)
Browse files Browse the repository at this point in the history
<!--
  !!!! 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
AndrewWinterman and malept authored Oct 14, 2024
1 parent e706de0 commit 07b206d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/cfg/env.go
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
}
49 changes: 49 additions & 0 deletions pkg/cfg/env_test.go
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)
}

0 comments on commit 07b206d

Please sign in to comment.